bundle: add cleanup support to npm, cargo, go, and uv extensions#21883
bundle: add cleanup support to npm, cargo, go, and uv extensions#21883mvanhorn wants to merge 2 commits intoHomebrew:mainfrom
Conversation
`brew bundle cleanup` already handles formulae, casks, taps, VSCode extensions, and Flatpak packages. But npm, cargo, go, and uv packages not listed in the Brewfile are silently ignored. Add `cleanup_heading`, `cleanup_items`, and `cleanup!` overrides to all four extensions, following the existing flatpak/vscode_extension pattern.
MikeMcQuaid
left a comment
There was a problem hiding this comment.
Hey @mvanhorn! Thanks, looking good so far.
Main concern here is that folks are may be surprised when they brew bundle cleanup --force and it nukes all their global cargo/go/npm/uv files.
I'm not sure how best to handle this. Any thoughts?
|
Good question. A few thoughts:
Open to other ideas too. |
MikeMcQuaid
left a comment
There was a problem hiding this comment.
Thanks @mvanhorn! You've convinced me given --force that we should just leave this as-is and consider additional flags/env if people complain.
Rereviewed and needs DRYd up a bit then good to go!
| sig { override.returns(String) } | ||
| def cleanup_heading | ||
| "Cargo packages" | ||
| end |
There was a problem hiding this comment.
Ideally we'd not need this at all given BANNER_NAME, maybe needs logic changes in Extension
| sig { params(entries: T::Array[Object]).returns(T::Array[String]) } | ||
| def cleanup_items(entries) | ||
| return [].freeze unless package_manager_installed? | ||
|
|
||
| kept_packages = entries.filter_map do |entry| | ||
| entry = T.cast(entry, Dsl::Entry) | ||
| entry.name if entry.type == type | ||
| end | ||
|
|
||
| return [].freeze if kept_packages.empty? | ||
|
|
||
| packages - kept_packages | ||
| end |
There was a problem hiding this comment.
This is copy/pasted below so should be moved to the Extension class
| Bundle.system(cargo.to_s, "uninstall", name, verbose: false) | ||
| end | ||
| end | ||
| puts "Uninstalled #{items.size} Cargo package#{"s" if items.size != 1}" |
There was a problem hiding this comment.
| puts "Uninstalled #{items.size} Cargo package#{"s" if items.size != 1}" | |
| puts "Uninstalled #{items.size} #{BANNER_NAME}#{"s" if items.size != 1}" |
|
|
||
| sig { override.returns(String) } | ||
| def cleanup_heading | ||
| "Go packages" |
| end | ||
|
|
||
| sig { params(entries: T::Array[Object]).returns(T::Array[String]) } | ||
| def cleanup_items(entries) |
| items.each do |name| | ||
| Bundle.system(npm.to_s, "uninstall", "-g", name, verbose: false) | ||
| end | ||
| puts "Uninstalled #{items.size} npm package#{"s" if items.size != 1}" |
|
|
||
| sig { override.returns(String) } | ||
| def cleanup_heading | ||
| "uv tools" |
| end | ||
|
|
||
| sig { params(entries: T::Array[Object]).returns(T::Array[String]) } | ||
| def cleanup_items(entries) |
| items.each do |name| | ||
| Bundle.system(uv.to_s, "tool", "uninstall", name, verbose: false) | ||
| end | ||
| puts "Uninstalled #{items.size} uv tool#{"s" if items.size != 1}" |
| def cleanup!(items) | ||
| uv = package_manager_executable | ||
| return if uv.nil? | ||
|
|
||
| items.each do |name| | ||
| Bundle.system(uv.to_s, "tool", "uninstall", name, verbose: false) | ||
| end |
There was a problem hiding this comment.
This is identical for all but go so should be moved into Extension.
Move cleanup_heading, cleanup_items, and the common cleanup! pattern into Extension. Each extension now only defines uninstall_package! with its specific uninstall args. Go keeps its custom cleanup! since it does binary-level deletion rather than a package manager call.
|
DRYed up in 4aa2387:
Net: -68 lines across the 4 extensions. |
brew lgtm(style, typechecking and tests) with your changes locally?brew lgtm --online, and verified by runningbrew bundle cleanupagainst a real Brewfile on my machine.brew bundle cleanuphandles formulae, casks, taps, VSCode extensions, and Flatpak packages not listed in the Brewfile. But it silently ignores npm, cargo, go, and uv packages.The Extension base class already defines
cleanup_heading,cleanup_items, andcleanup!hooks. Flatpak and VSCode Extension override them. The other 4 extensions don't, so their packages are invisible to cleanup.This PR adds the same 3 overrides to npm, cargo, go, and uv, following the flatpak pattern:
cleanup!commandnpm uninstall -g NAMEcargo uninstall NAME(withCARGO_HOME/RUSTUP_HOMEenv)GOBIN/GOPATHafter matching viago version -muv tool uninstall NAMETested locally. With 6 go packages installed and a Brewfile listing only one, cleanup now reports the other 5: