Class: Puppetfile

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

Constant Summary collapse

BUMP_TYPES =
%w{patch minor major}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(puppetfile = 'Puppetfile') ⇒ Puppetfile

Returns a new instance of Puppetfile.

Parameters:

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



12
13
14
15
# File 'lib/release_manager/puppetfile.rb', line 12

def initialize(puppetfile = 'Puppetfile')
  @puppetfile = puppetfile
  @puppetmodule = PuppetModule.new(base_path)
end

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



8
9
10
# File 'lib/release_manager/puppetfile.rb', line 8

def base_path
  @base_path
end

#dataObject

Returns the value of attribute data.



8
9
10
# File 'lib/release_manager/puppetfile.rb', line 8

def data
  @data
end

#modulesArray[ControlMod]

Returns - a list of control mod objects.

Returns:

  • (Array[ControlMod])
    • a list of control mod objects



55
56
57
# File 'lib/release_manager/puppetfile.rb', line 55

def modules
  @modules
end

#puppetfileObject

Returns the value of attribute puppetfile.



8
9
10
# File 'lib/release_manager/puppetfile.rb', line 8

def puppetfile
  @puppetfile
end

#puppetmoduleObject

Returns the value of attribute puppetmodule.



8
9
10
# File 'lib/release_manager/puppetfile.rb', line 8

def puppetmodule
  @puppetmodule
end

Class Method Details

.from_string(s) ⇒ Object



63
64
65
66
67
# File 'lib/release_manager/puppetfile.rb', line 63

def self.from_string(s)
  instance = new
  instance.data = s
  instance
end

.to_puppetfile(json_data) ⇒ Object



130
131
132
133
134
135
136
137
138
# File 'lib/release_manager/puppetfile.rb', line 130

def self.to_puppetfile(json_data)
  obj = JSON.parse(json_data)
  mods = obj.collect do |name, |
    name = "mod '#{name}',"
    data = .sort.map { |k, v| ":#{k} => '#{v}'" }.join(",\n\  ")
    "#{name}\n  #{data}\n"
  end.join("\n")
  mods
end

Instance Method Details

#add_module(name, metadata) ⇒ Object



38
39
40
# File 'lib/release_manager/puppetfile.rb', line 38

def add_module(name, )
  modules[name] = ControlMod.new(name, )
end

#bump(mod_name, type = 'patch') ⇒ Object



108
109
110
111
112
# File 'lib/release_manager/puppetfile.rb', line 108

def bump(mod_name, type = 'patch')
  raise "Invalid type, must be one of #{BUMP_TYPES}" unless BUMP_TYPES.include?(type)
  mod = find_mod(mod_name)
  find_mod(mod_name).send("bump_#{type}_version")
end

#commit(message) ⇒ Object



29
30
31
32
# File 'lib/release_manager/puppetfile.rb', line 29

def commit(message)
  puts `#{git_command} add #{puppetfile}`
  puts `#{git_command} commit -n -m "[ReleaseManager] - #{message}"`
end

#current_branchObject



34
35
36
# File 'lib/release_manager/puppetfile.rb', line 34

def current_branch
  `#{git_command} rev-parse --abbrev-ref HEAD`
end

#find_mod(name) ⇒ Object

Returns ControlMod - a ControlMod object.

Parameters:

  • name (String)
    • the name of the mod you wish to find in the puppetfile

Returns:

  • ControlMod - a ControlMod object

Raises:



83
84
85
86
87
88
89
90
# File 'lib/release_manager/puppetfile.rb', line 83

def find_mod(name)
  mod_name = name.strip.downcase
  mod = modules[mod_name] || modules.find{ |module_name, mod| mod.repo =~ /#{mod_name}/i }
  raise InvalidModuleNameException.new("Invalid module name #{name}, cannot locate in Puppetfile") unless mod
  # since find returns an array we need to grab the element ouf of the array first
  return mod.last if mod.instance_of?(Array)
  mod
end

#find_mods(names) ⇒ Object

Parameters:

  • Array (String)

    names - find all mods with the following names



71
72
73
74
75
76
77
78
79
# File 'lib/release_manager/puppetfile.rb', line 71

def find_mods(names)
  mods = {}
  return mods if names.nil?
  names.each do | mod_name |
    m = find_mod(mod_name)
    mods[m.name] = m
  end
  mods
end

#forge(name, *args) ⇒ Object



144
145
146
# File 'lib/release_manager/puppetfile.rb', line 144

def forge(name, *args)
  # skip this for now
end

#git_commandObject



25
26
27
# File 'lib/release_manager/puppetfile.rb', line 25

def git_command
  "git --work-tree=#{base_path} --git-dir=#{base_path}/.git"
end

#mod(name, *args) ⇒ Object



140
141
142
# File 'lib/release_manager/puppetfile.rb', line 140

def mod(name, *args)
  @modules[name] = ControlMod.new(name, args.flatten.first)
end

#push(remote, branch, force = false) ⇒ Object



42
43
44
45
# File 'lib/release_manager/puppetfile.rb', line 42

def push(remote, branch, force = false)
  opts = force ? '-f' : ''
  `#{git_command} push #{remote} #{branch} #{opts}`
end

#sourceObject



17
18
19
# File 'lib/release_manager/puppetfile.rb', line 17

def source
  puppetmodule.source
end

#to_json(pretty = false) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/release_manager/puppetfile.rb', line 114

def to_json(pretty = false)
  if pretty
    JSON.pretty_generate(modules)
  else
    modules.to_json
  end
end

#to_sObject



126
127
128
# File 'lib/release_manager/puppetfile.rb', line 126

def to_s
  modules.collect {|n, mod| mod.to_s }.join("\n\n")
end

#write_source(mod_name, src, branch = nil) ⇒ ControlMod

Parameters:

  • mod_name (String)
    • the module name found in the puppetfile

  • src (String)
    • the git url to the source

  • [String] (Hash)

    a customizable set of options

Returns:



101
102
103
104
105
106
# File 'lib/release_manager/puppetfile.rb', line 101

def write_source(mod_name, src, branch = nil)
  mod = find_mod(mod_name)
  mod.pin_url(src)
  mod.pin_branch(branch) if branch
  mod
end

#write_to_fileObject



122
123
124
# File 'lib/release_manager/puppetfile.rb', line 122

def write_to_file
  File.write(puppetfile, to_s)
end

#write_version(mod_name, version) ⇒ Object



92
93
94
95
# File 'lib/release_manager/puppetfile.rb', line 92

def write_version(mod_name, version)
  mod = find_mod(mod_name)
  mod.pin_version(version)
end