Skip to content

Commit 1883e8f

Browse files
committed
Add bundler before/after eval hooks for plugins
1 parent 46113c1 commit 1883e8f

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

bundler/lib/bundler/definition.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ def self.build(gemfile, lockfile, unlock)
3434

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

37-
Dsl.evaluate(gemfile, lockfile, unlock)
37+
Plugin.hook(Plugin::Events::GEM_BEFORE_EVAL, gemfile, lockfile, unlock)
38+
Dsl.evaluate(gemfile, lockfile, unlock).tap do |definition|
39+
Plugin.hook(Plugin::Events::GEM_AFTER_EVAL, definition)
40+
end
3841
end
3942

4043
#

bundler/lib/bundler/plugin/events.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ def self.defined_event?(event)
5656
# Includes an Array of Bundler::Dependency objects
5757
# GEM_AFTER_INSTALL_ALL = "after-install-all"
5858
define :GEM_AFTER_INSTALL_ALL, "after-install-all"
59+
60+
# @!parse
61+
# A hook called before the Gemfile is evaluated
62+
# Includes the Gemfile name, the Lockfile name, and the unlock options
63+
# GEM_BEFORE_EVAL = "before-eval"
64+
define :GEM_BEFORE_EVAL, "before-eval"
65+
66+
# @!parse
67+
# A hook called after the Gemfile is evaluated
68+
# Includes a Bundler::Definition
69+
# GEM_AFTER_EVAL = "after-eval"
70+
define :GEM_AFTER_EVAL, "after-eval"
5971
end
6072
end
6173
end

bundler/spec/plugins/hook_spec.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,55 @@
106106
expect(out).to include "installed gem rack : installed"
107107
end
108108
end
109+
110+
context "before-eval hook" do
111+
before do
112+
build_repo2 do
113+
build_plugin "before-eval-plugin" do |s|
114+
s.write "plugins.rb", <<-RUBY
115+
Bundler::Plugin::API.hook Bundler::Plugin::Events::GEM_BEFORE_EVAL do |gemfile, lockfile|
116+
puts "hooked eval start of \#{File.basename(gemfile)} to \#{File.basename(lockfile)}"
117+
end
118+
RUBY
119+
end
120+
end
121+
122+
bundle "plugin install before-eval-plugin --source #{file_uri_for(gem_repo2)}"
123+
end
124+
125+
it "runs before all rubygems are installed" do
126+
install_gemfile <<-G
127+
source "#{file_uri_for(gem_repo1)}"
128+
gem "rake"
129+
G
130+
131+
expect(out).to include "hooked eval start of Gemfile to Gemfile.lock"
132+
end
133+
end
134+
135+
context "after-eval hook" do
136+
before do
137+
build_repo2 do
138+
build_plugin "after-eval-plugin" do |s|
139+
s.write "plugins.rb", <<-RUBY
140+
Bundler::Plugin::API.hook Bundler::Plugin::Events::GEM_AFTER_EVAL do |defn|
141+
puts "hooked eval after with gems \#{defn.dependencies.map(&:name).join(", ")}"
142+
end
143+
RUBY
144+
end
145+
end
146+
147+
bundle "plugin install after-eval-plugin --source #{file_uri_for(gem_repo2)}"
148+
end
149+
150+
it "runs before all rubygems are installed" do
151+
install_gemfile <<-G
152+
source "#{file_uri_for(gem_repo1)}"
153+
gem "rack"
154+
gem "rake"
155+
G
156+
157+
expect(out).to include "hooked eval after with gems rack, rake"
158+
end
159+
end
109160
end

0 commit comments

Comments
 (0)