Class: PuppetfileEditor::Puppetfile

Inherits:
Object
  • Object
show all
Defined in:
lib/puppetfile_editor/puppetfile.rb

Overview

Puppetfile implementation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = 'Puppetfile', from_stdin = false, contents = nil) ⇒ Puppetfile

Returns a new instance of Puppetfile.

Parameters:

  • path (String) (defaults to: 'Puppetfile')

    path to Puppetfile



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/puppetfile_editor/puppetfile.rb', line 19

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_sectionsObject (readonly)

Returns the value of attribute module_sections.



16
17
18
# File 'lib/puppetfile_editor/puppetfile.rb', line 16

def module_sections
  @module_sections
end

#modulesObject (readonly)

Returns the value of attribute modules.



10
11
12
# File 'lib/puppetfile_editor/puppetfile.rb', line 10

def modules
  @modules
end

#puppetfile_pathObject (readonly)

Returns the value of attribute puppetfile_path.



14
15
16
# File 'lib/puppetfile_editor/puppetfile.rb', line 14

def puppetfile_path
  @puppetfile_path
end

Instance Method Details

#add_module(name, args) ⇒ Object

Parameters:

  • name (String)

    Module name

  • args (String, Hash)

    Module arguments



110
111
112
113
# File 'lib/puppetfile_editor/puppetfile.rb', line 110

def add_module(name, args)
  mod                = PuppetfileEditor::Module.new(name, args)
  @modules[mod.name] = mod
end

#compare_with(pfile, compare_across_types: false) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/puppetfile_editor/puppetfile.rb', line 83

def compare_with(pfile, compare_across_types: false)
  diff = {}
  pfile.modules.each do |mod_name, mod|
    next unless [:git, :hg, :forge].include? mod.type

    mod.type == :forge ? version_key = :version : version_key = :tag

    unless @modules.key? mod_name
      diff[mod_name] = { new: mod.params[version_key], type: mod.type } if mod.params.key?(version_key)
      next
    end

    local_mod = @modules[mod_name]

    unless compare_across_types
      next unless mod.type == local_mod.type
    end
    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], type: mod.type }
  end
  diff
end

#delete_module(name) ⇒ Object



115
116
117
# File 'lib/puppetfile_editor/puppetfile.rb', line 115

def delete_module(name)
  @modules.delete(name)
end

#dump(args = {}) ⇒ Object



73
74
75
# File 'lib/puppetfile_editor/puppetfile.rb', line 73

def dump(args = {})
  File.write(@puppetfile_path, generate_puppetfile(args)) if @loaded
end

#generate_puppetfile(args = {}) ⇒ Object

Raises:

  • (StandardError)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/puppetfile_editor/puppetfile.rb', line 52

def generate_puppetfile(args = {})
  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(args)
    end
    contents.push ''
  end

  contents.join("\n")
end

#loadObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/puppetfile_editor/puppetfile.rb', line 36

def load
  if @from_stdin
    puppetfile_contents = $stdin.gets(nil).chomp
  elsif @contents
    puppetfile_contents = @contents
  else
    raise(IOError, "'#{@puppetfile_path}' is missing or unreadable") unless File.readable?(@puppetfile_path)

    puppetfile_contents = File.read @puppetfile_path
  end

  dsl = PuppetfileEditor::DSL.new(self)
  dsl.instance_eval(puppetfile_contents)
  @loaded = true
end

#update_forge_url(url) ⇒ Object

Raises:

  • (StandardError)


119
120
121
122
123
# File 'lib/puppetfile_editor/puppetfile.rb', line 119

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

Raises:

  • (StandardError)


77
78
79
80
81
# File 'lib/puppetfile_editor/puppetfile.rb', line 77

def update_module(name, param, value)
  raise StandardError, "Module #{name} does not exist in your Puppetfile" unless @modules.key? name

  @modules[name].set(param, value, true)
end