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, Spinner

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?

Constructor Details

This class inherits a constructor from Homebrew::AbstractCommand

Instance Method Details

#argsHomebrew::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

#concurrencyObject

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.



72
73
74
# File 'cmd/fetch.rb', line 72

def concurrency
  @concurrency ||= args.concurrency&.to_i || 1
end

#download_queueObject

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.



76
77
78
79
80
81
# File 'cmd/fetch.rb', line 76

def download_queue
  @download_queue ||= begin
    require "download_queue"
    DownloadQueue.new(concurrency)
  end
end

#runvoid

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.



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

def run
  Formulary.enable_factory_cache!

  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
  else
    args.named.to_formulae_and_casks
  end.uniq

  os_arch_combinations = args.os_arch_combinations

  puts "Fetching: #{bucket * ", "}" if bucket.size > 1
  bucket.each do |formula_or_cask|
    case formula_or_cask
    when Formula
      formula = T.cast(formula_or_cask, Formula)
      ref = formula.loaded_from_api? ? formula.full_name : formula.path

      os_arch_combinations.each do |os, arch|
        SimulateSystem.with(os:, arch:) do
          formula = Formulary.factory(ref, args.HEAD? ? :head : :stable)

          formula.print_tap_action verb: "Fetching"

          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 = if (bottle_tag = args.bottle_tag&.to_sym)
                Utils::Bottles::Tag.from_symbol(bottle_tag)
              else
                Utils::Bottles::Tag.new(system: os, arch:)
              end

              bottle = formula.bottle_for_tag(bottle_tag)

              if bottle.nil?
                opoo "Bottle for tag #{bottle_tag.to_sym.inspect} is unavailable."
                next
              end

              if (manifest_resource = bottle.github_packages_manifest_resource)
                fetch_downloadable(manifest_resource)
              end
              fetch_downloadable(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

          fetch_downloadable(formula.resource)

          formula.resources.each do |r|
            fetch_downloadable(r)
            r.patches.each { |patch| fetch_downloadable(patch.resource) if patch.external? }
          end

          formula.patchlist.each { |patch| fetch_downloadable(patch.resource) if patch.external? }
        end
      end
    else
      cask = formula_or_cask
      ref = cask.loaded_from_api? ? cask.full_name : cask.sourcefile_path

      os_arch_combinations.each do |os, arch|
        next if os == :linux

        SimulateSystem.with(os:, arch:) do
          cask = Cask::CaskLoader.load(ref)

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

          quarantine = args.quarantine?
          quarantine = true if quarantine.nil?

          download = Cask::Download.new(cask, quarantine:)
          fetch_downloadable(download)
        end
      end
    end
  end

  if concurrency == 1
    downloads.each do |downloadable, promise|
      promise.wait!
    rescue ChecksumMismatchError => e
      opoo "#{downloadable.download_type.capitalize} reports different checksum: #{e.expected}"
      Homebrew.failed = true if downloadable.is_a?(Resource::Patch)
    end
  else
    spinner = Spinner.new
    remaining_downloads = downloads.dup
    previous_pending_line_count = 0

    begin
      $stdout.print Tty.hide_cursor
      $stdout.flush

      output_message = lambda do |downloadable, future|
        status = case future.state
        when :fulfilled
          "#{Tty.green}✔︎#{Tty.reset}"
        when :rejected
          "#{Tty.red}#{Tty.reset}"
        when :pending, :processing
          "#{Tty.blue}#{spinner}#{Tty.reset}"
        else
          raise future.state.to_s
        end

        message = "#{downloadable.download_type.capitalize} #{downloadable.name}"
        $stdout.puts "#{status} #{message}"
        $stdout.flush

        if future.rejected? && (e = future.reason).is_a?(ChecksumMismatchError)
          opoo "#{downloadable.download_type.capitalize} reports different checksum: #{e.expected}"
          Homebrew.failed = true if downloadable.is_a?(Resource::Patch)
          next 2
        end

        1
      end

      until remaining_downloads.empty?
        begin
          finished_states = [:fulfilled, :rejected]

          finished_downloads, remaining_downloads = remaining_downloads.partition do |_, future|
            finished_states.include?(future.state)
          end

          finished_downloads.each do |downloadable, future|
            previous_pending_line_count -= 1
            $stdout.print Tty.clear_to_end
            $stdout.flush
            output_message.call(downloadable, future)
          end

          previous_pending_line_count = 0
          remaining_downloads.each do |downloadable, future|
            # FIXME: Allow printing full terminal height.
            break if previous_pending_line_count >= [concurrency, (Tty.height - 1)].min

            $stdout.print Tty.clear_to_end
            $stdout.flush
            previous_pending_line_count += output_message.call(downloadable, future)
          end

          if previous_pending_line_count.positive?
            $stdout.print Tty.move_cursor_up_beginning(previous_pending_line_count)
            $stdout.flush
          end

          sleep 0.05
        rescue Interrupt
          remaining_downloads.each do |_, future|
            # FIXME: Implement cancellation of running downloads.
          end

          download_queue.cancel

          if previous_pending_line_count.positive?
            $stdout.print Tty.move_cursor_down(previous_pending_line_count - 1)
            $stdout.flush
          end

          raise
        end
      end
    ensure
      $stdout.print Tty.show_cursor
      $stdout.flush
    end
  end
ensure
  download_queue.shutdown
end