diff --git a/Library/Homebrew/github_packages.rb b/Library/Homebrew/github_packages.rb index 44c9cce78e..ec1bff665f 100644 --- a/Library/Homebrew/github_packages.rb +++ b/Library/Homebrew/github_packages.rb @@ -48,13 +48,7 @@ class GitHubPackages raise UsageError, "HOMEBREW_GITHUB_PACKAGES_USER is unset." if user.blank? raise UsageError, "HOMEBREW_GITHUB_PACKAGES_TOKEN is unset." if token.blank? - skopeo = [ - which("skopeo"), - which("skopeo", ENV["HOMEBREW_PATH"]), - HOMEBREW_PREFIX/"bin/skopeo", - ].compact.first - skopeo = ensure_formula_installed!("skopeo", - reason: "for upload").opt_bin/"skopeo" unless skopeo.exist? + skopeo = ensure_executable!("skopeo", reason: "for upload") require "json_schemer" diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index 141fa63811..cb22ed415b 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -494,6 +494,20 @@ module Kernel formula end + # Ensure the given executable is exist otherwise install the brewed version + def ensure_executable!(name, formula_name = nil, reason: "") + formula_name ||= name + + executable = [ + which(name), + which(name, ENV["HOMEBREW_PATH"]), + HOMEBREW_PREFIX/"bin/#{name}", + ].compact.first + return executable if executable.exist? + + ensure_formula_installed!(formula_name, reason: reason).opt_bin/name + end + def paths @paths ||= PATH.new(ENV["HOMEBREW_PATH"]).map do |p| File.expand_path(p).chomp("/")