From 5789e98e28a24b5ab14059b7480c144b75eeb2b4 Mon Sep 17 00:00:00 2001 From: John Britton Date: Mon, 30 Nov 2020 21:55:23 -0500 Subject: [PATCH 1/4] create `add_command_spec.rb` --- spec/bundle/commands/add_command_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 spec/bundle/commands/add_command_spec.rb diff --git a/spec/bundle/commands/add_command_spec.rb b/spec/bundle/commands/add_command_spec.rb new file mode 100644 index 000000000..3ebcb7445 --- /dev/null +++ b/spec/bundle/commands/add_command_spec.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Bundle::Commands::Add do + context "when `--global` is passed" do + end +end From 4446c9b3bf014e343ac6f7f2b5137970fc680907 Mon Sep 17 00:00:00 2001 From: John Britton Date: Mon, 30 Nov 2020 22:46:54 -0500 Subject: [PATCH 2/4] rough pass at implementing `brew bundle add` subcommand --- cmd/bundle.rb | 5 +++ lib/bundle/commands/add.rb | 31 +++++++++++++++++++ spec/bundle/commands/add_command_spec.rb | 39 +++++++++++++++++++++++- 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 lib/bundle/commands/add.rb diff --git a/cmd/bundle.rb b/cmd/bundle.rb index 236f53deb..6d66da0b0 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..299976cea --- /dev/null +++ b/lib/bundle/commands/add.rb @@ -0,0 +1,31 @@ +# 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 + + # read the relevant Brewfile + parsed_entries = Bundle::Dsl.new(Brewfile.read(global: global, file: file)).entries + + # check each of the entries in the specified Brewfile + parsed_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 + raise RuntimeError if entry.name == name + end + + # need some help / pointers here + # is it possible to use Bundle::Dsl to create an in memory representation + #of the brewfile that we read, add an entry and then dump that back to the file? + end + end + end +end diff --git a/spec/bundle/commands/add_command_spec.rb b/spec/bundle/commands/add_command_spec.rb index 3ebcb7445..b20508a20 100644 --- a/spec/bundle/commands/add_command_spec.rb +++ b/spec/bundle/commands/add_command_spec.rb @@ -3,6 +3,43 @@ require "spec_helper" describe Bundle::Commands::Add do - context "when `--global` is passed" 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") }.to_not 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 From 477f555b96f21c422d60fd8cf5e66bd64b6da70f Mon Sep 17 00:00:00 2001 From: John Britton Date: Mon, 14 Dec 2020 21:02:38 -0500 Subject: [PATCH 3/4] linting errors --- cmd/bundle.rb | 2 +- lib/bundle/commands/add.rb | 3 ++- spec/bundle/commands/add_command_spec.rb | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/cmd/bundle.rb b/cmd/bundle.rb index 6d66da0b0..d12384ee3 100755 --- a/cmd/bundle.rb +++ b/cmd/bundle.rb @@ -110,7 +110,7 @@ def bundle when "add" Bundle::Commands::Add.run( global: args.global?, - file: args.file + file: args.file, ) when "dump" Bundle::Commands::Dump.run( diff --git a/lib/bundle/commands/add.rb b/lib/bundle/commands/add.rb index 299976cea..ffd96d524 100644 --- a/lib/bundle/commands/add.rb +++ b/lib/bundle/commands/add.rb @@ -19,12 +19,13 @@ def run(*args, global: false, file: nil) parsed_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 # need some help / pointers here # is it possible to use Bundle::Dsl to create an in memory representation - #of the brewfile that we read, add an entry and then dump that back to the file? + # of the brewfile that we read, add an entry and then dump that back to the file? end end end diff --git a/spec/bundle/commands/add_command_spec.rb b/spec/bundle/commands/add_command_spec.rb index b20508a20..4410e463e 100644 --- a/spec/bundle/commands/add_command_spec.rb +++ b/spec/bundle/commands/add_command_spec.rb @@ -23,11 +23,11 @@ context "the formula is not in the Brewfile" do it "does not raise an error" do - expect { described_class.run("wget") }.to_not raise_error + expect { described_class.run("wget") }.not_to raise_error end it "adds the formula to the Brewfile" do - #TODO + # TODO end end From 78adf4d5c77415e1a7ee6256da3bf2ebfaee76ed Mon Sep 17 00:00:00 2001 From: John Britton Date: Tue, 15 Dec 2020 21:19:06 -0500 Subject: [PATCH 4/4] alternative approach --- lib/bundle/commands/add.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/bundle/commands/add.rb b/lib/bundle/commands/add.rb index ffd96d524..063d63823 100644 --- a/lib/bundle/commands/add.rb +++ b/lib/bundle/commands/add.rb @@ -12,20 +12,20 @@ def run(*args, global: false, file: nil) name = args.first # only support one formula at a time for now options = {} # we don't currently support passing options - # read the relevant Brewfile - parsed_entries = Bundle::Dsl.new(Brewfile.read(global: global, file: file)).entries + # parse the relevant Brewfile + dsl = Bundle::Dsl.new(Brewfile.read(global: global, file: file)) # check each of the entries in the specified Brewfile - parsed_entries.each do |entry| + 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 - # need some help / pointers here - # is it possible to use Bundle::Dsl to create an in memory representation - # of the brewfile that we read, add an entry and then dump that back to the file? + dsl.brew(name, options) + # execute brew bundle + # execute brew bundle dump end end end