-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathcask-analytics
executable file
·95 lines (72 loc) · 2.21 KB
/
cask-analytics
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
#!/usr/bin/env ruby
require 'date'
require 'json'
require 'open-uri'
require 'open3'
require 'optparse'
require 'pathname'
# Options
ARGV.push('--help') if ARGV.empty?
options = {}
OptionParser.new do |parser|
parser.banner = <<~BANNER
Show analytics information for casks in the main taps.
Usage:
#{File.basename($PROGRAM_NAME)} [options] <cask_name>
Options:
BANNER
parser.on(
'-a', '--no-age',
'Do not show when cask was added (faster output).'
)
end.parse!(into: options)
# Helpers
def shallow?(repo)
Open3.capture2(
'git', '-C', repo.to_path,
'rev-parse', '--is-shallow-repository'
).first.strip == 'true'
end
# Run
HBC_TAPS = Pathname.new(Open3.capture2('brew', '--repository', 'homebrew/cask').first).dirname
ARGV.each do |cask_name|
cask_path = HBC_TAPS.glob("homebrew-cask*/Casks/#{cask_name[0]}/#{cask_name}.rb").first
abort 'Did not find any cask locally named ' + cask_name if cask_path.nil?
puts cask_name
analytics_dir = Pathname.new('/tmp').join('cask-analytics')
analytics_dir.mkpath
%w[30 90 365].each do |days|
json_file = analytics_dir.join("#{days}d.json")
unless json_file.exist?
json_file.write(URI.parse(
"https://formulae.brew.sh/api/analytics/cask-install/#{days}d.json"
).read)
end
analytics = JSON.parse(json_file.read)['items']
cask_info = analytics.select { |hash| hash['cask'] == cask_name }.first
print "#{days} days: "
if cask_info.nil?
puts 'n/a'
else
puts "#{cask_info['count']} (##{cask_info['number']})"
end
end
cask_tap_dir = cask_path.dirname.dirname
unless options[:'no-age']
if shallow?(cask_tap_dir)
system('git', '-C', cask_tap_dir.to_path, 'fetch', '--unshallow')
end
cask_added_date = Date.parse(
Open3.capture2(
'git', '-C', cask_tap_dir.to_path,
'log', '--diff-filter=A',
'--follow', '--max-count=1',
'--format=%aI', cask_path.to_path
).first.strip
)
cask_added_formatted = cask_added_date.strftime('%Y, %B %d')
cask_age = (Date.today - cask_added_date).to_i.to_s
puts "Age: #{cask_age} days (added #{cask_added_formatted})"
end
puts # Empty line to separate multiple casks
end