Class: Packtory::Command

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/packtory/command.rb

Constant Summary

Constants included from Constants

Packtory::Constants::PACKTORY_BIN_NAME

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCommand

Returns a new instance of Command.



16
17
18
19
20
# File 'lib/packtory/command.rb', line 16

def initialize
  @options = { }
  @completed = false
  @exit_number = 0
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/packtory/command.rb', line 7

def options
  @options
end

Class Method Details

.run(argv) ⇒ Object



12
13
14
# File 'lib/packtory/command.rb', line 12

def self.run(argv)
  self.new.run(argv)
end

.silent!Object



9
# File 'lib/packtory/command.rb', line 9

def self.silent!; @@silent = true; end

.silent?Boolean

Returns:

  • (Boolean)


10
# File 'lib/packtory/command.rb', line 10

def self.silent?; defined?(@@silent) ? @@silent : false; end

Instance Method Details

#complete_and_exit!(e = 0) ⇒ Object



308
309
310
311
# File 'lib/packtory/command.rb', line 308

def complete_and_exit!(e = 0)
  @completed = true
  @exit_number = e
end

#completed?Boolean

Returns:

  • (Boolean)


306
# File 'lib/packtory/command.rb', line 306

def completed?; @completed; end

#detect_and_perform_command(argv) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/packtory/command.rb', line 272

def detect_and_perform_command(argv)
  detect_package_opts(argv)
  detect_ruby_opts(argv)

  if argv[0] == 'build'
    perform_build_command(argv)
  elsif argv[0] == 'man'
    show_man
  elsif argv[0].nil? || argv[0] == ''
    say 'ERROR: Build path not specified, aborting.'
    complete_and_exit! 1
  else
    @build_path = File.expand_path(argv[0])
    unless File.exists?(@build_path)
      say 'ERROR: Build path %s do not exist, aborting.' % @build_path
      complete_and_exit! 1
    end

    Packtory.config[:path] = @build_path
    say 'Using build path : %s' % @build_path
    perform_build_command(argv)
  end
end

#detect_fpm(argv) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/packtory/command.rb', line 22

def detect_fpm(argv)
  if !ENV['FPM_EXEC_PATH'].nil? && !ENV['FPM_EXEC_PATH'].empty?
    fpm_exec_path = File.expand_path(ENV['FPM_EXEC_PATH'])
  else
    fpm_exec_path = Packtory.bin_support_fpm_path
  end

  Packtory.config[:fpm_exec_path] = fpm_exec_path

  if !ENV['FPM_USE_RUBY_PATH'].nil? && !ENV['FPM_USE_RUBY_PATH'].empty?
    Packtory.config[:fpm_use_ruby_path] = ENV['FPM_USE_RUBY_PATH']
  elsif !ENV['RUBY_PATH'].nil? && !ENV['RUBY_PATH'].empty?
    Packtory.config[:fpm_use_ruby_path] = ENV['RUBY_PATH']
  end

  if ENV['FPM_EXEC_VERBOSE'] && ENV['FPM_EXEC_VERBOSE'] == '1'
    Packtory.config[:fpm_exec_verbose] = true
  end

  if !ENV['FPM_EXEC_LOG'].nil? && !ENV['FPM_EXEC_LOG'].empty?
    Packtory.config[:fpm_exec_verbose] = true
    Packtory.config[:fpm_exec_log] = ENV['FPM_EXEC_LOG']
  end

  self
end

#detect_gemfile(argv) ⇒ Object



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
# File 'lib/packtory/command.rb', line 79

