diff --git a/Library/Homebrew/dev-cmd/unpack.rb b/Library/Homebrew/dev-cmd/unpack.rb index 86a35544a0..1fa6afceeb 100644 --- a/Library/Homebrew/dev-cmd/unpack.rb +++ b/Library/Homebrew/dev-cmd/unpack.rb @@ -1,74 +1,78 @@ -# typed: true +# typed: strict # frozen_string_literal: true +require "abstract_command" +require "fileutils" require "stringio" require "formula" require "cli/parser" module Homebrew - sig { returns(CLI::Parser) } - def self.unpack_args - Homebrew::CLI::Parser.new do - description <<~EOS - Unpack the source files for into subdirectories of the current - working directory. - EOS - flag "--destdir=", - description: "Create subdirectories in the directory named by instead." - switch "--patch", - description: "Patches for will be applied to the unpacked source." - switch "-g", "--git", - description: "Initialise a Git repository in the unpacked source. This is useful for creating " \ - "patches for the software." - switch "-f", "--force", - description: "Overwrite the destination directory if it already exists." + module DevCmd + class Unpack < AbstractCommand + include FileUtils - conflicts "--git", "--patch" + cmd_args do + description <<~EOS + Unpack the source files for into subdirectories of the current + working directory. + EOS + flag "--destdir=", + description: "Create subdirectories in the directory named by instead." + switch "--patch", + description: "Patches for will be applied to the unpacked source." + switch "-g", "--git", + description: "Initialise a Git repository in the unpacked source. This is useful for creating " \ + "patches for the software." + switch "-f", "--force", + description: "Overwrite the destination directory if it already exists." - named_args :formula, min: 1 - end - end + conflicts "--git", "--patch" - def self.unpack - args = unpack_args.parse - - formulae = args.named.to_formulae - - if (dir = args.destdir) - unpack_dir = Pathname.new(dir).expand_path - unpack_dir.mkpath - else - unpack_dir = Pathname.pwd - end - - odie "Cannot write to #{unpack_dir}" unless unpack_dir.writable_real? - - formulae.each do |f| - stage_dir = unpack_dir/"#{f.name}-#{f.version}" - - if stage_dir.exist? - odie "Destination #{stage_dir} already exists!" unless args.force? - - rm_rf stage_dir + named_args :formula, min: 1 end - oh1 "Unpacking #{Formatter.identifier(f.full_name)} to: #{stage_dir}" + sig { override.void } + def run + formulae = args.named.to_formulae - # show messages about tar - with_env VERBOSE: "1" do - f.brew do - f.patch if args.patch? - cp_r getwd, stage_dir, preserve: true + if (dir = args.destdir) + unpack_dir = Pathname.new(dir).expand_path + unpack_dir.mkpath + else + unpack_dir = Pathname.pwd end - end - next unless args.git? + odie "Cannot write to #{unpack_dir}" unless unpack_dir.writable_real? - ohai "Setting up Git repository" - cd(stage_dir) do - system "git", "init", "-q" - system "git", "add", "-A" - system "git", "commit", "-q", "-m", "brew-unpack" + formulae.each do |f| + stage_dir = unpack_dir/"#{f.name}-#{f.version}" + + if stage_dir.exist? + odie "Destination #{stage_dir} already exists!" unless args.force? + + rm_rf stage_dir + end + + oh1 "Unpacking #{Formatter.identifier(f.full_name)} to: #{stage_dir}" + + # show messages about tar + with_env VERBOSE: "1" do + f.brew do + f.patch if args.patch? + cp_r getwd, stage_dir, preserve: true + end + end + + next unless args.git? + + ohai "Setting up Git repository" + cd(stage_dir) do + system "git", "init", "-q" + system "git", "add", "-A" + system "git", "commit", "-q", "-m", "brew-unpack" + end + end end end end diff --git a/Library/Homebrew/test/dev-cmd/unpack_spec.rb b/Library/Homebrew/test/dev-cmd/unpack_spec.rb index b00d51ce04..10251c7dda 100644 --- a/Library/Homebrew/test/dev-cmd/unpack_spec.rb +++ b/Library/Homebrew/test/dev-cmd/unpack_spec.rb @@ -1,8 +1,9 @@ # frozen_string_literal: true require "cmd/shared_examples/args_parse" +require "dev-cmd/unpack" -RSpec.describe "brew unpack" do +RSpec.describe Homebrew::DevCmd::Unpack do it_behaves_like "parseable arguments" it "unpacks a given Formula's archive", :integration_test do