-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
formula.rb
130 lines (107 loc) 路 4.04 KB
/
formula.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# typed: true
# frozen_string_literal: true
require "extend/cachable"
require "api/download"
module Homebrew
module API
# Helper functions for using the formula JSON API.
module Formula
extend Cachable
private_class_method :cache
sig { params(name: String).returns(Hash) }
def self.fetch(name)
Homebrew::API.fetch "formula/#{name}.json"
end
sig { params(formula: ::Formula).returns(::Formula) }
def self.source_download(formula)
path = formula.ruby_source_path || "Formula/#{formula.name}.rb"
git_head = formula.tap_git_head || "HEAD"
tap = formula.tap&.full_name || "Homebrew/homebrew-core"
download = Homebrew::API::Download.new(
"https://raw.githubusercontent.com/#{tap}/#{git_head}/#{path}",
formula.ruby_source_checksum,
cache: HOMEBREW_CACHE_API_SOURCE/"#{tap}/#{git_head}/Formula",
)
download.fetch
Formulary.factory(download.symlink_location,
formula.active_spec_sym,
alias_path: formula.alias_path,
flags: formula.class.build_flags)
end
sig { returns(T::Boolean) }
def self.download_and_cache_data!
if Homebrew::API.internal_json_v3?
json_formulae, updated = Homebrew::API.fetch_json_api_file "internal/v3/homebrew-core.jws.json"
overwrite_cache! T.cast(json_formulae, T::Hash[String, T.untyped])
else
json_formulae, updated = Homebrew::API.fetch_json_api_file "formula.jws.json"
cache["aliases"] = {}
cache["renames"] = {}
cache["formulae"] = json_formulae.to_h do |json_formula|
json_formula["aliases"].each do |alias_name|
cache["aliases"][alias_name] = json_formula["name"]
end
(json_formula["oldnames"] || [json_formula["oldname"]].compact).each do |oldname|
cache["renames"][oldname] = json_formula["name"]
end
[json_formula["name"], json_formula.except("name")]
end
end
updated
end
private_class_method :download_and_cache_data!
sig { returns(T::Hash[String, Hash]) }
def self.all_formulae
unless cache.key?("formulae")
json_updated = download_and_cache_data!
write_names_and_aliases(regenerate: json_updated)
end
cache["formulae"]
end
sig { returns(T::Hash[String, String]) }
def self.all_aliases
unless cache.key?("aliases")
json_updated = download_and_cache_data!
write_names_and_aliases(regenerate: json_updated)
end
cache["aliases"]
end
sig { returns(T::Hash[String, String]) }
def self.all_renames
unless cache.key?("renames")
json_updated = download_and_cache_data!
write_names_and_aliases(regenerate: json_updated)
end
cache["renames"]
end
sig { returns(Hash) }
def self.tap_migrations
# Not sure that we need to reload here.
unless cache.key?("tap_migrations")
json_updated = download_and_cache_data!
write_names_and_aliases(regenerate: json_updated)
end
cache["tap_migrations"]
end
sig { returns(String) }
def self.tap_git_head
# Note sure we need to reload here.
unless cache.key?("tap_git_head")
json_updated = download_and_cache_data!
write_names_and_aliases(regenerate: json_updated)
end
cache["tap_git_head"]
end
sig { params(regenerate: T::Boolean).void }
def self.write_names_and_aliases(regenerate: false)
download_and_cache_data! unless cache.key?("formulae")
return unless Homebrew::API.write_names_file(all_formulae.keys, "formula", regenerate:)
(HOMEBREW_CACHE_API/"formula_aliases.txt").open("w") do |file|
all_aliases.each do |alias_name, real_name|
file.puts "#{alias_name}|#{real_name}"
end
end
end
end
end
end