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, QuietError, StatusError

Constant Summary collapse

VERSION =

:nodoc:

'1.1.1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCartage

:nodoc:



133
134
135
# File 'lib/cartage.rb', line 133

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

Instance Attribute Details

#base_configObject

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



131
132
133
# File 'lib/cartage.rb', line 131

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.



94
95
96
# File 'lib/cartage.rb', line 94

def environment
  @environment
end

#load_config=(value) ⇒ Object (writeonly)

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



128
129
130
# File 'lib/cartage.rb', line 128

def load_config=(value)
  @load_config = value
end

#quietObject

Commands that normally output data will have that output suppressed.



85
86
87
# File 'lib/cartage.rb', line 85

def quiet
  @quiet
end

#verboseObject

Commands will be run with extra information.



88
89
90
# File 'lib/cartage.rb', line 88

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).



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/cartage.rb', line 476

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.



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
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
468
469
470
471
472
# File 'lib/cartage.rb', line 420

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
  return 0
rescue Exception => exception
  show_message_for exception, for_cartage: cartage
  return exitstatus_for(exception)
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


161
162
163
164
165
166
167
# File 'lib/cartage.rb', line 161

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]


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

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.



201
202
203
# File 'lib/cartage.rb', line 201

def display(message)
  __display(message)
end

#final_release_hashrefObject

The path to the resulting release_hashref.



193
194
195
196
# File 'lib/cartage.rb', line 193

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

#final_tarballObject

The path to the resulting package.



188
189
190
# File 'lib/cartage.rb', line 188

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

#packObject

Create the package.



138
139
140
141
142
143
144
145
146
# File 'lib/cartage.rb', line 138

def pack
  timestamp # Force the timestamp to be set now.
  prepare_work_area
  save_release_hashref
  fetch_bundler
  install_vendor_bundle
  restore_modified_files
  build_final_tarball
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.



171
172
173
174
175
# File 'lib/cartage.rb', line 171

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.



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

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