Module: Readall Private

Extended by:
Cachable, T::Generic, Utils::Output::Mixin
Defined in:
readall.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.

Helper module for validating syntax in taps.

Constant Summary collapse

Cache =

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

type_template { { fixed: T::Hash[Symbol, T.untyped] } }

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Cachable

cache, clear_cache

Methods included from Utils::Output::Mixin

issue_reporting_message, odebug, odeprecated, odie, odisabled, ofail, oh1, oh1_title, ohai, ohai_title, onoe, opoo, opoo_outside_github_actions, opoo_without_github_actions_annotation, pretty_deprecated, pretty_disabled, pretty_duration, pretty_install_status, pretty_installed, pretty_uninstalled, pretty_unmarked, pretty_upgradable, pretty_warning

Class Attribute Details

.warning_bufferArray<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.

Returns:



38
39
40
# File 'readall.rb', line 38

def warning_buffer
  @warning_buffer
end

Class Method Details

.valid_aliases?(alias_dir, formula_dir) ⇒ Boolean

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:

  • (Boolean)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'readall.rb', line 54

def self.valid_aliases?(alias_dir, formula_dir)
  return true unless alias_dir.directory?

  formula_basenames = Set.new(formula_dir.glob("**/*.rb").map { |formula_file| formula_file.basename.to_s })

  failed = T.let(false, T::Boolean)
  alias_dir.each_child do |f|
    if !f.symlink?
      onoe "Non-symlink alias: #{f}"
      failed = true
    elsif !f.file?
      onoe "Non-file alias: #{f}"
      failed = true
    end

    if formula_basenames.include?("#{f.basename}.rb")
      onoe "Formula duplicating alias: #{f}"
      failed = true
    end
  end
  !failed
end

.valid_casks?(tap, os_name: nil, arch: nil, files: nil) ⇒ Boolean

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:

  • (Boolean)


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
# File 'readall.rb', line 119

def self.valid_casks?(tap, os_name: nil, arch: nil, files: nil)
  validating_linux = if os_name.nil?
    Homebrew::SimulateSystem.current_os == :linux
  else
    os_name == :linux
  end
  return true unless validating_linux

  os_and_arch = "Linux"
  os_and_arch += " on #{(arch == :intel) ? "Intel x86_64" : "ARM64"}" if arch

  success = T.let(true, T::Boolean)
  (files || tap.cask_files).each do |file|
    cask = if arch
      Homebrew::SimulateSystem.with(os: :macos, arch:) do
        loaded_cask = Cask::CaskLoader.load(file)
        loaded_cask if loaded_cask.supports_linux?
      end
    else
      Homebrew::SimulateSystem.with(os: :macos) do
        loaded_cask = Cask::CaskLoader.load(file)
        loaded_cask if loaded_cask.supports_linux?
      end
    end
    next unless cask

    check_linux_sha256 = lambda do
      cask.refresh
      arch_types = cask.depends_on.arch&.map { |cask_arch| cask_arch[:type] }
      # `depends_on arch:` excludes this architecture, so no Linux
      # checksum is expected for it.
      next true if arch_types&.exclude?(Homebrew::SimulateSystem.current_arch)

      !cask.sha256.nil?
    end
    linux_sha256_valid = if arch
      Homebrew::SimulateSystem.with(os: :linux, arch:, &check_linux_sha256)
    else
      Homebrew::SimulateSystem.with(os: :linux, &check_linux_sha256)
    end
    # No `sha256` matched Linux, so the cask cannot be downloaded there
    # despite not being marked macOS-only.
    next if linux_sha256_valid

    onoe "Invalid cask (#{os_and_arch}): #{file}"
    $stderr.puts "Missing Linux stanzas can leave Linux `sha256` as nil. " \
                 "Add `depends_on :macos` if this cask is macOS-only or " \
                 "`depends_on arch:` if it does not support this architecture."
    success = false
  rescue Interrupt
    raise
  # Handle all possible exceptions reading casks.
  rescue Exception => e # rubocop:disable Lint/RescueException
    onoe "Invalid cask (#{os_and_arch}): #{file}"
    $stderr.puts e
    success = false
  end
  success
