Class: Packtory::Packer

Inherits:
Object
  • Object
show all
Defined in:
lib/packtory/packer.rb

Defined Under Namespace

Classes: TemplateFile

Constant Summary collapse

BUNDLE_TARGET_PATH =
'bundle'
BUNDLE_EXTENSIONS_PATH =
'extensions'
BUNDLE_PACKTORY_TOOLS_PATH =
'packtory_tools'
BUNDLE_BUNDLER_SETUP_FILE =
'bundler/setup.rb'
DEFAULT_BIN_PATH =
'/usr/local/bin'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = { }) ⇒ Packer

Returns a new instance of Packer.



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

def initialize(opts = { })
  @opts = Packtory.config.merge(opts)

  unless @opts[:gemfile].nil?
    @gemfile = Pathname.new(@opts[:gemfile])
  else
    @gemfile = nil
  end

  if @opts[:gemspec].nil?
    @gemspec_file = find_default_gemspec_file
  else
    @gemspec_file = @opts[:gemspec]
  end

  if @gemfile.nil?
    @gemfile = autogenerate_clean_gemfile
  end

  @files = nil

  self
end

Instance Attribute Details

#gemfileObject (readonly)

Returns the value of attribute gemfile.



23
24
25
# File 'lib/packtory/packer.rb', line 23

def gemfile
  @gemfile
end

#gemspec_fileObject (readonly)

Returns the value of attribute gemspec_file.



24
25
26
# File 'lib/packtory/packer.rb', line 24

def gemspec_file
  @gemspec_file
end

#optsObject (readonly)

Returns the value of attribute opts.



22
23
24
# File 'lib/packtory/packer.rb', line 22

def opts
  @opts
end

Class Method Details

.silence_warningsObject



14
15
16
17
18
19
20
# File 'lib/packtory/packer.rb', line 14

def self.silence_warnings
  original_verbosity = $VERBOSE
  $VERBOSE = nil
  ret = yield
  $VERBOSE = original_verbosity
  ret
end

Instance Method Details

#add_bin_files(files, target_prefix_path, package_path = nil) ⇒ Object



505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# File 'lib/packtory/packer.rb', line 505

def add_bin_files(files, target_prefix_path, package_path = nil)
  bin_path = @opts[:bin_path] || DEFAULT_BIN_PATH

  if @opts[:binstub].nil?
    unless gemspec.nil? || gemspec.executables.nil? || gemspec.executables.empty?
      @opts[:binstub] = { }

      gemspec.executables.each do |exec_fname|
        @opts[:binstub][exec_fname] = File.join(bin_path, exec_fname)
      end
    end
  end

  @opts[:binstub].each do |binstub_fname, binstub_file|
    src_binstub_file = create_binstub(binstub_fname, target_prefix_path)
    files[src_binstub_file] = binstub_file
  end unless @opts[:binstub].nil?

  files
end

#add_ruby_build_dependencies!Object



305
306
307
308
309
310
311
312
313
314
315
# File 'lib/packtory/packer.rb', line 305

def add_ruby_build_dependencies!
  unless @opts[:dependencies].include?('ruby-dev')
    @opts[:dependencies]['ruby-dev'] = @opts[:dependencies]['ruby']
  end

  unless @opts[:dependencies].include?('ruby-build')
    @opts[:dependencies]['ruby-build'] = @opts[:dependencies]['ruby']
  end

  @opts[:dependencies]
end

#after_install_script_pathObject



535
536
537
538
# File 'lib/packtory/packer.rb', line 535

def after_install_script_path
  ftarget_path, _ = path_to_gathered_file(@files, Packtory.after_install_script_path)
  ftarget_path
end

#architectureObject



241
242
243
# File 'lib/packtory/packer.rb', line 241

def architecture
  @opts[:architecture]
end

#authorObject



222
223
224
# File 'lib/packtory/packer.rb', line 222

def author
  gemspec.authors.join(', ')
end

#autogenerate_clean_gemfileObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/packtory/packer.rb', line 152

def autogenerate_clean_gemfile
  FileUtils.mkpath(working_path)

  gemfile_path = File.join(working_path, 'Gemfile')
  File.open(gemfile_path, 'w') do |f|
    gemfile_content = <<GEMFILE
