Class: Cartage

Inherits:
Object
  • Object
show all
Defined in:
lib/cartage.rb,
lib/cartage/config.rb,
lib/cartage/plugin.rb,
lib/cartage/command.rb,
lib/cartage/pack_command.rb,
lib/cartage/manifest/commands.rb

Overview

Cartage, a package builder.

Defined Under Namespace

Classes: Command, Config, Manifest, PackCommand, Plugin

Constant Summary collapse

VERSION =

:nodoc:

'1.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCartage

:nodoc:


108
109
110
# File 'lib/cartage.rb', line 108

def initialize #:nodoc:
  @load_config = :default
end

Instance Attribute Details

#base_configObject

The base config file. This should not be used by clients.


106
107
108
# File 'lib/cartage.rb', line 106

def base_config
  @base_config
end

#environmentObject

The environment to be used when resolving configuration options from a configuration file. Cartage configuration files do not usually have environment partitions, but if they do, use this to select the environment partition.


69
70
71
# File 'lib/cartage.rb', line 69

def environment
  @environment
end

#load_config=(value) ⇒ Object (writeonly)

The configuration file to read. This should not be used by clients.


103
104
105
# File 'lib/cartage.rb', line 103

def load_config=(value)
  @load_config = value
end

#quietObject

:method: default_without_environments

The default environments to exclude from a bundle install. The default is [ 'test', 'development' ].


60
61
62
# File 'lib/cartage.rb', line 60

def quiet
  @quiet
end

#verboseObject

Commands will be run with extra information.


63
64
65
# File 'lib/cartage.rb', line 63

def verbose
  @verbose
end

Class Method Details

.common_build_options(opts, cartage) ⇒ Object

Set options common to anything that builds a package (that is, it calls Cartage#pack).


442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/cartage.rb', line 442

def common_build_options(opts, cartage)
  opts.on(
    '-t', '--target PATH',
    'The build package will be placed in PATH, which defaults to \'tmp\'.'
  ) { |t| cartage.target = t }
  opts.on(
    '-n', '--name NAME',
    "Set the package name. Defaults to '#{cartage.default_name}'."
  ) { |n| cartage.name = n }
  opts.on(
    '-r', '--root-path PATH',
    'Set the root path. Defaults to the repository root.'
  ) { |r| cartage.root_path = r }
  opts.on(
    '--timestamp TIMESTAMP',
    'The timestamp used for the final package.'
  ) { |t| cartage.timestamp = t }
  opts.on(
    '--bundle-cache PATH',
    'Set the bundle cache path.'
  ) { |b| cartage.bundle_cache(b) }
  opts.on(
    '--without GROUP1,GROUP2', Array,
    'Set the groups to be excluded from bundle installation.',
  ) { |w| cartage.without_environments = w }
end

.run(args) ⇒ Object

Run the Cartage command-line program.


387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/cartage.rb', line 387

def run(args) #:nodoc:
  require_relative 'cartage/plugin'
  Cartage::Plugin.load
  Cartage::Plugin.decorate(Cartage)

  cartage = Cartage.new

  cli = CmdParse::CommandParser.new(handle_exceptions: true)
  cli.main_options.program_name = 'cartage'
  cli.main_options.version = Cartage::VERSION.split(/\./)
  cli.main_options.banner = 'Manage releaseable packages.'

  cli.global_options do |opts|
    # opts.on('--[no-]quiet', 'Silence normal command output.') { |q|
    #   cartage.quiet = !!q
    # }
    opts.on('--[no-]verbose', 'Show verbose output.') { |v|
      cartage.verbose = !!v
    }
    opts.on(
      '-E', '--environment [ENVIRONMENT]', <<-desc
Set the environment to be used when necessary. If an environment name is not
provided, it will check the values of $RAILS_ENV and RACK_ENV. If neither is
set, this option is ignored.
      desc
    ) { |e| cartage.environment = e || ENV['RAILS_ENV'] || ENV['RACK_ENV'] }
    opts.on(
      '-C', '--[no-]config-file load_config', <<-desc
Configure Cartage from a default configuration file or a specified
configuration file.
      desc
    ) { |c| cartage.load_config = c }
  end

  cli.add_command(CmdParse::HelpCommand.new)
  cli.add_command(CmdParse::VersionCommand.new)
  cli.add_command(Cartage::PackCommand.new(cartage))

  Cartage::Plugin.registered.each do |plugin|
    if plugin.respond_to?(:commands)
      Array(plugin.commands).flatten.each do |command|
        registered_commands << command
      end
    end
  end

  registered_commands.uniq.each { |cmd| cli.add_command(cmd.new(cartage)) }
  cli.parse
rescue StandardError => e
  $stderr.puts "Error:\n    " + e.message
  $stderr.puts e.backtrace.join("\n") if cartage.verbose
end

Instance Method Details

#bundle_cache(location = nil) ⇒ Object

Set or return the bundle cache. The bundle cache is a compressed tarball of vendor/bundle in the working path.

If it exists, it will be extracted into vendor/bundle before bundle install --deployment is run, and it will be created after the bundle has been installed. In this way, bundle installation works almost the same way as Capistrano’s shared bundle concept as long as the path to the bundle_cache has been set to a stable location.

On Semaphore CI, this should be created relative to $SEMAPHORE_CACHE.

cartage pack --bundle-cache $SEMAPHORE_CACHE

136
137
138
139
140
141
142
# File 'lib/cartage.rb', line 136

def bundle_cache(location = nil)
  if location || !defined?(@bundle_cache)
    @bundle_cache = Pathname(location || tmp_path).
      join('vendor-bundle.tar.bz2').expand_path
  end
  @bundle_cache
end

#config(with_environment: true, for_plugin: nil) ⇒ Object

The Config object. If with_environment is true (the default) and #environment has been set, only the subset of the config matching #environment will be returned. If with_environment is false, the full configuration will be returned.

If for_plugin is specified, only the subset of the config for the named plug-in will be returned.

# Assume that #environment is 'development'.
cartage.config
  # => base_config[:development]
cartage.config(with_environment: false)
  # => base_config
cartage.config(for_plugin: 's3')
  # => base_config[:development][:plugins][:s3]
cartage.config(for_plugin: 's3', with_environment: false)
  # => base_config[:plugins][:s3]

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

def config(with_environment: true, for_plugin: nil)
  env  = environment.to_sym if with_environment && environment
  plug = for_plugin.to_sym if for_plugin

  cfg = if env
          base_config[env]
        else
          base_config
        end

  cfg = cfg.plugins[plug] if plug && cfg.plugins
  cfg
end

#display(message) ⇒ Object

A utility method for Cartage plug-ins to display a message only if verbose is on. Unless the command implemented by the plug-in is output only, this should be used.


176
177
178
# File 'lib/cartage.rb', line 176

def display(message)
  puts message if verbose && !quiet
end

#final_release_hashrefObject

The path to the resulting release_hashref.


168
169
170
171
# File 'lib/cartage.rb', line 168

def final_release_hashref
  @final_release_hashref ||=
    Pathname("#{final_name}-release-hashref.txt")
end

#final_tarballObject

The path to the resulting package.


163
164
165
# File 'lib/cartage.rb', line 163

def final_tarball
  @final_tarball ||= Pathname("#{final_name}.tar.bz2")
end

#packObject

Create the package.


113
114
115
116
117
118
119
120
121
# File 'lib/cartage.rb', line 113

def pack
  timestamp # Force the timestamp NOW.
  prepare_work_area
  save_release_hashref
  fetch_bundler
  install_vendor_bundle
  restore_modified_files
  build
end

#release_hashref(save_to: nil) ⇒ Object

Return the release hashref. If the optional save_to parameter is provided, the release hashref will be written to the specified file.


146
147
148
149
150
# File 'lib/cartage.rb', line 146

def release_hashref(save_to: nil)
  @release_hashref ||= %x(git rev-parse HEAD).chomp
  File.open(save_to, 'w') { |f| f.write @release_hashref } if save_to
  @release_hashref
end

#repo_urlObject

The repository URL.


153
154
155
156
157
158
159
160
# File 'lib/cartage.rb', line 153

def repo_url
  unless defined? @repo_url
    origin = %x(git remote show -n origin)
    match = origin.match(%r{\n\s+Fetch URL: (?<fetch>[^\n]+)})
    @repo_url = match[:fetch]
  end
  @repo_url
end