def detect_gemfile(argv)
  @bundle_gemfile = nil

  if ENV['BUNDLE_GEMFILE'] && !ENV['BUNDLE_GEMFILE'].empty?
    @bundle_gemfile = ENV['BUNDLE_GEMFILE']

    unless @bundle_gemfile =~ /^\/(.+)$/
      @bundle_gemfile = File.join(@build_path, @bundle_gemfile)
    end

    unless File.exists?(@bundle_gemfile)
      say 'ERROR: Specified bundle gemfile %s not found, aborting.' % @bundle_gemfile
      exit 1
    end

    say 'Using Gemfile    : %s' % @bundle_gemfile
    Packtory.config[:gemfile] = @bundle_gemfile
  end

  if ENV['BUNDLER_INCLUDE']
    Packtory.config[:bundler_include] = true
  end

  self
end

#detect_package_opts(argv) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/packtory/command.rb', line 105

def detect_package_opts(argv)
  packages = Packtory.config[:packages] || [ ]

  if ENV['PACKAGE_OUTPUT'] == 'rpm'
    packages << :rpm
  elsif ENV['PACKAGE_OUTPUT'] == 'deb'
    packages << :deb
  elsif ENV['PACKAGE_OUTPUT'] == 'tgz'
    packages << :tgz
  elsif ENV['PACKAGE_OUTPUT'] == 'brew'
    packages << :brew
  elsif packages.empty?
    packages << :deb
  end

  Packtory.config[:packages] = packages

  if ENV['PACKAGE_PATH'] && Packtory.config[:pkg_path].nil?
    Packtory.config[:pkg_path] = File.expand_path(ENV['PACKAGE_PATH'])
  end

  if !ENV['PACKAGE_NAME'].nil? && !ENV['PACKAGE_NAME'].empty?
    Packtory.config[:package_name] = ENV['PACKAGE_NAME']
  end

  if ENV['PACKAGE_DEPENDENCIES']
    deps = ENV['PACKAGE_DEPENDENCIES'].split(',')
    deps.each do |d|
      if d =~ /^([^\<\>\=]+)(.+)?$/
        pname = $~[1]
        pver = $~[2]

        Packtory.config[:dependencies][pname] = pver
      end
    end
  end

  self
end

#detect_ruby_opts(argv) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/packtory/command.rb', line 145

def detect_ruby_opts(argv)
  ruby_ver = nil
  if ENV['PACKAGE_RUBY_VERSION'] && !ENV['PACKAGE_RUBY_VERSION'].empty?
    ruby_ver = ENV['PACKAGE_RUBY_VERSION']
  end

  Packtory.config[:dependencies]['ruby'] = ruby_ver
end

#detect_specfile(argv) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/packtory/command.rb', line 49

def detect_specfile(argv)
  if ENV['GEM_SPECFILE']
    @gemspec_file = ENV['GEM_SPECFILE']
    unless @gemspec_file =~ /^\/(.+)$/
      @gemspec_file = File.join(@build_path, @gemspec_file)
    end

    unless File.exists?(@gemspec_file)
      say 'ERROR: Specified gemspec file %s not found, aborting.' % @gemspec_file
      exit 1
    end
  else
    paths = Dir.glob(File.join(@build_path, '/*.gemspec'))
    if paths.empty?
      say 'ERROR: No gemspec file found, aborting.'
      exit 1
    elsif paths.count > 1
      say 'ERROR: Multiple gemspec file found, aborting.'
      exit 1
    end

    @gemspec_file = paths[0]
  end

  say 'Using spec file  : %s' % @gemspec_file
  Packtory.config[:gemspec] = @gemspec_file

  self
end

#exec_fpmObject



221
222
223
224
225
226
227
228
# File 'lib/packtory/command.rb', line 221

def exec_fpm
  if Packtory.config[:fpm_use_ruby_path].nil?
    Packtory.config[:fpm_exec_path]
  else
    'env FPM_USE_RUBY_PATH=%s %s' % [ Packtory.config[:fpm_use_ruby_path],
                                      Packtory.config[:fpm_exec_path] ]
  end
end

#options_parserObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/packtory/command.rb', line 154