source 'http://rubygems.org'
gemspec :path => '%s', :name => '%s'
GEMFILE

    gemfile_content = gemfile_content % [ File.dirname(@gemspec_file),
                                          File.basename(@gemspec_file, '.gemspec') ]
    f.write(gemfile_content)
  end

  Pathname.new(gemfile_path)
end

#build_file_map(target_prefix_path, package_path = nil) ⇒ Object



495
496
497
498
499
500
501
502
503
# File 'lib/packtory/packer.rb', line 495

def build_file_map(target_prefix_path, package_path = nil)
  files = { }

  source_path = File.join(package_working_path, package_name, '/')
  target_path = (package_path || target_prefix_path) % package_name
  files[source_path] = target_path

  files
end

#bundle_gemsObject



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/packtory/packer.rb', line 255

def bundle_gems
  bgems = { }

  specs = bundler_definition.specs_for([ :default ])
  specs.each do |spec|
    next if gemspec.nil? || spec.name == gemspec.name

    if spec.name == 'bundler'
      if detect_bundler_include
        spec = @bundler_spec
      else
        next
      end
    end

    orig_spec = spec
    if spec.is_a?(Bundler::EndpointSpecification)
      rs = spec.instance_variable_get(:@remote_specification)
      if rs
        spec = rs
      elsif spec._local_specification
        spec = spec._local_specification
      end
    end

    bhash = { }

    bhash[:orig_spec] = orig_spec
    bhash[:spec] = spec
    bhash[:gem_path] = spec.full_gem_path

    bhash[:files] = Dir.glob(File.join(spec.full_gem_path, '**/*')).inject([ ]) { |a, f|
      unless File.directory?(f)
        a << f.gsub('%s/' % spec.full_gem_path, '')
      end

      a
    }.uniq

    bhash[:require_paths] = spec.full_require_paths.collect { |path|
      path.include?(bhash[:gem_path]) ?
        path.gsub(bhash[:gem_path], '') : nil
    }.compact.uniq

    bgems[spec.name] = bhash
  end

  bgems
end

#bundle_working_pathObject



194
195
196
# File 'lib/packtory/packer.rb', line 194

def bundle_working_path
  @opts[:bundle_working_path] || File.join(working_path, 'bundle')
end

#bundler_definitionObject



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/packtory/packer.rb', line 72

def bundler_definition
  if defined?(@bundle_def)
    @bundle_def
  else
    ENV['BUNDLE_GEMFILE'] = @gemfile.to_s

    install_opts = { }
    install_opts[:with] = [ :default ]
    install_opts[:without] = [ :development, :test, :documentation ]
    install_opts[:path] = bundle_working_path
    install_opts[:retry] = 3
    install_opts[:jobs] = 3

    # in case, we are called multiple times
    Bundler.reset!

    if @opts[:bundler_silent]
      Bundler.ui = Bundler::UI::Silent.new
    else
      Bundler.ui = Bundler::UI::Shell.new
      Bundler.ui.info 'Bundling with: %s' % @gemfile.to_s
    end

    if @opts[:bundler_local]
      install_opts[:local] = true
    end

    self.class.silence_warnings do
      Bundler.settings.temporary(install_opts) do
        @bundle_def = ::Bundler.definition
        @bundle_def.validate_runtime!
      end

      Bundler.settings.temporary({ :no_install => true }.merge(install_opts)) do
        Bundler::Installer.install(Bundler.root, @bundle_def, install_opts)
      end
    end

    rubygems_dir = Bundler.rubygems.gem_dir
    FileUtils.mkpath(File.join(rubygems_dir, 'specifications'))

    cached_gems = { }
    @bundle_def.specs.each do |spec|
      next unless spec.source.is_a?(Bundler::Source::Rubygems)
      cached_gems[spec.name] = spec.source.send(:cached_gem, spec)
    end

    if detect_bundler_include
      bundler_source = Bundler::Source::Rubygems.new('remotes' => 'https://rubygems.org')

      @bundler_spec =
        Bundler::RemoteSpecification.new('bundler', Bundler::VERSION,
                                         Gem::Platform::RUBY, bundler_source.fetchers.first)

      @bundler_spec.remote = Bundler::Source::Rubygems::Remote.new(bundler_source.remotes.first)

      bundler_source.send(:fetch_gem, @bundler_spec)
      cached_gems['bundler'] = bundler_source.send(:cached_gem, @bundler_spec)
    end

    cached_gems.each do |gem_name, cached_gem|
      installer = Gem::Installer.at(cached_gem, :path => rubygems_dir)
      installer.extract_files
      installer.write_spec
    end

    @bundle_def
  end
