Module: Homebrew::Bundle::Trust Private

Defined in:
bundle/trust.rb

Overview

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Converts Brewfile trusted options into trust-store entries.

Class Method Summary collapse

Class Method Details

.entries(entries) ⇒ Array<Array<(Symbol, String)>>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



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
# File 'bundle/trust.rb', line 20

def self.entries(entries)
  # Resolve every item through `Homebrew::Trust.target`, the same canonicalizer `brew trust`
  # uses, so bundle does not write a second, divergent entry for a custom-remote tap. A
  # `brew`/`cask` entry takes its remote from the matching `tap` entry, which can appear later
  # in the Brewfile, so map each tap name to its declared remote first.
  tap_remotes = entries.filter_map do |entry|
    next if entry.type != :tap

    clone_target = entry.options[:clone_target].presence
    [entry.name.downcase, clone_target.to_s] if clone_target
  end.to_h

  entries.flat_map do |entry|
    trusted = entry.options[:trusted]
    next [] if trusted.blank?

    targets = T.let([], T::Array[[Symbol, String, T.nilable(String)]])
    case entry.type
    when :tap
      tap_remote = entry.options[:clone_target].presence&.to_s
      if trusted == true
        targets << [:tap, entry.name, tap_remote]
      elsif trusted.is_a?(Hash)
        unsupported_keys = trusted.keys - TRUSTED_ITEM_KEYS.values.flatten
        if unsupported_keys.present?
          raise UsageError,
                "Unsupported trusted keys: #{unsupported_keys.join(", ")}"
        end

        TRUSTED_ITEM_KEYS.each do |type, keys|
          keys.each do |key|
            Array(trusted[key]).each do |item|
              item_name = case item
              when String, Symbol, Integer
                Utils.name_from_full_name(item.to_s)
              end
              next if item_name.blank?

              targets << [type, "#{entry.name}/#{item_name}", tap_remote]
            end
          end
        end
      end
    when :brew, :cask
      full_name = T.cast(entry.options.fetch(:full_name, entry.name), String)
      next [] if trusted != true
      # Only fully-qualified names map to a tap, so unqualified names cannot be trusted.
      next [] unless Utils.full_name?(full_name)

      type = (entry.type == :brew) ? :formula : :cask
      tap_name = Utils.tap_from_full_name(full_name)
      canonical_tap_name = Dsl.sanitize_tap_name(tap_name) if tap_name
      tap_remote = tap_remotes[canonical_tap_name] if canonical_tap_name
      targets << [type, full_name, tap_remote]
    end

    targets.map { |type, name, tap_remote| Homebrew::Trust.target(name, type:, tap_remote:) }
  end.uniq
end