diff options
author | Samuel E. Giddins <segiddins@segiddins.me> | 2015-03-11 19:38:59 -0700 |
---|---|---|
committer | Samuel E. Giddins <segiddins@segiddins.me> | 2015-03-11 19:38:59 -0700 |
commit | c88852b876991ed8de426a99c14b3116e2859e35 (patch) | |
tree | 222068ecd08a91528b5a92589afe85590eb15c4c | |
parent | 56b64f5abeac20c7b046073d8c0de903d6429afb (diff) | |
download | bundler-seg-post-install-hook.tar.gz |
[Gemfile] Add a post_install hookseg-post-install-hook
-rw-r--r-- | lib/bundler/definition.rb | 9 | ||||
-rw-r--r-- | lib/bundler/dsl.rb | 23 | ||||
-rw-r--r-- | lib/bundler/installer.rb | 7 | ||||
-rw-r--r-- | spec/install/gemfile_spec.rb | 34 |
4 files changed, 62 insertions, 11 deletions
diff --git a/lib/bundler/definition.rb b/lib/bundler/definition.rb index 4c15866577..77214e8fc4 100644 --- a/lib/bundler/definition.rb +++ b/lib/bundler/definition.rb @@ -5,7 +5,7 @@ module Bundler class Definition include GemHelpers - attr_reader :dependencies, :platforms, :ruby_version, :locked_deps + attr_reader :dependencies, :platforms, :ruby_version, :post_install_hook, :locked_deps # Given a gemfile and lockfile creates a Bundler definition # @@ -43,7 +43,7 @@ module Bundler # @param unlock [Hash, Boolean, nil] Gems that have been requested # to be updated or true if all gems should be updated # @param ruby_version [Bundler::RubyVersion, nil] Requested Ruby Version - def initialize(lockfile, dependencies, sources, unlock, ruby_version = nil) + def initialize(lockfile, dependencies, sources, unlock, ruby_version = nil, post_install_hook = nil) @unlocking = unlock == true || !unlock.empty? @dependencies, @sources, @unlock = dependencies, sources, unlock @@ -51,6 +51,7 @@ module Bundler @specs = nil @lockfile_contents = "" @ruby_version = ruby_version + @post_install_hook = post_install_hook if lockfile && File.exist?(lockfile) @lockfile_contents = Bundler.read_file(lockfile) @@ -234,6 +235,10 @@ module Bundler dependencies.map { |d| d.groups }.flatten.uniq end + def specs_by_group + Hash[groups.map { |g| [g, specs_for([g]).to_a] }] + end + def lock(file) contents = to_lock diff --git a/lib/bundler/dsl.rb b/lib/bundler/dsl.rb index e6c4b975ef..32e892e692 100644 --- a/lib/bundler/dsl.rb +++ b/lib/bundler/dsl.rb @@ -16,14 +16,15 @@ module Bundler attr_accessor :dependencies def initialize - @source = nil - @sources = SourceList.new - @git_sources = {} - @dependencies = [] - @groups = [] - @platforms = [] - @env = nil - @ruby_version = nil + @source = nil + @sources = SourceList.new + @git_sources = {} + @dependencies = [] + @groups = [] + @platforms = [] + @env = nil + @ruby_version = nil + @post_install_hook = nil add_git_sources end @@ -158,7 +159,7 @@ module Bundler end def to_definition(lockfile, unlock) - Definition.new(lockfile, @dependencies, @sources, unlock, @ruby_version) + Definition.new(lockfile, @dependencies, @sources, unlock, @ruby_version, @post_install_hook) end def group(*args, &blk) @@ -327,5 +328,9 @@ module Bundler end end + def post_install(&blk) + @post_install_hook = blk + end + end end diff --git a/lib/bundler/installer.rb b/lib/bundler/installer.rb index 10526115a4..436f634a6f 100644 --- a/lib/bundler/installer.rb +++ b/lib/bundler/installer.rb @@ -92,6 +92,13 @@ module Bundler lock unless Bundler.settings[:frozen] generate_standalone(options[:standalone]) if options[:standalone] + run_post_install_hook + end + + def run_post_install_hook + if hook = @definition.post_install_hook + hook.call(root, @definition.specs_by_group) + end end def install_gem_from_spec(spec, standalone = false, worker = 0) diff --git a/spec/install/gemfile_spec.rb b/spec/install/gemfile_spec.rb index bfe239396a..91d5908e5b 100644 --- a/spec/install/gemfile_spec.rb +++ b/spec/install/gemfile_spec.rb @@ -67,4 +67,38 @@ describe "bundle install" do end end + context "with a post_install hook" do + before :each do + in_app_root + end + + it "runs with no arguments" do + gemfile <<-G + source "file://#{gem_repo1}" + gem "rack" + post_install { puts "POST INSTALL RAN" } + G + + bundle :install + expect(out).to match(/POST INSTALL RAN/) + end + + it "passes in the root and specs by group" do + gemfile <<-G + source "file://#{gem_repo1}" + gem "rack" + post_install do |root, specs_by_group| + puts "POST INSTALL \#{root}" + puts 'POST INSTALL ' + specs_by_group.keys.join(' ') + puts 'POST INSTALL ' + specs_by_group.values.flatten.map(&:name).join(' ') + end + G + + bundle :install + expect(out).to include("POST INSTALL #{Bundler.root}") + expect(out).to include("POST INSTALL default") + expect(out).to include("POST INSTALL bundler rack") + end + end + end |