Skip to content

Commit e4c818a

Browse files
Update casks-without-zap to include and sort by download count (#144751)
1 parent 8b5982c commit e4c818a

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

developer/bin/casks-without-zap

+27-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ require "open3"
55
require "optparse"
66
require "pathname"
77
require "tmpdir"
8+
require "json"
9+
require "open-uri"
810

911
# Exit cleanup
1012
TMP_DIR = Pathname.new(Dir.mktmpdir).freeze
@@ -13,6 +15,17 @@ at_exit { TMP_DIR.rmtree }
1315
# Constants
1416
ONLINE_ISSUE = "https://github.com/Homebrew/homebrew-cask/issues/88469"
1517
CASK_REPOS = %w[homebrew-cask homebrew-cask-versions homebrew-cask-drivers].freeze
18+
CASK_JSON_URL = "https://formulae.brew.sh/api/analytics/cask-install/365d.json"
19+
20+
# Download the file and save it to the specified directory
21+
File.open("#{TMP_DIR}/cask.json", "wb") do |output_file|
22+
URI.parse(CASK_JSON_URL).open do |input_file|
23+
output_file.write(input_file.read)
24+
end
25+
end
26+
27+
CASK_JSON = File.read("#{TMP_DIR}/cask.json").freeze
28+
CASK_DATA = JSON.parse(CASK_JSON).freeze
1629

1730
# Helpers
1831
def cask_name(cask_path)
@@ -26,6 +39,10 @@ def cask_url(tap_dir, cask_path)
2639
"https://github.com/Homebrew/#{tap_base}/blob/master/Casks/#{cask_base}"
2740
end
2841

42+
def find_count(cask_name, data)
43+
data["items"].find { |item| item["cask"] == cask_name.to_s }&.dig("count") || "0"
44+
end
45+
2946
# Options
3047
ARGV.push("--help") unless ARGV.include?("run")
3148

@@ -64,9 +81,10 @@ end.freeze
6481
CASKS_NO_ZAP = ALL_CASKS.each_with_object({}) do |(tap_dir, casks), without_zap|
6582
without_zap[tap_dir] = []
6683

67-
# Populate hash with casks without a zap
84+
# Populate hash with casks without a zap that are not discontinued
6885
casks
6986
.reject { |file| file.readlines.any? { _1.start_with?(/\s+(# No )?zap /) } }
87+
.reject { |file| file.readlines.any? { _1.start_with?(/\s+discontinued /) } }
7088
.each { without_zap[tap_dir].push(_1) }
7189

7290
# Reject tap directory if there are no casks without zap
@@ -77,7 +95,14 @@ CASK_LISTS = CASKS_NO_ZAP.each_with_object([]) do |(tap_dir, casks), message|
7795
message.push("<details><summary>#{tap_dir.dirname.basename.to_path}</summary>")
7896
message.push("") # Empty line so the markdown still works inside the HTML
7997

80-
casks.each { message.push("* [`#{cask_name(_1)}`](#{cask_url(tap_dir, _1)})") }
98+
# Sort casks by count
99+
sorted_casks = casks.sort_by { |cask_file| -find_count(cask_name(cask_file), CASK_DATA).delete(",").to_i }
100+
101+
sorted_casks.each do |cask_file|
102+
cask_name = cask_name(cask_file)
103+
count = find_count(cask_name, CASK_DATA)
104+
message.push("* [`#{cask_name}`](#{cask_url(tap_dir, cask_file)}) - Downloads: #{count}")
105+
end
81106

82107
message.push("</details>")
83108
end.freeze

0 commit comments

Comments
 (0)