Skip to content

Commit

Permalink
Merge pull request #17242 from Homebrew/fix-cask-source-file-path-loa…
Browse files Browse the repository at this point in the history
…ding-issues

Fix cask source file path loading issues
  • Loading branch information
apainintheneck authored May 9, 2024
2 parents 21e1300 + 3dcb260 commit c44e053
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 19 deletions.
12 changes: 8 additions & 4 deletions Library/Homebrew/cask/cask_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ def load(config:)
@content = path.read(encoding: "UTF-8")
@config = config

return FromAPILoader.new(token, from_json: JSON.parse(@content)).load(config:) if path.extname == ".json"
if path.extname == ".json"
return FromAPILoader.new(token, from_json: JSON.parse(@content), path:).load(config:)
end

begin
instance_eval(content, path).tap do |cask|
Expand Down Expand Up @@ -278,10 +280,11 @@ def self.try_new(ref, warn: false)
new("#{tap}/#{token}")
end

sig { params(token: String, from_json: Hash).void }
def initialize(token, from_json: T.unsafe(nil))
sig { params(token: String, from_json: Hash, path: T.nilable(Pathname)).void }
def initialize(token, from_json: T.unsafe(nil), path: nil)
@token = token.sub(%r{^homebrew/(?:homebrew-)?cask/}i, "")
@path = CaskLoader.default_path(@token)
@sourcefile_path = path
@path = path || CaskLoader.default_path(@token)
@from_json = from_json
end

Expand All @@ -290,6 +293,7 @@ def load(config:)

cask_options = {
loaded_from_api: true,
sourcefile_path: @sourcefile_path,
source: JSON.pretty_generate(json_cask),
config:,
loader: self,
Expand Down
22 changes: 22 additions & 0 deletions Library/Homebrew/cli/named_args.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,28 @@ def to_formulae_to_casks(only: parent&.only_formula_or_cask, method: nil)
.map(&:freeze).freeze
end

# Returns formulae and casks after validating that a tap is present for each of them.
def to_formulae_and_casks_with_taps
formulae_and_casks_with_taps, formulae_and_casks_without_taps =
to_formulae_and_casks.partition do |formula_or_cask|
T.cast(formula_or_cask, T.any(Formula, Cask::Cask)).tap&.installed?
end

return formulae_and_casks_with_taps if formulae_and_casks_without_taps.blank?

types = []
types << "formulae" if formulae_and_casks_without_taps.any?(Formula)
types << "casks" if formulae_and_casks_without_taps.any?(Cask::Cask)

odie <<~ERROR
These #{types.join(" and ")} are not in any locally installed taps!
#{formulae_and_casks_without_taps.sort_by(&:to_s).join("\n ")}
You may need to run `brew tap` to install additional taps.
ERROR
end

def to_formulae_and_casks_and_unavailable(only: parent&.only_formula_or_cask, method: nil)
@to_formulae_casks_unknowns ||= {}
@to_formulae_casks_unknowns[method] = downcased_unique_named.map do |name|
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/dev-cmd/audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def run
"brew audit [name ...]"
end

args.named.to_formulae_and_casks
args.named.to_formulae_and_casks_with_taps
.partition { |formula_or_cask| formula_or_cask.is_a?(Formula) }
end
end
Expand Down
8 changes: 1 addition & 7 deletions Library/Homebrew/dev-cmd/bump.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,7 @@ def run
casks = args.formula? ? [] : Cask::Caskroom.casks
formulae + casks
elsif args.named.present?
if args.formula?
args.named.to_formulae
elsif args.cask?
args.named.to_casks
else
args.named.to_formulae_and_casks
end
args.named.to_formulae_and_casks_with_taps
end

formulae_and_casks = formulae_and_casks&.sort_by do |formula_or_cask|
Expand Down
8 changes: 1 addition & 7 deletions Library/Homebrew/dev-cmd/livecheck.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,7 @@ def run
casks = args.formula? ? [] : Cask::Cask.all(eval_all: args.eval_all?)
formulae + casks
elsif args.named.present?
if args.formula?
args.named.to_formulae
elsif args.cask?
args.named.to_casks
else
args.named.to_formulae_and_casks
end
args.named.to_formulae_and_casks_with_taps
elsif File.exist?(watchlist_path)
begin
names = Pathname.new(watchlist_path).read.lines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,14 @@
/invalid 'depends_on macos' value: unknown or unsupported macOS version:/)
end
end

context "with a JSON cask file" do
let(:sourcefile_path) { TEST_FIXTURE_DIR/"cask/everything.json" }

it "loads a cask with a source file path" do
cask = described_class.new(sourcefile_path).load(config: nil)
expect(cask.sourcefile_path).to eq sourcefile_path
end
end
end
end

0 comments on commit c44e053

Please sign in to comment.