forked from Homebrew/homebrew-cask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_cask_token
executable file
·418 lines (356 loc) · 12.9 KB
/
generate_cask_token
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#!/System/Library/Frameworks/Ruby.framework/Versions/Current/usr/bin/ruby
#
# generate_cask_token
#
# todo:
#
# remove Ruby 2.0 dependency and change shebang line
#
# detect Cask files which differ only by the placement of hyphens.
#
# merge entirely into "brew cask create" command
#
###
### dependencies
###
require "pathname"
require "open3"
begin
# not available by default
require "active_support/inflector"
rescue LoadError
end
###
### configurable constants
###
EXPANDED_SYMBOLS = {
"+" => "plus",
"@" => "at",
}.freeze
CASK_FILE_EXTENSION = ".rb".freeze
# Hardcode App names that cannot be transformed automatically.
# Example: in "x48.app", "x48" is not a version number.
# The value in the hash should be a valid Cask token.
APP_EXCEPTION_PATS = {
# looks like a trailing version, but is not.
%r{\Aiterm\Z}i => "iterm2",
%r{\Aiterm2\Z}i => "iterm2",
%r{\Apgadmin3\Z}i => "pgadmin3",
%r{\Ax48\Z}i => "x48",
%r{\Avitamin-r[\s\d\.]*\Z}i => "vitamin-r",
%r{\Aimagealpha\Z}i => "imagealpha",
# upstream is in the midst of changing branding
%r{\Abitcoin-?qt\Z}i => "bitcoin-core",
# "mac" cannot be separated from the name because it is in an English phrase
%r{\Aplayonmac\Z}i => "playonmac",
%r{\Acleanmymac[\s\d\.]*\Z}i => "cleanmymac",
# arguably we should not have kept these two exceptions
%r{\Akismac\Z}i => "kismac",
%r{\Avoicemac\Z}i => "voicemac",
}.freeze
# Preserve trailing patterns on App names that could be mistaken
# for version numbers, etc
PRESERVE_TRAILING_PATS = [
%r{id3}i,
%r{mp3}i,
%r{3[\s-]*d}i,
%r{diff3}i,
%r{\A[^\d]+\+\Z}i,
].freeze
# The code that employs these patterns against App names
# - hacks a \b (word-break) between CamelCase and snake_case transitions
# - anchors the pattern to end-of-string
# - applies the patterns repeatedly until there is no match
REMOVE_TRAILING_PATS = [
# spaces
%r{\s+}i,
# generic terms
%r{\bapp}i,
%r{\b(?:quick[\s-]*)?launcher}i,
# "mac", "for mac", "for OS X", "macOS", "for macOS".
%r{\b(?:for)?[\s-]*mac(?:intosh|OS)?}i,
%r{\b(?:for)?[\s-]*os[\s-]*x}i,
# hardware designations such as "for x86", "32-bit", "ppc"
%r{(?:\bfor\s*)?x.?86}i,
%r{(?:\bfor\s*)?\bppc}i,
%r{(?:\bfor\s*)?\d+.?bits?}i,
# frameworks
%r{\b(?:for)?[\s-]*(?:oracle|apple|sun)*[\s-]*(?:jvm|java|jre)}i,
%r{\bgtk}i,
%r{\bqt}i,
%r{\bwx}i,
%r{\bcocoa}i,
# localizations
%r{en\s*-\s*us}i,
# version numbers
%r{[^a-z0-9]+}i,
%r{\b(?:version|alpha|beta|gamma|release|release.?candidate)(?:[\s\.\d-]*\d[\s\.\d-]*)?}i,
%r{\b(?:v|ver|vsn|r|rc)[\s\.\d-]*\d[\s\.\d-]*}i,
%r{\d+(?:[a-z\.]\d+)*}i,
%r{\b\d+\s*[a-z]}i,
%r{\d+\s*[a-c]}i, # constrained to a-c b/c of false positives
].freeze
# Patterns which are permitted (undisturbed) following an interior version number
AFTER_INTERIOR_VERSION_PATS = [
%r{ce}i,
%r{pro}i,
%r{professional}i,
%r{client}i,
%r{server}i,
%r{host}i,
%r{viewer}i,
%r{launcher}i,
%r{installer}i,
].freeze
###
### classes
###
class AppName < String
def self.remove_trailing_pat
@@remove_trailing_pat ||= %r{(?<=.)(?:#{REMOVE_TRAILING_PATS.join('|')})\Z}i
end
def self.preserve_trailing_pat
@@preserve_trailing_pat ||= %r{(?:#{PRESERVE_TRAILING_PATS.join('|')})\Z}i
end
def self.after_interior_version_pat
@@after_interior_version_pat ||= %r{(?:#{AFTER_INTERIOR_VERSION_PATS.join('|')})}i
end
def english_from_app_bundle
return self if ascii_only?
return self unless File.exist?(self)
# check Info.plist CFBundleDisplayName
bundle_name = Open3.popen3(*%w[
/usr/libexec/PlistBuddy -c
],
"Print CFBundleDisplayName",
Pathname.new(self).join("Contents", "Info.plist").to_s) do |_stdin, stdout, _stderr|
begin
stdout.gets.force_encoding("UTF-8").chomp
rescue
end
end
return AppName.new(bundle_name) if bundle_name && bundle_name.ascii_only?
# check Info.plist CFBundleName
bundle_name = Open3.popen3(*%w[
/usr/libexec/PlistBuddy -c
],
"Print CFBundleName",
Pathname.new(self).join("Contents", "Info.plist").to_s) do |_stdin, stdout, _stderr|
begin
stdout.gets.force_encoding("UTF-8").chomp
rescue
end
end
return AppName.new(bundle_name) if bundle_name && bundle_name.ascii_only?
# check localization strings
local_strings_file = Pathname.new(self).join("Contents", "Resources", "en.lproj", "InfoPlist.strings")
local_strings_file = Pathname.new(self).join("Contents", "Resources", "English.lproj", "InfoPlist.strings") unless local_strings_file.exist?
if local_strings_file.exist?
bundle_name = File.open(local_strings_file, "r:UTF-16LE:UTF-8") do |fh|
%r{\ACFBundle(?:Display)?Name\s*=\s*"(.*)";\Z}.match(fh.readlines.grep(%r{^CFBundle(?:Display)?Name\s*=\s*}).first) do |match|
match.captures.first
end
end
return AppName.new(bundle_name) if bundle_name && bundle_name.ascii_only?
end
# check Info.plist CFBundleExecutable
bundle_name = Open3.popen3(*%w[
/usr/libexec/PlistBuddy -c
],
"Print CFBundleExecutable",
Pathname.new(self).join("Contents", "Info.plist").to_s) do |_stdin, stdout, _stderr|
begin
stdout.gets.force_encoding("UTF-8").chomp
rescue
end
end
return AppName.new(bundle_name) if bundle_name && bundle_name.ascii_only?
self
end
def basename
if Pathname.new(self).exist?
AppName.new(Pathname.new(self).basename.to_s)
else
self
end
end
def remove_extension
sub(%r{\.app\Z}i, "")
end
def decompose_to_ascii
# crudely (and incorrectly) decompose extended latin characters to ASCII
return self if ascii_only?
return self unless respond_to?(:mb_chars)
AppName.new(mb_chars.normalize(:kd).each_char.select(&:ascii_only?).join)
end
def hardcoded_exception
APP_EXCEPTION_PATS.each do |regexp, exception|
return AppName.new(exception) if regexp.match(self)
end
nil
end
def insert_vertical_tabs_for_camel_case
app_name = AppName.new(self)
if app_name.sub!(%r{(#{self.class.preserve_trailing_pat})\Z}i, "")
trailing = Regexp.last_match(1)
end
app_name.gsub!(%r{([^A-Z])([A-Z])}, "\\1\v\\2")
app_name.sub!(%r{\Z}, trailing) if trailing
app_name
end
def insert_vertical_tabs_for_snake_case
gsub(%r{_}, "\v")
end
def clean_up_vertical_tabs
gsub(%r{\v}, "")
end
def remove_interior_versions!
# done separately from REMOVE_TRAILING_PATS because this
# requires a substitution with a backreference
sub!(%r{(?<=.)[\.\d]+(#{self.class.after_interior_version_pat})\Z}i, '\1')
sub!(%r{(?<=.)[\s\.\d-]*\d[\s\.\d-]*(#{self.class.after_interior_version_pat})\Z}i, '-\1')
end
def remove_trailing_strings_and_versions
app_name = insert_vertical_tabs_for_camel_case
.insert_vertical_tabs_for_snake_case
while self.class.remove_trailing_pat.match(app_name) &&
!self.class.preserve_trailing_pat.match(app_name)
app_name.sub!(self.class.remove_trailing_pat, "")
end
app_name.remove_interior_versions!
app_name.clean_up_vertical_tabs
end
def simplified
return @simplified if @simplified
@simplified = english_from_app_bundle
.basename
.decompose_to_ascii
.remove_extension
@simplified = @simplified.hardcoded_exception || @simplified.remove_trailing_strings_and_versions
@simplified
end
end
class CaskFileName < String
def spaces_to_hyphens
gsub(%r{ +}, "-")
end
def delete_invalid_chars
gsub(%r{[^a-z0-9-]+}, "")
end
def collapse_multiple_hyphens
gsub(%r{--+}, "-")
end
def delete_leading_hyphens
gsub(%r{^--+}, "")
end
def delete_hyphens_before_numbers
gsub(%r{-([0-9])}, '\1')
end
def spell_out_symbols
cask_file_name = self
EXPANDED_SYMBOLS.each do |k, v|
cask_file_name.gsub!(k, " #{v} ")
end
cask_file_name.sub(%r{ +\Z}, "")
end
def add_extension
sub(%r{(?:#{escaped_cask_file_extension})?\Z}i, CASK_FILE_EXTENSION)
end
def remove_extension
sub(%r{#{escaped_cask_file_extension}\Z}i, "")
end
def from_simplified_app_name
return @from_simplified_app_name if @from_simplified_app_name
@from_simplified_app_name = if APP_EXCEPTION_PATS.rassoc(remove_extension)
remove_extension
else
remove_extension
.downcase
.spell_out_symbols
.spaces_to_hyphens
.delete_invalid_chars
.collapse_multiple_hyphens
.delete_leading_hyphens
.delete_hyphens_before_numbers
end
raise "Could not determine Simplified App name" if @from_simplified_app_name.empty?
@from_simplified_app_name.add_extension
end
end
###
### methods
###
def project_root
Dir.chdir File.dirname(File.expand_path(__FILE__))
@git_root ||= Open3.popen3(*%w[
git rev-parse --show-toplevel
]) do |_stdin, stdout, _stderr|
begin
Pathname.new(stdout.gets.chomp)
rescue
raise "could not find project root"
end
end
raise "could not find project root" unless @git_root.exist?
@git_root
end
def escaped_cask_file_extension
@escaped_cask_file_extension ||= Regexp.escape(CASK_FILE_EXTENSION)
end
def simplified_app_name
@simplified_app_name ||= AppName.new(ARGV.first.dup.force_encoding("UTF-8")).simplified
end
def cask_file_name
@cask_file_name ||= CaskFileName.new(simplified_app_name).from_simplified_app_name
end
def cask_token
@cask_token ||= cask_file_name.remove_extension
end
def warnings
return @warnings if @warnings
@warnings = []
unless APP_EXCEPTION_PATS.rassoc(cask_token)
if %r{\d} =~ cask_token
@warnings.push "WARNING: '#{cask_token}' contains digits. Digits which are version numbers should be removed."
end
end
filename = project_root.join("Casks", cask_file_name)
if filename.exist?
@warnings.push "WARNING: the file '#{filename}' already exists. Prepend the vendor name if this is not a duplicate."
end
@warnings
end
def report
puts "Proposed Simplified App name: #{simplified_app_name}" if $debug
puts "Proposed token: #{cask_token}"
puts "Proposed file name: #{cask_file_name}"
puts "Cask Header Line: cask '#{cask_token}' do"
unless warnings.empty?
$stderr.puts "\n"
$stderr.puts warnings
$stderr.puts "\n"
exit 1
end
end
###
### main
###
usage = <<-EOS
Usage: generate_cask_token [ -debug ] <application.app>
Given an Application name or a path to an Application, propose a
Cask token, filename, and header line.
With -debug, also provide the internal "Simplified App Name".
EOS
if ARGV.first =~ %r{^-+h(elp)?$}i
puts usage
exit 0
end
if ARGV.first =~ %r{^-+debug?$}i
$debug = 1
ARGV.shift
end
unless ARGV.length == 1
puts usage
exit 1
end
report