Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bundler before/after eval hooks for plugins #6961

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion bundler/lib/bundler/definition.rb
Expand Up @@ -35,7 +35,10 @@ def self.build(gemfile, lockfile, unlock)

raise GemfileNotFound, "#{gemfile} not found" unless gemfile.file?

Dsl.evaluate(gemfile, lockfile, unlock)
Plugin.hook(Plugin::Events::GEM_BEFORE_EVAL, gemfile, lockfile, unlock)
Dsl.evaluate(gemfile, lockfile, unlock).tap do |definition|
Plugin.hook(Plugin::Events::GEM_AFTER_EVAL, definition)
end
end

#
Expand Down
12 changes: 12 additions & 0 deletions bundler/lib/bundler/plugin/events.rb
Expand Up @@ -56,6 +56,18 @@ def self.defined_event?(event)
# Includes an Array of Bundler::Dependency objects
# GEM_AFTER_INSTALL_ALL = "after-install-all"
define :GEM_AFTER_INSTALL_ALL, "after-install-all"

# @!parse
# A hook called before the Gemfile is evaluated
# Includes the Gemfile name, the Lockfile name, and the unlock options
# GEM_BEFORE_EVAL = "before-eval"
define :GEM_BEFORE_EVAL, "before-eval"

# @!parse
# A hook called after the Gemfile is evaluated
# Includes a Bundler::Definition
# GEM_AFTER_EVAL = "after-eval"
define :GEM_AFTER_EVAL, "after-eval"
end
end
end
51 changes: 51 additions & 0 deletions bundler/spec/plugins/hook_spec.rb
Expand Up @@ -106,4 +106,55 @@
expect(out).to include "installed gem rack : installed"
end
end

context "before-eval hook" do
before do
build_repo2 do
build_plugin "before-eval-plugin" do |s|
s.write "plugins.rb", <<-RUBY
Bundler::Plugin::API.hook Bundler::Plugin::Events::GEM_BEFORE_EVAL do |gemfile, lockfile|
puts "hooked eval start of \#{File.basename(gemfile)} to \#{File.basename(lockfile)}"
end
RUBY
end
end

bundle "plugin install before-eval-plugin --source #{file_uri_for(gem_repo2)}"
end

it "runs before all rubygems are installed" do
install_gemfile <<-G
source "#{file_uri_for(gem_repo1)}"
gem "rake"
G

expect(out).to include "hooked eval start of Gemfile to Gemfile.lock"
end
end

context "after-eval hook" do
before do
build_repo2 do
build_plugin "after-eval-plugin" do |s|
s.write "plugins.rb", <<-RUBY
Bundler::Plugin::API.hook Bundler::Plugin::Events::GEM_AFTER_EVAL do |defn|
puts "hooked eval after with gems \#{defn.dependencies.map(&:name).join(", ")}"
end
RUBY
end
end

bundle "plugin install after-eval-plugin --source #{file_uri_for(gem_repo2)}"
end

it "runs before all rubygems are installed" do
install_gemfile <<-G
source "#{file_uri_for(gem_repo1)}"
gem "rack"
gem "rake"
G

expect(out).to include "hooked eval after with gems rack, rake"
end
end
end