From c249742ce8c0a0122d5521693f194d7d4f9acb6d Mon Sep 17 00:00:00 2001 From: "james.powis" Date: Tue, 3 Jan 2017 21:23:24 -0700 Subject: [PATCH] allow .erb extension This change allows for files to be named with or without .erb extension This also addresses when there are numerous .rubocop.yml files which may error if containing erb code. Settings may include .erb extension if desired but not required even if the template file is named *.erb test reflect this agnostic behavior. reference: https://github.com/voxpupuli/modulesync_config/issues/295 --- features/update.feature | 105 +++++++++++++++++++++++++++++++++++++ lib/modulesync/renderer.rb | 12 +++-- lib/modulesync/settings.rb | 10 +++- 3 files changed, 123 insertions(+), 4 deletions(-) diff --git a/features/update.feature b/features/update.feature index 4858dcf0..52c7f064 100644 --- a/features/update.feature +++ b/features/update.feature @@ -589,3 +589,108 @@ 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' + """ 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 212bd210..672a295d 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)