codgen 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.idea/codgen.iml +5 -0
  4. data/.idea/workspace.xml +393 -252
  5. data/codgen.gemspec +2 -0
  6. data/lib/codgen/auto_style.rb +19 -23
  7. data/lib/codgen/template.rb +10 -2
  8. data/lib/codgen/template_provider.rb +96 -0
  9. data/lib/codgen/version.rb +1 -1
  10. data/test/data/Input/{CreateDatabase.sql → CreateDatabase.sql.mustache} +0 -0
  11. data/test/data/Input/{DbContextTemplate.cs → DbContextTemplate.cs.mustache} +0 -0
  12. data/test/data/Input/{ModelTemplate.cs → ModelTemplate.cs.mustache} +0 -0
  13. data/test/data/Input/example-data.json +1 -1
  14. data/test/data/Input/explicit_names.txt +21 -0
  15. data/test/data/Input/i_can_ride_my_bike_with_no.handlebars +27 -0
  16. data/test/data/Input/{model_template.rb.ctemp → model_template.rb.mustache} +0 -0
  17. data/test/data/Input/static_text.txt +1 -0
  18. data/test/data/config.json +18 -4
  19. data/test/data/expected_output/explicit_names.txt +21 -0
  20. data/test/data/expected_output/i_can_ride_my_bike_with_no +31 -0
  21. data/test/data/expected_output/static_text.txt +1 -0
  22. metadata +38 -31
  23. data/test/data/Input/test_conditionals +0 -7
  24. data/test/data/Input/test_escapes +0 -6
  25. data/test/data/Output/CreateMyBlogDatabase.sql +0 -17
  26. data/test/data/Output/MsMvc/MyBlog/DAL/MyBlogContext.cs +0 -17
  27. data/test/data/Output/MsMvc/MyBlog/Models/Comment.cs +0 -14
  28. data/test/data/Output/MsMvc/MyBlog/Models/Post.cs +0 -17
  29. data/test/data/Output/Ruby/my_blog/Models/comment.rb +0 -3
  30. data/test/data/Output/Ruby/my_blog/Models/post.rb +0 -3
  31. data/test/data/Output/package_hello_world.txt +0 -1
  32. data/test/data/Output/package_zip_hello_world.txt +0 -1
data/codgen.gemspec CHANGED
@@ -22,6 +22,7 @@ If you find bugs or bad error messages be sure to log an issue on GitHub."
22
22
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
23
23
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
24
24
  spec.require_paths = ["lib"]
25
+ spec.required_ruby_version = ">= 1.9.2"
25
26
 
26
27
  spec.add_development_dependency "bundler", "~> 1.7"
27
28
  spec.add_development_dependency "rake", "~> 10.0"
@@ -31,4 +32,5 @@ If you find bugs or bad error messages be sure to log an issue on GitHub."
31
32
  spec.add_runtime_dependency "json"
32
33
  spec.add_runtime_dependency "activerecord"
33
34
  spec.add_runtime_dependency 'rubyzip'
35
+ spec.add_runtime_dependency 'handlebars'
34
36
  end
@@ -23,10 +23,10 @@ module Codgen
23
23
  end
24
24
 
25
25
  add_props.each do |key, value|
26
- add_property_group(json_object, key, value, AutoStyle.method(:to_camel), '!camelCase')
27
- add_property_group(json_object, key, value, AutoStyle.method(:to_cap_camel), '!CapCamel')
28
- add_property_group(json_object, key, value, AutoStyle.method(:to_underscore), '!underscored')
29
- add_property_group(json_object, key, value, AutoStyle.method(:to_camel), '!CAPS_UNDERSCORE')
26
+ add_property_group(json_object, key, value, AutoStyle.method(:to_camel), '?camelCase')
27
+ add_property_group(json_object, key, value, AutoStyle.method(:to_cap_camel), '?CapCamel')
28
+ add_property_group(json_object, key, value, AutoStyle.method(:to_underscore), '?underscored')
29
+ add_property_group(json_object, key, value, AutoStyle.method(:to_camel), '?CAPS_UNDERSCORE')
30
30
  end
31
31
  end
32
32
 
@@ -39,32 +39,28 @@ module Codgen
39
39
  private
40
40
  def self.add_property_group(json_object, key, value, translate, explicit_postfix)
41
41
  should_translate_val = value != nil && value.is_a?(String) && key.index('@')
42
- is_plural = key.index('?plural')
42
+ is_plural = key.index('#plural')
43
43
 
44
- new_key = translate.call(key)
44
+ new_key = translate.call(key.gsub('#plural', ''))
45
45
  new_value = should_translate_val ? translate.call(value) : value
