Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion bundler/lib/bundler/definition.rb
Original file line number Diff line number Diff line change
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)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the hook should be in the dsl file. If the intent is to let plugin extend the dsl, then we should pass the dsl class to the hook.
So the plugin would be able to do something like:

Bundler::Plugin::API.hook(Plugin::Events) do |dsl|
  dsl.define_method(:overrides!) {}
end

We'd also need to passthrough method_missing

def method_missing(name, *args)
in the DSL plugin, otherwise users won't be able to use ours custom dsl methods.
If a user accidentaly call a methods that doesn't exist (either in the vanilla Bundler dsl or our custom plugins) it will be caught afterwards when Bundler evaluates the DSL from the DSL class
def method_missing(name, *args)
raise GemfileError, "Undefined local variable or method `#{name}' for Gemfile"
end

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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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