From cc9cc05fa18912ba002a6dcbadb1e6fc4bc28fc6 Mon Sep 17 00:00:00 2001 From: Dane Harrigan Date: Mon, 7 Nov 2011 22:19:58 -0800 Subject: [PATCH] + Added post install hook to gemspec A gem author can add post install code like so: spec.post_install do # ... end --- lib/rubygems/specification.rb | 15 +++++++++++++ test/rubygems/test_gem_installer.rb | 28 +++++++++++++++++++++++++ test/rubygems/test_gem_specification.rb | 1 + 3 files changed, 44 insertions(+) diff --git a/lib/rubygems/specification.rb b/lib/rubygems/specification.rb index 099d9787da66..ac5aa1e20b63 100644 --- a/lib/rubygems/specification.rb +++ b/lib/rubygems/specification.rb @@ -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'], @@ -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. @@ -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}" diff --git a/test/rubygems/test_gem_installer.rb b/test/rubygems/test_gem_installer.rb index 6490a8be71da..1bed6e17437c 100644 --- a/test/rubygems/test_gem_installer.rb +++ b/test/rubygems/test_gem_installer.rb @@ -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 diff --git a/test/rubygems/test_gem_specification.rb b/test/rubygems/test_gem_specification.rb index 1e3cab43f9c6..fc3405c83e8d 100644 --- a/test/rubygems/test_gem_specification.rb +++ b/test/rubygems/test_gem_specification.rb @@ -82,6 +82,7 @@ def test_self_attribute_names metadata name platform + post_install post_install_message rdoc_options require_paths