Skip to content

Commit

Permalink
Add bundler before/after eval hooks for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
ccutrer committed Sep 14, 2023
1 parent 46113c1 commit 1883e8f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
5 changes: 4 additions & 1 deletion bundler/lib/bundler/definition.rb
Expand Up @@ -34,7 +34,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

0 comments on commit 1883e8f

Please sign in to comment.