-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add `brew mark`, a new command to mark or unmark formulae as installed on request, or installed as dependency, as previously discussed in #17125 (comment). Sample usage: $ brew mark --installed-on-request --no-installed-as-dependency curl ==> curl is now marked as installed on request and not installed as dependency. $ brew mark --installed-on-request --no-installed-as-dependency curl ==> curl is already marked as installed on request and not installed as dependency. $ brew autoremove --dry-run [no output] $ brew mark --no-installed-on-request --installed-as-dependency curl ==> curl is now marked as not installed on request and installed as dependency. $ brew autoremove --dry-run ==> Would autoremove 2 unneeded formulae: curl rtmpdump
- Loading branch information
1 parent
633b46a
commit 75a5230
Showing
10 changed files
with
308 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
require "abstract_command" | ||
require "formula" | ||
require "tab" | ||
|
||
module Homebrew | ||
module Cmd | ||
class Mark < AbstractCommand | ||
cmd_args do | ||
description <<~EOS | ||
Mark or unmark <formula> as installed on request, or installed as | ||
dependency. This can be useful if you want to control whether an | ||
installed formula should be removed by `brew autoremove`. | ||
EOS | ||
switch "--installed-on-request", | ||
description: "Mark <formula> as installed on request." | ||
switch "--no-installed-on-request", | ||
description: "Mark <formula> as not installed on request." | ||
switch "--installed-as-dependency", | ||
description: "Mark <formula> as installed as dependency." | ||
switch "--no-installed-as-dependency", | ||
description: "Mark <formula> as not installed as dependency." | ||
|
||
conflicts "--installed-on-request", "--no-installed-on-request" | ||
conflicts "--installed-as-dependency", "--no-installed-as-dependency" | ||
|
||
named_args :formula, min: 1 | ||
end | ||
|
||
sig { override.void } | ||
def run | ||
installed_on_request = if args.installed_on_request? | ||
true | ||
elsif args.no_installed_on_request? | ||
false | ||
end | ||
installed_as_dependency = if args.installed_as_dependency? | ||
true | ||
elsif args.no_installed_as_dependency? | ||
false | ||
end | ||
raise UsageError, "No marking option specified." if installed_on_request.nil? && installed_as_dependency.nil? | ||
|
||
formulae = args.named.to_formulae | ||
if (formula_not_installed = formulae.find { |f| !f.any_version_installed? }) | ||
odie "#{formula_not_installed.name} is not installed." | ||
end | ||
|
||
formulae.each do |formula| | ||
mark_formula formula, installed_on_request:, installed_as_dependency: | ||
end | ||
end | ||
|
||
private | ||
|
||
sig { | ||
params( | ||
formula: Formula, | ||
installed_on_request: T.nilable(T::Boolean), | ||
installed_as_dependency: T.nilable(T::Boolean), | ||
).void | ||
} | ||
def mark_formula(formula, installed_on_request:, installed_as_dependency:) | ||
tab = Tab.for_formula(formula) | ||
raise ArgumentError, "Tab file for #{formula.name} does not exist." unless tab.tabfile.exist? | ||
|
||
unchanged_statuses = [] | ||
changed_statuses = [] | ||
unless installed_on_request.nil? | ||
status = "#{installed_on_request ? "" : "not "}installed on request" | ||
if tab.installed_on_request ^ installed_on_request | ||
changed_statuses << status | ||
else | ||
unchanged_statuses << status | ||
end | ||
end | ||
unless installed_as_dependency.nil? | ||
status = "#{installed_as_dependency ? "" : "not "}installed as dependency" | ||
if tab.installed_as_dependency ^ installed_as_dependency | ||
changed_statuses << status | ||
else | ||
unchanged_statuses << status | ||
end | ||
end | ||
|
||
unless unchanged_statuses.empty? | ||
ohai "#{formula.name} is already marked as #{unchanged_statuses.to_sentence}." | ||
end | ||
|
||
return if changed_statuses.empty? | ||
|
||
tab.installed_on_request = installed_on_request unless installed_on_request.nil? | ||
tab.installed_as_dependency = installed_as_dependency unless installed_as_dependency.nil? | ||
tab.write | ||
ohai "#{formula.name} is now marked as #{changed_statuses.to_sentence}." | ||
end | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# frozen_string_literal: true | ||
|
||
require "cmd/mark" | ||
require "cmd/shared_examples/args_parse" | ||
require "tab" | ||
|
||
RSpec.describe Homebrew::Cmd::Mark do | ||
it_behaves_like "parseable arguments" | ||
|
||
describe "integration tests", :integration_test do | ||
def installed_on_request?(formula) | ||
# `brew` subprocesses can change the tab, invalidating the cached values. | ||
Tab.clear_cache | ||
Tab.for_formula(formula).installed_on_request | ||
end | ||
|
||
def installed_as_dependency?(formula) | ||
# `brew` subprocesses can change the tab, invalidating the cached values. | ||
Tab.clear_cache | ||
Tab.for_formula(formula).installed_as_dependency | ||
end | ||
|
||
let(:testball) { Formula["testball"] } | ||
let(:baz) { Formula["baz"] } | ||
|
||
before do | ||
install_test_formula "testball", tab_attributes: { | ||
"installed_on_request" => false, | ||
"installed_as_dependency" => true, | ||
} | ||
setup_test_formula "baz" | ||
end | ||
|
||
it "marks or unmarks a formula as installed on request" do | ||
expect(installed_on_request?(testball)).to be false | ||
|
||
expect { brew "mark", "--installed-on-request", "testball" } | ||
.to be_a_success | ||
.and output(/testball is now marked as installed on request/).to_stdout | ||
.and not_to_output.to_stderr | ||
expect(installed_on_request?(testball)).to be true | ||
|
||
expect { brew "mark", "--installed-on-request", "testball" } | ||
.to be_a_success | ||
.and output(/testball is already marked as installed on request/).to_stdout | ||
.and not_to_output.to_stderr | ||
expect(installed_on_request?(testball)).to be true | ||
|
||
expect { brew "mark", "--no-installed-on-request", "testball" } | ||
.to be_a_success | ||
.and output(/testball is now marked as not installed on request/).to_stdout | ||
.and not_to_output.to_stderr | ||
expect(installed_on_request?(testball)).to be false | ||
end | ||
|
||
it "marks or unmarks a formula as installed as dependency" do | ||
expect(installed_as_dependency?(testball)).to be true | ||
|
||
expect { brew "mark", "--no-installed-as-dependency", "testball" } | ||
.to be_a_success | ||
.and output(/testball is now marked as not installed as dependency/).to_stdout | ||
.and not_to_output.to_stderr | ||
expect(installed_as_dependency?(testball)).to be false | ||
|
||
expect { brew "mark", "--no-installed-as-dependency", "testball" } | ||
.to be_a_success | ||
.and output(/testball is already marked as not installed as dependency/).to_stdout | ||
.and not_to_output.to_stderr | ||
expect(installed_as_dependency?(testball)).to be false | ||
|
||
expect { brew "mark", "--installed-as-dependency", "testball" } | ||
.to be_a_success | ||
.and output(/testball is now marked as installed as dependency/).to_stdout | ||
.and not_to_output.to_stderr | ||
expect(installed_as_dependency?(testball)).to be true | ||
end | ||
|
||
it "raises an error when a formula is not installed" do | ||
expect { brew "mark", "--installed-on-request", "testball", "baz" } | ||
.to be_a_failure | ||
.and not_to_output.to_stdout | ||
.and output(/baz is not installed/).to_stderr | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ livecheck | |
ln | ||
log | ||
ls | ||
mark | ||
migrate | ||
missing | ||
nodenv-sync | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters