-
Notifications
You must be signed in to change notification settings - Fork 0
/
bur
executable file
·241 lines (185 loc) · 5.23 KB
/
bur
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env ruby
# bur: brew-update report
#
# With the output of `brew update` in the paste buffer, or in a file passed as an argument,
# generate and load an HTML page listing the formulae mentioned.
#
# TODO:
# - Highlight row under cursor.
# - Make entire row clickable, if possible.
#
# SOMEDAY:
# - Turn all URLs in descriptions into links.
# - Smart-quote all descriptions.
# http://www.w3schools.com/css/css_table.asp
require 'date'
require 'json'
require 'colorize'
require 'haml'
require 'tempfile'
require 'html/table'
include HTML
require 'erb'
include ERB::Util
STYLESHEET =<<EOF
a:link {
text-decoration: none;
color: #000099;
}
a:visited {
color: #000099;
}
a:hover {
color:#0000FF;
}
a:active {
color:#DADAFF;
}
body {
font-family: -apple-system, "HelveticaNeue", "Helvetica";
}
table {
horizontal-align: center;
border-collapse: collapse;
empty-cells: hide;
}
tr {
padding: 10em;
}
tr:nth-child(even) {
background-color: #e6f3ff;
}
/*
* tr:hover {
* background-color: #f0f000;
* }
*/
tr.title {
font-size: 1.5em;
/* text-align: right; */
/* column-span: 2; */
}
td {
padding: 0.5em;
}
td.name {
text-align: right;
}
EOF
template =<<EOF
%html
%head
%title= "bur #{Date.today}"
%style= STYLESHEET
%body= monolith_table(groups, metadata)
EOF
# Take `brew update` output, and turn it into a hash of group names to Formula
# names. Groups names are things like "New", "Updated", and "Renamed". We
# delete the "Deleted" group, since by definition the members are no longer of
# interest to us, and we can no longer retrieve their metadata.
#
# If we were passed a file name, open that and use its contents as the input;
# otherwise, pull the content out of the paste buffer.
def extract_groups
groups = Hash.new { |hash, key| hash[key] = [] }
type_exp = /^==> (?<type>\S+) Formulae$/
column_divider = /\s{2,}|\n/
cur_type = nil
lines = ( ARGV.empty? ?
%x(pbpaste).split("\n") :
File.open(ARGV.first).readlines.map(&:chomp) )
lines.each do |line|
if matched = type_exp.match(line)
cur_type = matched[:type]
elsif cur_type
groups[cur_type].concat(line.split(column_divider))
end
end
# Now that we've extracted the data from the output, get the proper names.
# This might well be split into its own method.
regexes = {
'Updated' => /^(?<name>\S+)(?: ✔)?$/,
'Renamed' => /^(?:\S+(?: ✔)? -> )(?<name>\S+)$/
}
%w[Updated Renamed].each do |group|
if groups.has_key?(group)
groups[group].map! do |entry|
regexes[group].match(entry)[:name]
end
end
end
groups.delete('Deleted')
groups.each { |_, names| names.sort! }
groups
end
# Generate a unique set of formula names, and get the metadata for them.
# Return a hash whose keys are the full names for the Formulae, and values are
# the Formula hashes.
def load_metadata(groups)
nameset = groups.values.flatten.uniq
basenames = nameset.map { |n| File.basename(n) }
names = basenames.join(' ')
raw = JSON.parse(%x(brew info --json=v1 #{names}))
raw.inject({}) do |named, entry|
named[entry['full_name']] = entry
named
end
end
# Having assembled both the names of the Formulae, grouped by the nature of
# the change to them, and the metadata for all of them, generate one big table
# laying them all out.
def monolith_table(groups, metadata)
checkmark = " <span style='color: limegreen'>✔</span>"
# TODO: The checkmark crap below needs to be abstracted.
formatters = {
'New' => proc { |f, l| ['', '', l] },
'Updated' => proc { |f, l| ['', '', l + (metadata[f]['installed'].any? ? checkmark : '' ) ] },
'Renamed' => proc { |f, l| ["#{metadata[f]['oldname']}" + (metadata[f]['installed'].any? ? checkmark : '' ), "→", l ] }
}
table = HTML::Table.new do |t|
t.align = 'center'
end
groups.each do |group_name, members|
title = Table::Row.new do |r|
r.content = ['', '', group_name]
end
title.class_ = 'title'
table.push title
formatter = formatters[group_name]
members.each do |formula|
content = Table::Row.new do |r|
link = "<a href=#{metadata[formula]['homepage']}>#{formula}</a>"
r.content = formatter.call(formula, link).push(
h(metadata[formula]['desc'])
)
r.first.class_ = 'name'
end
table.push content
end
end
table
end
def create_html(template, groups, metadata)
engine = Haml::Engine.new(template)
engine.render(Object.new, { groups: groups, metadata: metadata })
end
# Create a temporary HTML file, and invoke the browser on it. If we just
# called `open` and then let the block exit, the tempfile would be deleted
# immediately, so quickly that the browser might not have a chance to load it.
# So we sleep for 5 minutes. We wrap the whole act in a `fork` so that the
# script itself exits immediately.
def launch_browser(html)
fork do
Tempfile.open(%w[bud .html]) do |handle|
handle.write(html)
handle.flush
system("open #{handle.path}")
sleep 300
end
end
end
# ------------------
groups = extract_groups
metadata = load_metadata(groups)
html = create_html(template, groups, metadata)
launch_browser(html)