metamodel 0.2.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  public extension <%= model.name %> {
2
2
  static var count: Int {
3
3
  get {
4
- let countSQL = "SELECT count(*) FROM \(tableName.unwrapped)"
4
+ let countSQL = "SELECT count(*) FROM \(tableName)"
5
5
  guard let count = executeScalarSQL(countSQL) as? Int64 else { return 0 }
6
6
  return Int(count)
7
7
  }
@@ -4,7 +4,7 @@ extension <%= model.name %> {
4
4
  executeSQL(initializeTableSQL)
5
5
  }
6
6
  static func deinitialize() {
7
- let dropTableSQL = "DROP TABLE \(tableName.unwrapped)"
7
+ let dropTableSQL = "DROP TABLE \(tableName)"
8
8
  executeSQL(dropTableSQL)
9
9
  }
10
10
  }
@@ -1,5 +1,5 @@
1
1
  module MetaModel
2
2
  # The version of the MetaModel command line tool.
3
3
  #
4
- VERSION = '0.2.0' unless defined? MetaModel::VERSION
4
+ VERSION = '0.3.1' unless defined? MetaModel::VERSION
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metamodel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Draveness Zuo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-13 00:00:00.000000000 Z
11
+ date: 2016-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: claide
@@ -147,20 +147,24 @@ files:
147
147
  - bin/meta
148
148
  - lib/metamodel.rb
149
149
  - lib/metamodel/command.rb
150
- - lib/metamodel/command/build.rb
151
- - lib/metamodel/command/build/renderer.rb
152
- - lib/metamodel/command/build/resolver.rb
153
- - lib/metamodel/command/build/translator.rb
154
150
  - lib/metamodel/command/clean.rb
155
151
  - lib/metamodel/command/generate.rb
156
152
  - lib/metamodel/command/init.rb
153
+ - lib/metamodel/command/install.rb
157
154
  - lib/metamodel/config.rb
158
155
  - lib/metamodel/erbal_template.rb
156
+ - lib/metamodel/installer.rb
157
+ - lib/metamodel/installer/renderer.rb
158
+ - lib/metamodel/installer/validator.rb
159
+ - lib/metamodel/metafile.rb
160
+ - lib/metamodel/metafile/dsl.rb
159
161
  - lib/metamodel/record/association.rb
160
162
  - lib/metamodel/record/model.rb
161
163
  - lib/metamodel/record/property.rb
164
+ - lib/metamodel/template/belongs_to_association.swift
162
165
  - lib/metamodel/template/file_header.swift
163
166
  - lib/metamodel/template/foreign_key.swift
167
+ - lib/metamodel/template/has_many_association.swift
164
168
  - lib/metamodel/template/helper.swift
165
169
  - lib/metamodel/template/json.swift
166
170
  - lib/metamodel/template/metamodel.swift
@@ -192,7 +196,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
192
196
  version: '0'
193
197
  requirements: []
194
198
  rubyforge_project:
195
- rubygems_version: 2.6.6
199
+ rubygems_version: 2.6.4
196
200
  signing_key:
197
201
  specification_version: 4
198
202
  summary: The Cocoa models generator.
@@ -1,148 +0,0 @@
1
- require 'git'
2
-
3
- module MetaModel
4
- class Command
5
- class Build < Command
6
- include Config::Mixin
7
- require 'metamodel/command/build/resolver'
8
- require 'metamodel/command/build/renderer'
9
- require 'metamodel/command/build/translator'
10
-
11
- self.summary = "Build a MetaModel.framework from Metafile"
12
- self.description = <<-DESC
13
- Clone a metamodel project template from GitHub, parsing Metafile, validating models,
14
- generate model swift file and build MetaModel.framework.
15
- DESC
16
-
17
- attr_accessor :models
18
-
19
- def initialize(argv)
20
- super
21
- end
22
-
23
- def run
24
- UI.section "Building MetaModel.framework in project" do
25
- clone_project
26
- models, associations = resolve_template
27
- @models = compact_associtions_into_models models, associations
28
- validate_models
29
- render_model_files
30
- update_initialize_method
31
- build_metamodel_framework unless config.skip_build?
32
- end
33
- UI.notice "Please drag MetaModel.framework into Embedded Binaries phrase.\n"
34
- end
35
-
36
- def clone_project
37
- if File.exist? config.metamodel_xcode_project
38
- UI.message "Existing project `#{config.metamodel_xcode_project}`"
39
- else
40
- UI.section "Cloning MetaModel project into `./metamodel` folder" do
41
- Git.clone(config.metamodel_template_uri, 'metamodel', :depth => 1)
42
- UI.message "Using `#{config.metamodel_xcode_project}` to build module"
43
- end
44
- end
45
- end
46
-
47
- def resolve_template
48
- resolver = Resolver.new
49
- resolver.resolve
50
- end
51
-
52
- def compact_associtions_into_models(models, associations)
53
- Translator.new(models, associations).translate
54
- end
55
-
56
- def validate_models
57
- existing_types = @models.map { |m| m.properties.map { |property| property.type } }.flatten.uniq
58
- unsupported_types = existing_types - supported_types
59
- raise Informative, "Unsupported types #{unsupported_types}" unless unsupported_types == []
60
- end
61
-
62
- def render_model_files
63
- UI.section "Generating model files" do
64
- Renderer.render(@models)
65
- end
66
- end
67
-
68
- def update_initialize_method
69
- template = File.read File.expand_path(File.join(File.dirname(__FILE__), "../template/metamodel.swift"))
70
- result = ErbalTemplate::render_from_hash(template, { :models => @models })
71
- model_path = Pathname.new("./metamodel/MetaModel/MetaModel.swift")
72
- File.write model_path, result
73
- end
74
-
75
- def build_metamodel_framework
76
- UI.section "Generating MetaModel.framework" do
77
-
78
- build_iphoneos = "xcodebuild -scheme MetaModel \
79
- -project MetaModel/MetaModel.xcodeproj \
80
- -configuration Release -sdk iphoneos \
81
- -derivedDataPath './metamodel' \
82
- BITCODE_GENERATION_MODE=bitcode \
83
- ONLY_ACTIVE_ARCH=NO \
84
- CODE_SIGNING_REQUIRED=NO \
85
- CODE_SIGN_IDENTITY= \
86
- clean build"
87
- build_iphonesimulator = "xcodebuild -scheme MetaModel \
88
- -project MetaModel/MetaModel.xcodeproj \
89
- -configuration Release -sdk iphonesimulator \
90
- -derivedDataPath './metamodel' \
91
- ONLY_ACTIVE_ARCH=NO \
92
- CODE_SIGNING_REQUIRED=NO \
93
- CODE_SIGN_IDENTITY= \
94
- clean build"
95
- result = system "#{build_iphoneos} > /dev/null &&
96
- #{build_iphonesimulator} > /dev/null"
97
-
98
- raise Informative, 'Building framework failed.' unless result
99
-
100
- build_products_folder = "./metamodel/Build/Products"
101
-
102
- copy_command = "cp -rf #{build_products_folder}/Release-iphoneos/MetaModel.framework . && \
103
- cp -rf #{build_products_folder}/Release-iphonesimulator/MetaModel.framework/Modules/MetaModel.swiftmodule/* \
104
- MetaModel.framework/Modules/MetaModel.swiftmodule/"
105
- lipo_command = "lipo -create -output MetaModel.framework/MetaModel \
106
- #{build_products_folder}/Release-iphonesimulator/MetaModel.framework/MetaModel \
107
- #{build_products_folder}/Release-iphoneos/MetaModel.framework/MetaModel"
108
-
109
- result = system "#{copy_command} && #{lipo_command}"
110
-
111
- raise Informative, 'Copy framework to current folder failed.' unless result
112
- UI.message "-> ".green + "MetaModel.framework located in current folder"
113
- end
114
- end
115
-
116
- def validate!
117
- super
118
- raise Informative, 'No Metafile in current directory' unless config.metafile_in_dir(Pathname.pwd)
119
- end
120
-
121
- private
122
-
123
- CURRENT_SUPPORTED_BUILT_IN_TYPES = %w[
124
- Int
125
- Double
126
- Float
127
- String
128
- Bool
129
- NSDate
130
- ]
131
-
132
- def built_in_types
133
- CURRENT_SUPPORTED_BUILT_IN_TYPES.map do |t|
134
- [t, "#{t}?"]
135
- end.flatten
136
- end
137
-
138
- def supported_types
139
- @models.reduce(CURRENT_SUPPORTED_BUILT_IN_TYPES) { |types, model|
140
- types << model.name.to_s
141
- }.map { |type|
142
- [type, "#{type}?"]
143
- }.flatten
144
- end
145
-
146
- end
147
- end
148
- end
@@ -1,55 +0,0 @@
1
- require 'xcodeproj'
2
-
3
- module MetaModel
4
- class Command
5
- class Build
6
- class Renderer
7
- class << self
8
-
9
- def templates
10
- results = []
11
- # file_paths = %w{file_header attributes json recordable initialize static_methods instance_methods model_query model_relation}
12
- file_paths = %w{file_header table_initialize model_initialize model_update model_query model_delete static_methods foreign_key helper}
13
- file_paths.each do |file_path|
14
- template = File.read File.expand_path(File.join(File.dirname(__FILE__), "../../template/#{file_path}.swift"))
15
- results << template
16
- end
17
- results
18
- end
19
-
20
- def render(models)
21
- project = Xcodeproj::Project.open(Config.instance.metamodel_xcode_project)
22
- target = project.targets.first
23
- models.each do |model|
24
- target.source_build_phase.files_references.each do |file_ref|
25
- target.source_build_phase.remove_file_reference(file_ref) if file_ref && "#{model.name}.swift" == file_ref.name
26
- end
27
- end
28
- models_group = project.main_group.find_subpath('MetaModel/Models', true)
29
- models_group.clear
30
- models_group.set_source_tree('SOURCE_ROOT')
31
-
32
- file_refs = []
33
- models.each do |model|
34
- result = templates.map { |template|
35
- ErbalTemplate::render_from_hash(template, { :model => model })
36
- }.join("\n")
37
- model_path = Pathname.new("./metamodel/MetaModel/#{model.name}.swift")
38
- File.write model_path, result
39
-
40
- file_refs << models_group.new_reference(Pathname.new("MetaModel/#{model.name}.swift"))
41
-
42
- UI.message '-> '.green + "Using #{model.name}.swift file"
43
- end
44
- target.add_file_references file_refs
45
-
46
- project.save
47
- end
48
- end
49
-
50
- private
51
-
52
- end
53
- end
54
- end
55
- end
@@ -1,72 +0,0 @@
1
- module MetaModel
2
- class Command
3
- class Build
4
- class Resolver
5
-
6
- include Config::Mixin
7
-
8
- require 'metamodel/record/model'
9
- require 'metamodel/record/property'
10
- require 'metamodel/record/association'
11
-
12
- attr_accessor :models
13
- attr_accessor :associations
14
-
15
- attr_accessor :current_model
16
-
17
- def initialize
18
- @models = []
19
- @associations = []
20
- end
21
-
22
- def resolve
23
- UI.section "Analyzing Metafile" do
24
- metafile_path = config.metafile_path
25
- eval File.read(metafile_path)
26
- end
27
- return models, associations
28
- end
29
-
30
- private
31
-
32
- def metamodel_version(version)
33
- raise Informative,
34
- "Meta file #{version} not matched with current metamodel version #{VERSION}" if version != VERSION
35
- end
36
-
37
- def define(model_name)
38
- UI.message '-> '.green + "Resolving `#{model_name.to_s.camelize}`"
39
- @current_model = Record::Model.new(model_name)
40
- yield
41
- @models << current_model
42
- end
43
-
44
- def attr(key, type = :string, **args)
45
- current_model.properties << Record::Property.new(key, type, args)
46
- end
47
-
48
- def has_one(name, model_name = nil, **args)
49
- model_name = name.to_s.singularize.camelize if model_name.nil?
50
- association = Record::Association.new(name, current_model.name, model_name, :has_one, args)
51
- @associations << association
52
- end
53
-
54
- def has_many(name, model_name = nil, **args)
55
- model_name = name.to_s.singularize.camelize if model_name.nil?
56
- raise Informative, "has_many relation can't be created with optional model name" if model_name.end_with? "?"
57
- association = Record::Association.new(name, current_model.name, model_name, :has_many, args)
58
- @associations << association
59
- end
60
-
61
- def belongs_to(name, model_name = nil, **args)
62
- model_name = name.to_s.singularize.camelize if model_name.nil?
63
- association = Record::Association.new(name, current_model.name, model_name, :belongs_to, args)
64
- @associations << association
65
- end
66
-
67
- private
68
-
69
- end
70
- end
71
- end
72
- end
@@ -1,66 +0,0 @@
1
- module MetaModel
2
- class Command
3
- class Build
4
- class Translator
5
- require 'metamodel/record/model'
6
- require 'metamodel/record/property'
7
- require 'metamodel/record/association'
8
-
9
- attr_reader :models
10
- attr_reader :associations
11
-
12
- def initialize(models, associations)
13
- @models = models
14
- @associations = associations
15
- end
16
-
17
- def translate
18
- name_model_hash = Hash[@models.collect { |model| [model.name, model] }]
19
- @associations.map! do |association|
20
- major_model = name_model_hash[association.major_model]
21
- major_model.associations << association
22
- association.major_model = major_model
23
- association.secondary_model = name_model_hash[association.secondary_model]
24
- size = association.through ? 3 : 2
25
- association.through = name_model_hash[association.through]
26
- raise Informative, "Associations not satisfied in `Metafile`" \
27
- unless [association.major_model, association.secondary_model, association.through].compact.size == size
28
- association
29
- end
30
-
31
- satisfy_constraint = @associations.reduce([]) do |remain, association|
32
- expect = remain.select { |assoc| assoc.expect_constraint? association }
33
- if expect.empty?
34
- remain << association
35
- else
36
- remain.delete expect.first
37
- end
38
- remain
39
- end
40
- raise Informative, "Unsatisfied constraints in #{satisfy_constraint.map \
41
- { |x| x.debug_description }}" \
42
- if satisfy_constraint.size > 0
43
-
44
- @associations.each do |association|
45
- major_model = association.major_model
46
- secondary_model = association.secondary_model
47
- case association.relation
48
- when :has_one, :has_many then
49
- name = association.through ? association.through.foreign_id : major_model.foreign_id
50
- property = Record::Property.new(name, :int, :foreign, :default => 0)
51
- secondary_model.properties << property
52
- when :belongs_to then
53
- name = association.through ? association.through.foreign_id : secondary_model.foreign_id
54
- property = Record::Property.new(name, :int, :foreign, :default => 0)
55
- major_model.properties << property
56
- end
57
- end
58
-
59
- @models.each do |model|
60
- model.properties.uniq! { |prop| [prop.name] }
61
- end
62
- end
63
- end
64
- end
65
- end
66
- end