reality-generators 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 80b9b3a6dde58be318658fb9824a8dee006a8ff8
4
- data.tar.gz: ea40d91550d4beb72da94a1557c8747079bf9ca2
3
+ metadata.gz: a9ad0b0940ce5ac12939a1a706dc9ad33bcc11dc
4
+ data.tar.gz: eb2a63bbafeb93019936dbe82e04be952267484d
5
5
  SHA512:
6
- metadata.gz: 6a8a126a39b6507b90d33f9543a8f118eec084026da4809b127b4c04205d01bc9c43ac580748a400201a5375e7f0d82425ea6301683e27fc4eb74752eee494d3
7
- data.tar.gz: 9daa8383b83889aa0e579f2bb675b0c602fb9658e92fdf1b639836d5f573f08a3e20ecd9710fca221b1905df39b8c423dd5ab54953d29c9670e1e31d3804ca61
6
+ metadata.gz: 6bec7f97ecedf32f57af104a8b759b8236c7aaeb7dd56062854632462792918752040c0c218c80fba42f0eabaf35a7c64db7b9eee048a56d79f7d302afcfb385
7
+ data.tar.gz: 6a8d123251caaccd2f19e200ef26772402a3bed3d95f8ff9d234b0997003311238cead078c4edd6eff99912bbf0d3d63c09b757a131c86ec480fe44f39ef92a2
@@ -15,88 +15,86 @@
15
15
  module Reality #nodoc
16
16
  module Generators #nodoc
17
17
  module Generator
18
- class << self
19
- # This method is called from a Rake or Buildr task to configure the Buildr
20
- # project so that it knows the location of all the generated artifacts and
21
- # adds them to the appropriate compile paths etc.
22
- def configure_buildr_project(buildr_project, generator_task, templates, target_dir)
23
- if buildr_project.nil?
24
- task('clean') do
25
- rm_rf target_dir
26
- end
27
- else
28
- buildr_project.clean { rm_rf target_dir }
29
- file(File.expand_path(target_dir) => [generator_task])
18
+ # This method is called from a Rake or Buildr task to configure the Buildr
19
+ # project so that it knows the location of all the generated artifacts and
20
+ # adds them to the appropriate compile paths etc.
21
+ def configure_buildr_project(buildr_project, generator_task, templates, target_dir)
22
+ if buildr_project.nil?
23
+ task('clean') do
24
+ rm_rf target_dir
25
+ end
26
+ else
27
+ buildr_project.clean { rm_rf target_dir }
28
+ file(File.expand_path(target_dir) => [generator_task])
30
29
 
31
- # Is there java source generated in project?
32
- if templates.any? { |template| template.output_path =~ /^main\/java\/.*/ }
33
- main_java_dir = "#{target_dir}/main/java"
34
- file(main_java_dir => [generator_task]) do
35
- mkdir_p main_java_dir
36
- end
37
- buildr_project.compile.using :javac
38
- buildr_project.compile.from main_java_dir
39
- # Need to force this as it may have already been cached and thus will not recalculate
40
- buildr_project.iml.main_generated_source_directories << main_java_dir if buildr_project.iml?
30
+ # Is there java source generated in project?
31
+ if templates.any? { |template| template.output_path =~ /^main\/java\/.*/ }
32
+ main_java_dir = "#{target_dir}/main/java"
33
+ file(main_java_dir => [generator_task]) do
34
+ mkdir_p main_java_dir
41
35
  end
36
+ buildr_project.compile.using :javac
37
+ buildr_project.compile.from main_java_dir
38
+ # Need to force this as it may have already been cached and thus will not recalculate
39
+ buildr_project.iml.main_generated_source_directories << main_java_dir if buildr_project.iml?
40
+ end
42
41
 
43
- # Is there resources generated in project?
44
- if templates.any? { |template| template.output_path =~ /^main\/resources\/.*/ }
45
- main_resources_dir = "#{target_dir}/main/resources"
46
- file(main_resources_dir => [generator_task]) do
47
- mkdir_p main_resources_dir
48
- end
49
- buildr_project.resources.enhance([generator_task])
50
- buildr_project.resources.filter.into buildr_project.path_to(:target, :main, :resources) unless buildr_project.resources.target
51
- buildr_project.resources do |t|
52
- t.enhance do
53
- if File.exist?(main_resources_dir)
54
- FileUtils.mkdir_p buildr_project.resources.target.to_s
55
- FileUtils.cp_r "#{main_resources_dir}/.", buildr_project.resources.target.to_s
56
- end
42
+ # Is there resources generated in project?
43
+ if templates.any? { |template| template.output_path =~ /^main\/resources\/.*/ }
44
+ main_resources_dir = "#{target_dir}/main/resources"
45
+ file(main_resources_dir => [generator_task]) do
46
+ mkdir_p main_resources_dir
47
+ end
48
+ buildr_project.resources.enhance([generator_task])
49
+ buildr_project.resources.filter.into buildr_project.path_to(:target, :main, :resources) unless buildr_project.resources.target
50
+ buildr_project.resources do |t|
51
+ t.enhance do
52
+ if File.exist?(main_resources_dir)
53
+ FileUtils.mkdir_p buildr_project.resources.target.to_s
54
+ FileUtils.cp_r "#{main_resources_dir}/.", buildr_project.resources.target.to_s
57
55
  end
