Class: Homebrew::Cmd::FetchCmd Private

Inherits:
AbstractCommand show all
Includes:
Fetch
Defined in:
cmd/fetch.rb,
sorbet/rbi/dsl/homebrew/cmd/fetch_cmd.rbi

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

Defined Under Namespace

Classes: Args

Constant Summary collapse

FETCH_MAX_TRIES =

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.

5

Instance Method Summary collapse

Methods included from Fetch

#fetch_bottle?

Methods inherited from AbstractCommand

command, command_name, dev_cmd?, #initialize, parser, ruby_cmd?

Methods included from Utils::Output::Mixin

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

Constructor Details

This class inherits a constructor from Homebrew::AbstractCommand

Instance Method Details

#args ⇒ Homebrew::Cmd::FetchCmd::Args

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.



10
# File 'sorbet/rbi/dsl/homebrew/cmd/fetch_cmd.rbi', line 10

def args; end

#cask_downloads(cask) ⇒ Array<Cask::Download>

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:



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
# File 'cmd/fetch.rb', line 200

def cask_downloads(cask)
  if args.all_platforms? && cask.loaded_from_api?
    opoo "Cask #{cask} was loaded from the API; cannot fetch all operating system and " \
         "architecture variants. Set `HOMEBREW_NO_INSTALL_FROM_API=1` to fetch them all."
  end

  # With `--all-platforms`, a cask without `on_system` blocks resolves
  # identically everywhere, so one combination covers the whole matrix.
  cask_combinations = args.os_arch_combinations

  if args.all_platforms? && !cask.on_system_blocks_exist?
    supports_all_platforms = cask_combinations.all? do |os, arch|
      tag = Utils::Bottles::Tag.from_arg(nil, os:, arch:)
      cask.platform_supported?(tag)
    end

    cask_combinations = cask_combinations.first(1) if supports_all_platforms
  end

  downloads = T.let([], T::Array[Cask::Download])
  enqueued_urls = Set.new

  cask_combinations.each do |os, arch|
    SimulateSystem.with(os:, arch:) do
      loaded_cask = begin
        cask.refresh_copy
      rescue Cask::CaskInvalidError, Cask::CaskUnreadableError
        raise unless cask.on_system_blocks_exist?
      end
      if loaded_cask.nil? || loaded_cask.depends_on.arch&.none? { |dep_arch| dep_arch[:type] == arch }
        opoo "Cask #{cask} is not supported on os #{os} and arch #{arch}"
        next
      end

      languages = (loaded_cask.languages if args.all_platforms?)
      languages = [nil] if languages.blank?

      languages.each do |language|
        localized_cask = loaded_cask
        if language
          # Copy per language: `Cask::Download` reads `sha256`/`url`
          # lazily, so each download needs its own cask instance.
          localized_cask = loaded_cask.refresh_copy(
            config: loaded_cask.config.merge(Cask::Config.new(explicit: { languages: [language] })),
          )
        end

        if localized_cask.url.nil? || localized_cask.sha256.nil?
          opoo "Cask #{cask} is not supported on os #{os} and arch #{arch}"
          next
        end

        next unless enqueued_urls.add?(localized_cask.url.to_s)

        downloads << Cask::Download.new(
          localized_cask,
          require_sha: Homebrew::EnvConfig.cask_opts_require_sha?,
        )
      end
    end
  end

  downloads
end

#run ⇒ void

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.

This method returns an undefined value.



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

