Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/rubygems/specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class Gem::Specification
:metadata => {},
:name => nil,
:platform => Gem::Platform::RUBY,
:post_install => nil,
:post_install_message => nil,
:rdoc_options => [],
:require_paths => ['lib'],
Expand Down Expand Up @@ -374,6 +375,19 @@ def files

attr_accessor :metadata

##
# A block that gets called after the gem is installed.
#
# Usage:
# spec.post_install do
# # something amazing...
# end

def post_install(&block)
return unless block_given?
Gem.post_install_hooks << block
end

##
# Adds a development dependency named +gem+ with +requirements+ to this
# gem.
Expand Down Expand Up @@ -1937,6 +1951,7 @@ def ruby_code(obj)
when Time then obj.strftime('%Y-%m-%d').dump
when Numeric then obj.inspect
when true, false, nil then obj.inspect
when Proc then obj.inspect
when Gem::Platform then "Gem::Platform.new(#{obj.to_a.inspect})"
when Gem::Requirement then "Gem::Requirement.new(#{obj.to_s.inspect})"
else raise Gem::Exception, "ruby_code case not handled: #{obj.class}"
Expand Down
28 changes: 28 additions & 0 deletions test/rubygems/test_gem_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,34 @@ def test_install_with_message
assert_match %r|I am a shiny gem!|, @ui.output
end

def test_install_with_successful_post_install
@spec.post_install do
@ui.say "Running post install hook!"
end

use_ui @ui do
path = Gem::Builder.new(@spec).build

@installer = Gem::Installer.new path
@installer.install
end

assert_match %r|Running post install hook!|, @ui.output
end

def test_install_with_failed_post_install
@spec.post_install do
raise Exception
end

assert_raises Exception do
path = Gem::Builder.new(@spec).build

@installer = Gem::Installer.new path
@installer.install
end
end

def test_install_wrong_ruby_version
use_ui @ui do
installer = Gem::Installer.new old_ruby_required
Expand Down
1 change: 1 addition & 0 deletions test/rubygems/test_gem_specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def test_self_attribute_names
metadata
name
platform
post_install
post_install_message
rdoc_options
require_paths
Expand Down