Class: PuppetfileEditor::Puppetfile
- Inherits:
-
Object
- Object
- PuppetfileEditor::Puppetfile
- Defined in:
- lib/puppetfile_editor/puppetfile.rb
Overview
Puppetfile implementation
Instance Attribute Summary collapse
-
#module_sections ⇒ Object
readonly
Returns the value of attribute module_sections.
-
#modules ⇒ Object
readonly
Returns the value of attribute modules.
-
#puppetfile_path ⇒ Object
readonly
Returns the value of attribute puppetfile_path.
Instance Method Summary collapse
- #add_module(name, args) ⇒ Object
- #compare_with(pf) ⇒ Object
- #dump ⇒ Object
- #generate_puppetfile ⇒ Object
-
#initialize(path = 'Puppetfile', from_stdin = false, contents = nil) ⇒ Puppetfile
constructor
A new instance of Puppetfile.
- #load ⇒ Object
- #update_forge_url(url) ⇒ Object
- #update_module(name, param, value) ⇒ Object
Constructor Details
#initialize(path = 'Puppetfile', from_stdin = false, contents = nil) ⇒ Puppetfile
Returns a new instance of Puppetfile.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/puppetfile_editor/puppetfile.rb', line 17 def initialize(path = 'Puppetfile', from_stdin = false, contents = nil) @puppetfile_path = path @from_stdin = from_stdin @contents = contents @modules = {} @loaded = false @forge = nil @module_sections = { local: 'Local modules', forge: 'Modules from the Puppet Forge', hg: 'Mercurial modules', git: 'Git modules', undef: 'Other modules', } end |
Instance Attribute Details
#module_sections ⇒ Object (readonly)
Returns the value of attribute module_sections.
14 15 16 |
# File 'lib/puppetfile_editor/puppetfile.rb', line 14 def module_sections @module_sections end |
#modules ⇒ Object (readonly)
Returns the value of attribute modules.
8 9 10 |
# File 'lib/puppetfile_editor/puppetfile.rb', line 8 def modules @modules end |
#puppetfile_path ⇒ Object (readonly)
Returns the value of attribute puppetfile_path.
12 13 14 |
# File 'lib/puppetfile_editor/puppetfile.rb', line 12 def puppetfile_path @puppetfile_path end |
Instance Method Details
#add_module(name, args) ⇒ Object
106 107 108 109 |
# File 'lib/puppetfile_editor/puppetfile.rb', line 106 def add_module(name, args) mod = PuppetfileEditor::Module.new(name, args) @modules[mod.name] = mod end |
#compare_with(pf) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/puppetfile_editor/puppetfile.rb', line 81 def compare_with(pf) diff = {} pf.modules.each do |mod_name, mod| next unless [:git, :hg, :forge].include? mod.type version_key = @type == :forge ? :version : :tag unless @modules.key? mod_name if mod.params.key?(version_key) diff[mod_name] = { new: mod.params[version_key] } end next end local_mod = @modules[mod_name] next unless mod.type == local_mod.type next unless mod.params.key?(version_key) && local_mod.params.key?(version_key) next if mod.params[version_key] == local_mod.params[version_key] diff[mod_name] = { old: local_mod.params[version_key], new: mod.params[version_key] } end diff end |
#dump ⇒ Object
69 70 71 |
# File 'lib/puppetfile_editor/puppetfile.rb', line 69 def dump File.write(@puppetfile_path, generate_puppetfile) if @loaded end |
#generate_puppetfile ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/puppetfile_editor/puppetfile.rb', line 49 def generate_puppetfile raise StandardError, 'File is not loaded' unless @loaded contents = [] contents.push "forge '#{@forge}'\n" if @forge @module_sections.each do |module_type, module_comment| module_list = modules.select { |_, mod| mod.type == module_type } next unless module_list.any? contents.push "# #{module_comment}" module_list.values.sort_by(&:name).each do |mod| contents.push mod.dump end contents.push '' end contents.join("\n") end |
#load ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/puppetfile_editor/puppetfile.rb', line 34 def load puppetfile_contents = if @from_stdin $stdin.gets(nil).chomp elsif @contents @contents else raise(IOError, "'#{@puppetfile_path}' is missing or unreadable") unless File.readable?(@puppetfile_path) File.read @puppetfile_path end dsl = PuppetfileEditor::DSL.new(self) dsl.instance_eval(puppetfile_contents) @loaded = true end |
#update_forge_url(url) ⇒ Object
111 112 113 114 |
# File 'lib/puppetfile_editor/puppetfile.rb', line 111 def update_forge_url(url) raise StandardError, "Forge URL must be a String, but it is a #{url.class}" unless url.is_a? String @forge = url end |
#update_module(name, param, value) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/puppetfile_editor/puppetfile.rb', line 73 def update_module(name, param, value) if @modules.key? name @modules[name].set(param, value, true) else raise StandardError, "Module #{name} does not exist in your Puppetfile" end end |