Class: Rookie::Tasks::Gem

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/rookie/tasks/gem.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gemspec = nil, gem_dir = 'gem') {|_self| ... } ⇒ Gem

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!

Yields:

  • (_self)

Yield Parameters:



45
46
47
48
49
# 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

Instance Attribute Details

#dirObject

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!



19
20
21
# File 'lib/rookie/tasks/gem.rb', line 19

def dir
  @dir
end

#specObject

The gem specification.



12
13
14
# File 'lib/rookie/tasks/gem.rb', line 12

def spec
  @spec
end

Instance Method Details

#build_gemObject

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



62
63
64
65
66
67
68
69
70
71
72
73
# 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!Object

Removes the gem directory.



81
82
83
# File 'lib/rookie/tasks/gem.rb', line 81

def clean!
  FileUtils.rm_rf dir
end

#define_tasks!Object

Defines the gem tasks.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 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

#gem(cmd, arg = gem_file) ⇒ Object

Executes a gem command.



76
77
78
# File 'lib/rookie/tasks/gem.rb', line 76

def gem(cmd, arg = gem_file)
  "gem #{cmd} #{arg}"
end

#gem_fileObject

The full path to the gem file.



57
58
59
# File 'lib/rookie/tasks/gem.rb', line 57

def gem_file
  File.join dir, gem_file_name
end

#gem_file_nameObject

The name of the packaged gem file.



52
53
54
# File 'lib/rookie/tasks/gem.rb', line 52

def gem_file_name
  "#{spec.name}-#{spec.version}.gem"
end