def run
  Formulary.enable_factory_cache!
  os_arch_combinations = args.os_arch_combinations

  if enqueue_api_formula_bottles? || enqueue_api_cask_downloads?
    download_queue.fetch(verbose: args.verbose?)
    return
  end

  bucket = if args.deps?
    args.named.to_formulae_and_casks.flat_map do |formula_or_cask|
      case formula_or_cask
      when Formula
        formula = formula_or_cask
        [formula, *formula.recursive_dependencies.map(&:to_formula)]
      else
        formula_or_cask
      end
    end
  elsif args.test?
    args.named.to_formulae
  else
    args.named.to_formulae_and_casks
  end.uniq

  current_os_arch = [SimulateSystem.current_os, SimulateSystem.current_arch]
  fetch_hook_formulae = T.let([], T::Array[Formula])

  puts "Fetching: #{bucket.join(", ")}" if bucket.size > 1
  bucket.each do |formula_or_cask|
    case formula_or_cask
    when Formula
      formula = formula_or_cask
      ref = formula.reloadable_ref

      os_arch_combinations.each do |os, arch|
        SimulateSystem.with(os:, arch:) do
          spec = args.HEAD? ? :head : :stable
          formula = if ref.is_a?(Pathname)
            Formulary.from_path(ref, spec)
          else
            Formulary.factory(ref, spec)
          end

          formula.print_tap_action verb: "Fetching"

          if args.test?
            formula = Homebrew::API::Formula.source_download_formula(formula) if formula.loaded_from_api?
            formula.enqueue_resources_and_patches(download_queue:, test: true)
          end

          fetched_bottle = false
          if fetch_bottle?(
            formula,
            force_bottle:               args.force_bottle?,
            bottle_tag:                 args.bottle_tag&.to_sym,
            build_from_source_formulae: args.build_from_source_formulae,
            os:                         args.os&.to_sym,
            arch:                       args.arch&.to_sym,
          )
            begin
              formula.clear_cache if args.force?

              bottle_tag = Utils::Bottles::Tag.from_arg(args.bottle_tag&.to_sym, os:, arch:)

              bottle = formula.bottle_for_tag(bottle_tag)

              if bottle.nil?
                message = "The loaded metadata for #{formula.full_name} has no bottle for tag #{bottle_tag}."
                message += "\nAPI metadata covers only the host platform." if formula.loaded_from_internal_api?
                opoo message
                next
              end

              if (manifest_resource = bottle.github_packages_manifest_resource)
                download_queue.enqueue(manifest_resource)
              end
              download_queue.enqueue(bottle)
            rescue Interrupt
              raise
            rescue => e
              raise if Homebrew::EnvConfig.developer?

              fetched_bottle = false
              onoe e.message
              opoo "Bottle fetch failed, fetching the source instead."
            else
              fetched_bottle = true
            end
          end

          next if fetched_bottle

          formula = Homebrew::API::Formula.source_download_formula(formula) if formula.loaded_from_api?

          if (resource = formula.resource)
            download_queue.enqueue(resource)
          end

          formula.enqueue_resources_and_patches(download_queue:)
          fetch_hook_formulae << formula if [os, arch] == current_os_arch
        end
      end
    when Cask::Cask
      cask_downloads(formula_or_cask).each { |download| download_queue.enqueue(download) }
    else
      odie "Invalid formula or cask: #{formula_or_cask}"
    end
  end

  download_queue.fetch(verbose: args.verbose?)

  fetch_hook_formulae.each { |formula| run_fetch_hook(formula) }
ensure
  download_queue.shutdown
end

#run_fetch_hook(formula) ⇒ void

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.

This method returns an undefined value.

A formula's fetch runs in the build environment, so it needs the dependencies that build.rb would put on PATH to be installed.

Parameters:



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
# File 'cmd/fetch.rb', line 268

def run_fetch_hook(formula)
  formula = Homebrew::API::Formula.source_download_formula(formula) if formula.loaded_from_api?
  return unless formula.fetch_defined?

  missing_dependencies = formula.recursive_dependencies do |_dependent, dependency|
    next Dependable::PRUNE if dependency.implicit? || dependency.optional?
    next Dependable::PRUNE if dependency.test? && !dependency.build?
  end.map(&:to_formula).reject(&:any_version_installed?)
  if missing_dependencies.present?
    missing_names = missing_dependencies.map(&:full_name).join(", ")
    odebug <<~EOS
      Not running #{formula.full_name}'s `fetch` as these dependencies are not installed:
        #{missing_names}
      Install them first with:
        brew install --only-dependencies #{formula.full_name}
    EOS
    return
  end

  FormulaInstaller.new(
    formula,
    build_from_source_formulae: args.build_from_source_formulae,
    force_bottle:               args.force_bottle?,
    **{ debug: args.debug?, quiet: args.quiet?, verbose: args.verbose? }.compact,
  ).run_fetch
end