Skip to content

Commit

Permalink
allow .erb extension
Browse files Browse the repository at this point in the history
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: voxpupuli/modulesync_config#295
  • Loading branch information
james.powis committed Mar 2, 2017
1 parent 834326f commit c249742
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 4 deletions.
105 changes: 105 additions & 0 deletions features/update.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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'
"""
12 changes: 9 additions & 3 deletions lib/modulesync/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 9 additions & 1 deletion lib/modulesync/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c249742

Please sign in to comment.