Class: Pkgr::Config

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/pkgr/config.rb

Constant Summary collapse

DISTRO_COMPATIBILITY_MAPPING =
{
  "ubuntu-lucid" => "ubuntu-10.04",
  "ubuntu-precise" => "ubuntu-12.04",
  "debian-squeeze" => "debian-6",
  "debian-wheezy" => "debian-7",
  "debian-jessie" => "debian-8"
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_file(path, distribution) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pkgr/config.rb', line 17

def load_file(path, distribution)
  config = YAML.load_file(path) || {}
  Pkgr.debug "Configuration from file: #{config.inspect} - Distribution: #{distribution.inspect}."

  targets = config.delete("targets") || {}

  # backward compatibility
  DISTRO_COMPATIBILITY_MAPPING.each do |from, to|
    if targets.has_key?(from)
      targets[to] = targets.delete(from)
    end
  end

  distro_config = targets[distribution.to_s]
  if distro_config.is_a?(Hash)
    distro_config.each do |k,v|
      config[k] = v
    end
  end

  self.new(config)
end

Instance Method Details

#after_hookObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/pkgr/config.rb', line 164

def after_hook
  if after_precompile.nil? || after_precompile.empty?
    after_steps = self.after || []

    if after_steps.empty?
      nil
    else
      tmpfile = Tempfile.new(["after_hook", ".sh"])
      after_steps.each{|step| tmpfile.puts step}
      tmpfile.close
      tmpfile.path
    end
  else
    after_precompile
  end
end

#after_installObject



186
187
188
189
# File 'lib/pkgr/config.rb', line 186

def after_install
  return nil if @table[:after_install].nil?
  Pathname.new(source_dir).join(@table[:after_install]).realpath.to_s
end

#after_removeObject



196
197
198
199
# File 'lib/pkgr/config.rb', line 196

def after_remove
  return nil if @table[:after_remove].nil?
  Pathname.new(source_dir).join(@table[:after_remove]).realpath.to_s
end

#architectureObject



91
92
93
# File 'lib/pkgr/config.rb', line 91

def architecture
  @table[:architecture] || "x86_64"
end

#before_hookObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/pkgr/config.rb', line 147

def before_hook
  if before_precompile.nil? || before_precompile.empty?
    before_steps = self.before || []

    if before_steps.empty?
      nil
    else
      tmpfile = Tempfile.new(["before_hook", ".sh"])
      before_steps.each{|step| tmpfile.puts step}
      tmpfile.close
      tmpfile.path
    end
  else
    before_precompile
  end
end

#before_installObject



181
182
183
184
# File 'lib/pkgr/config.rb', line 181

def before_install
  return nil if @table[:before_install].nil?
  Pathname.new(source_dir).join(@table[:before_install]).realpath.to_s
end

#before_removeObject



191
192
193
194
# File 'lib/pkgr/config.rb', line 191

def before_remove
  return nil if @table[:before_remove].nil?
  Pathname.new(source_dir).join(@table[:before_remove]).realpath.to_s
end

#cli?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/pkgr/config.rb', line 75

def cli?
  @table.has_key?(:cli) ? @table[:cli] : true
end

#cronsObject



143
144
145
# File 'lib/pkgr/config.rb', line 143

def crons
  @table[:crons] || []
end

#delete(key) ⇒ Object



67
68
69
# File 'lib/pkgr/config.rb', line 67

def delete(key)
  @table.delete(key)
end

#descriptionObject



99
100
101
# File 'lib/pkgr/config.rb', line 99

def description
  @table[:description] || "No description given"
end

#eachObject



60
61
62
63
64
65
# File 'lib/pkgr/config.rb', line 60

def each
  @table.each do |k,v|
    next if v.nil?
    yield k, v
  end
end

#envObject



107
108
109
# File 'lib/pkgr/config.rb', line 107

def env
  @table[:env].is_a?(Pkgr::Env) ? @table[:env] : Pkgr::Env.new(@table[:env])
end

#errorsObject



122
123
124
# File 'lib/pkgr/config.rb', line 122

def errors
  @errors ||= []
end

#groupObject



87
88
89
# File 'lib/pkgr/config.rb', line 87

def group
  @table[:group] || user
end

#homeObject



79
80
81
# File 'lib/pkgr/config.rb', line 79

def home
  @table[:home] || "/opt/#{name}"
end

#homepageObject



95
96
97
# File 'lib/pkgr/config.rb', line 95

def homepage
  @table[:homepage] || "http://example.com/no-uri-given"
end

#installerObject



126
127
128
129
# File 'lib/pkgr/config.rb', line 126

def installer
  return nil if @table[:installer].nil? || @table[:installer] == false
  @table[:installer]
end

#maintainerObject



103
104
105
# File 'lib/pkgr/config.rb', line 103

def maintainer
  @table[:maintainer] || "<someone@pkgr>"
end

#merge(other) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pkgr/config.rb', line 45

def merge(other)
  new_config = self.class.new
  self.each{|k,v|
    new_value = case v
    when Array
      v | (other.delete(k) || [])
    else
      v
    end
    new_config.send("#{k}=".to_sym, new_value)
  }
  other.each{|k,v| new_config.send("#{k}=".to_sym, v)}
  new_config
end

#safe_nameObject



71
72
73
# File 'lib/pkgr/config.rb', line 71

def safe_name
  name.gsub("-", "_")
end

#sesameObject



41
42
43
# File 'lib/pkgr/config.rb', line 41

def sesame
  binding
end

#to_argsObject

TODO: DRY this with cli.rb



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/pkgr/config.rb', line 202

def to_args
  args = [
    "--name \"#{name}\"",
    "--version \"#{version}\"",
    "--user \"#{user}\"",
    "--group \"#{group}\"",
    "--iteration \"#{iteration}\"",
    "--homepage \"#{homepage}\"",
    "--architecture \"#{architecture}\"",
    "--description \"#{description}\"",
    "--maintainer \"#{maintainer}\"",
    "--vendor \"#{vendor}\""
  ]
  args.push "--dependencies #{dependencies.map{|d| "\"#{d}\""}.join}" unless dependencies.nil? || dependencies.empty?
  args.push "--build-dependencies #{build_dependencies.map{|d| "\"#{d}\""}.join}" unless build_dependencies.nil? || build_dependencies.empty?
  args.push "--compile-cache-dir \"#{compile_cache_dir}\"" unless compile_cache_dir.nil? || compile_cache_dir.empty?
  args.push "--before-precompile \"#{before_precompile}\"" unless before_precompile.nil? || before_precompile.empty?
  args.push "--after-precompile \"#{after_precompile}\"" unless after_precompile.nil? || after_precompile.empty?
  args.push "--before-install \"#{before_install}\"" unless before_install.nil? || before_install.empty?
  args.push "--after-install \"#{after_install}\"" unless after_install.nil? || after_install.empty?
  args.push "--before-remove \"#{before_remove}\"" unless before_remove.nil? || before_remove.empty?
  args.push "--after-remove \"#{after_remove}\"" unless after_remove.nil? || after_remove.empty?

  args.push "--license \"#{license}\"" unless license.nil? || license.empty?
  args.push "--buildpack \"#{buildpack}\"" unless buildpack.nil? || buildpack.empty?
  args.push "--buildpack_list \"#{buildpack_list}\"" unless buildpack_list.nil? || buildpack_list.empty?
  args.push "--force-os \"#{force_os}\"" unless force_os.nil? || force_os.empty?
  args.push "--runner \"#{runner}\"" unless runner.nil? || runner.empty?
  args.push "--env #{env.variables.map{|v| "\"#{v}\""}.join(" ")}" if env.present?
  args.push "--auto" if auto
  args.push "--verbose" if verbose
  args.push "--store-cache" if store_cache
  args.push "--debug" if debug
  args.push "--verify" if verify
  args.push "--no-clean" if !clean
  args.push "--no-edge" if !edge
  args
end

#userObject



83
84
85
# File 'lib/pkgr/config.rb', line 83

def user
  @table[:user] || name
end

#valid?Boolean

Returns:

  • (Boolean)


111
112
113
114
115
116
117
118
119
120
# File 'lib/pkgr/config.rb', line 111

def valid?
  @errors = []
  @errors.push("name can't be blank") if name.nil? || name.empty?
  @errors.push("version can't be blank") if version.nil? || version.empty?
  @errors.push("version must start with a digit") if !version.empty? && version !~ /^\d/
  @errors.push("iteration can't be blank") if iteration.nil? || iteration.empty?
  @errors.push("user can't be blank") if user.nil? || user.empty?
  @errors.push("group can't be blank") if group.nil? || group.empty?
  @errors.empty?
end

#wizardsObject



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/pkgr/config.rb', line 131

def wizards
  @wizards ||= (@table[:wizards] || []).map do |wizard_string|
    wizard_string.split(/\s*\|\s*/).map do |wizard|
      if wizard.start_with?('.')
        wizard = Pathname.new(source_dir).join(wizard).realpath
      end

      Addon.new(wizard)
    end
  end
end