Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --describe arg to brew add #14

Merged
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
10 changes: 10 additions & 0 deletions cmd/add.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 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."

conflicts "formula", "cask"

Expand Down Expand Up @@ -98,6 +103,11 @@ 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/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
Expand Down
17 changes: 14 additions & 3 deletions cmd/drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, why bother with this ?! match here? To avoid capturing places where someone commented out a line like # brew "foo" ?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's the goal. It's a pretty ugly hack but I think it'll address the most common cases where a comment would otherwise incorrectly be removed.

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
Expand Down