46
- explicit_key = new_key + explicit_postfix
47
46
 
48
- requantified = pluralize_singularize(new_key, explicit_key, new_value, is_plural, should_translate_val)
47
+ requantified_key = pluralize_singularize(new_key, is_plural)
48
+ requantified_val = should_translate_val ? pluralize_singularize(new_value, is_plural) : new_value
49
+
50
+ requantified_suffix = is_plural ? '?singular' : '?plural'
51
+ original_quantity_suffix = is_plural ? '?plural' : '?singular'
49
52
 
50
53
  try_add_prop(json_object, new_key, new_value)
51
- try_add_prop(json_object, explicit_key, new_value)
52
- try_add_prop(json_object, requantified.short_key, requantified.value)
53
- try_add_prop(json_object, requantified.explicit_key, requantified.value)
54
+ try_add_prop(json_object, new_key+explicit_postfix, new_value)
55
+ try_add_prop(json_object, new_key+explicit_postfix+original_quantity_suffix, new_value)
56
+
57
+ try_add_prop(json_object, requantified_key, requantified_val)
58
+ try_add_prop(json_object, requantified_key+explicit_postfix, requantified_val)
59
+ try_add_prop(json_object, requantified_key+explicit_postfix+requantified_suffix, requantified_val)
54
60
  end
55
61
 
56
- def self.pluralize_singularize(short_key, explicit_key, value, is_plural, should_translate_value)
57
- return_obj = OpenStruct.new
58
- if is_plural
59
- return_obj.short_key = short_key.singularize
60
- return_obj.explicit_key = explicit_key.singularize
61
- return_obj.value = should_translate_value ? value.singularize : value
62
- else
63
- return_obj.short_key = short_key.pluralize
64
- return_obj.explicit_key = explicit_key.pluralize
65
- return_obj.value = should_translate_value ? value.pluralize : value
66
- end
67
- return_obj
62
+ def self.pluralize_singularize(val, is_plural)
63
+ is_plural ? val.singularize : val.pluralize
68
64
  end
69
65
 
70
66
 
@@ -1,6 +1,7 @@
1
1
  require 'mustache'
2
2
  require_relative 'flattener'
3
3
  require_relative 'logger'
4
+ require_relative 'template_provider'
4
5
 
5
6
  module Codgen
6
7
  class Template
@@ -13,17 +14,24 @@ module Codgen
13
14
  source_location = source_raw != nil ? source_raw.split('.') : []
14
15
  @data = Flattener.merge(data_root, source_location)
15
16
  @template = Template.get_template(@input_path)
17
+ @template_provider = Codgen.template_provider.named(template_info['template engine'])
18
+ if @template_provider == nil
19
+ @template_provider = Codgen.template_provider.for_extension(File.extname(@input_path))
20
+ end
21
+ if @template_provider == nil
22
+ @template_provider = Codgen.template_provider.named('static')
23
+ end
16
24
  end
17
25
 
18
26
  def fill_template
19
27
  if @data.is_a?(Array)
20
28
  output = Hash.new
21
29
  @data.each { |data|
22
- output.store(Mustache.render(@out_raw, data), Mustache.render(@template, data))
30
+ output.store(Mustache.render(@out_raw, data), @template_provider.render(@template, data))
23
31
  }
24
32
  return output
25
33
  elsif @data.is_a?(Hash)
26
- return { Mustache.render(@out_raw, data) => Mustache.render(@template, @data) }
34
+ return { Mustache.render(@out_raw, data) => @template_provider.render(@template, data) }
27
35
  end
28
36
  end
29
37
 
@@ -0,0 +1,96 @@
1
+ require 'mustache'
2
+ require 'handlebars'
3
+
4
+ module Codgen
5
+ class TemplateProvider
6
+ # @param [String] template: The template to be filled
7
+ # @param [Hash] data: A hash of data model
8
+ # @return [String]: The filled template
9
+ def render(template, data)
10
+ throw 'Not Implemented'
11
+ end
12
+
13
+ def name
14
+ throw 'Not Implemented'
15
+ end
16
+
17
+ def extension
18
+ throw 'Not Implemented'
19
+ end
20
+ end
21
+
22
+ class MustacheTemplateProvider < TemplateProvider
23
+ def render(template, data)
24
+ Mustache.render(template, data)
25
+ end
26
+
27
+ def name
28
+ 'mustache'
29
+ end
30
+
31
+ def extension
32
+ '.mustache'
33
+ end
34
+ end
35
+
36
+ class HandleBarsTemplateProvider < TemplateProvider
37
+ def render(template, data)
38
+ handlebars = Handlebars::Context.new
39
+ template = handlebars.compile(template)
40
+ template.call(data)
41
+ end
42
+
43
+ def name
44
+ 'handlebars'
45
+ end
46
+
47
+ def extension
48
+ '.handlebars'
49
+ end
50
+ end
51
+
52
+ class StaticTemplateProvider < TemplateProvider
53
+ def render(template, data)
54
+ template
55
+ end
56
+
57
+ def name
58
+ 'static'
59
+ end
60
+
61
+ def extension
62
+ nil
63
+ end
64
+ end
65
+
66
+ class TemplateProviderList
67
+ def initialize
68
+ @by_ext = Hash.new
69
+ @by_name = Hash.new
70
+ end
71
+
72
+ def add(template_provider)
73
+ @by_name[template_provider.name] = template_provider
74
+ if template_provider.extension != nil
75
+ @by_ext[template_provider.extension] = template_provider
76
+ end
77
+ end
78
+
79
+ def for_extension(extension)
80
+ @by_ext[extension]
81
+ end
82
+
83
+ def named(name)
84
+ @by_name[name]
85
+ end
86
+ end
87
+
88
+ @@templates_providers = TemplateProviderList.new
89
+ @@templates_providers.add(MustacheTemplateProvider.new)
90
+ @@templates_providers.add(HandleBarsTemplateProvider.new)
91
+ @@templates_providers.add(StaticTemplateProvider.new)
92
+
93
+ def self.template_provider
94
+ @@templates_providers
95
+ end
96
+ end
@@ -1,3 +1,3 @@
1
1
  module Codgen
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -72,5 +72,5 @@
72
72
  "c": false
73
73
  }
74
74
  ],
75
- "name" : "World"
75
+ "@name" : "World"
76
76
  }
@@ -0,0 +1,21 @@
1
+ {{ApplicationName}}
2
+ {{ApplicationNames}}
3
+ {{ApplicationName?CapCamel}}
4
+ {{ApplicationNames?CapCamel}}
5
+ {{ApplicationName?CapCamel?singular}}
6
+ {{ApplicationNames?CapCamel?plural}}
7
+
8
+
9
+ {{application_name}}
10
+ {{application_names}}
11
+ {{application_name?underscored}}
12
+ {{application_names?underscored}}
13
+ {{application_name?underscored?singular}}
14
+ {{application_names?underscored?plural}}
15
+
16
+ {{applicationName}}
17
+ {{applicationNames}}
18
+ {{applicationName?camelCase}}
19
+ {{applicationNames?camelCase}}
20
+ {{applicationName?camelCase?singular}}
21
+ {{applicationNames?camelCase?plural}}
@@ -0,0 +1,27 @@
1
+ {{ApplicationName}}
2
+ {{ApplicationNames}}
3
+ {{ApplicationName?CapCamel}}
4
+ {{ApplicationNames?CapCamel}}
5
+ {{ApplicationName?CapCamel?singular}}
6
+ {{ApplicationNames?CapCamel?plural}}
7
+
8
+
9
+ {{application_name}}
10
+ {{application_names}}
11
+ {{application_name?underscored}}
12
+ {{application_names?underscored}}
13
+ {{application_name?underscored?singular}}
14
+ {{application_names?underscored?plural}}
15
+
16
+ {{applicationName}}
17
+ {{applicationNames}}
18
+ {{applicationName?camelCase}}
19
+ {{applicationNames?camelCase}}
20
+ {{applicationName?camelCase?singular}}
21
+ {{applicationNames?camelCase?plural}}
22
+
23
+ {{#each entities}}
24
+ {{EntityName}}
25
+ {{entity_name}}
26
+ {{entityName}}
27
+ {{ENTITY_NAME}}{{/each}}
@@ -0,0 +1 @@
1
+ foo {{bar}}
@@ -2,23 +2,37 @@
2
2
  "map": "Map.json",
3
3
  "templates": [
4
4
  {
5
- "in": "Input/model_template.rb.ctemp",
5
+ "in": "Input/model_template.rb.mustache",
6
6
  "source": "entities",
7
7
  "out": "Output/Ruby/{{application_name}}/Models/{{entity_name}}.rb"
8
8
  },
9
9
  {
10
- "in": "Input/ModelTemplate.cs",
10
+ "in": "Input/ModelTemplate.cs.mustache",
11
11
  "source": "entities",
12
12
  "out": "Output/MsMvc/{{ApplicationName}}/Models/{{EntityName}}.cs"
13
13
  },
14
14
  {
15
- "in": "Input/DbContextTemplate.cs",
15
+ "in": "Input/DbContextTemplate.cs.mustache",
16
16
  "source": "entities",
17
17
  "out": "Output/MsMvc/{{ApplicationName}}/DAL/{{ApplicationName}}Context.cs"
18
18
  },
19
19
  {
20
- "in": "Input/CreateDatabase.sql",
20
+ "in": "Input/CreateDatabase.sql.mustache",
21
21
  "out": "Output/Create{{ApplicationName}}Database.sql"
22
+ },
23
+ {
24
+ "in": "Input/explicit_names.txt",
25
+ "out": "Output/explicit_names.txt",
26
+ "template engine": "mustache"
27
+ },
28
+ {
29
+ "in": "Input/static_text.txt",
30
+ "out": "Output/static_text.txt",
31
+ "template engine": "static"
32
+ },
33
+ {
34
+ "in": "Input/i_can_ride_my_bike_with_no.handlebars",
35
+ "out": "Output/i_can_ride_my_bike_with_no"
22
36
  }
23
37
  ],
24
38
 
@@ -0,0 +1,21 @@
1
+ MyBlog
2
+ MyBlogs
3
+ MyBlog
4
+ MyBlogs
5
+ MyBlog
6
+ MyBlogs
7
+
8
+
9
+ my_blog
10
+ my_blogs
11
+ my_blog
12
+ my_blogs
13
+ my_blog
14
+ my_blogs
15
+
16
+ myBlog
17
+ myBlogs
18
+ myBlog
19
+ myBlogs
20
+ myBlog
21
+ myBlogs
@@ -0,0 +1,31 @@
1
+ MyBlog
2
+ MyBlogs
3
+ MyBlog
4
+ MyBlogs
5
+ MyBlog
6
+ MyBlogs
7
+
8
+
9
+ my_blog
10
+ my_blogs
11
+ my_blog
12
+ my_blogs
13
+ my_blog
14
+ my_blogs
15
+
16
+ myBlog
17
+ myBlogs
18
+ myBlog
19
+ myBlogs
20
+ myBlog
21
+ myBlogs
22
+
23
+
24
+ Post
25
+ post
26
+ post
27
+
28
+ Comment
29
+ comment
30
+ comment
31
+
@@ -0,0 +1 @@
1
+ foo {{bar}}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: codgen
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Beatty
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-27 00:00:00.000000000 Z
11
+ date: 2014-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - '>='
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: handlebars
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
125
139
  description: |-
126
140
  A cross language, template based, code generator, capable of generating multiple applications from a common model.
127
141
 
@@ -175,30 +189,24 @@ files:
175
189
  - lib/codgen/package.rb
176
190
  - lib/codgen/statement.rb
177
191
  - lib/codgen/template.rb
192
+ - lib/codgen/template_provider.rb
178
193
  - lib/codgen/version.rb
179
194
  - test/behavior/basic_test.rb
180
195
  - test/behavior/output_spec.rb
181
196
  - test/behavior/test_utils.rb
182
- - test/data/Input/CreateDatabase.sql
183
- - test/data/Input/DbContextTemplate.cs
184
- - test/data/Input/ModelTemplate.cs
197
+ - test/data/Input/CreateDatabase.sql.mustache
198
+ - test/data/Input/DbContextTemplate.cs.mustache
199
+ - test/data/Input/ModelTemplate.cs.mustache
185
200
  - test/data/Input/example-data.json
201
+ - test/data/Input/explicit_names.txt
186
202
  - test/data/Input/hello_world.txt.mustache
187
- - test/data/Input/model_template.rb.ctemp
203
+ - test/data/Input/i_can_ride_my_bike_with_no.handlebars
204
+ - test/data/Input/model_template.rb.mustache
188
205
  - test/data/Input/package_test_dir/config.json
189
206
  - test/data/Input/package_test_dir/templates/package_hello_world.txt.mustache
190
207
  - test/data/Input/package_test_zip.zip
191
- - test/data/Input/test_conditionals
192
- - test/data/Input/test_escapes
208
+ - test/data/Input/static_text.txt
193
209
  - test/data/Map.json
194
- - test/data/Output/CreateMyBlogDatabase.sql
195
- - test/data/Output/MsMvc/MyBlog/DAL/MyBlogContext.cs
196
- - test/data/Output/MsMvc/MyBlog/Models/Comment.cs
197
- - test/data/Output/MsMvc/MyBlog/Models/Post.cs
198
- - test/data/Output/Ruby/my_blog/Models/comment.rb
199
- - test/data/Output/Ruby/my_blog/Models/post.rb
200
- - test/data/Output/package_hello_world.txt
201
- - test/data/Output/package_zip_hello_world.txt
202
210
  - test/data/__MACOSX/package_test_zip/._.DS_Store
203
211
  - test/data/config.json
204
212
  - test/data/expected_output/MsMvc/MyBlog/DAL/MyBlogContext.cs
@@ -206,8 +214,11 @@ files:
206
214
  - test/data/expected_output/MsMvc/MyBlog/Models/Post.cs
207
215
  - test/data/expected_output/Ruby/my_blog/Models/comment.rb
208
216
  - test/data/expected_output/Ruby/my_blog/Models/post.rb
217
+ - test/data/expected_output/explicit_names.txt
218
+ - test/data/expected_output/i_can_ride_my_bike_with_no
209
219
  - test/data/expected_output/package_hello_world.txt
210
220
  - test/data/expected_output/package_zip_hello_world.txt
221
+ - test/data/expected_output/static_text.txt
211
222
  - test/data/package_test_zip/config.json
212
223
  - test/data/package_test_zip/templates/config.json
213
224
  - test/data/package_test_zip/templates/package_zip_hello_world.txt.mustache
@@ -228,7 +239,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
228
239
  requirements:
229
240
  - - '>='
230
241
  - !ruby/object:Gem::Version
231
- version: '0'
242
+ version: 1.9.2
232
243
  required_rubygems_version: !ruby/object:Gem::Requirement
233
244
  requirements:
234
245
  - - '>='
@@ -245,26 +256,19 @@ test_files:
245
256
  - test/behavior/basic_test.rb
246
257
  - test/behavior/output_spec.rb
247
258
  - test/behavior/test_utils.rb
248
- - test/data/Input/CreateDatabase.sql
249
- - test/data/Input/DbContextTemplate.cs
250
- - test/data/Input/ModelTemplate.cs
259
+ - test/data/Input/CreateDatabase.sql.mustache
260
+ - test/data/Input/DbContextTemplate.cs.mustache
261
+ - test/data/Input/ModelTemplate.cs.mustache
251
262
  - test/data/Input/example-data.json
263
+ - test/data/Input/explicit_names.txt
252
264
  - test/data/Input/hello_world.txt.mustache
253
- - test/data/Input/model_template.rb.ctemp
265
+ - test/data/Input/i_can_ride_my_bike_with_no.handlebars
266
+ - test/data/Input/model_template.rb.mustache
254
267
  - test/data/Input/package_test_dir/config.json
255
268
  - test/data/Input/package_test_dir/templates/package_hello_world.txt.mustache
256
269
  - test/data/Input/package_test_zip.zip
257
- - test/data/Input/test_conditionals
258
- - test/data/Input/test_escapes
270
+ - test/data/Input/static_text.txt
259
271
  - test/data/Map.json
260
- - test/data/Output/CreateMyBlogDatabase.sql
261
- - test/data/Output/MsMvc/MyBlog/DAL/MyBlogContext.cs
262
- - test/data/Output/MsMvc/MyBlog/Models/Comment.cs
263
- - test/data/Output/MsMvc/MyBlog/Models/Post.cs
264
- - test/data/Output/Ruby/my_blog/Models/comment.rb
265
- - test/data/Output/Ruby/my_blog/Models/post.rb
266
- - test/data/Output/package_hello_world.txt
267
- - test/data/Output/package_zip_hello_world.txt
268
272
  - test/data/__MACOSX/package_test_zip/._.DS_Store
269
273
  - test/data/config.json
270
274
  - test/data/expected_output/MsMvc/MyBlog/DAL/MyBlogContext.cs
@@ -272,8 +276,11 @@ test_files:
272
276
  - test/data/expected_output/MsMvc/MyBlog/Models/Post.cs
273
277
  - test/data/expected_output/Ruby/my_blog/Models/comment.rb
274
278
  - test/data/expected_output/Ruby/my_blog/Models/post.rb
279
+ - test/data/expected_output/explicit_names.txt
280
+ - test/data/expected_output/i_can_ride_my_bike_with_no
275
281
  - test/data/expected_output/package_hello_world.txt
276
282
  - test/data/expected_output/package_zip_hello_world.txt
283
+ - test/data/expected_output/static_text.txt
277
284
  - test/data/package_test_zip/config.json
278
285
  - test/data/package_test_zip/templates/config.json
279
286
  - test/data/package_test_zip/templates/package_zip_hello_world.txt.mustache