58
56
  end
59
- buildr_project.iml.main_generated_resource_directories << main_resources_dir if buildr_project.iml?
60
57
  end
58
+ buildr_project.iml.main_generated_resource_directories << main_resources_dir if buildr_project.iml?
59
+ end
61
60
 
62
- # Is there assets generated in project?
63
- if templates.any? { |template| template.output_path =~ /^main\/webapp\/.*/ }
64
- webapp_dir = File.expand_path("#{target_dir}/main/webapp")
65
- buildr_project.assets.enhance([generator_task])
66
- buildr_project.assets.paths << file(webapp_dir => [generator_task]) do
67
- mkdir_p webapp_dir
68
- end
61
+ # Is there assets generated in project?
62
+ if templates.any? { |template| template.output_path =~ /^main\/webapp\/.*/ }
63
+ webapp_dir = File.expand_path("#{target_dir}/main/webapp")
64
+ buildr_project.assets.enhance([generator_task])
65
+ buildr_project.assets.paths << file(webapp_dir => [generator_task]) do
66
+ mkdir_p webapp_dir
69
67
  end
68
+ end
70
69
 
71
- # Is there test java source generated in project?
72
- if templates.any? { |template| template.output_path =~ /^test\/java\/.*/ }
73
- test_java_dir = "#{target_dir}/test/java"
74
- file(test_java_dir => [generator_task]) do
75
- mkdir_p test_java_dir
76
- end
77
- buildr_project.test.compile.from test_java_dir
78
- # Need to force this as it may have already been cached and thus will not recalculate
79
- buildr_project.iml.test_generated_source_directories << test_java_dir if buildr_project.iml?
70
+ # Is there test java source generated in project?
71
+ if templates.any? { |template| template.output_path =~ /^test\/java\/.*/ }
72
+ test_java_dir = "#{target_dir}/test/java"
73
+ file(test_java_dir => [generator_task]) do
74
+ mkdir_p test_java_dir
80
75
  end
76
+ buildr_project.test.compile.from test_java_dir
77
+ # Need to force this as it may have already been cached and thus will not recalculate
78
+ buildr_project.iml.test_generated_source_directories << test_java_dir if buildr_project.iml?
79
+ end
81
80
 
82
- # Is there resources generated in project?
83
- if templates.any? { |template| template.output_path =~ /^test\/resources\/.*/ }
84
- test_resources_dir = "#{target_dir}/test/resources"
85
- file(test_resources_dir => [generator_task]) do
86
- mkdir_p test_resources_dir
87
- end
88
- buildr_project.test.resources.enhance([generator_task])
89
- buildr_project.test.resources.filter.into buildr_project.path_to(:target, :test, :resources) unless buildr_project.test.resources.target
90
- buildr_project.test.resources do |t|
91
- t.enhance do
92
- if File.exist?(test_resources_dir)
93
- FileUtils.mkdir_p buildr_project.test.resources.target.to_s
94
- FileUtils.cp_r "#{test_resources_dir}/.", buildr_project.test.resources.target.to_s
95
- end
81
+ # Is there resources generated in project?
82
+ if templates.any? { |template| template.output_path =~ /^test\/resources\/.*/ }
83
+ test_resources_dir = "#{target_dir}/test/resources"
84
+ file(test_resources_dir => [generator_task]) do
85
+ mkdir_p test_resources_dir
86
+ end
87
+ buildr_project.test.resources.enhance([generator_task])
88
+ buildr_project.test.resources.filter.into buildr_project.path_to(:target, :test, :resources) unless buildr_project.test.resources.target
89
+ buildr_project.test.resources do |t|
90
+ t.enhance do
91
+ if File.exist?(test_resources_dir)
92
+ FileUtils.mkdir_p buildr_project.test.resources.target.to_s
93
+ FileUtils.cp_r "#{test_resources_dir}/.", buildr_project.test.resources.target.to_s
96
94
  end
