From 1097496a063265d9ff6709da265e1b704b769311 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Mon, 18 Mar 2024 16:42:40 -0700 Subject: [PATCH] Port Homebrew::DevCmd::BumpRevision --- Library/Homebrew/dev-cmd/bump-revision.rb | 125 +++++++++--------- .../test/dev-cmd/bump-revision_spec.rb | 5 +- 2 files changed, 65 insertions(+), 65 deletions(-) diff --git a/Library/Homebrew/dev-cmd/bump-revision.rb b/Library/Homebrew/dev-cmd/bump-revision.rb index 683ea0b1b6..2bbf5b085f 100644 --- a/Library/Homebrew/dev-cmd/bump-revision.rb +++ b/Library/Homebrew/dev-cmd/bump-revision.rb @@ -1,76 +1,75 @@ -# typed: true +# typed: strict # frozen_string_literal: true +require "abstract_command" require "formula" require "cli/parser" module Homebrew - module_function + module DevCmd + class BumpRevision < AbstractCommand + cmd_args do + description <<~EOS + Create a commit to increment the revision of . If no revision is + present, "revision 1" will be added. + EOS + switch "-n", "--dry-run", + description: "Print what would be done rather than doing it." + switch "--remove-bottle-block", + description: "Remove the bottle block in addition to bumping the revision." + switch "--write-only", + description: "Make the expected file modifications without taking any Git actions." + flag "--message=", + description: "Append to the default commit message." - sig { returns(CLI::Parser) } - def bump_revision_args - Homebrew::CLI::Parser.new do - description <<~EOS - Create a commit to increment the revision of . If no revision is - present, "revision 1" will be added. - EOS - switch "-n", "--dry-run", - description: "Print what would be done rather than doing it." - switch "--remove-bottle-block", - description: "Remove the bottle block in addition to bumping the revision." - switch "--write-only", - description: "Make the expected file modifications without taking any Git actions." - flag "--message=", - description: "Append to the default commit message." + conflicts "--dry-run", "--write-only" - conflicts "--dry-run", "--write-only" - - named_args :formula, min: 1, without_api: true - end - end - - def bump_revision - args = bump_revision_args.parse - - # As this command is simplifying user-run commands then let's just use a - # user path, too. - ENV["PATH"] = PATH.new(ORIGINAL_PATHS).to_s - - args.named.to_formulae.each do |formula| - current_revision = formula.revision - new_revision = current_revision + 1 - - if args.dry_run? - unless args.quiet? - old_text = "revision #{current_revision}" - new_text = "revision #{new_revision}" - if current_revision.zero? - ohai "add #{new_text.inspect}" - else - ohai "replace #{old_text.inspect} with #{new_text.inspect}" - end - end - else - Homebrew.install_bundler_gems!(groups: ["ast"]) - require "utils/ast" - - formula_ast = Utils::AST::FormulaAST.new(formula.path.read) - if current_revision.zero? - formula_ast.add_stanza(:revision, new_revision) - else - formula_ast.replace_stanza(:revision, new_revision) - end - formula_ast.remove_stanza(:bottle) if args.remove_bottle_block? - formula.path.atomic_write(formula_ast.process) + named_args :formula, min: 1, without_api: true end - message = "#{formula.name}: revision bump #{args.message}" - if args.dry_run? - ohai "git commit --no-edit --verbose --message=#{message} -- #{formula.path}" - elsif !args.write_only? - formula.path.parent.cd do - safe_system "git", "commit", "--no-edit", "--verbose", - "--message=#{message}", "--", formula.path + sig { override.void } + def run + # As this command is simplifying user-run commands then let's just use a + # user path, too. + ENV["PATH"] = PATH.new(ORIGINAL_PATHS).to_s + + args.named.to_formulae.each do |formula| + current_revision = formula.revision + new_revision = current_revision + 1 + + if args.dry_run? + unless args.quiet? + old_text = "revision #{current_revision}" + new_text = "revision #{new_revision}" + if current_revision.zero? + ohai "add #{new_text.inspect}" + else + ohai "replace #{old_text.inspect} with #{new_text.inspect}" + end + end + else + Homebrew.install_bundler_gems!(groups: ["ast"]) + require "utils/ast" + + formula_ast = Utils::AST::FormulaAST.new(formula.path.read) + if current_revision.zero? + formula_ast.add_stanza(:revision, new_revision) + else + formula_ast.replace_stanza(:revision, new_revision) + end + formula_ast.remove_stanza(:bottle) if args.remove_bottle_block? + formula.path.atomic_write(formula_ast.process) + end + + message = "#{formula.name}: revision bump #{args.message}" + if args.dry_run? + ohai "git commit --no-edit --verbose --message=#{message} -- #{formula.path}" + elsif !args.write_only? + formula.path.parent.cd do + safe_system "git", "commit", "--no-edit", "--verbose", + "--message=#{message}", "--", formula.path + end + end end end end diff --git a/Library/Homebrew/test/dev-cmd/bump-revision_spec.rb b/Library/Homebrew/test/dev-cmd/bump-revision_spec.rb index 13d9833bf0..10c2646451 100644 --- a/Library/Homebrew/test/dev-cmd/bump-revision_spec.rb +++ b/Library/Homebrew/test/dev-cmd/bump-revision_spec.rb @@ -1,7 +1,8 @@ # frozen_string_literal: true require "cmd/shared_examples/args_parse" +require "dev-cmd/bump-revision" -RSpec.describe "brew bump-revision" do - it_behaves_like "parseable arguments" +RSpec.describe Homebrew::DevCmd::BumpRevision do + it_behaves_like "parseable arguments", argv: ["foo"] end