Class: Pkgr::Config

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_file(path, distribution) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/pkgr/config.rb', line 7

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

  targets = config.delete("targets") || {}
  (targets[distribution.to_s] || {}).each do |k,v|
    config[k] = v
  end

  self.new(config)
end

Instance Method Details

#after_hookObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/pkgr/config.rb', line 114

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

#architectureObject



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

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

#before_hookObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/pkgr/config.rb', line 97

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

#delete(key) ⇒ Object



46
47
48
# File 'lib/pkgr/config.rb', line 46

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

#descriptionObject



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

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

#eachObject



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

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

#envObject



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

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

#errorsObject



93
94
95
# File 'lib/pkgr/config.rb', line 93

def errors
  @errors ||= []
end

#groupObject



58
59
60
# File 'lib/pkgr/config.rb', line 58

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

#homeObject



50
51
52
# File 'lib/pkgr/config.rb', line 50

def home
  "/opt/#{name}"
end

#homepageObject



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

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

#maintainerObject



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

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

#merge(other) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pkgr/config.rb', line 24

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

#sesameObject



20
21
22
# File 'lib/pkgr/config.rb', line 20

def sesame
  binding
end

#to_argsObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/pkgr/config.rb', line 131

def to_args
  args = [
    "--name \"#{name}\"",
    "--version \"#{version}\"",
    "--user \"#{user}\"",
    "--group \"#{group}\"",
    "--iteration \"#{iteration}\"",
    "--homepage \"#{homepage}\"",
    "--architecture \"#{architecture}\"",
    "--target \"#{target}\"",
    "--description \"#{description}\"",
    "--maintainer \"#{maintainer}\""
  ]
  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 "--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 "--env #{env.variables.map{|v| "\"#{v}\""}.join(" ")}" if env.present?
  args.push "--auto" if auto
  args.push "--verbose" if verbose
  args.push "--debug" if debug
  args.push "--no-clean" if !clean
  args.push "--no-edge" if !edge
  args
end

#userObject



54
55
56
# File 'lib/pkgr/config.rb', line 54

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

#valid?Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
91
# File 'lib/pkgr/config.rb', line 82

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