97
95
  end
98
- buildr_project.iml.test_generated_resource_directories << test_resources_dir if buildr_project.iml?
99
96
  end
97
+ buildr_project.iml.test_generated_resource_directories << test_resources_dir if buildr_project.iml?
100
98
  end
101
99
  end
102
100
  end
@@ -14,115 +14,111 @@
14
14
 
15
15
  module Reality #nodoc
16
16
  module Generators #nodoc
17
-
18
17
  module Generator
19
- class << self
18
+ # Return a list of templates loaded from specified template_set_keys
19
+ def load_templates_from_template_sets(template_set_container, template_set_keys)
20
+ template_map = {}
21
+ load_templates(template_set_container, template_map, template_set_keys, [])
22
+ template_map.values
23
+ end
20
24
 
21
- # Return a list of templates loaded from specified template_set_keys
22
- def load_templates_from_template_sets(template_set_container, template_set_keys)
23
- template_map = {}
24
- load_templates(template_set_container, template_map, template_set_keys, [])
25
- template_map.values
25
+ # Actually perform the generation of files from specified templates.
26
+ # Files are generated to the specified target directory. Any files that
27
+ # are not generated by this process are deleted. The files generated are
28
+ # based on the specified templates.
29
+ # The traversal starts from a root element of specified element_type and
30
+ # traverses all elements that are contained transitively by the root element.
31
+ # The templates then generate files from traversed elements.
32
+ def generate(template_set_container, element_type, element, directory, templates, filter)
33
+ unprocessed_files = (Dir["#{directory}/**/*.*"] + Dir["#{directory}/**/*"]).uniq
34
+
35
+ Generators.debug "Templates to process: #{templates.collect { |t| t.name }.inspect}"
36
+
37
+ targets = {}
38
+ collect_generation_targets(template_set_container, element_type, element, element, targets)
39
+
40
+ templates.each do |template|
41
+ Generators.debug "Evaluating template: #{template.name}"
42
+ elements = targets[template.target]
43
+
44
+ elements.each do |element_pair|
45
+ element = element_pair[1]
46
+ if template.applicable?(element_pair[0]) && (filter.nil? || filter.call(template.target, element))
47
+ template.generate(directory, element, unprocessed_files)
48
+ end
49
+ end if elements
26
50
  end
27
51
 
28
- # Actually perform the generation of files from specified templates.
29
- # Files are generated to the specified target directory. Any files that
30
- # are not generated by this process are deleted. The files generated are
31
- # based on the specified templates.
32
- # The traversal starts from a root element of specified element_type and
33
- # traverses all elements that are contained transitively by the root element.
34
- # The templates then generate files from traversed elements.
35
- def generate(template_set_container, element_type, element, directory, templates, filter)
36
- unprocessed_files = (Dir["#{directory}/**/*.*"] + Dir["#{directory}/**/*"]).uniq
37
-
38
- Generators.debug "Templates to process: #{templates.collect { |t| t.name }.inspect}"
39
-
40
- targets = {}
41
- collect_generation_targets(template_set_container, element_type, element, element, targets)
42
-
43
- templates.each do |template|
44
- Generators.debug "Evaluating template: #{template.name}"
45
- elements = targets[template.target]
46
-
47
- elements.each do |element_pair|
48
- element = element_pair[1]
49
- if template.applicable?(element_pair[0]) && (filter.nil? || filter.call(template.target, element))
50
- template.generate(directory, element, unprocessed_files)
51
- end
52
- end if elements
53
- end
54
-
55
- unprocessed_files.sort.reverse.each do |file|
56
- if File.directory?(file)
57
- if (Dir.entries(file) - %w(. ..)).empty?
58
- Generators.debug "Removing #{file} as no longer generated"
59
- FileUtils.rmdir file
60
- end
61
- else
52
+ unprocessed_files.sort.reverse.each do |file|
53
+ if File.directory?(file)
54
+ if (Dir.entries(file) - %w(. ..)).empty?
62
55
  Generators.debug "Removing #{file} as no longer generated"
63
- FileUtils.rm_f file
56
+ FileUtils.rmdir file
64
57
  end
58
+ else
59
+ Generators.debug "Removing #{file} as no longer generated"
60
+ FileUtils.rm_f file
65
61
  end
66
-
67
- Generators.info 'Generator completed'
68
62
  end
69
63
 
70
- private
64
+ Generators.info 'Generator completed'
65
+ end
71
66
 
