diff --git a/features/update.feature b/features/update.feature index 1bc82254..08e5b63c 100644 --- a/features/update.feature +++ b/features/update.feature @@ -642,6 +642,111 @@ Feature: update echo 'https://github.com/maestrodev' """ + Scenario: Using .erb extension template + Given a file named "managed_modules.yml" with: + """ + --- + - puppet-test + """ + And a file named "modulesync.yml" with: + """ + --- + namespace: maestrodev + git_base: https://github.com/ + """ + And a file named "config_defaults.yml" with: + """ + --- + README.md.erb: + """ + And a directory named "moduleroot" + And a file named "moduleroot/README.md.erb" with: + """ + echo '<%= @configs[:git_base] + @configs[:namespace] %>' + """ + When I run `msync update --noop` + Then the exit status should be 0 + And the output should match: + """ + Files changed: + +diff --git a/README.md b/README.md + """ + Given I run `cat modules/puppet-test/README.md` + Then the output should contain: + """ + echo 'https://github.com/maestrodev' + """ + + Scenario: Using .erb extension template but settings ommit .erb + Given a file named "managed_modules.yml" with: + """ + --- + - puppet-test + """ + And a file named "modulesync.yml" with: + """ + --- + namespace: maestrodev + git_base: https://github.com/ + """ + And a file named "config_defaults.yml" with: + """ + --- + README.md: + """ + And a directory named "moduleroot" + And a file named "moduleroot/README.md.erb" with: + """ + echo '<%= @configs[:git_base] + @configs[:namespace] %>' + """ + When I run `msync update --noop` + Then the exit status should be 0 + And the output should match: + """ + Files changed: + +diff --git a/README.md b/README.md + """ + Given I run `cat modules/puppet-test/README.md` + Then the output should contain: + """ + echo 'https://github.com/maestrodev' + """ + + Scenario: Using non-.erb extension template but settings use .erb + Given a file named "managed_modules.yml" with: + """ + --- + - puppet-test + """ + And a file named "modulesync.yml" with: + """ + --- + namespace: maestrodev + git_base: https://github.com/ + """ + And a file named "config_defaults.yml" with: + """ + --- + README.md.erb: + """ + And a directory named "moduleroot" + And a file named "moduleroot/README.md" with: + """ + echo '<%= @configs[:git_base] + @configs[:namespace] %>' + """ + When I run `msync update --noop` + Then the exit status should be 0 + And the output should match: + """ + Files changed: + +diff --git a/README.md b/README.md + """ + Given I run `cat modules/puppet-test/README.md` + Then the output should contain: + """ + echo 'https://github.com/maestrodev' + """ + Scenario: Running the same update twice and pushing to a remote branch Given a mocked git configuration And a remote module repository diff --git a/lib/modulesync/renderer.rb b/lib/modulesync/renderer.rb index d1a15724..6250a6b6 100644 --- a/lib/modulesync/renderer.rb +++ b/lib/modulesync/renderer.rb @@ -10,8 +10,14 @@ def initialize(configs = {}) end def self.build(from_erb_template) - erb_obj = ERB.new(File.read(from_erb_template), nil, '-') - erb_obj.filename = from_erb_template.chomp('.erb') + from_erb_template = from_erb_template.chomp('.erb') + template_file = if File.exist?("#{from_erb_template}.erb") + "#{from_erb_template}.erb" + else + from_erb_template + end + erb_obj = ERB.new(File.read(template_file), nil, '-') + erb_obj.filename = from_erb_template erb_obj.def_method(ForgeModuleFile, 'render()') erb_obj end @@ -27,7 +33,7 @@ def self.render(_template, configs = {}) def self.sync(template, to_file) path = to_file.rpartition('/').first FileUtils.mkdir_p(path) unless path.empty? - File.open(to_file, 'w') do |file| + File.open(to_file.chomp('.erb'), 'w') do |file| file.write(template) end end diff --git a/lib/modulesync/settings.rb b/lib/modulesync/settings.rb index a62adf6e..25e5182d 100644 --- a/lib/modulesync/settings.rb +++ b/lib/modulesync/settings.rb @@ -12,8 +12,16 @@ def initialize(global_defaults, defaults, module_defaults, module_configs, addit @additional_settings = additional_settings end + def lookup_config(hash, filename) + hash[filename] || {} + end + def build_file_configs(filename) - global_defaults.merge(defaults[filename] || {}).merge(module_defaults).merge(module_configs[filename] || {}).merge(additional_settings) + file_def = lookup_config(defaults, filename) + file_md = lookup_config(module_defaults, filename) + file_mc = lookup_config(module_configs, filename) + + global_defaults.merge(file_def).merge(file_md).merge(file_mc).merge(additional_settings) end def managed?(filename)