Sha256: 767cbd2eef9750f165e92957aed7365b76cadc7959aa1ab7b4e7069d5e42621d

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

require 'fileutils'
require 'anvil/task'
require 'anvil/rubygems'

class GemBuildTask < Anvil::Task
  description 'Builds a gem for you and can install it on your system.'

  parser do
    arguments %w[gemspec_file]

    on('-i', '--[no-]install', 'Install gem') do |i|
      options[:install] = i
    end
  end

  attr_reader :gemspec_file, :options

  def initialize(gemspec_file, options = {})
    @gemspec_file = gemspec_file
    @options = options
  end

  def task
    path = File.dirname(gemspec_file)

    Dir.chdir(path) do
      gem_file = build_gem(gemspec_file)
      Anvil::Rubygems.install gem_file if install?

      gem_file
    end
  end

  def build_gem(gemspec_file)
    rubygems_output = Anvil::Rubygems.build(gemspec_file)
    gem_file        = extract_gem_file(rubygems_output)

    FileUtils.mkdir_p('pkg')
    FileUtils.move(gem_file, 'pkg')

    File.expand_path("pkg/#{gem_file}")
  end

  def extract_gem_file(output)
    output.match(/File: (.*)$/)[1]
  end

  def install?
    options.fetch(:install, true)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
anvil-core-0.0.1.alpha.1 lib/tasks/gem_build_task.rb