Skip to content

Commit

Permalink
Update casks-without-zap to include and sort by download count (#144751)
Browse files Browse the repository at this point in the history
  • Loading branch information
razvanazamfirei committed Apr 10, 2023
1 parent 8b5982c commit e4c818a
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions developer/bin/casks-without-zap
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ require "open3"
require "optparse"
require "pathname"
require "tmpdir"
require "json"
require "open-uri"

# Exit cleanup
TMP_DIR = Pathname.new(Dir.mktmpdir).freeze
Expand All @@ -13,6 +15,17 @@ at_exit { TMP_DIR.rmtree }
# Constants
ONLINE_ISSUE = "https://github.com/Homebrew/homebrew-cask/issues/88469"
CASK_REPOS = %w[homebrew-cask homebrew-cask-versions homebrew-cask-drivers].freeze
CASK_JSON_URL = "https://formulae.brew.sh/api/analytics/cask-install/365d.json"

# Download the file and save it to the specified directory
File.open("#{TMP_DIR}/cask.json", "wb") do |output_file|
URI.parse(CASK_JSON_URL).open do |input_file|
output_file.write(input_file.read)
end
end

CASK_JSON = File.read("#{TMP_DIR}/cask.json").freeze
CASK_DATA = JSON.parse(CASK_JSON).freeze

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

def find_count(cask_name, data)
data["items"].find { |item| item["cask"] == cask_name.to_s }&.dig("count") || "0"
end

# Options
ARGV.push("--help") unless ARGV.include?("run")

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

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

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

casks.each { message.push("* [`#{cask_name(_1)}`](#{cask_url(tap_dir, _1)})") }
# Sort casks by count
sorted_casks = casks.sort_by { |cask_file| -find_count(cask_name(cask_file), CASK_DATA).delete(",").to_i }

sorted_casks.each do |cask_file|
cask_name = cask_name(cask_file)
count = find_count(cask_name, CASK_DATA)
message.push("* [`#{cask_name}`](#{cask_url(tap_dir, cask_file)}) - Downloads: #{count}")
end

message.push("</details>")
end.freeze
Expand Down

0 comments on commit e4c818a

Please sign in to comment.