Skip to content

Commit 31adbf1

Browse files
MrMarvinclaude
authored andcommitted
Add plugin hooks around gem fetch and git source fetch
Adds four new hook points: - before-fetch / after-fetch: fires in Source::Rubygems#download_gem around actual network downloads, avoiding noise from cache hits. - before-git-fetch / after-git-fetch: fires in Source::Git#specs around fetch/checkout operations. Based on the original proposal in #8162 with adjustments: - Moved gem fetch hooks from fetch_gem_if_possible to download_gem so they only fire on actual network I/O. - Dropped the source argument since spec.source provides it. - Renamed git hooks to before-git-fetch / after-git-fetch for consistency with the existing before-*/after-* pattern. - Removed GEM_BEFORE_FETCH/GEM_AFTER_FETCH from Source::Git#install since using gem fetch events for git sources is semantically inconsistent. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent cd3825c commit 31adbf1

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

bundler/lib/bundler/plugin/events.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@ def self.defined_event?(event)
4545
# GEM_AFTER_INSTALL = "after-install"
4646
define :GEM_AFTER_INSTALL, "after-install"
4747

48+
# @!parse
49+
# A hook called before each individual gem is downloaded from a remote source.
50+
# Includes a Gem::Specification. Does not fire on cache hits.
51+
# GEM_BEFORE_FETCH = "before-fetch"
52+
define :GEM_BEFORE_FETCH, "before-fetch"
53+
54+
# @!parse
55+
# A hook called after each individual gem is downloaded from a remote source.
56+
# Includes a Gem::Specification. Does not fire on cache hits.
57+
# GEM_AFTER_FETCH = "after-fetch"
58+
define :GEM_AFTER_FETCH, "after-fetch"
59+
60+
# @!parse
61+
# A hook called before a git source is fetched.
62+
# Includes a Bundler::Source::Git reference.
63+
# GIT_BEFORE_FETCH = "before-git-fetch"
64+
define :GIT_BEFORE_FETCH, "before-git-fetch"
65+
66+
# @!parse
67+
# A hook called after a git source is fetched.
68+
# Includes a Bundler::Source::Git reference.
69+
# GIT_AFTER_FETCH = "after-git-fetch"
70+
define :GIT_AFTER_FETCH, "after-git-fetch"
71+
4872
# @!parse
4973
# A hook called before any gems install
5074
# Includes an Array of Bundler::Dependency objects

bundler/lib/bundler/source/git.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,10 @@ def specs(*)
191191
set_cache_path!(app_cache_path) if use_app_cache?
192192

193193
if requires_checkout? && !@copied
194+
Plugin.hook(Plugin::Events::GIT_BEFORE_FETCH, self)
194195
fetch unless use_app_cache?
195196
checkout
197+
Plugin.hook(Plugin::Events::GIT_AFTER_FETCH, self)
196198
end
197199

198200
local_specs

bundler/lib/bundler/source/rubygems.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,11 @@ def download_gem(spec, download_cache_path, previous_spec = nil)
477477
Bundler.ui.confirm("Fetching #{version_message(spec, previous_spec)}")
478478
gem_remote_fetcher = remote_fetchers.fetch(spec.remote).gem_remote_fetcher
479479

480+
Plugin.hook(Plugin::Events::GEM_BEFORE_FETCH, spec)
480481
Gem.time("Downloaded #{spec.name} in", 0, true) do
481482
Bundler.rubygems.download_gem(spec, uri, download_cache_path, gem_remote_fetcher)
482483
end
484+
Plugin.hook(Plugin::Events::GEM_AFTER_FETCH, spec)
483485
end
484486

485487
# Returns the global cache path of the calling Rubygems::Source object.

0 commit comments

Comments
 (0)