-
-
Notifications
You must be signed in to change notification settings - Fork 11k
Expand file tree
/
Copy pathformula.rb
More file actions
5027 lines (4452 loc) · 161 KB
/
formula.rb
File metadata and controls
5027 lines (4452 loc) · 161 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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# typed: strict
# frozen_string_literal: true
require "autobump_constants"
require "cache_store"
require "did_you_mean"
require "keg_only_reason"
require "lock_file"
require "formula_pin"
require "hardware"
require "utils"
require "utils/bottles"
require "utils/gzip"
require "utils/inreplace"
require "utils/shebang"
require "utils/shell"
require "utils/git_repository"
require "build_environment"
require "build_options"
require "formulary"
require "software_spec"
require "bottle"
require "pour_bottle_check"
require "head_software_spec"
require "bottle_specification"
require "livecheck"
require "service"
require "install_renamed"
require "pkg_version"
require "keg"
require "migrator"
require "linkage_checker"
require "extend/ENV"
require "language/java"
require "language/php"
require "language/python"
require "tab"
require "mktemp"
require "find"
require "utils/spdx"
require "on_system"
require "api"
require "api_hashable"
require "utils/output"
require "pypi_packages"
# A formula provides instructions and metadata for Homebrew to install a piece
# of software. Every Homebrew formula is a {Formula}.
# All subclasses of {Formula} (and all Ruby classes) have to be named
# `UpperCase` and `not-use-dashes`.
# A formula specified in `this-formula.rb` should have a class named
# `ThisFormula`. Homebrew does enforce that the name of the file and the class
# correspond.
# Make sure you check with `brew search` that the name is free!
# @abstract
# @see SharedEnvExtension
# @see Pathname
# @see https://www.rubydoc.info/stdlib/fileutils FileUtils
# @see https://docs.brew.sh/Formula-Cookbook Formula Cookbook
# @see https://rubystyle.guide Ruby Style Guide
#
# ### Example
#
# ```ruby
# class Wget < Formula
# homepage "https://www.gnu.org/software/wget/"
# url "https://ftp.gnu.org/gnu/wget/wget-1.15.tar.gz"
# sha256 "52126be8cf1bddd7536886e74c053ad7d0ed2aa89b4b630f76785bac21695fcd"
#
# def install
# system "./configure", "--prefix=#{prefix}"
# system "make", "install"
# end
# end
# ```
class Formula
include FileUtils
include Utils::Shebang
include Utils::Shell
include Utils::Output::Mixin
include Context
include OnSystem::MacOSAndLinux
include Homebrew::Livecheck::Constants
extend Forwardable
extend Cachable
extend APIHashable
extend T::Helpers
extend Utils::Output::Mixin
abstract!
# Used to track formulae that cannot be installed at the same time.
FormulaConflict = Struct.new(:name, :reason)
SUPPORTED_NETWORK_ACCESS_PHASES = [:build, :test, :postinstall].freeze
private_constant :SUPPORTED_NETWORK_ACCESS_PHASES
DEFAULT_NETWORK_ACCESS_ALLOWED = true
private_constant :DEFAULT_NETWORK_ACCESS_ALLOWED
# The name of this {Formula}.
# e.g. `this-formula`
#
# @api public
sig { returns(String) }
attr_reader :name
# The path to the alias that was used to identify this {Formula}.
# e.g. `/usr/local/Library/Taps/homebrew/homebrew-core/Aliases/another-name-for-this-formula`
sig { returns(T.nilable(Pathname)) }
attr_reader :alias_path
# The name of the alias that was used to identify this {Formula}.
# e.g. `another-name-for-this-formula`
sig { returns(T.nilable(String)) }
attr_reader :alias_name
# The fully-qualified name of this {Formula}.
# For core formulae it's the same as {#name}.
# e.g. `homebrew/tap-name/this-formula`
#
# @api public
sig { returns(String) }
attr_reader :full_name
# The fully-qualified alias referring to this {Formula}.
# For core formulae it's the same as {#alias_name}.
# e.g. `homebrew/tap-name/another-name-for-this-formula`
sig { returns(T.nilable(String)) }
attr_reader :full_alias_name
# The full path to this {Formula}.
# e.g. `/usr/local/Library/Taps/homebrew/homebrew-core/Formula/t/this-formula.rb`
#
# @api public
sig { returns(Pathname) }
attr_reader :path
# The {Tap} instance associated with this {Formula}.
# If it's `nil`, then this formula is loaded from a path or URL.
#
# @api internal
sig { returns(T.nilable(Tap)) }
attr_reader :tap
# The stable (and default) {SoftwareSpec} for this {Formula}.
# This contains all the attributes (e.g. URL, checksum) that apply to the
# stable version of this formula.
#
# @api internal
sig { returns(T.nilable(SoftwareSpec)) }
attr_reader :stable
# The HEAD {SoftwareSpec} for this {Formula}.
# Installed when using `brew install --HEAD`.
# This is always installed with the version `HEAD` and taken from the latest
# commit in the version control system.
# `nil` if there is no HEAD version.
#
# @see #stable
sig { returns(T.nilable(SoftwareSpec)) }
attr_reader :head
# The currently active {SoftwareSpec}.
# @see #determine_active_spec
sig { returns(SoftwareSpec) }
attr_reader :active_spec
protected :active_spec
# A symbol to indicate currently active {SoftwareSpec}.
# It's either `:stable` or `:head`.
# @see #active_spec
sig { returns(Symbol) }
attr_reader :active_spec_sym
# The most recent modified time for source files.
sig { returns(T.nilable(Time)) }
attr_reader :source_modified_time
# Used for creating new Homebrew versions of software without new upstream
# versions.
# @see .revision=
sig { returns(Integer) }
attr_reader :revision
# Used to change version schemes for packages.
# @see .version_scheme=
sig { returns(Integer) }
attr_reader :version_scheme
# Used to indicate API/ABI compatibility for dependencies.
# @see .compatibility_version=
sig { returns(T.nilable(Integer)) }
attr_reader :compatibility_version
# The current working directory during builds.
# Will only be non-`nil` inside {#install}.
sig { returns(T.nilable(Pathname)) }
attr_reader :buildpath
# The current working directory during tests.
# Will only be non-`nil` inside {.test}.
sig { returns(T.nilable(Pathname)) }
attr_reader :testpath
# When installing a bottle (binary package) from a local path this will be
# set to the full path to the bottle tarball. If not, it will be `nil`.
sig { returns(T.nilable(Pathname)) }
attr_accessor :local_bottle_path
# When performing a build, test, or other loggable action, indicates which
# log file location to use.
sig { returns(T.nilable(String)) }
attr_reader :active_log_type
# The {BuildOptions} or {Tab} for this {Formula}. Lists the arguments passed
# and any {.option}s in the {Formula}. Note that these may differ at
# different times during the installation of a {Formula}. This is annoying
# but is the result of state that we're trying to eliminate.
sig { returns(T.any(BuildOptions, Tab)) }
attr_reader :build
# Information about PyPI mappings for this {Formula} is stored
# as {PypiPackages} object.
sig { returns(PypiPackages) }
attr_reader :pypi_packages_info
# Whether this formula should be considered outdated
# if the target of the alias it was installed with has since changed.
# Defaults to true.
sig { returns(T::Boolean) }
attr_accessor :follow_installed_alias
alias follow_installed_alias? follow_installed_alias
# Whether or not to force the use of a bottle.
sig { returns(T::Boolean) }
attr_accessor :force_bottle
sig {
params(name: String, path: Pathname, spec: Symbol, alias_path: T.nilable(Pathname),
tap: T.nilable(Tap), force_bottle: T::Boolean).void
}
def initialize(name, path, spec, alias_path: nil, tap: nil, force_bottle: false)
# Only allow instances of subclasses. The base class does not hold any spec information (URLs etc).
raise "Do not call `Formula.new' directly without a subclass." unless self.class < Formula
# Stop any subsequent modification of a formula's definition.
# Changes do not propagate to existing instances of formulae.
# Now that we have an instance, it's too late to make any changes to the class-level definition.
self.class.freeze
@name = name
@unresolved_path = path
@path = T.let(path.resolved_path, Pathname)
@alias_path = alias_path
@alias_name = T.let((File.basename(alias_path) if alias_path), T.nilable(String))
@revision = T.let(self.class.revision || 0, Integer)
@version_scheme = T.let(self.class.version_scheme || 0, Integer)
@compatibility_version = T.let(self.class.compatibility_version, T.nilable(Integer))
@head = T.let(nil, T.nilable(SoftwareSpec))
@stable = T.let(nil, T.nilable(SoftwareSpec))
@autobump = T.let(true, T::Boolean)
@no_autobump_message = T.let(nil, T.nilable(T.any(String, Symbol)))
@force_bottle = force_bottle
@tap = T.let(tap, T.nilable(Tap))
@tap ||= if path == Formulary.core_path(name)
CoreTap.instance
else
Tap.from_path(path)
end
@pypi_packages_info = T.let(self.class.pypi_packages_info || PypiPackages.new, PypiPackages)
@full_name = T.let(T.must(full_name_with_optional_tap(name)), String)
@full_alias_name = T.let(full_name_with_optional_tap(@alias_name), T.nilable(String))
self.class.spec_syms.each do |sym|
spec_eval sym
end
@active_spec = T.let(determine_active_spec(spec), SoftwareSpec)
@active_spec_sym = T.let(head? ? :head : :stable, Symbol)
validate_attributes!
@build = T.let(active_spec.build, T.any(BuildOptions, Tab))
@pin = T.let(FormulaPin.new(self), FormulaPin)
@follow_installed_alias = T.let(true, T::Boolean)
@prefix_returns_versioned_prefix = T.let(false, T.nilable(T::Boolean))
@oldname_locks = T.let([], T::Array[FormulaLock])
@on_system_blocks_exist = T.let(false, T::Boolean)
@fully_loaded_formula = T.let(nil, T.nilable(Formula))
end
sig { params(spec_sym: Symbol).void }
def active_spec=(spec_sym)
spec = send(spec_sym)
raise FormulaSpecificationError, "#{spec_sym} spec is not available for #{full_name}" unless spec
old_spec_sym = @active_spec_sym
@active_spec = spec
@active_spec_sym = spec_sym
validate_attributes!
@build = active_spec.build
return if spec_sym == old_spec_sym
Dependency.clear_cache
Requirement.clear_cache
end
sig { params(build_options: T.any(BuildOptions, Tab)).void }
def build=(build_options)
old_options = @build
@build = build_options
return if old_options.used_options == build_options.used_options &&
old_options.unused_options == build_options.unused_options
Dependency.clear_cache
Requirement.clear_cache
end
# Ensure the given formula is installed.
# This is useful for installing a utility formula (e.g. `shellcheck` for `brew style`).
sig {
params(
reason: String,
latest: T::Boolean,
output_to_stderr: T::Boolean,
quiet: T::Boolean,
).returns(T.self_type)
}
def ensure_installed!(reason: "", latest: false, output_to_stderr: true, quiet: false)
if output_to_stderr || quiet
file = if quiet
File::NULL
else
$stderr
end
# Call this method itself with redirected stdout
redirect_stdout(file) do
return ensure_installed!(latest:, reason:, output_to_stderr: false)
end
end
reason = " for #{reason}" if reason.present?
unless any_version_installed?
ohai "Installing `#{name}`#{reason}..."
safe_system HOMEBREW_BREW_FILE, "install", "--formula", full_name
end
if latest && !latest_version_installed?
ohai "Upgrading `#{name}`#{reason}..."
safe_system HOMEBREW_BREW_FILE, "upgrade", "--formula", full_name
end
self
end
sig { returns(T::Boolean) }
def preserve_rpath? = self.class.preserve_rpath?
private
# Allow full name logic to be re-used between names, aliases and installed aliases.
sig { params(name: T.nilable(String)).returns(T.nilable(String)) }
def full_name_with_optional_tap(name)
if name.nil? || @tap.nil? || @tap.core_tap?
name
else
"#{@tap}/#{name}"
end
end
sig { params(name: T.any(String, Symbol)).void }
def spec_eval(name)
spec = self.class.send(name).dup
return unless spec.url
spec.owner = self
add_global_deps_to_spec(spec)
instance_variable_set(:"@#{name}", spec)
end
sig { params(spec: SoftwareSpec).void }
def add_global_deps_to_spec(spec); end
sig { params(requested: T.any(String, Symbol)).returns(SoftwareSpec) }
def determine_active_spec(requested)
spec = send(requested) || stable || head
spec || raise(FormulaSpecificationError, "#{full_name}: formula requires at least a URL")
end
sig { void }
def validate_attributes!
if name.blank? || name.match?(/\s/) || !Utils.safe_filename?(name)
raise FormulaValidationError.new(full_name, :name, name)
end
url = active_spec.url
raise FormulaValidationError.new(full_name, :url, url) if url.blank? || url.match?(/\s/)
val = version.respond_to?(:to_str) ? version.to_str : version
return if val.present? && !val.match?(/\s/) && Utils.safe_filename?(val)
raise FormulaValidationError.new(full_name, :version, val)
end
public
# The alias path that was used to install this formula, if it exists.
# Can differ from {#alias_path}, which is the alias used to find the formula,
# and is specified to this instance.
sig { returns(T.nilable(Pathname)) }
def installed_alias_path
build_tab = build
path = build_tab.source["path"] if build_tab.is_a?(Tab)
return unless path&.match?(%r{#{HOMEBREW_TAP_DIR_REGEX}/Aliases}o)
path = Pathname(path)
return unless path.symlink?
path
end
sig { returns(T.nilable(String)) }
def installed_alias_name = installed_alias_path&.basename&.to_s
sig { returns(T.nilable(String)) }
def full_installed_alias_name = full_name_with_optional_tap(installed_alias_name)
sig { returns(Pathname) }
def tap_path
return path unless (t = tap)
return Formulary.core_path(name) if t.core_tap?
return path unless t.installed?
t.formula_files_by_name[name] || path
end
# The path that was specified to find this formula.
sig { returns(T.nilable(Pathname)) }
def specified_path
return Homebrew::API::Formula.cached_json_file_path if loaded_from_api?
return alias_path if alias_path&.exist?
return @unresolved_path if @unresolved_path.exist?
return local_bottle_path if local_bottle_path.presence&.exist?
alias_path || @unresolved_path
end
# The name specified to find this formula.
sig { returns(String) }
def specified_name
alias_name || name
end
# The name (including tap) specified to find this formula.
sig { returns(String) }
def full_specified_name
full_alias_name || full_name
end
# The name specified to install this formula.
sig { returns(String) }
def installed_specified_name
installed_alias_name || name
end
# The name (including tap) specified to install this formula.
sig { returns(String) }
def full_installed_specified_name
full_installed_alias_name || full_name
end
# Is the currently active {SoftwareSpec} a {#stable} build?
sig { returns(T::Boolean) }
def stable?
active_spec == stable
end
# Is the currently active {SoftwareSpec} a {#head} build?
sig { returns(T::Boolean) }
def head?
active_spec == head
end
# Is this formula HEAD-only?
sig { returns(T::Boolean) }
def head_only?
!!head && !stable
end
# Stop RuboCop from erroneously indenting hash target
delegate [ # rubocop:disable Layout/HashAlignment
:bottle_defined?,
:bottle_tag?,
:bottled?,
:bottle_specification,
:downloader,
] => :active_spec
# The {Bottle} object for the currently active {SoftwareSpec}.
sig { returns(T.nilable(Bottle)) }
def bottle
@bottle ||= T.let(Bottle.new(self, bottle_specification), T.nilable(Bottle)) if bottled?
end
# The {Bottle} object for given tag.
sig { params(tag: T.nilable(Utils::Bottles::Tag)).returns(T.nilable(Bottle)) }
def bottle_for_tag(tag = nil)
Bottle.new(self, bottle_specification, tag) if bottled?(tag)
end
# The description of the software.
# @!method desc
# @see .desc
delegate desc: :"self.class"
# The SPDX ID of the software license.
# @!method license
# @see .license
delegate license: :"self.class"
# The homepage for the software.
# @!method homepage
# @see .homepage
delegate homepage: :"self.class"
# The `livecheck` specification for the software.
# @!method livecheck
# @see .livecheck
delegate livecheck: :"self.class"
# Is a `livecheck` specification defined for the software?
# @!method livecheck_defined?
# @see .livecheck_defined?
delegate livecheck_defined?: :"self.class"
# This is a legacy alias for `#livecheck_defined?`.
# @!method livecheckable?
# @see .livecheckable?
delegate livecheckable?: :"self.class"
# Exclude the formula from the autobump list.
# @!method no_autobump!
# @see .no_autobump!
delegate no_autobump!: :"self.class"
# Is the formula in the autobump list?
# @!method autobump?
# @see .autobump?
delegate autobump?: :"self.class"
# Is a `no_autobump!` method defined?
# @!method no_autobump_defined?
# @see .no_autobump_defined?
delegate no_autobump_defined?: :"self.class"
delegate no_autobump_message: :"self.class"
# Is a service specification defined for the software?
# @!method service?
# @see .service?
delegate service?: :"self.class"
# The version for the currently active {SoftwareSpec}.
# The version is autodetected from the URL and/or tag so only needs to be
# declared if it cannot be autodetected correctly.
# @!method version
# @see .version
delegate version: :active_spec
# Stop RuboCop from erroneously indenting hash target
delegate [ # rubocop:disable Layout/HashAlignment
:allow_network_access!,
:deny_network_access!,
:network_access_allowed?,
] => :"self.class"
# Whether this formula was loaded using the formulae.brew.sh API.
# @!method loaded_from_api?
# @see .loaded_from_api?
delegate loaded_from_api?: :"self.class"
# The API source data used to load this formula.
# Returns `nil` if the formula was not loaded from the API.
# @!method api_source
# @see .api_source
delegate api_source: :"self.class"
sig { void }
def update_head_version
return unless head?
head_spec = T.must(head)
return unless head_spec.downloader.is_a?(VCSDownloadStrategy)
return unless head_spec.downloader.cached_location.exist?
path = if ENV["HOMEBREW_ENV"]
ENV.fetch("PATH")
else
PATH.new(ORIGINAL_PATHS)
end
with_env(PATH: path) do
head_spec.version.update_commit(head_spec.downloader.last_commit)
end
end
# The {PkgVersion} for this formula with {version} and {#revision} information.
sig { returns(PkgVersion) }
def pkg_version = PkgVersion.new(version, revision)
# If this is a `@`-versioned formula.
sig { returns(T::Boolean) }
def versioned_formula? = name.include?("@")
# Returns any other `@`-versioned formulae names for any Formula (including versioned formulae).
sig { returns(T::Array[String]) }
def versioned_formulae_names
name_prefix = unversioned_formula_name || name
versioned_names = if (formula_tap = tap)
formula_tap.prefix_to_versioned_formulae_names.fetch(name_prefix, [])
else
versioned_formula_glob = if name_prefix.end_with?("-full")
"#{name_prefix.delete_suffix("-full")}@*-full.rb"
else
"#{name_prefix}@*.rb"
end
formula_names_for_glob(versioned_formula_glob)
end
versioned_names.reject do |versioned_name|
versioned_name == name
end
end
# Returns any `@`-versioned Formula objects for any Formula (including versioned formulae).
sig { returns(T::Array[Formula]) }
def versioned_formulae
versioned_formulae_names.filter_map do |name|
Formula[name]
rescue FormulaUnavailableError
nil
end.sort_by(&:version).reverse
end
sig { returns(T.nilable(String)) }
def unversioned_formula_name
return unless versioned_formula?
name.sub(/@[\d.]+(?=-full$|$)/, "")
end
sig { params(glob: String).returns(T::Array[String]) }
def formula_names_for_glob(glob)
@formula_names_for_glob ||= T.let({}, T.nilable(T::Hash[String, T::Array[String]]))
@formula_names_for_glob[glob] ||= if (formula_tap = tap)
formula_name = File.basename(glob, ".rb")
if formula_tap.formula_files_by_name.key?(formula_name)
[formula_name]
else
[]
end
elsif path.exist?
Pathname.glob((path.dirname/glob).to_s)
.map { |path| path.basename(".rb").to_s }
.sort
else
raise "Either tap or path is required to list sibling formulae"
end
end
private :formula_names_for_glob
# Returns the sibling `-full` or non-`-full` formula names for any Formula.
sig { returns(T::Array[String]) }
def full_formulae_names
sibling_name = if name.end_with?("-full")
name.delete_suffix("-full")
else
"#{name}-full"
end
formula_names_for_glob("#{sibling_name}.rb")
end
# Returns sibling `-full` or non-`-full` Formula objects for any Formula.
sig { returns(T::Array[Formula]) }
def full_formulae
full_formulae_names.filter_map do |formula_name|
Formula[formula_name]
rescue FormulaUnavailableError
nil
end.sort_by(&:version).reverse
end
sig { returns(T.nilable(String)) }
def link_overwrite_reason
installed_overwrite_formulae = link_overwrite_formulae.select(&:any_version_installed?)
return if installed_overwrite_formulae.empty?
reason_formulae = installed_overwrite_formulae.select(&:linked?)
status = if reason_formulae.empty?
reason_formulae = installed_overwrite_formulae
"installed"
else
"linked"
end
"#{reason_formulae.map(&:full_name).to_sentence} #{reason_formulae.one? ? "is" : "are"} already #{status}"
end
sig { returns(T::Array[String]) }
def link_overwrite_related_formula_names
[*versioned_formulae_names, *full_formulae_names, unversioned_formula_name].compact
end
# Returns sibling Formula names whose prefix links should be replaced when this Formula is linked.
sig { returns(T::Array[String]) }
def link_overwrite_formulae_names
formula_names = T.let(Set.new, T::Set[String])
pending_formula_names = T.let([name], T::Array[String])
pending_formula_names.each do |current_name|
current_formula = begin
if current_name == name
self
else
Formula[current_name]
end
rescue FormulaUnavailableError
next
end
current_formula.link_overwrite_related_formula_names.each do |related_formula_name|
next if related_formula_name == name
next unless formula_names.add?(related_formula_name)
pending_formula_names << related_formula_name
end
end
formula_names.to_a.sort
end
# Returns sibling Formulae whose prefix links should be replaced when this Formula is linked.
sig { returns(T::Array[Formula]) }
def link_overwrite_formulae
link_overwrite_formulae_names.filter_map do |formula_name|
Formula[formula_name]
rescue FormulaUnavailableError
nil
end
end
sig { params(path: Pathname).returns(T.nilable(T.any(String, Symbol))) }
def link_overwrite_keg_name(path)
# Don't overwrite files not created by Homebrew.
return if path.stat.uid != HOMEBREW_ORIGINAL_BREW_FILE.stat.uid
keg = Keg.for(path)
# This keg doesn't belong to any current core/tap formula, most likely coming from a DIY install.
return if keg.tab.tap.nil?
keg.name
rescue NotAKegError, Errno::ENOENT
# File doesn't belong to any keg.
:missing
end
sig {
params(keg_name: T.nilable(T.any(String, Symbol)), overwrite_formulae: T::Array[Formula]).returns(T::Boolean)
}
def implied_link_overwrite?(keg_name, overwrite_formulae)
return false if overwrite_formulae.empty?
return false if keg_name.nil?
case keg_name
when :missing
# File doesn't belong to any keg, so implied overwrites do not apply.
false
else
overwrite_formulae.any? do |formula|
formula.possible_names.include?(keg_name)
end
end
end
# Whether this {Formula} is version-synced with other formulae.
sig { returns(T::Boolean) }
def synced_with_other_formulae?
return false if @tap.nil?
@tap.synced_versions_formulae.any? { |synced_formulae| synced_formulae.include?(name) }
end
# A named {Resource} for the currently active {SoftwareSpec}.
# Additional downloads can be defined as {#resource}s.
# {Resource#stage} will create a temporary directory and yield to a block.
#
# ### Example
#
# ```ruby
# resource("additional_files").stage { bin.install "my/extra/tool" }
# ```
#
# FIXME: This should not actually take a block. All resources should be defined
# at the top-level using {Formula.resource} instead
# (see https://github.com/Homebrew/brew/issues/17203#issuecomment-2093654431).
#
# @api public
sig {
params(name: String, klass: T.class_of(Resource), block: T.nilable(T.proc.bind(Resource).void))
.returns(T.nilable(Resource))
}
def resource(name = T.unsafe(nil), klass = T.unsafe(nil), &block)
if klass.nil?
active_spec.resource(*name, &block)
else
active_spec.resource(name, klass, &block)
end
end
# Old names for the formula.
#
# @api internal
sig { returns(T::Array[String]) }
def oldnames
@oldnames ||= T.let(
if (tap = self.tap)
Tap.tap_migration_oldnames(tap, name) + tap.formula_reverse_renames.fetch(name, [])
else
[]
end, T.nilable(T::Array[String])
)
end
# All aliases for the formula.
#
# @api internal
sig { returns(T::Array[String]) }
def aliases
@aliases ||= T.let(
if (tap = self.tap)
tap.alias_reverse_table.fetch(full_name, []).map { it.split("/").fetch(-1) }
else
[]
end, T.nilable(T::Array[String])
)
end
# The {Resource}s for the currently active {SoftwareSpec}.
# @!method resources
def_delegator :"active_spec.resources", :values, :resources
# The {Dependency}s for the currently active {SoftwareSpec}.
#
# @api internal
delegate deps: :active_spec
# The declared {Dependency}s for the currently active {SoftwareSpec} (i.e. including those provided by macOS).
delegate declared_deps: :active_spec
# The {Requirement}s for the currently active {SoftwareSpec}.
delegate requirements: :active_spec
# The cached download for the currently active {SoftwareSpec}.
delegate cached_download: :active_spec
# Deletes the download for the currently active {SoftwareSpec}.
delegate clear_cache: :active_spec
# The list of patches for the currently active {SoftwareSpec}.
def_delegator :active_spec, :patches, :patchlist
# The options for the currently active {SoftwareSpec}.
delegate options: :active_spec
# The deprecated options for the currently active {SoftwareSpec}.
delegate deprecated_options: :active_spec
# The deprecated option flags for the currently active {SoftwareSpec}.
delegate deprecated_flags: :active_spec
# If a named option is defined for the currently active {SoftwareSpec}.
# @!method option_defined?
delegate option_defined?: :active_spec
# All the {.fails_with} for the currently active {SoftwareSpec}.
delegate compiler_failures: :active_spec
# If this {Formula} is installed.
# This is actually just a check for if the {#latest_installed_prefix} directory
# exists and is not empty.
sig { returns(T::Boolean) }
def latest_version_installed?
(dir = latest_installed_prefix).directory? && !dir.children.empty?
end
# If at least one version of {Formula} is installed.
#
# @api public
sig { returns(T::Boolean) }
def any_version_installed?
installed_prefixes.any? { |keg| (keg/AbstractTab::FILENAME).file? }
end
# The link status symlink directory for this {Formula}.
# You probably want {#opt_prefix} instead.
#
# @api internal
sig { returns(Pathname) }
def linked_keg
linked_keg = possible_names.map { |name| HOMEBREW_LINKED_KEGS/name }
.find(&:directory?)
return linked_keg if linked_keg.present?
HOMEBREW_LINKED_KEGS/name
end
sig { returns(T.nilable(PkgVersion)) }
def latest_head_version
head_versions = installed_prefixes.filter_map do |pn|
pn_pkgversion = PkgVersion.parse(pn.basename.to_s)
pn_pkgversion if pn_pkgversion.head?
end
head_versions.max_by do |pn_pkgversion|
[Keg.new(prefix(pn_pkgversion)).tab.source_modified_time, pn_pkgversion.revision]
end
end
sig { returns(T.nilable(Pathname)) }
def latest_head_prefix
head_version = latest_head_version
prefix(head_version) if head_version
end
sig { params(version: PkgVersion, fetch_head: T::Boolean).returns(T::Boolean) }
def head_version_outdated?(version, fetch_head: false)
tab = Tab.for_keg(prefix(version))
return true if tab.version_scheme < version_scheme
tab_stable_version = tab.stable_version
return true if stable && tab_stable_version && tab_stable_version < T.must(stable).version
return false unless fetch_head
return false unless head&.downloader.is_a?(VCSDownloadStrategy)
downloader = T.must(head).downloader
with_context quiet: true do
downloader.commit_outdated?(version.version.commit)
end
end
sig { params(fetch_head: T::Boolean).returns(PkgVersion) }
def latest_head_pkg_version(fetch_head: false)
return pkg_version unless (latest_version = latest_head_version)
return latest_version unless head_version_outdated?(latest_version, fetch_head:)
downloader = T.must(head).downloader
with_context quiet: true do
PkgVersion.new(Version.new("HEAD-#{downloader.last_commit}"), revision)
end
end
# The latest prefix for this formula. Checks for {#head} and then {#stable}'s {#prefix}.
sig { returns(Pathname) }
def latest_installed_prefix
if head && (head_version = latest_head_version) && !head_version_outdated?(head_version)
T.must(latest_head_prefix)
elsif stable && (stable_prefix = prefix(PkgVersion.new(T.must(stable).version, revision))).directory?
stable_prefix
else
prefix
end
end
# The directory in the Cellar that the formula is installed to.
# This directory points to {#opt_prefix} if it exists and if {#prefix} is not
# called from within the same formula's {#install} or {#post_install} methods.
# Otherwise, return the full path to the formula's keg (versioned Cellar path).
#
# @api public
sig { params(version: T.any(String, PkgVersion)).returns(Pathname) }
def prefix(version = pkg_version)
versioned_prefix = versioned_prefix(version)
version = PkgVersion.parse(version) if version.is_a?(String)
if !@prefix_returns_versioned_prefix && version == pkg_version &&
versioned_prefix.directory? && Keg.new(versioned_prefix).optlinked?