72
- def load_templates(template_set_container, template_map, template_set_keys, processed_template_sets)
73
- template_set_keys.each do |template_set_key|
74
- next if processed_template_sets.include?(template_set_key)
75
- template_set = template_set_container.template_set_by_name(template_set_key)
76
- processed_template_sets << template_set_key
77
- load_templates(template_set_container, template_map, template_set.required_template_sets, processed_template_sets)
78
- template_set.templates.each do |template|
79
- template_map[template.name] = template
80
- end
67
+ private
68
+
69
+ def load_templates(template_set_container, template_map, template_set_keys, processed_template_sets)
70
+ template_set_keys.each do |template_set_key|
71
+ next if processed_template_sets.include?(template_set_key)
72
+ template_set = template_set_container.template_set_by_name(template_set_key)
73
+ processed_template_sets << template_set_key
74
+ load_templates(template_set_container, template_map, template_set.required_template_sets, processed_template_sets)
75
+ template_set.templates.each do |template|
76
+ template_map[template.name] = template
81
77
  end
82
78
  end
79
+ end
83
80
 
84
- # Collect all generation targets. This is a map of type to an array of element pairs of that type.
85
- # The element pair includes two elements, the "parent" standard element that is facet as per normal
86
- # and the actual element that is used for generation. The first element is used when checking if
87
- # element is applicable? to be generated while the second is basis of generation.
88
- # i.e.
89
- #
90
- # {
91
- # :repository => [ [repository, repository] ],
92
- # :data_module => [ [module1, module1], [module2, module2]],
93
- # :entity => [[entity1, entity1], [entity2, entity2]],
94
- # :'keycloak.client' => [[repository, client]],
95
- # ...
96
- # }
97
- #
98
- def collect_generation_targets(template_set_container, element_type, scope_element, element, targets)
99
- (targets[element_type] ||= []) << [scope_element, element]
100
-
101
- template_set_container.target_manager.targets_by_container(element_type).each do |target|
102
- next unless handle_subelement?(element, target.key)
103
- subelements = nil
104
- subscope = nil
105
- if target.standard?
106
- subelements = element.send(target.access_method)
107
- elsif element.facet_enabled?(target.facet_key)
108
- subelements = element.send(target.facet_key).send(target.access_method)
109
- subscope = element
110
- end
81
+ # Collect all generation targets. This is a map of type to an array of element pairs of that type.
82
+ # The element pair includes two elements, the "parent" standard element that is facet as per normal
83
+ # and the actual element that is used for generation. The first element is used when checking if
84
+ # element is applicable? to be generated while the second is basis of generation.
85
+ # i.e.
86
+ #
87
+ # {
88
+ # :repository => [ [repository, repository] ],
89
+ # :data_module => [ [module1, module1], [module2, module2]],
90
+ # :entity => [[entity1, entity1], [entity2, entity2]],
91
+ # :'keycloak.client' => [[repository, client]],
92
+ # ...
93
+ # }
94
+ #
95
+ def collect_generation_targets(template_set_container, element_type, scope_element, element, targets)
96
+ (targets[element_type] ||= []) << [scope_element, element]
97
+
98
+ template_set_container.target_manager.targets_by_container(element_type).each do |target|
99
+ next unless handle_subelement?(element, target.key)
100
+ subelements = nil
101
+ subscope = nil
102
+ if target.standard?
103
+ subelements = element.send(target.access_method)
104
+ elsif element.facet_enabled?(target.facet_key)
105
+ subelements = element.send(target.facet_key).send(target.access_method)
106
+ subscope = element
107
+ end
111
108
 
112
- next unless subelements
113
- subelements = [subelements] unless subelements.is_a?(Array)
109
+ next unless subelements
110
+ subelements = [subelements] unless subelements.is_a?(Array)
114
111
 
115
- subelements.each do |subelement|
116
- collect_generation_targets(template_set_container, target.qualified_key, subscope || subelement, subelement, targets)
117
- end
112
+ subelements.each do |subelement|
113
+ collect_generation_targets(template_set_container, target.qualified_key, subscope || subelement, subelement, targets)
118
114
  end
119
115
  end
116
+ end
120
117
 
121
- # A hook to control whether certain paths in model should
122
- # be follow when collecting generation targets
123
- def handle_subelement?(object, sub_feature_key)
124
- true
125
- end
118
+ # A hook to control whether certain paths in model should
119
+ # be follow when collecting generation targets
120
+ def handle_subelement?(object, sub_feature_key)
121
+ true
126
122
  end
127
123
  end
128
124
  end
@@ -44,6 +44,10 @@ module Reality #nodoc
44
44
  @target_manager ||= Reality::Generators::TargetManager.new(self)
