diff --git a/cmd/bundle.rb b/cmd/bundle.rb index 236f53deb..d12384ee3 100755 --- a/cmd/bundle.rb +++ b/cmd/bundle.rb @@ -107,6 +107,11 @@ def bundle zap: args.zap?, ) end + when "add" + Bundle::Commands::Add.run( + global: args.global?, + file: args.file, + ) when "dump" Bundle::Commands::Dump.run( global: args.global?, diff --git a/lib/bundle/commands/add.rb b/lib/bundle/commands/add.rb new file mode 100644 index 000000000..063d63823 --- /dev/null +++ b/lib/bundle/commands/add.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +module Bundle + module Commands + module Add + module_function + + def run(*args, global: false, file: nil) + raise UsageError, "No arguments were specified!" if args.blank? + + type = :brew # default to brew + name = args.first # only support one formula at a time for now + options = {} # we don't currently support passing options + + # parse the relevant Brewfile + dsl = Bundle::Dsl.new(Brewfile.read(global: global, file: file)) + + # check each of the entries in the specified Brewfile + dsl.entries.each do |entry| + # raise an error if the entry already exists in the Brewfile + # this could also be a noop, or print a friendly message + opoo "'#{name}' already exists in Brewfile." + raise RuntimeError if entry.name == name + end + + dsl.brew(name, options) + # execute brew bundle + # execute brew bundle dump + end + end + end +end diff --git a/spec/bundle/commands/add_command_spec.rb b/spec/bundle/commands/add_command_spec.rb new file mode 100644 index 000000000..4410e463e --- /dev/null +++ b/spec/bundle/commands/add_command_spec.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Bundle::Commands::Add do + context "when a Brewfile is not found" do + it "raises an error" do + expect { described_class.run("wget") }.to raise_error(RuntimeError) + end + end + + context "when a Brewfile is found" do + before do + allow_any_instance_of(Pathname).to receive(:read) + .and_return("brew 'openssl'") + end + + context "when no arguments are passed" do + it "raises an error" do + expect { described_class.run }.to raise_error(UsageError) + end + end + + context "the formula is not in the Brewfile" do + it "does not raise an error" do + expect { described_class.run("wget") }.not_to raise_error + end + + it "adds the formula to the Brewfile" do + # TODO + end + end + + context "the formula is in the Brewfile" do + before do + allow_any_instance_of(Pathname).to receive(:read) + .and_return("brew 'openssl'\nbrew 'wget'") + end + + it "raises an error" do + expect { described_class.run("wget") }.to raise_error(RuntimeError) + end + end + end +end