class Tara::Installer

@private

Public Class Methods

new(package_dir, fetcher, options={}) click to toggle source
# File lib/tara/installer.rb, line 6
def initialize(package_dir, fetcher, options={})
  @package_dir = package_dir
  @fetcher = fetcher
  @without_groups = options[:without_groups]
  @app_dir = Pathname.new(options[:app_dir])
  @bundle_env = bundle_env(options[:bundle_ignore_config])
  @bundle_jobs = options[:bundle_jobs] || 4
  @shell = options[:shell] || Shell
  @build_command = options[:build_command]
end

Public Instance Methods

execute() click to toggle source
# File lib/tara/installer.rb, line 17
def execute
  bundle_gems
  extract_ruby
  extract_native_gems
  strip_tests
  strip_docs
  strip_leftovers
  strip_java_files
  strip_git_files
  strip_empty_directories
end

Private Instance Methods

bundle_env(ignore_config) click to toggle source
# File lib/tara/installer.rb, line 31
def bundle_env(ignore_config)
  env = {'BUNDLE_GEMFILE' => 'lib/vendor/Gemfile'}
  env['BUNDLE_IGNORE_CONFIG'] = '1' if ignore_config
  env
end
bundle_gems() click to toggle source
# File lib/tara/installer.rb, line 45
def bundle_gems
  FileUtils.mkdir_p(vendor_path)
  copy_gem_files(vendor_path)
  Dir.chdir(@package_dir) do
    Bundler.with_clean_env do
      copy_local_gems
      @shell.exec_with_env(bundler_command, @bundle_env)
      if @build_command
        @shell.exec_with_env(@build_command, @bundle_env)
      end
    end
    Dir['lib/vendor/*/*/cache/*'].each do |cache_file|
      FileUtils.rm_rf(cache_file)
    end
    Dir['lib/vendor/ruby/*/extensions/*'].each do |ext_file|
      FileUtils.rm_rf(ext_file)
    end
    %w[o so bundle].each do |ext|
      find_and_remove_files('lib/vendor/ruby/*/gems', %(*.#{ext}))
    end
  end
end
bundler_command() click to toggle source
# File lib/tara/installer.rb, line 37
def bundler_command
  @bundler_command ||= begin
    command = "bundle install --jobs #{@bundle_jobs} --frozen --path . --gemfile lib/vendor/Gemfile"
    command << %( --without #{@without_groups.join(' ')}) if @without_groups.any?
    command
  end
end
bundler_gems_glob() click to toggle source
# File lib/tara/installer.rb, line 196
def bundler_gems_glob
  @bundler_gems_glob ||= ruby_vendor_path.join('*', 'bundler', 'gems')
end
copy_gem_files(path) click to toggle source
# File lib/tara/installer.rb, line 127
def copy_gem_files(path)
  Dir.chdir(@app_dir) do
    Dir['Gemfile', 'Gemfile.lock', '*.gemspec'].each do |file|
      FileUtils.cp(file, path.join(File.basename(file)))
    end
  end
end
copy_local_gems() click to toggle source
# File lib/tara/installer.rb, line 87
def copy_local_gems
  local_gems = find_installed_gems
  target_directory = File.join(@package_dir, 'lib/vendor', Bundler.ruby_scope)

  spec_dir = File.join(target_directory, 'specifications')
  FileUtils.mkdir_p(spec_dir)
  FileUtils.mkdir_p(File.join(target_directory, 'bin'))
  FileUtils.mkdir_p(File.join(target_directory, 'gems'))
  FileUtils.mkdir_p(File.join(target_directory, 'bundler', 'gems'))

  local_gems.each do |gemspec|
    FileUtils.cp_r(gemspec[:full_gem_path], File.join(target_directory, gemspec[:relative_path]))
    FileUtils.cp(gemspec[:spec_file], spec_dir) if File.exists?(gemspec[:spec_file])
  end
end
extract_native_gems() click to toggle source
# File lib/tara/installer.rb, line 110
def extract_native_gems
  native_gems = find_native_gems
  native_gems.each do |name, version|
    gem_archive_path = @fetcher.fetch_native_gem(name, version)
    @shell.exec(%(tar -xzf #{gem_archive_path} -C #{ruby_vendor_path}))
  end
end
extract_ruby() click to toggle source
# File lib/tara/installer.rb, line 103
def extract_ruby
  FileUtils.mkdir_p(ruby_path)
  FileUtils.mkdir_p(ruby_vendor_path)
  ruby_archive_path = @fetcher.fetch_ruby
  @shell.exec(%(tar -xzf #{ruby_archive_path} -C #{ruby_path}))
end
find_and_remove_directories(dir, glob) click to toggle source
# File lib/tara/installer.rb, line 176
def find_and_remove_directories(dir, glob)
  @shell.exec(%(find #{dir} -name "#{glob}" -type d -exec rm -rf "{}" \\; 2> /dev/null || true))
end
find_and_remove_files(dir, glob) click to toggle source
# File lib/tara/installer.rb, line 172
def find_and_remove_files(dir, glob)
  @shell.exec(%(find #{dir} -name "#{glob}" -type f -exec rm -f "{}" \\; 2> /dev/null || true))
end
find_installed_gems() click to toggle source
# File lib/tara/installer.rb, line 68
def find_installed_gems
  definition = Bundler::Definition.build('lib/vendor/Gemfile', 'lib/vendor/Gemfile.lock', false)
  return [] unless definition.has_optional_groups?
  @without_groups.each do |group|
    definition.add_optional_group(group)
  end
  definition.specs.each_with_object([]) do |gem_spec, specs|
    if gem_spec.full_gem_path.start_with?(Bundler.bundle_path.to_s) # Local gem
      specs << {
        :full_gem_path => gem_spec.full_gem_path,
        :spec_file => gem_spec.spec_file,
        :relative_path => Pathname.new(gem_spec.full_gem_path).relative_path_from(Bundler.bundle_path).to_s,
      }
    end
  end
rescue Bundler::GemNotFound => e
  []
end
find_native_gems() click to toggle source
# File lib/tara/installer.rb, line 118
def find_native_gems
  gemspecs = Dir[ruby_vendor_path.join('*/specifications/*.gemspec')]
  specs = gemspecs.map { |gemspec| Gem::Specification.load(gemspec) }
  with_ext = specs.select { |s| s.extensions.any? }
  with_ext.each_with_object({}) do |spec, hash|
    hash[spec.name] = spec.version.to_s
  end
end
lib_path() click to toggle source
# File lib/tara/installer.rb, line 180
def lib_path
  @lib_path ||= Pathname.new(@package_dir).join('lib')
end
ruby_path() click to toggle source
# File lib/tara/installer.rb, line 192
def ruby_path
  @ruby_path ||= lib_path.join('ruby')
end
ruby_vendor_path() click to toggle source
# File lib/tara/installer.rb, line 188
def ruby_vendor_path
  @ruby_vendor_path ||= vendor_path.join('ruby')
end
strip_docs() click to toggle source
# File lib/tara/installer.rb, line 139
def strip_docs
  strip_from_gems(%w[doc* example* *.txt *.md *.rdoc])
end
strip_empty_directories() click to toggle source
# File lib/tara/installer.rb, line 168
def strip_empty_directories
  @shell.exec(%(find #{@package_dir} -type d -empty -delete 2> /dev/null || true))
end
strip_from_gems(things) click to toggle source
# File lib/tara/installer.rb, line 161
def strip_from_gems(things)
  things.each do |thing|
    FileUtils.rm_r(Dir[vendor_gems_glob.join('*', thing)])
    FileUtils.rm_r(Dir[bundler_gems_glob.join('*', thing)])
  end
end
strip_git_files() click to toggle source
# File lib/tara/installer.rb, line 156
def strip_git_files
  find_and_remove_directories(vendor_gems_glob, '.git')
  find_and_remove_directories(bundler_gems_glob, '.git')
end
strip_java_files() click to toggle source
# File lib/tara/installer.rb, line 152
def strip_java_files
  find_and_remove_files(vendor_gems_glob, '*.java')
end
strip_leftovers() click to toggle source
# File lib/tara/installer.rb, line 143
def strip_leftovers
  %w[c cpp h rl].each do |ext|
    find_and_remove_files(ruby_vendor_path, %(*.#{ext}))
  end
  find_and_remove_files(ruby_vendor_path, 'extconf.rb')
  find_and_remove_files(vendor_gems_glob.join('*', 'ext'), 'Makefile')
  find_and_remove_directories(vendor_gems_glob.join('*', 'ext'), 'tmp')
end
strip_tests() click to toggle source
# File lib/tara/installer.rb, line 135
def strip_tests
  strip_from_gems(%w[tests test spec])
end
vendor_gems_glob() click to toggle source
# File lib/tara/installer.rb, line 200
def vendor_gems_glob
  @vendor_gems_glob ||= ruby_vendor_path.join('*', 'gems')
end
vendor_path() click to toggle source
# File lib/tara/installer.rb, line 184
def vendor_path
  @vendor_path ||= lib_path.join('vendor')
end