Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions cmd/bundle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def run
require_relative "../lib/bundle"

subcommand = args.named.first.presence
if ["exec", "add"].exclude?(subcommand) && args.named.size > 1
if ["exec", "add", "remove"].exclude?(subcommand) && args.named.size > 1
raise UsageError, "This command does not take more than 1 subcommand argument."
end

Expand Down Expand Up @@ -239,21 +239,32 @@ def run
whalebrew: args.whalebrew? || args.all?,
vscode: args.vscode? || args.all?,
)
when "add"
raise UsageError, "`add` does not support `--mas`." if args.mas?

# We intentionally omit the `s` from `brews` and `casks` for ease of handling later.
when "add", "remove"
# We intentionally omit the `s` from `brews`, `casks`, and `taps` for ease of handling later.
type_hash = {
brew: args.brews? || no_type_args,
brew: args.brews?,
cask: args.casks?,
tap: args.taps?,
mas: args.mas?,
whalebrew: args.whalebrew?,
vscode: args.vscode?,
none: no_type_args,
}
selected_types = type_hash.select { |_, v| v }.keys
raise UsageError, "`add` supports only one type of entry at a time." if selected_types.count > 1
raise UsageError, "`#{subcommand}` supports only one type of entry at a time." if selected_types.count != 1

_, *named_args = args.named
if subcommand == "add"
type = case (t = selected_types.first)
when :none then :brew
when :mas then raise UsageError, "`add` does not support `--mas`."
else t
end

_subcommand, *named_args = args.named
Bundle::Commands::Add.run(*named_args, type: selected_types.first, global:, file:)
Bundle::Commands::Add.run(*named_args, type:, global:, file:)
else
Bundle::Commands::Remove.run(*named_args, type: selected_types.first, global:, file:)
end
else
raise UsageError, "unknown subcommand: #{subcommand}"
end
Expand Down
2 changes: 2 additions & 0 deletions lib/bundle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require "bundle/dsl"
require "bundle/adder"
require "bundle/checker"
require "bundle/remover"
require "bundle/skipper"
require "bundle/brew_services"
require "bundle/brew_service_checker"
Expand All @@ -33,6 +34,7 @@
require "bundle/commands/exec"
require "bundle/commands/list"
require "bundle/commands/add"
require "bundle/commands/remove"
require "bundle/whalebrew_installer"
require "bundle/whalebrew_dumper"
require "bundle/vscode_extension_checker"
Expand Down
14 changes: 14 additions & 0 deletions lib/bundle/commands/remove.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# typed: true
# frozen_string_literal: true

module Bundle
module Commands
module Remove
module_function

def run(*args, type:, global:, file:)
Bundle::Remover.remove(*args, type:, global:, file:)
end
end
end
end
20 changes: 20 additions & 0 deletions lib/bundle/remover.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# typed: true
# frozen_string_literal: true

module Bundle
module Remover
module_function

def remove(*args, type:, global:, file:)
brewfile = Brewfile.read(global:, file:)
content = brewfile.input.split("\n")
entry_type = type.to_s if type != :none
escaped_args = args.map { |arg| Regexp.escape(arg) }
content = content.grep_v(/#{entry_type}(\s+|\(\s*)"(#{escaped_args.join("|")})"/)
.join("\n") << "\n"
path = Dumper.brewfile_path(global:, file:)

Dumper.write_file path, content
end
end
end
25 changes: 25 additions & 0 deletions spec/bundle/commands/remove_command_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require "cask/cask_loader"

describe Bundle::Commands::Remove do
subject(:remove) do
described_class.run(*args, type:, global:, file:)
end

before { File.write(file, "brew \"hello\"\n") }
after { FileUtils.rm_f file }

let(:global) { false }

context "when called with a valid formula" do
let(:args) { ["hello"] }
let(:type) { :brew }
let(:file) { "/tmp/some_random_brewfile#{Random.rand(2 ** 16)}" }

it "removes entries from the given Brewfile" do
expect { remove }.not_to raise_error
expect(File.read(file)).not_to include("#{type} \"#{args.first}\"")
end
end
end