-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.rb
More file actions
506 lines (397 loc) · 10.1 KB
/
Copy pathutils.rb
File metadata and controls
506 lines (397 loc) · 10.1 KB
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# typed: false
# frozen_string_literal: true
Homebrew.install_bundler_gems!
require "active_support/all"
ENV["HOMEBREW_LIBRARY_PATH"] ||= HOMEBREW_LIBRARY_PATH
HOME ||= Etc.getpwuid.dir
VERSION_REGEX ||= '([0-9]+(?:\.[0-9]+)+)'
QUOTE_REGEX ||= '["\']'
SPACE ||= " "
WHITESPACE ||= /\s+/.freeze
NEWLINE ||= "\n"
DOT ||= "."
BLANK ||= ""
CHARS ||= { comma: ",", dot: DOT, hyphen: "-", underscore: "_" }
$; = $, = ENV.fetch "IFS", nil
def tokens
token.split "-", 2
end
def glob(*args)
options = args.pop.keys.map do |option|
File.const_get "FNM_#{option.upcase}"
end if args.last.instance_of? Hash
Pathname.glob args.flatten.map(&:to_p).map(&:expand_path), *options
end
class Pathname
def to_p
sub "~", HOME
end
def b
to_s
end
def glob(*args)
super join(args.first || BLANK), *args.drop(1)
end
def no_ext
sub_ext BLANK
end
def chomp
to_s.chomp.to_p
end
def parent(n = 1)
self + "../" * n
end
def start_with?(string)
to_s.try __method__, string
end
def repository?
(self/".git").directory?
end
end
def File.source
caller_locations.first.path.to_p
end
class Dir
def self.source
File.source.dirname
end
def self.home(user = nil)
"~#{user}".to_p.expand_path
end
end
def inreplace(*args)
Utils::Inreplace.inreplace *args rescue nil
end
def parse_json(*json, **options)
_parse JSON, :parse, *json
end
def parse_yaml(*yaml, aliases: true, **options)
_parse YAML, :safe_load, *yaml, aliases: aliases, **options
end
private def _parse(format, method, *data, **options)
options.except! :symbolize_names
hash = format.try method, read(*data), **options
hash.with_indifferent_access
end
def read(*data)
data.map(&:to_p).map {|data| data.file? ? data.read : "#{data.chomp}\n" }.join
end
alias argf_read read
def load(token_or_path)
path = token_or_path.to_p
Formulary.try :factory, token_or_path rescue Cask::CaskLoader.public_send __method__, path
rescue
info = {
format: "file",
path: path,
token: path.basename.no_ext.to_s
}
repo = path.ascend.find(&:repository?)
info[:tap] = Tap.try :fetch, repo.each_filename.last(2).join_path if repo.present?
info[:formula?] = info[:cask?] = false
attributes = Struct.new *info.keys, :url, :appcast
object = Object.const_set "Ruby#{info[:format].classify}", attributes
object.new *info.values
end
def alias_methods(*aliases, method)
aliases.each {|a| alias_method a, method }
end
class Hash
alias except excluding
def with_indifferent_access
deep_symbolize_keys.merge stringify_keys
end
end
class String
extend = Cask::DSL::Version
methods = /(_to|^(no|before|after))_|csv/
extend.instance_methods.grep(methods).each do |method|
define_method(method) { extend.new(self).try method }
end
CHARS.each do |character, char|
characters = character.to_s.pluralize
define_method("#{characters}_to_spaces") { tr char, SPACE }
method = define_method("space_to_#{characters}") { gsub WHITESPACE, char }
alias_method "spaces_to_#{characters}", method
method = define_method("split_#{character}") { split char }
alias_method method.to_s.pluralize, method
next if character == :comma
method = define_method("#{character}s_to_csv") { split(char).to_csv }
alias_method "#{characters}_to_commas", method
end
def words
split(/[_\W]+|(?=[A-Z])/).compact_blank
end
def kebabcase downcase = true
words.join_hyphen.public_send downcase ? :downcase : :to_s
end
def to_arg length = :long
kebabcase.prepend "-" * %i[short long].offset.index(length)
end
def camelcase first_letter = :upper
titleize.split.join.camelize first_letter
end
def snakecase downcase = true
words.join_underscore.public_send downcase ? :downcase : :to_s
end
def titlecase
except = %w[a an and as at but by en for if in of on or the to v v. via vs vs.]
humanize.gsub /\b(?<!['’`])(?!#{except.join_regex}\b)[a-z]/, &:capitalize
end
def sentencecase
humanize.gsub /[.!?…]\s+[a-z]/, &:upcase
end
def no_space
remove WHITESPACE
end
alias no_spaces no_space
alias squash no_space
def to_p
Pathname.new(self).sub "~", HOME
end
%i[file? directory?].each do |method|
define_method(method) { to_p.public_send __method__ }
end
def no_ext
to_p.public_send(__method__).to_s
end
def to_uri
URI.parse self
end
def unquote
delete "\"'"
end
def expand
`echo #{self}`.chomp
end
def to_regexp(*options)
Regexp.new self, *options
end
alias to_regex to_regexp
end
def expand string
string.public_send __method__
end
class Symbol
def to_arg
to_s.public_send __method__
end
end
class Array
alias except excluding
def to_proc
proc {|i| i.try *self }
end
def rdrop(n = 1)
reverse.drop(n).reverse
end
def offset n = 1
Array.new(n).concat self
end
chars = { **CHARS, space: SPACE, newline: NEWLINE }
chars.each do |character, char|
method = define_method("join_#{character}") { join char }
alias_method method.to_s.pluralize, method
end
alias to_csv join_comma
alias join_dash join_hyphen
alias join__ join_underscore
def join_path
join File::SEPARATOR
end
def join_pipe
join "|"
end
def join_regex
Regexp.union self
end
classes = [String, Symbol, Integer, Float, Hash, Array, Enumerable, Regexp, Pathname, URI]
classes.each do |type|
method = define_method(type.name.downcase.pluralize) { grep type }
alias_method "filter_#{method}", method
end
alias filter_paths filter_pathnames
def filter_numbers
filter_integers + filter_floats
end
alias numbers filter_numbers
end
module Enumerable
def filter_map &block
map(&block).compact_blank
end
def find_yield &block
(length >= 10_000 ? lazy : self).map(&block).find(&:itself)
end
def last n = 1
to_a.last n
end
end
class Object
def values_from(*methods)
methods.filter_map {|method| method.to_s.split_dot.reduce self, :try }
end
def value_from(*methods)
values_from(*methods).first
end
end
class URI::HTTPS
def to_uri
self
end
end
class Formula
def formula?
true
end
def cask?
false
end
def format
"formula"
end
def token
name
end
def installed?
Formula.installed.lazy.include? self
end
end
class Cask::DSL::Version
def divide
words
end
end
class Cask::Cask
def cask?
true
end
def formula?
false
end
def format
"cask"
end
def path
sourcefile_path
end
end
def zsh(*args)
options = args.last.class == Hash ? args.pop : {}
options[:chdir], = glob options[:chdir] if options[:chdir]
args.map! do |arg|
path, = glob arg
path.nil? ? arg.class == Pathname ? arg : arg.split : "'#{path}'"
end
output = system_command __method__, input: args.flatten.join_space, **options
output.stdout.chomp
end
module Git
extend Utils::Git
# https://git-scm.com/book/en/Git-Internals-Environment-Variables
@root = caller_locations.second.path.to_p.ascend.find(&:repository?)
git = "git -C #{@root}"
@remote, @branch = `#{git} ls-remote --get-url; #{git} branch --show-current`.split
class << self
def author
`#{git} config user.name`.chomp
end
def remote
@remote.no_ext.to_uri
end
def branch
@branch
end
def root
@root
end
def repo
root.basename.to_s
end
def dir
@root/".git"
end
end
end
def GitHub.username
user["login"]
end
# https://docs.github.com/actions/learn-github-actions/contexts#github-context
def GitHub.repository
Git.remote.path[1..]
end
def GitHub.repository_owner
Git.remote.path.to_p.each_filename.first
end
# https://docs.brew.sh/Formula-Cookbook#specifying-the-download-strategy-explicitly
class NoDownloadStrategy < NoUnzipCurlDownloadStrategy
def fetch(timeout: nil, **)
touch cached_location
return if cache.basename.to_s != "Cask"
Process.fork do
sleep 10
cask = HOMEBREW_PREFIX/"Caskroom"/name/version
cask.children.each &:delete
end
end
end
class PasswordUnzipCurlDownloadStrategy < NoUnzipCurlDownloadStrategy
def fetch(timeout: nil, **)
sha256, password, name = meta[:data].fetch_values :sha256, :password, :name
staged_path, = mkdir_p HOMEBREW_PREFIX/"Caskroom"/@name/version
staged_path.cd do
zip = Pathname "#{name}.zip"
curl_download url, to: zip unless zip.exist?
zip.verify_checksum sha256
quiet = verbose? ? "v" : "qq"
system "unzip", "-#{quiet}P", password, zip
unless cached_location.exist?
v = "v" if verbose?
system *%W[ditto -ck#{v} --rsrc --sequesterRsrc --keepParent], zip, cached_location
end
symlink_location.make_symlink cached_location
zip.delete
end
end
end
class DropboxCurlDownloadStrategy < NoUnzipCurlDownloadStrategy
def fetch(timeout: nil, **)
uri = url.to_uri
uri.query = "dl=1"
curl_download uri, to: cached_location unless cached_location.exist?
ln_sf cached_location, symlink_location
end
end
class << DownloadStrategyDetector
alias detect_from_symbol _detect_from_symbol
def detect_from_symbol(symbol)
case symbol
when :no_download then NoDownloadStrategy
when :password_unzip then PasswordUnzipCurlDownloadStrategy
when :dropbox then DropboxCurlDownloadStrategy
else _detect_from_symbol symbol
end
end
end
def colors(*args)
_artifact "~/Library/Colors", *args
end
private def _artifact(*args, target: nil)
path, artifact, target = [*args, target].compact.map(&:to_p)
target ||= artifact.basename
target = path/target unless target.absolute?
artifact artifact, target: target
end
return if (caller_locations.map(&:label) & %w[install reinstall]).empty?
formula, relative = caller_locations
.filter {|c| c.label == "require_relative" }
.reverse.map(&:path).map(&:to_p)
name = formula.basename.no_ext
relative ||= File.source
Process.fork do
sleep 8
pattern = "C*/#{name}/{.metadata/,}*/{.brew,*/Casks}/#{name}.rb"
formula_copy = HOMEBREW_PREFIX.glob(pattern).max_by(&File.method(:ctime))
path = relative.no_ext.relative_path_from formula.parent
inreplace formula_copy, /(require)_relative\s+"#{path}"$/, "\\1 '#{relative}'"
end