end

.valid_formulae?(tap, bottle_tag: nil, files: nil) ⇒ Boolean

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:

  • (Boolean)


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
# File 'readall.rb', line 82

def self.valid_formulae?(tap, bottle_tag: nil, files: nil)
  cache[:valid_formulae] ||= {}

  success = T.let(true, T::Boolean)
  (files || tap.formula_files).each do |file|
    valid = cache[:valid_formulae][file]
    next if valid == true || valid&.include?(bottle_tag)

    formula_name = file.basename(".rb").to_s
    formula_contents = file.read.force_encoding("UTF-8")

    readall_namespace = "ReadallNamespace"
    readall_formula_class = Formulary.load_formula(formula_name, file, formula_contents, readall_namespace,
                                                   flags: [], ignore_errors: false)
    readall_formula = readall_formula_class.new(formula_name, file, :stable, tap:)
    readall_formula.to_hash
    cache[:valid_formulae][file] = if readall_formula.on_system_blocks_exist?
      [bottle_tag, *cache[:valid_formulae][file]]
    else
      true
    end
  rescue Interrupt
    raise
  # Handle all possible exceptions reading formulae.
  rescue Exception => e # rubocop:disable Lint/RescueException
    onoe "Invalid formula (#{bottle_tag}): #{file}"
    $stderr.puts e
    success = false
  end
  success
end

.valid_ruby_syntax?(ruby_files) ⇒ Boolean

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:

  • (Boolean)


42
43
44
45
46
47
48
49
50
51
# File 'readall.rb', line 42

def self.valid_ruby_syntax?(ruby_files)
  parallel_slices_valid?(ruby_files) do |files|
    failed = T.let(false, T::Boolean)
    files.each do |ruby_file|
      # As a side effect, print syntax errors/warnings to `$stderr`.
      failed = true if syntax_errors_or_warnings?(ruby_file)
    end
    !failed
  end
end

.valid_tap?(tap, aliases: false, no_simulate: false, os_arch_combinations: OnSystem::ALL_OS_ARCH_COMBINATIONS) ⇒ Boolean

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:

  • tap (Tap)
  • aliases (Boolean) (defaults to: false)
  • no_simulate (Boolean) (defaults to: false)
  • os_arch_combinations (Array<Array<(Symbol, Symbol)>>) (defaults to: OnSystem::ALL_OS_ARCH_COMBINATIONS)

Returns:

  • (Boolean)


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
# File 'readall.rb', line 184

def self.valid_tap?(tap, aliases: false, no_simulate: false,
                    os_arch_combinations: OnSystem::ALL_OS_ARCH_COMBINATIONS)
  success = true

  if aliases
    valid_aliases = valid_aliases?(tap.alias_dir, tap.formula_dir)
    success = false unless valid_aliases
  end

  items = tap.formula_files.map { |file| [:formula, file] } +
          tap.cask_files.map { |file| [:cask, file] }

  all_files_valid = parallel_slices_valid?(items) do |slice|
    formula_files = slice.filter_map { |type, file| file if type == :formula }
    cask_files = slice.filter_map { |type, file| file if type == :cask }

    slice_success = T.let(true, T::Boolean)
    if no_simulate
      slice_success = false unless valid_formulae?(tap, files: formula_files)
      slice_success = false unless valid_casks?(tap, files: cask_files)
    else
      os_arch_combinations.each do |os, arch|
        bottle_tag = Utils::Bottles::Tag.new(system: os, arch:)
        next unless bottle_tag.valid_combination?

        Homebrew::SimulateSystem.with(os:, arch:) do
          slice_success = false unless valid_formulae?(tap, bottle_tag:, files: formula_files)
          slice_success = false unless valid_casks?(tap, os_name: os, arch:, files: cask_files)
        end
      end
    end
    slice_success
  end
  success = false unless all_files_valid

  success
end