From 86cbda62ac909726628458f40707c2768446a66a Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Fri, 22 Dec 2023 20:37:12 -0500 Subject: [PATCH 1/5] Add `--describe` arg to `brew add` --- cmd/add.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/cmd/add.rb b/cmd/add.rb index 733444c..6cb1658 100755 --- a/cmd/add.rb +++ b/cmd/add.rb @@ -24,6 +24,11 @@ def add_args switch "--formula", "--formulae", description: "Treat all named arguments as formulae." switch "--cask", "--casks", description: "Treat all named arguments as casks." + switch "--describe", + env: :bundle_dump_describe, + description: "`add` adds a description comment above each line, unless the " \ + "package does not have a description. " \ + "This is enabled by default if HOMEBREW_BUNDLE_DUMP_DESCRIBE is set." conflicts "formula", "cask" @@ -98,7 +103,15 @@ def add # Add the formula/cask to the file if it hasn't already been added. unless current_bundle_list.include?(brew.full_name) || (brew_name_resolves_to_full_name && current_bundle_list.include?(brew_name)) - file << "#{brewfile_prefix_type} #{quote_type}#{brew.full_name}#{quote_type}" << "\n" + # Adapted from `BrewDumper.dump`: + # https://github.com/Homebrew/homebrew-bundle/blob/master/lib/bundle/brew_dumper.rb#L59-L64 + brewline = if args.describe? && brew.desc + brew.desc.split("\n").map { |s| "# #{s}\n" }.join + else + "" + end + + file << brewline + "#{brewfile_prefix_type} #{quote_type}#{brew.full_name}#{quote_type}" << "\n" oh1 "Added #{display_type} #{Formatter.identifier(brew.full_name)} to Brewfile" unless silent From f74f5c2fd6cb09a1ab98d4885496a65bf9a9a2e9 Mon Sep 17 00:00:00 2001 From: Olivia Kinnear <51250849+superatomic@users.noreply.github.com> Date: Mon, 25 Dec 2023 21:26:44 -0600 Subject: [PATCH 2/5] Update usage message --- cmd/add.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/add.rb b/cmd/add.rb index 6cb1658..503bad3 100755 --- a/cmd/add.rb +++ b/cmd/add.rb @@ -26,9 +26,9 @@ def add_args switch "--cask", "--casks", description: "Treat all named arguments as casks." switch "--describe", env: :bundle_dump_describe, - description: "`add` adds a description comment above each line, unless the " \ + description: "Add a descriptive comment above each line, unless the " \ "package does not have a description. " \ - "This is enabled by default if HOMEBREW_BUNDLE_DUMP_DESCRIBE is set." + "This is enabled by default if `HOMEBREW_BUNDLE_DUMP_DESCRIBE` is set." conflicts "formula", "cask" From 49d7702961c752e484f2b1034f6f66f797ae2c50 Mon Sep 17 00:00:00 2001 From: Olivia Kinnear <51250849+superatomic@users.noreply.github.com> Date: Mon, 25 Dec 2023 21:30:40 -0600 Subject: [PATCH 3/5] Simplify code for adding descriptive comments --- cmd/add.rb | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/cmd/add.rb b/cmd/add.rb index 503bad3..49ccf25 100755 --- a/cmd/add.rb +++ b/cmd/add.rb @@ -103,15 +103,12 @@ def add # Add the formula/cask to the file if it hasn't already been added. unless current_bundle_list.include?(brew.full_name) || (brew_name_resolves_to_full_name && current_bundle_list.include?(brew_name)) + # Add a descriptive comment if requested. # Adapted from `BrewDumper.dump`: - # https://github.com/Homebrew/homebrew-bundle/blob/master/lib/bundle/brew_dumper.rb#L59-L64 - brewline = if args.describe? && brew.desc - brew.desc.split("\n").map { |s| "# #{s}\n" }.join - else - "" - end - - file << brewline + "#{brewfile_prefix_type} #{quote_type}#{brew.full_name}#{quote_type}" << "\n" + # https://github.com/Homebrew/homebrew-bundle/blob/e4798d8075e1a793f065be3e5e1674ec09193d17/lib/bundle/brew_dumper.rb#L59-L63 + file << brew.desc.split("\n").map { |s| "# #{s}\n" }.join if args.describe? && brew.desc + + file << "#{brewfile_prefix_type} #{quote_type}#{brew.full_name}#{quote_type}" << "\n" oh1 "Added #{display_type} #{Formatter.identifier(brew.full_name)} to Brewfile" unless silent From feb64212d9d4746871c5899e100d7470e6ae848d Mon Sep 17 00:00:00 2001 From: Olivia Kinnear <51250849+superatomic@users.noreply.github.com> Date: Mon, 25 Dec 2023 21:50:13 -0600 Subject: [PATCH 4/5] Drop descriptive comments from Brewfile --- cmd/drop.rb | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/cmd/drop.rb b/cmd/drop.rb index 0e1f5fa..fc7a9ed 100755 --- a/cmd/drop.rb +++ b/cmd/drop.rb @@ -81,13 +81,24 @@ def drop lines = [] has_removed_line = false + # Store comments that look like they were generated by `--describe` in a buffer. + # If it turns out the comments are directly preceding a line that should be dropped, + # drop the comments retroactively by resetting the array without pushing it to `lines`. + comment_line_buffer = [] + File.foreach(brewfile) do |line| - unless line.match(/^\s*#{brewfile_prefix_type}\s+["'](#{regex_name})["']/) - lines.push(line) + if line.match(/^\s*# (?!brew|cask|tap|mas|whalebrew|vscode)\w/) + comment_line_buffer.push(line) else - has_removed_line = true + unless line.match(/^\s*#{brewfile_prefix_type}\s+["'](#{regex_name})["']/) + lines.push(*comment_line_buffer, line) + else + has_removed_line = true + end + comment_line_buffer = [] end end + lines.push(*comment_line_buffer) if comment_line_buffer # Check to see if any lines were dropped from the file. If not, that's an error! if has_removed_line From 9a9d4fa3e3cfb28b8b3083c0d14e8e9b96f787de Mon Sep 17 00:00:00 2001 From: Olivia Kinnear <51250849+superatomic@users.noreply.github.com> Date: Mon, 25 Dec 2023 21:56:43 -0600 Subject: [PATCH 5/5] Document `--describe` --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 1a55da7..2f5712f 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,9 @@ tap "superatomic/bundle-extensions" To use single quotes instead of double quotes for Brewfile lines (e.g. `brew 'bat'` instead of `brew "bat"`), set the environment variable `HOMEBREW_BUNDLE_QUOTE_TYPE` to the value `single`. + To add a descriptive comment above each line (like `brew bundle dump --describe`), use the `--describe` option. + This is enabled by default if the environment variable `HOMEBREW_BUNDLE_DUMP_DESCRIBE` is set. + - **`brew drop [FORMULA/CASK...]`** Removes one or more provided formulae and/or casks from a `Brewfile`.