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.

  • :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.

  • :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.


54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/tara/archive.rb', line 54

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[: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


72
73
74
# File 'lib/tara/archive.rb', line 72

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


81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/tara/archive.rb', line 81

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])
    install_dependencies(package_dir, fetcher)
    copy_source(project_dir, package_dir)
    copy_executables(project_dir, package_dir)
    Dir.chdir(tmp_dir) do
      create_archive(build_dir)
    end
    File.join(build_dir, @config[:archive_name])
  end
end