-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
args.rb
220 lines (179 loc) 路 5.73 KB
/
args.rb
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
# frozen_string_literal: true
require "ostruct"
module Homebrew
module CLI
class Args < OpenStruct
attr_reader :processed_options, :args_parsed
# undefine tap to allow --tap argument
undef tap
def initialize(argv:)
super
@argv = argv
@args_parsed = false
@processed_options = []
end
def freeze_processed_options!(processed_options)
@processed_options += processed_options
@processed_options.freeze
@args_parsed = true
end
def option_to_name(option)
option.sub(/\A--?/, "")
.tr("-", "_")
end
def cli_args
return @cli_args if @cli_args
@cli_args = []
processed_options.each do |short, long|
option = long || short
switch = "#{option_to_name(option)}?".to_sym
flag = option_to_name(option).to_sym
if @table[switch] == true || @table[flag] == true
@cli_args << option
elsif @table[flag].instance_of? String
@cli_args << option + "=" + @table[flag]
elsif @table[flag].instance_of? Array
@cli_args << option + "=" + @table[flag].join(",")
end
end
@cli_args
end
def options_only
@options_only ||= cli_args.select { |arg| arg.start_with?("-") }
end
def flags_only
@flags_only ||= cli_args.select { |arg| arg.start_with?("--") }
end
def passthrough
options_only - CLI::Parser.global_options.values.map(&:first).flatten
end
def named
return [] if remaining.nil?
remaining
end
def no_named?
named.blank?
end
# If the user passes any flags that trigger building over installing from
# a bottle, they are collected here and returned as an Array for checking.
def collect_build_args
build_flags = []
build_flags << "--HEAD" if head
build_flags << "--universal" if build_universal
build_flags << "--build-bottle" if build_bottle
build_flags << "--build-from-source" if build_from_source
build_flags
end
def formulae
require "formula"
@formulae ||= (downcased_unique_named - casks).map do |name|
if name.include?("/") || File.exist?(name)
Formulary.factory(name, spec)
else
Formulary.find_with_priority(name, spec)
end
end.uniq(&:name)
end
def resolved_formulae
require "formula"
@resolved_formulae ||= (downcased_unique_named - casks).map do |name|
Formulary.resolve(name, spec: spec(nil))
end.uniq(&:name)
end
def casks
@casks ||= downcased_unique_named.grep HOMEBREW_CASK_TAP_CASK_REGEX
end
def kegs
require "keg"
require "formula"
require "missing_formula"
@kegs ||= downcased_unique_named.map do |name|
raise UsageError if name.empty?
rack = Formulary.to_rack(name.downcase)
dirs = rack.directory? ? rack.subdirs : []
if dirs.empty?
if (reason = Homebrew::MissingFormula.suggest_command(name, "uninstall"))
$stderr.puts reason
end
raise NoSuchKegError, rack.basename
end
linked_keg_ref = HOMEBREW_LINKED_KEGS/rack.basename
opt_prefix = HOMEBREW_PREFIX/"opt/#{rack.basename}"
begin
if opt_prefix.symlink? && opt_prefix.directory?
Keg.new(opt_prefix.resolved_path)
elsif linked_keg_ref.symlink? && linked_keg_ref.directory?
Keg.new(linked_keg_ref.resolved_path)
elsif dirs.length == 1
Keg.new(dirs.first)
else
f = if name.include?("/") || File.exist?(name)
Formulary.factory(name)
else
Formulary.from_rack(rack)
end
unless (prefix = f.installed_prefix).directory?
raise MultipleVersionsInstalledError, rack.basename
end
Keg.new(prefix)
end
rescue FormulaUnavailableError
raise <<~EOS
Multiple kegs installed to #{rack}
However we don't know which one you refer to.
Please delete (with rm -rf!) all but one and then try again.
EOS
end
end
end
def build_stable?
!(HEAD? || devel?)
end
private
def downcased_unique_named
# Only lowercase names, not paths, bottle filenames or URLs
arguments = if args_parsed
named
else
cmdline_args.reject { |arg| arg.start_with?("-") }
end
arguments.map do |arg|
if arg.include?("/") || arg.end_with?(".tar.gz") || File.exist?(arg)
arg
else
arg.downcase
end
end.uniq
end
def head
return true if args_parsed && HEAD?
cmdline_args.include?("--HEAD")
end
def devel
return true if args_parsed && devel?
cmdline_args.include?("--devel")
end
def build_universal
return true if args_parsed && universal?
cmdline_args.include?("--universal")
end
def build_bottle
return true if args_parsed && build_bottle?
cmdline_args.include?("--build-bottle")
end
def build_from_source
return true if args_parsed && (build_from_source? || s?)
cmdline_args.include?("--build-from-source") || cmdline_args.include?("-s")
end
def spec(default = :stable)
if head
:head
elsif devel
:devel
else
default
end
end
end
end
end