Class: Tara::Archive

Inherits:
Object
  • Object
show all
Defined in:
lib/tara/archive.rb

Overview

Packs an application along with a Ruby runtime and dependencies into a TAR archive.

The archive will include the source code of your project (which is assumed to be in the ‘lib` directory), wrapper scripts for each executable (assumed to be in the `bin` directory), and all gems that aren’t in the ‘test` or `development` groups in your project’s Gemfile.

Examples:

Creating an archive from a Rake task

task :archive do
  Tara::Archive.new.create
end

Configuring the archive to be created

task :archive do
  archive = Tara::Archive.new(
    target: 'osx',
    traveling_ruby_version: '20150204',
    without_groups: %w[test],
  )
  archive.create
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ Archive

Create a new instance of ‘Archive` with the specified configuration.

Tara attempts to use sane defaults in most of all cases, for example like assuming that the source code is in the ‘lib` directory of your project, that the name of the application is the same as the project directory.

Parameters:

  • config (Hash) (defaults to: {})

Options Hash (config):

  • :app_dir (String) — default: Dir.pwd

    absolute path to the application directory.

  • :app_name (String) — default: File.basename(@config[:app_dir])

    name of the application.

  • :build_dir (String) — default: File.join(@config[:app_dir], 'build')

    the directory where the archive will be created.

  • :bundle_ignore_config (Boolean) — default: false

    if Bundler config should be ignored when installing dependencies

  • :bundle_jobs (Integer) — default: 4

    the number of parallel workers to be used when installing gems

  • :download_dir (String) — default: File.join(@config[:build_dir], 'downloads')

    the directory where Traveling Ruby artifacts will be downloaded.

  • :archive_name (String) — default: @config[:app_name] + '.tgz'

    name of the archive

  • :files (Array<String>) — default: %w[lib/**/*.rb]

    list of globs that will be expanded when including source files in archive. Should be relative from :app_dir.

  • :executables (Array<String>) — default: %w[bin/*]

    list of globs that will be expanded when including executables in archive. Should be relative from :app_dir.

  • :gem_executables (Array<String, String>) — default: []

    list of gem and exec name pairs which will be included as executables in archive.

  • :target (String) — default: linux-x86_64

    target platform that the archive will be created for. Should be one of “linux-x86”, “linux-x86_64”, or “osx”.

  • :traveling_ruby_version (String) — default: 20150210

    release of Traveling Ruby that should be used.

  • :without_groups (Array<String>) — default: %w[development test]

    list of gem groups to exclude from the archive.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tara/archive.rb', line 60

def initialize(config={})
  @config = config
  @config[:app_dir] ||= Dir.pwd
  @config[:app_name] ||= File.basename(@config[:app_dir])
  @config[:build_dir] ||= File.join(@config[:app_dir], 'build')
  @config[:download_dir] ||= File.join(@config[:build_dir], 'downloads')
  @config[:archive_name] ||= @config[:app_name] + '.tgz'
  @config[:files] ||= %w[lib/**/*.rb]
  @config[:executables] ||= %w[bin/*]
  @config[:gem_executables] ||= []
  @config[:target] ||= 'linux-x86_64'
  @config[:traveling_ruby_version] ||= '20150210'
  @config[:without_groups] ||= %w[development test]
end

Class Method Details

.create(config = {}) ⇒ String

Short for ‘Archive.new(config).create`

Returns:

  • (String)

    Path to the archive



79
80
81
# File 'lib/tara/archive.rb', line 79

def self.create(config={})
  new(config).create
end

Instance Method Details

#createString

Create an archive using the instance’s configuration.

Returns:

  • (String)

    Path to the archive



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/tara/archive.rb', line 88

def create
  Dir.mktmpdir do |tmp_dir|
    project_dir = Pathname.new(@config[:app_dir])
    package_dir = Pathname.new(tmp_dir)
    build_dir = Pathname.new(@config[:build_dir])
    copy_source(project_dir, package_dir)
    copy_executables(project_dir, package_dir)
    create_gem_shims(package_dir)
    install_dependencies(package_dir, fetcher)
    Dir.chdir(tmp_dir) do
      create_archive(build_dir)
    end
    File.join(build_dir, @config[:archive_name])
  end
end