Skip to content

Commit

Permalink
Merge pull request #93 from voxpupuli/fix_moduleroot_rubocop
Browse files Browse the repository at this point in the history
allow .erb extension
  • Loading branch information
domcleal authored Mar 3, 2017
2 parents 7ddc50d + 41ef2e6 commit 5d5be63
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 @@ -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
Expand Down
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 5d5be63

Please sign in to comment.