end

#create_binstub(binstub_fname, target_prefix_path) ⇒ Object



461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/packtory/packer.rb', line 461

def create_binstub(binstub_fname, target_prefix_path)
  binstub_code = <<CODE
#!/usr/bin/ruby

ENV['RUBY_PATH'] = '/usr/bin/ruby'

package_prefix = %s
require File.join(package_prefix, '%s')
load File.join(package_prefix, '%s')
CODE

  src_bin_path = File.join(package_working_path, 'bin', binstub_fname)
  FileUtils.mkpath(File.dirname(src_bin_path))

  if @opts[:install_prefix_as_code]
    target_path = @opts[:install_prefix_as_code] % package_name
  else
    target_path = '"%s"' % (target_prefix_path % package_name)
  end

  bundler_setup_path = File.join(BUNDLE_TARGET_PATH, BUNDLE_BUNDLER_SETUP_FILE)

  bindir_name = gemspec.nil? ? 'bin' : gemspec.bindir
  actual_bin_path = File.join(bindir_name, binstub_fname)

  File.open(src_bin_path, 'w') do |f|
    f.write(binstub_code % [ target_path, bundler_setup_path, actual_bin_path ])
  end

  FileUtils.chmod(0755, src_bin_path)

  src_bin_path
end

#descriptionObject



214
215
216
# File 'lib/packtory/packer.rb', line 214

def description
  gemspec.description.strip
end

#detect_bundler_includeObject



245
246
247
248
249
250
251
252
253
# File 'lib/packtory/packer.rb', line 245

def detect_bundler_include
  if @opts[:bundler_include]
    true
  elsif !gemspec.dependencies.detect { |d| d.name == 'bundler' }.nil?
    true
  else
    false
  end
end

#find_default_gemspec_fileObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/packtory/packer.rb', line 50

def find_default_gemspec_file
  files = [ ]
  try_paths = [ ]

  unless @gemfile.nil?
    try_paths << @gemfile.untaint.expand_path.parent.to_s
  end

  try_paths << root_path

  try_paths.detect do |tpath|
    files.concat(Dir.glob(File.join(tpath, '{,*}.gemspec')))
    files.count > 0
  end

  unless files.empty?
    File.expand_path(files.first)
  else
    nil
  end
end

#for_each_bundle_gems(&block) ⇒ Object



361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/packtory/packer.rb', line 361

def for_each_bundle_gems(&block)
  bgems = bundle_gems
  bgems.each do |gem_name, bhash|
    src_paths = bhash[:require_paths]
    if src_paths.count > 0
      rpaths = src_paths.dup
    else
      rpaths = [ '/lib' ]
    end

    yield(gem_name, bhash, rpaths)
  end
end

#gather_filesObject



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/packtory/packer.rb', line 375

def gather_files
  prefix_path = File.join(package_working_path, package_name)

  if File.exists?(prefix_path)
    FileUtils.rm_r(prefix_path)
  end

  FileUtils.mkpath(prefix_path)

  files = gather_files_for_package
  files.each do |fsrc, ftarget|
    if fsrc =~ /^\//
      fsrc_path = fsrc
    else
      fsrc_path = File.join(root_path, fsrc)
    end

    ftarget_path, tvalues = path_to_gathered_file(files, fsrc)

    FileUtils.mkpath(File.dirname(ftarget_path))
    FileUtils.cp_r(fsrc_path, ftarget_path)

    if ftarget.is_a?(Hash)
      tf = TemplateFile.new(self, ftarget_path, tvalues)
      tf.evaluate!
    end

    ftarget_path
  end

  files
end

#gather_files_for_packageObject



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/packtory/packer.rb', line 317

def gather_files_for_package
  files = { }

  unless gemspec.nil?
    (gemspec.files - gemspec.test_files).each do |fname|
      next if File.directory?(fname)
      fname = fname.gsub('%s/' % gemspec.full_gem_path, '')
      files[fname] = fname
    end
  end

  bgems = bundle_gems
  bgems.each do |gem_name, bhash|
    bhash[:files].each do |file|
      src_full_path = File.join(bhash[:gem_path], file)
      files[src_full_path] = File.join(BUNDLE_TARGET_PATH, gem_name, file)
    end

    unless bhash[:spec].extensions.empty?
      add_ruby_build_dependencies!
      files[bhash[:orig_spec].loaded_from] = File.join(BUNDLE_EXTENSIONS_PATH, '%s.gemspec' % gem_name)
    end
  end

  files = gather_packtory_tools_for_package(files)
  files[Packtory.bundler_setup_path] = { :target_path => File.join(BUNDLE_TARGET_PATH, BUNDLE_BUNDLER_SETUP_FILE) }

  files
