@@ -5,6 +5,8 @@ require "open3"
5
5
require "optparse"
6
6
require "pathname"
7
7
require "tmpdir"
8
+ require "json"
9
+ require "open-uri"
8
10
9
11
# Exit cleanup
10
12
TMP_DIR = Pathname . new ( Dir . mktmpdir ) . freeze
@@ -13,6 +15,17 @@ at_exit { TMP_DIR.rmtree }
13
15
# Constants
14
16
ONLINE_ISSUE = "https://github.com/Homebrew/homebrew-cask/issues/88469"
15
17
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
16
29
17
30
# Helpers
18
31
def cask_name ( cask_path )
@@ -26,6 +39,10 @@ def cask_url(tap_dir, cask_path)
26
39
"https://github.com/Homebrew/#{ tap_base } /blob/master/Casks/#{ cask_base } "
27
40
end
28
41
42
+ def find_count ( cask_name , data )
43
+ data [ "items" ] . find { |item | item [ "cask" ] == cask_name . to_s } &.dig ( "count" ) || "0"
44
+ end
45
+
29
46
# Options
30
47
ARGV . push ( "--help" ) unless ARGV . include? ( "run" )
31
48
@@ -64,9 +81,10 @@ end.freeze
64
81
CASKS_NO_ZAP = ALL_CASKS . each_with_object ( { } ) do |( tap_dir , casks ) , without_zap |
65
82
without_zap [ tap_dir ] = [ ]
66
83
67
- # Populate hash with casks without a zap
84
+ # Populate hash with casks without a zap that are not discontinued
68
85
casks
69
86
. reject { |file | file . readlines . any? { _1 . start_with? ( /\s +(# No )?zap / ) } }
87
+ . reject { |file | file . readlines . any? { _1 . start_with? ( /\s +discontinued / ) } }
70
88
. each { without_zap [ tap_dir ] . push ( _1 ) }
71
89
72
90
# 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|
77
95
message . push ( "<details><summary>#{ tap_dir . dirname . basename . to_path } </summary>" )
78
96
message . push ( "" ) # Empty line so the markdown still works inside the HTML
79
97
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
81
106
82
107
message . push ( "</details>" )
83
108
end . freeze
0 commit comments