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

Commit ec97f55

Browse files
committed
Implement brew bundle add
Part of #818
1 parent 949a403 commit ec97f55

File tree

5 files changed

+52
-2
lines changed

5 files changed

+52
-2
lines changed

cmd/bundle.rb

+15-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def run
136136
require_relative "../lib/bundle"
137137

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

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

lib/bundle.rb

+2
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

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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: - validate formulae and casks
12+
# - support `:describe`
13+
content << args.map { |arg| "#{type} \"#{arg}\"" }
14+
.join("\n") << "\n"
15+
path = Dumper.brewfile_path(global:, file:)
16+
17+
Dumper.write_file path, content
18+
end
19+
end
20+
end

lib/bundle/commands/add.rb

+14
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

+1-1
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

0 commit comments

Comments
 (0)