class Rookie::Tasks::Gem

Adds various gem management tasks that allows you to build, install, uninstall and release your gem with ease.

Attributes

dir[R]

The directory where .gem files will be stored. 'gem' by default.

This directory will be completely erased by the clean task. Make sure it is not in use!

spec[R]

The gem specification.

Public Class Methods

new(gemspec = nil, gem_dir = 'gem') { |self| ... } click to toggle source

Creates a new gem task with the given gem specification and temporary gem directory.

Tasks do not get defined automatically; don't forget to call define_tasks!

# File lib/rookie/tasks/gem.rb, line 45
def initialize(gemspec = nil, gem_dir = 'gem')
  self.spec = gemspec
  self.dir = gem_dir
  yield self if block_given?
end

Public Instance Methods

build_gem() click to toggle source

Builds the gem from the specification and moves it to the gem directory.

# File lib/rookie/tasks/gem.rb, line 62
def build_gem
  FileUtils.mkdir_p dir

  if RUBY_VERSION >= '2.0.0'
    require 'rubygems/package'
    gem = ::Gem::Package.build(spec)
  else
    gem = ::Gem::Builder.new(spec).build
  end

  FileUtils.mv gem, dir
end
clean!() click to toggle source

Removes the gem directory.

# File lib/rookie/tasks/gem.rb, line 81
def clean!
  FileUtils.rm_rf dir
end
define_tasks!() click to toggle source

Defines the gem tasks.

# File lib/rookie/tasks/gem.rb, line 86
def define_tasks!
  directory dir

  namespace :gem do
    desc 'Builds the gem from the specification'
    task :build => dir do
      build_gem
    end

    desc 'Pushes the gem to rubygems.org'
    task :push => :build do
      sh gem :push
    end

    desc 'Same as gem:push'
    task :release => :push

    desc 'Installs the gem locally'
    task :install => :build do
      sh gem :install
    end

    desc 'Uninstalls the gem'
    task :uninstall do
      sh gem(:uninstall, spec.name)
    end

    desc 'Removes the gem package directory'
    task :clean do
      clean!
    end

    desc 'Installs the gem locally and cleans up'
    task :setup => [ :install, :clean ]
  end

  desc 'Same as gem:build'
  task :gem => 'gem:build'
end
dir=(dir) click to toggle source

The directory where packaged gems will be stored. Always relative to the current directory.

This directory will be completely erased by the clean task. Make sure it is not in use!

# File lib/rookie/tasks/gem.rb, line 36
def dir=(dir)
  @dir = File.expand_path File.join(Dir.pwd, dir)
end
gem(cmd, arg = gem_file) click to toggle source

Executes a gem command.

# File lib/rookie/tasks/gem.rb, line 76
def gem(cmd, arg = gem_file)
  "gem #{cmd} #{arg}"
end
gem_file() click to toggle source

The full path to the gem file.

# File lib/rookie/tasks/gem.rb, line 57
def gem_file
  File.join dir, gem_file_name
end
gem_file_name() click to toggle source

The name of the packaged gem file.

# File lib/rookie/tasks/gem.rb, line 52
def gem_file_name
  "#{spec.name}-#{spec.version}.gem"
end
spec=(gemspec) click to toggle source

Sets this task's gem specification. Loads it from a file if given a string.

# File lib/rookie/tasks/gem.rb, line 23
def spec=(gemspec)
  @spec = case gemspec
    when ::Gem::Specification then gemspec
    when String then ::Gem::Specification.load gemspec
    else nil
  end
end