Class: PuppetfileEditor::Module

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

Overview

Module class represents a puppet module in Puppetfile

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, args = nil) ⇒ Module

Returns a new instance of Module.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/puppetfile_editor/module.rb', line 12

def initialize(title, args = nil)
  @type    = :undef
  @params  = nil
  @message = nil
  @status  = nil
  if [{ local: true }, :local].include? args
    @type = :local
  elsif args.nil? || args.is_a?(String) || args.is_a?(Symbol)
    @type   = :forge
    @params = { version: args } unless args.nil?
  elsif args.is_a? Hash
    if args.key? :hg
      @type = :hg
    elsif args.key? :git
      @type = :git
    end
    @params = args
    calculate_indent
  end
  @author, @name = parse_title title
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



9
10
11
# File 'lib/puppetfile_editor/module.rb', line 9

def message
  @message
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/puppetfile_editor/module.rb', line 8

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



7
8
9
# File 'lib/puppetfile_editor/module.rb', line 7

def params
  @params
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

#typeObject (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/puppetfile_editor/module.rb', line 6

def type
  @type
end

Instance Method Details

#dump(args = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/puppetfile_editor/module.rb', line 120

def dump(args = {})
  output = []
  case @type
  when :hg, :git
    output.push "mod '#{full_title}'"
    @params.each do |param_name, param_value|
      if param_value == :latest
        value = ':latest'
      else
        value = "'#{param_value}'"
      end
      param = "#{param_name}:".ljust(@indent)
      output.push "    #{param} #{value}"
    end
  when :local
    if args[:legacy_local]
      output.push("mod '#{full_title}', :local")
    else
      output.push("mod '#{full_title}', local: true")
    end
  else
    if @params.nil?
      output.push("mod '#{full_title}'")
    else
      output.push("mod '#{full_title}', '#{@params[:version]}'")
    end
  end
  output.join(",\n")
end

#full_titleObject



150
151
152
153
154
# File 'lib/puppetfile_editor/module.rb', line 150

def full_title
  return "#{@author}/#{@name}" if @author

  @name
end

#full_versionObject



156
157
158
159
160
161
162
163
164
165
# File 'lib/puppetfile_editor/module.rb', line 156

def full_version
  case @type
  when :hg, :git
    @params.reject { |param, _| param.eql? @type }.map { |param, value| "#{param}: #{value}" }.sort.join(', ')
  when :forge
    return @params[:version] if @params.key? :version

    nil
  end
end

#merge_with(mod, force = false) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/puppetfile_editor/module.rb', line 62

def merge_with(mod, force = false)
  unless mod.type == @type
    set_message("type mismatch ('#{@type}' vs '#{mod.type}')", :type_mismatched)
    return
  end
  case @type
  when :hg, :git
    new = mod.params.reject { |param, _| param.eql? @type }
    if !force && new.keys == [:tag] && !([:branch, :ref, :changeset] & @params.keys).empty?
      set_message("kept at #{full_version}", :wont_upgrade)
      return
    end
    if full_version == mod.full_version
      set_message("versions match (#{full_version})", :matched)
      return
    end

    begin
      old_version = Gem::Version.new(full_version.sub('tag: ', ''))
      new_version = Gem::Version.new(mod.full_version.sub('tag: ', ''))
    rescue ArgumentError
      # Older rubies throw ArgumentError when version is '0.1.0-dev1', for example.
      # Sad.
      set_message("could not compare versions, so updating (#{full_version} to #{mod.full_version})", :updated)
    else
      if old_version > new_version
        set_message("not downgrading (#{full_version} > #{mod.full_version})", :matched)
        return
      else
        set_message("updated (#{full_version} to #{mod.full_version})", :updated)
      end
    end

    @params.delete_if { |param, _| [:branch, :tag, :ref, :changeset].include? param }
    @params.merge!(new)
    calculate_indent
  when :forge
    unless force
      if mod.params.nil? || mod.params.is_a?(Symbol)
        set_message("won't upgrade to #{mod.full_version}", :wont_upgrade)
        return
      end
    end
    if full_version == mod.full_version
      set_message("versions match (#{full_version})", :matched)
      return
    elsif Gem::Version.new(full_version) > Gem::Version.new(mod.full_version)
      set_message("not downgrading (#{full_version} > #{mod.full_version})", :matched)
      return
    else
      set_message("updated (#{full_version} to #{mod.full_version})", :updated)
    end
    @params = mod.params
  else
    set_message('only git, forge, and hg modules are supported for merging', :skipped)
  end
end

#set(param, newvalue, force = false) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/puppetfile_editor/module.rb', line 34

def set(param, newvalue, force = false)
  case @type
  when :hg, :git
    if !force && !([:branch, :ref, :changeset] & @params.keys).empty?
      set_message("kept at (#{full_version})", :wont_upgrade)
    elsif !%w[branch tag ref changeset].include? param
      set_message("only 'branch', 'tag', 'ref', and 'changeset' are supported for '#{@type}' modules.", :unsupported)
    else
      set_message("updated (#{full_version} to #{param}: #{newvalue}", :updated)
      @params.delete :branch
      @params.delete :tag
      @params.delete :ref
      @params.delete :changeset
      @params[param.to_sym] = newvalue
      calculate_indent
    end
  when :forge
    if param == 'version'
      @params[:version] = newvalue
      set_message("successfully set #{param} to #{newvalue} for #{@name}.", :updated)
    else
      set_message("only 'version' is supported for forge modules.", :unsupported)
    end
  else
    set_message("editing params for '#{@type}' modules is not supported.", :unsupported)
  end
end

#set_message(message, status) ⇒ Object



167
168
169
170
# File 'lib/puppetfile_editor/module.rb', line 167

def set_message(message, status)
  @message = message
  @status  = status
end