Skip to content

Commit

Permalink
Drop description lines along with brew lines
Browse files Browse the repository at this point in the history
Also move the describe switch after everything else to be more
consistent with `brew bundle dump`. This doesn't affect help output
ordering.
  • Loading branch information
ian-h-chamberlain committed Dec 24, 2023
1 parent 3529c0e commit 614faa4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
12 changes: 7 additions & 5 deletions cmd/add.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ 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"

named_args [:formula, :cask], min: 1

switch "--describe",
env: :bundle_dump_describe,
description: "A description comment is added above each line, unless the " \
"package does not have a description. " \
"This is enabled by default if HOMEBREW_BUNDLE_DUMP_DESCRIBE is set."
end
end

Expand Down Expand Up @@ -106,6 +107,7 @@ def add
# 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
puts "got env #{ENV.fetch('HOMEBREW_BUNDLE_DUMP_DESCRIBE', nil)}"
brew.desc.split("\n").map { |s| "# #{s}\n" }.join
else
""
Expand Down
16 changes: 15 additions & 1 deletion cmd/drop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,28 @@ def drop
brew.full_name
end

# Adapted from `BrewDumper.dump`:
# https://github.com/Homebrew/homebrew-bundle/blob/master/lib/bundle/brew_dumper.rb#L59-L64
desc = if brew.desc
brew.desc.split("\n").map { |s| "# #{s}\n" }
else
""
end

lines = []
has_removed_line = false

File.foreach(brewfile) do |line|
File.foreach(brewfile).each_with_index do |line, i|
unless line.match(/^\s*#{brewfile_prefix_type}\s+["'](#{regex_name})["']/)
lines.push(line)
else
has_removed_line = true

# This might not be the best way to do this, but it works
# as long as the user never modified the description line.
if lines[-desc.length..-1].eql? desc
lines.pop(desc.length)
end
end
end

Expand Down

0 comments on commit 614faa4

Please sign in to comment.