Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit f851419

Browse files
authored
Merge pull request #1632 from Homebrew/bundle-add
2 parents 32eefd7 + c9638d8 commit f851419

File tree

7 files changed

+104
-2
lines changed

7 files changed

+104
-2
lines changed

cmd/bundle.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def run
137137
require_relative "../lib/bundle"
138138

139139
subcommand = args.named.first.presence
140-
if subcommand != "exec" && args.named.size > 1
140+
if ["exec", "add"].exclude?(subcommand) && args.named.size > 1
141141
raise UsageError, "This command does not take more than 1 subcommand argument."
142142
end
143143

@@ -241,6 +241,20 @@ def run
241241
whalebrew: args.whalebrew? || args.all?,
242242
vscode: args.vscode? || args.all?,
243243
)
244+
when "add"
245+
# We intentionally omit the `s` from `brews` and `casks` for ease of handling later.
246+
type_hash = {
247+
brew: args.brews? || no_type_args,
248+
cask: args.casks?,
249+
mas: args.mas?,
250+
whalebrew: args.whalebrew?,
251+
vscode: args.vscode?,
252+
}
253+
selected_types = type_hash.select { |_, v| v }.keys
254+
raise UsageError, "`add` supports only one type of entry at a time." if selected_types.count > 1
255+
256+
_subcommand, *named_args = args.named
257+
Bundle::Commands::Add.run(*named_args, type: selected_types.first, global:, file:)
244258
else
245259
raise UsageError, "unknown subcommand: #{subcommand}"
246260
end

lib/bundle.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require "bundle/brewfile"
77
require "bundle/bundle"
88
require "bundle/dsl"
9+
require "bundle/adder"
910
require "bundle/checker"
1011
require "bundle/skipper"
1112
require "bundle/brew_services"
@@ -31,6 +32,7 @@
3132
require "bundle/commands/check"
3233
require "bundle/commands/exec"
3334
require "bundle/commands/list"
35+
require "bundle/commands/add"
3436
require "bundle/whalebrew_installer"
3537
require "bundle/whalebrew_dumper"
3638
require "bundle/vscode_extension_checker"

lib/bundle/adder.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# typed: true
2+
# frozen_string_literal: true
3+
4+
module Bundle
5+
module Adder
6+
module_function
7+
8+
def add(*args, type:, global:, file:)
9+
brewfile = Brewfile.read(global:, file:)
10+
content = brewfile.input
11+
# TODO: - support `:describe`
12+
new_content = args.map do |arg|
13+
case type
14+
when :brew
15+
Formulary.factory(arg)
16+
when :cask
17+
Cask::CaskLoader.load(arg)
18+
end
19+
20+
"#{type} \"#{arg}\""
21+
end
22+
23+
content << new_content.join("\n") << "\n"
24+
path = Dumper.brewfile_path(global:, file:)
25+
26+
Dumper.write_file path, content
27+
end
28+
end
29+
end

lib/bundle/commands/add.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# typed: true
2+
# frozen_string_literal: true
3+
4+
module Bundle
5+
module Commands
6+
module Add
7+
module_function
8+
9+
def run(*args, type:, global:, file:)
10+
Bundle::Adder.add(*args, type:, global:, file:)
11+
end
12+
end
13+
end
14+
end

lib/bundle/dsl.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def to_s
1717
end
1818
end
1919

20-
attr_reader :entries, :cask_arguments
20+
attr_reader :entries, :cask_arguments, :input
2121

2222
def initialize(path)
2323
@path = path
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
require "cask/cask_loader"
4+
5+
describe Bundle::Commands::Add do
6+
subject(:add) do
7+
described_class.run(*args, type:, global:, file:)
8+
end
9+
10+
before { FileUtils.touch file }
11+
after { FileUtils.rm_f file }
12+
13+
let(:global) { false }
14+
15+
context "when called with a valid formula" do
16+
let(:args) { ["hello"] }
17+
let(:type) { :brew }
18+
let(:file) { "/tmp/some_random_brewfile#{Random.rand(2 ** 16)}" }
19+
20+
it "adds entries to the given Brewfile" do
21+
expect { add }.not_to raise_error
22+
expect(File.read(file)).to include("#{type} \"#{args.first}\"")
23+
end
24+
end
25+
26+
context "when called with a valid cask" do
27+
let(:args) { ["alacritty"] }
28+
let(:type) { :cask }
29+
let(:file) { "/tmp/some_random_brewfile#{Random.rand(2 ** 16)}" }
30+
31+
it "adds entries to the given Brewfile" do
32+
expect { add }.not_to raise_error
33+
expect(File.read(file)).to include("#{type} \"#{args.first}\"")
34+
end
35+
end
36+
end

spec/stub/cask/cask_loader.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
module Cask
4+
module CaskLoader
5+
def self.load(*); end
6+
end
7+
end

0 commit comments

Comments
 (0)