45
45
  end
46
46
 
47
+ def generator
48
+ @generator ||= new_generator
49
+ end
50
+
47
51
  protected
48
52
 
49
53
  def register_template_set(template_set)
@@ -55,6 +59,10 @@ module Reality #nodoc
55
59
  Generators.error('new_template_set not implemented')
56
60
  end
57
61
 
62
+ def new_generator
63
+ Generators.error('new_generator not implemented')
64
+ end
65
+
58
66
  private
59
67
 
60
68
  def template_set_map
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{reality-generators}
5
- s.version = '1.2.0'
5
+ s.version = '1.3.0'
6
6
  s.platform = Gem::Platform::RUBY
7
7
 
8
8
  s.authors = ['Peter Donald']
@@ -90,7 +90,7 @@ class Reality::Generators::TestGenerator < Reality::TestCase
90
90
  TestTemplateSetContainer.target_manager.target(:unit, :repository, :facet_key => :jpa)
91
91
 
92
92
  targets = {}
93
- Reality::Generators::Generator.send(:collect_generation_targets, TestTemplateSetContainer, :repository, repository, repository, targets)
93
+ TestTemplateSetContainer.generator.send(:collect_generation_targets, TestTemplateSetContainer, :repository, repository, repository, targets)
94
94
 
95
95
  assert_equal true, targets.include?(:repository)
96
96
  assert_equal true, targets.include?(:entity)
@@ -119,7 +119,7 @@ class Reality::Generators::TestGenerator < Reality::TestCase
119
119
  repository.enable_jpa!
120
120
 
121
121
  targets = {}
122
- Reality::Generators::Generator.send(:collect_generation_targets, TestTemplateSetContainer, :repository, repository, repository, targets)
122
+ TestTemplateSetContainer.generator.send(:collect_generation_targets, TestTemplateSetContainer, :repository, repository, repository, targets)
123
123
 
124
124
  # No units have been defined so no extra targets
125
125
  assert_equal 3, targets.size
@@ -128,7 +128,7 @@ class Reality::Generators::TestGenerator < Reality::TestCase
128
128
  repository.jpa.unit(:MyUnit2)
129
129
 
130
130
  targets = {}
131
- Reality::Generators::Generator.send(:collect_generation_targets, TestTemplateSetContainer, :repository, repository, repository, targets)
131
+ TestTemplateSetContainer.generator.send(:collect_generation_targets, TestTemplateSetContainer, :repository, repository, repository, targets)
132
132
 
133
133
  assert_equal true, targets.include?(:repository)
134
134
  assert_equal true, targets.include?(:entity)
@@ -210,7 +210,7 @@ class Reality::Generators::TestGenerator < Reality::TestCase
210
210
  original_mtime = File.mtime(repo_file)
211
211
 
212
212
  filter = Proc.new { |artifact_type, artifact| artifact_type != :attribute || %w(MyAttr1 MyAttr2).include?(artifact.name.to_s) }
213
- Reality::Generators::Generator.
213
+ TestTemplateSetContainer.generator.
214
214
  generate(TestTemplateSetContainer, :repository, repository, target_directory, template_set.templates, filter)
215
215
 
216
216
  assert_equal false, File.directory?("#{target_directory}/some")
@@ -269,7 +269,7 @@ class Reality::Generators::TestGenerator < Reality::TestCase
269
269
  end
270
270
 
271
271
  template_set_keys = [:template_set_1, :template_set_4]
272
- templates = Reality::Generators::Generator.
272
+ templates = TestTemplateSetContainer.generator.
273
273
  load_templates_from_template_sets(TestTemplateSetContainer, template_set_keys)
274
274
 
275
275
  assert_equal 6, templates.size
data/test/helper.rb CHANGED
@@ -7,6 +7,12 @@ require 'reality/generators'
7
7
  class Reality::TestCase < Minitest::Test
8
8
  include Test::Unit::Assertions
9
9
 
10
+ module TestGenerator
11
+ class << self
12
+ include Reality::Generators::Generator
13
+ end
14
+ end
15
+
10
16
  module TestTemplateSetContainer
11
17
  class << self
12
18
  include Reality::Generators::TemplateSetContainer
@@ -15,6 +21,10 @@ class Reality::TestCase < Minitest::Test
15
21
  Reality::Generators::TemplateSet.new(self, name, options, &block)
16
22
  end
17
23
 
24
+ def new_generator
25
+ TestGenerator
26
+ end
27
+
18
28
  def reset
19
29
  template_set_map.clear
20
30
  target_manager.reset_targets
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reality-generators
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Donald