def options_parser
  OptionParser.new do |opts|
    opts.banner = 'Usage: %s [options]' % PACKTORY_BIN_NAME

    opts.on('-t TYPE', '--type=TYPE', 'Type of package to build (e.g. deb | rpm | brew)') do |t|
      Packtory.config[:packages] = [ t.to_sym ]
    end

    opts.on('-n PACKAGE_NAME', '--pkgname=PACKAGE_NAME', 'Override detected and set specific package name') do |n|
      Packtory.config[:package_name] = n
    end

    opts.on('-p PACKAGE_PATH', '--pkgpath=PACKAGE_PATH', 'Path where to create package') do |p|
      Packtory.config[:pkg_path] = File.expand_path(p)
    end

    opts.on('-v', 'Show version') do |v|
      options[:show_version] = true
    end
  end
end

#perform_build_command(argv) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/packtory/command.rb', line 252

def perform_build_command(argv)
  detect_fpm(argv)
  detect_specfile(argv)
  detect_gemfile(argv)

  $:.unshift(@build_path)
  Dir.chdir(@build_path)

  Packtory.setup
  show_configs

  if ENV['TEST_DUMPINFO']
    test_dumpinfo(argv)
  elsif ENV['TEST_NOBUILD']
    complete_and_exit!
  else
    Packtory.build_package
  end
end

#perform_optionsObject



176
177
178
179
180
# File 'lib/packtory/command.rb', line 176

def perform_options
  if options[:show_version]
    show_version
  end
end

#run(argv) ⇒ Object



296
297
298
299
300
301
302
303
304
# File 'lib/packtory/command.rb', line 296

def run(argv)
  options_parser.parse!(argv)
  perform_options

  detect_and_perform_command(argv) unless completed?
  complete_and_exit! unless completed?

  @exit_number
end

#say(msg) ⇒ Object



313
314
315
316
317
# File 'lib/packtory/command.rb', line 313

def say(msg)
  unless self.class.silent?
    puts msg
  end
end

#show_configsObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/packtory/command.rb', line 192

def show_configs
  say 'Using fpm path   : %s' % Packtory.config[:fpm_exec_path]
  unless Packtory.config[:fpm_use_ruby_path].nil?
    say 'Fpm using ruby   : %s' % Packtory.config[:fpm_use_ruby_path]
  end
  say 'Using fpm        : %s' % `#{exec_fpm} -v`.strip

  say 'Package output   : %s' % Packtory.config[:packages].join(', ')

  unless Packtory.config[:pkg_path].nil?
    say 'Package path     : %s' % Packtory.config[:pkg_path]
  end

  unless Packtory.config[:package_name].nil?
    say 'Package name     : %s' % Packtory.config[:package_name]
  end

  if Packtory.config[:dependencies].include?('ruby')
    ruby_ver = Packtory.config[:dependencies]['ruby']
    if ruby_ver.nil?
      say 'Ruby deps        : latest'
    else
      say 'Ruby deps        : %s' % ruby_ver
    end
  end

  say '=================='
end

#show_manObject



187
188
189
190
# File 'lib/packtory/command.rb', line 187

def show_man
  man_path = File.expand_path('../../../man/packtory.1', __FILE__)
  Kernel.exec('man %s' % man_path)
end

#show_versionObject



182
183
184
185
# File 'lib/packtory/command.rb', line 182

def show_version
  say '%s' % Packtory::VERSION
  complete_and_exit!
end

#test_dumpinfo(argv) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/packtory/command.rb', line 230

def test_dumpinfo(argv)
  info_h = {
    :version => ::Packtory::VERSION,
    :fpm_version => `#{exec_fpm} -v`.strip
  }.merge(Packtory.config)

  dump_file = ENV['TEST_DUMPINFO'] || '1'
  if dump_file == '1'
    f = StringIO.new
  else
    f = File.open(File.expand_path(dump_file), 'w+')
    say 'Created dump file: %s' % dump_file
  end

  f.write(YAML.dump(info_h))
  f.rewind
  say f.read
  f.close

  self
end