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, #fetch_cli, #find_or_create_remote, #find_ref, #find_tag, #get_content, #git_command, #git_url?, #push_branch, #push_tags, #rebase_branch, #ref_exists?, #remote_exists?, #remote_from_name, #remote_from_url, #remote_url_matches?, #remove_file, #repo, #tag_exists?, #tags, #transports, #up2date?

Constructor Details

#initialize(puppetfile = 'Puppetfile') ⇒ Puppetfile

Returns a new instance of Puppetfile.

Parameters:

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



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

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

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



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

def base_path
  @base_path
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#modulesArray[ControlMod]

Returns - a list of control mod objects.

Returns:

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



70
71
72
# File 'lib/release_manager/puppetfile.rb', line 70

def modules
  @modules
end

#puppetfileObject

Returns the value of attribute puppetfile.



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

def puppetfile
  @puppetfile
end

#puppetmoduleObject

Returns the value of attribute puppetmodule.



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

def puppetmodule
  @puppetmodule
end

Class Method Details

.from_string(s) ⇒ Object



78
79
80
81
82
# File 'lib/release_manager/puppetfile.rb', line 78

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

.to_puppetfile(json_data) ⇒ Object



165
166
167
168
169
170
171
172
173
# File 'lib/release_manager/puppetfile.rb', line 165

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



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

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

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



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

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



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

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

#diff(a, b) ⇒ Object



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

def diff(a,b)
  FileUtils.compare_stream(a,b)
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:



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

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



86
87
88
89
90
91
92
93
94
# File 'lib/release_manager/puppetfile.rb', line 86

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



179
180
181
# File 'lib/release_manager/puppetfile.rb', line 179

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

#mod(name, *args) ⇒ Object



175
176
177
# File 'lib/release_manager/puppetfile.rb', line 175

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



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

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

#pathObject

Returns the value of attribute base_path.



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

def base_path
  @base_path
end

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

Parameters:

  • remote (String)
    • the remote name

  • branch (String)
    • the branch to push

  • force (Boolean) (defaults to: false)
    • force push , defaults to false



57
58
59
60
# File 'lib/release_manager/puppetfile.rb', line 57

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

#sourceObject



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

def source
  puppetmodule.source
end

#to_json(pretty = false) ⇒ Object



139
140
141
142
143
144
145
# File 'lib/release_manager/puppetfile.rb', line 139

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

#to_sObject



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

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:



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

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_fileBoolean

Returns - true if writing to file occurred, false otherwise.

Returns:

  • (Boolean)
    • true if writing to file occurred, false otherwise



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

def write_to_file
  contents = to_s
  a = StringIO.new(contents)
  b = File.new(puppetfile)
  # do not write to the file if nothing changed
  return false if FileUtils.compare_stream(a,b)
  File.write(puppetfile, to_s)
end

#write_version(mod_name, version) ⇒ Object



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

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