end

#gather_packtory_tools_for_package(files) ⇒ Object



347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/packtory/packer.rb', line 347

def gather_packtory_tools_for_package(files)
  target_tools_path = File.join(BUNDLE_PACKTORY_TOOLS_PATH)

  gem_build_extensions_path = Packtory.gem_build_extensions_path
  files[gem_build_extensions_path] = File.join(target_tools_path, File.basename(gem_build_extensions_path))

  after_install_script_path = Packtory.after_install_script_path
  files[after_install_script_path] = { :target_path => File.join(target_tools_path,
                                                                 File.basename(after_install_script_path)),
                                       :values => { 'pg_PACKAGE_PATH' => '$(dirname "$this_dir")' } }

  files
end

#gemspecObject



142
143
144
145
146
147
148
149
150
# File 'lib/packtory/packer.rb', line 142

def gemspec
  if defined?(@spec)
    @spec
  elsif !@gemspec_file.nil?
    @spec = Gem::Specification.load(@gemspec_file)
  else
    @spec = nil
  end
end

#homepageObject



218
219
220
# File 'lib/packtory/packer.rb', line 218

def homepage
  gemspec.homepage
end

#licenseObject



226
227
228
# File 'lib/packtory/packer.rb', line 226

def license
  gemspec.license
end

#maintainerObject



230
231
232
233
234
235
236
237
238
239
# File 'lib/packtory/packer.rb', line 230

def maintainer
  case gemspec.email
  when Array
    gemspec.email.first
  when String
    gemspec.email
  else
    nil
  end
end

#package_nameObject



202
203
204
205
206
207
208
# File 'lib/packtory/packer.rb', line 202

def package_name
  if @opts[:package_name].nil?
    gemspec.name
  else
    @opts[:package_name]
  end
end

#package_working_pathObject



198
199
200
# File 'lib/packtory/packer.rb', line 198

def package_working_path
  File.join(working_path, 'package')
end

#path_to_gathered_file(files, fkey) ⇒ Object



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/packtory/packer.rb', line 408

def path_to_gathered_file(files, fkey)
  prefix_path = File.join(package_working_path, package_name)

  ftarget = files[fkey]
  ftarget_path = nil
  tvalues = nil

  case ftarget
  when String
    ftarget_path = File.join(prefix_path, ftarget)
  when Hash
    ftarget_path = File.join(prefix_path, ftarget[:target_path])
    tvalues = ftarget[:values]
  end

  [ ftarget_path, tvalues ]
end

#pkg_pathObject



178
179
180
181
182
183
184
# File 'lib/packtory/packer.rb', line 178

def pkg_path
  if @opts[:pkg_path].nil?
    File.join(root_path, 'pkg')
  else
    @opts[:pkg_path]
  end
end

#prepare_files(target_prefix_path, package_path = nil) ⇒ Object



526
527
528
529
530
531
532
533
# File 'lib/packtory/packer.rb', line 526

def prepare_files(target_prefix_path, package_path = nil)
  @files = gather_files

  sfiles_map = build_file_map(target_prefix_path, package_path)
  sfiles_map = add_bin_files(sfiles_map, target_prefix_path, package_path)

  sfiles_map
end

#root_pathObject



170
171
172
173
174
175
176
# File 'lib/packtory/packer.rb', line 170

def root_path
  if @opts[:path].nil?
    File.expand_path('./')
  else
    @opts[:path]
  end
end

#template_scripts_valuesObject



540
541
542
# File 'lib/packtory/packer.rb', line 540

def template_scripts_values
  { }
end

#versionObject



210
211
212
# File 'lib/packtory/packer.rb', line 210

def version
  gemspec.version
end

#working_pathObject



186
187
188
189
190
191
192
# File 'lib/packtory/packer.rb', line 186

def working_path
  if @opts[:working_path].nil?
    File.join(root_path, 'tmp/packtory_wp')
  else
    @opts[:working_path]
  end
end