Class: Puppetfile

Inherits:
Object
  • Object
show all
Includes:
ReleaseManager::Git::Utilities, ReleaseManager::Logger, ReleaseManager::VCSManager
Defined in:
lib/release_manager/puppetfile.rb

Constant Summary collapse

BUMP_TYPES =
%w{patch minor major}

Instance Attribute Summary collapse

Attributes included from ReleaseManager::VCSManager

#vcs

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ReleaseManager::VCSManager

adapter_instance, adapter_types, default_instance

Methods included from ReleaseManager::Logger

#color, #log_level, #logger

Methods included from ReleaseManager::Git::Utilities

#add_all, #add_file, #add_remote, #apply_diff, #apply_patch, #author, #author_email, #author_name, #branch_exist?, #changed_files, #checkout_branch, #cherry_pick, #cli_create_commit, #clone, #create_branch, #create_commit, #create_diff, #create_diff_obj, #create_local_tag, #credentials, #current_branch, #current_branch?, #delete_branch, #fetch, #find_or_create_remote, #find_ref, #get_content, #git_command, #git_url?, #push_branch, #push_tags, #rebase_branch, #remote_exists?, #remote_from_name, #remote_from_url, #remote_url_matches?, #remove_file, #repo, #transports, #up2date?, #update_cli_index

Constructor Details

#initialize(puppetfile = 'Puppetfile') ⇒ Puppetfile

Returns a new instance of Puppetfile.

Parameters:

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



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

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



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

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



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

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

.to_puppetfile(json_data) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/release_manager/puppetfile.rb', line 150

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



48
49
50
# File 'lib/release_manager/puppetfile.rb', line 48

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

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



128
129
130
131
132
# File 'lib/release_manager/puppetfile.rb', line 128

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, remote = false) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/release_manager/puppetfile.rb', line 31

def commit(message, remote = false)
  message = "[ReleaseManager] - #{message}"
  if remote
    actions = [{
       action: 'update',
       file_path: puppetfile.split(repo.workdir).last,
       content: to_s
    }]
    obj = vcs_create_commit(source, 'master', message, actions)
    obj.id if obj
  else
    write_to_file
    add_file(puppetfile)
    create_commit(message)
  end
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:



93
94
95
96
97
98
99
100
# File 'lib/release_manager/puppetfile.rb', line 93

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



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

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



164
165
166
# File 'lib/release_manager/puppetfile.rb', line 164

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

#mod(name, *args) ⇒ Object



160
161
162
# File 'lib/release_manager/puppetfile.rb', line 160

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

#mod_exists?(name) ⇒ Boolean

Returns - true if the module is found.

Parameters:

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

Returns:

  • (Boolean)
    • true if the module is found



104
105
106
107
108
109
110
# File 'lib/release_manager/puppetfile.rb', line 104

def mod_exists?(name)
  begin
    !!find_mod(name)
  rescue InvalidModuleNameException
    false
  end
end

#pathObject

Returns the value of attribute base_path.



15
16
17
# File 'lib/release_manager/puppetfile.rb', line 15

def base_path
  @base_path
end

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



52
53
54
55
# File 'lib/release_manager/puppetfile.rb', line 52

def push(remote, branch, force = false)
  push_branch(remote, branch, force)
  push_tags(remote)
end

#sourceObject



23
24
25
# File 'lib/release_manager/puppetfile.rb', line 23

def source
  puppetmodule.source
end

#to_json(pretty = false) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/release_manager/puppetfile.rb', line 134

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

#to_sObject



146
147
148
# File 'lib/release_manager/puppetfile.rb', line 146

def to_s
  modules.sort.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:



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

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



142
143
144
# File 'lib/release_manager/puppetfile.rb', line 142

def write_to_file
  File.write(puppetfile, to_s)
end

#write_version(mod_name, version) ⇒ Object



112
113
114
115
# File 'lib/release_manager/puppetfile.rb', line 112

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