Class: Reporter

Inherits:
Object
  • Object
show all
Defined in:
Library/Homebrew/cmd/update-report.rb

Defined Under Namespace

Classes: ReporterRevisionUnsetError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tap) ⇒ Reporter

Returns a new instance of Reporter



328
329
330
331
332
333
334
335
336
337
338
# File 'Library/Homebrew/cmd/update-report.rb', line 328

def initialize(tap)
  @tap = tap

  initial_revision_var = "HOMEBREW_UPDATE_BEFORE#{repo_var}"
  @initial_revision = ENV[initial_revision_var].to_s
  raise ReporterRevisionUnsetError, initial_revision_var if @initial_revision.empty?

  current_revision_var = "HOMEBREW_UPDATE_AFTER#{repo_var}"
  @current_revision = ENV[current_revision_var].to_s
  raise ReporterRevisionUnsetError, current_revision_var if @current_revision.empty?
end

Instance Attribute Details

#current_revisionObject (readonly)

Returns the value of attribute current_revision



326
327
328
# File 'Library/Homebrew/cmd/update-report.rb', line 326

def current_revision
  @current_revision
end

#initial_revisionObject (readonly)

Returns the value of attribute initial_revision



326
327
328
# File 'Library/Homebrew/cmd/update-report.rb', line 326

def initial_revision
  @initial_revision
end

#tapObject (readonly)

Returns the value of attribute tap



326
327
328
# File 'Library/Homebrew/cmd/update-report.rb', line 326

def tap
  @tap
end

Instance Method Details

#migrate_formula_renameObject



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
# File 'Library/Homebrew/cmd/update-report.rb', line 505

def migrate_formula_rename
  Formula.installed.each do |formula|
    next unless Migrator.needs_migration?(formula)

    oldname = formula.oldname
    oldname_rack = HOMEBREW_CELLAR/oldname

    if oldname_rack.subdirs.empty?
      oldname_rack.rmdir_if_possible
      next
    end

    new_name = tap.formula_renames[oldname]
    next unless new_name

    new_full_name = "#{tap}/#{new_name}"

    begin
      f = Formulary.factory(new_full_name)
    rescue Exception => e # rubocop:disable Lint/RescueException
      onoe "#{e.message}\n#{e.backtrace.join "\n"}" if ARGV.homebrew_developer?
      next
    end

    Migrator.migrate_if_needed(f)
  end
end

#migrate_tap_migrationObject



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
# File 'Library/Homebrew/cmd/update-report.rb', line 431

def migrate_tap_migration
  (report[:D] + report[:DC]).each do |full_name|
    name = full_name.split("/").last
    new_tap_name = tap.tap_migrations[name]
    next if new_tap_name.nil? # skip if not in tap_migrations list.

    new_tap_user, new_tap_repo, new_tap_new_name = new_tap_name.split("/")
    new_name = if new_tap_new_name
      new_full_name = new_tap_new_name
      new_tap_name = "#{new_tap_user}/#{new_tap_repo}"
      new_tap_new_name
    else
      new_full_name = "#{new_tap_name}/#{name}"
      name
    end

    # This means it is a Cask
    if report[:DC].include? full_name
      next unless (HOMEBREW_PREFIX/"Caskroom"/new_name).exist?
      new_tap = Tap.fetch(new_tap_name)
      new_tap.install unless new_tap.installed?
      ohai "#{name} has been moved to Homebrew.", <<~EOS
        To uninstall the cask run:
          brew cask uninstall --force #{name}
      EOS
      next if (HOMEBREW_CELLAR/new_name.split("/").last).directory?
      ohai "Installing #{new_name}..."
      system HOMEBREW_BREW_FILE, "install", new_full_name
      begin
        unless Formulary.factory(new_full_name).keg_only?
          system HOMEBREW_BREW_FILE, "link", new_full_name, "--overwrite"
        end
      rescue Exception => e # rubocop:disable Lint/RescueException
        onoe "#{e.message}\n#{e.backtrace.join "\n"}" if ARGV.homebrew_developer?
      end
      next
    end

    next unless (dir = HOMEBREW_CELLAR/name).exist? # skip if formula is not installed.
    tabs = dir.subdirs.map { |d| Tab.for_keg(Keg.new(d)) }
    next unless tabs.first.tap == tap # skip if installed formula is not from this tap.
    new_tap = Tap.fetch(new_tap_name)
    # For formulae migrated to cask: Auto-install cask or provide install instructions.
    if new_tap_name == "caskroom/cask"
      if new_tap.installed? && (HOMEBREW_PREFIX/"Caskroom").directory?
        ohai "#{name} has been moved to Homebrew-Cask."
        ohai "brew unlink #{name}"
        system HOMEBREW_BREW_FILE, "unlink", name
        ohai "brew prune"
        system HOMEBREW_BREW_FILE, "prune"
        ohai "brew cask install #{new_name}"
        system HOMEBREW_BREW_FILE, "cask", "install", new_name
        ohai <<~EOS
          #{name} has been moved to Homebrew-Cask.
          The existing keg has been unlinked.
          Please uninstall the formula when convenient by running:
            brew uninstall --force #{name}
        EOS
      else
        ohai "#{name} has been moved to Homebrew-Cask.", <<~EOS
          To uninstall the formula and install the cask run:
            brew uninstall --force #{name}
            brew cask install #{new_name}
        EOS
      end
    else
      new_tap.install unless new_tap.installed?
      # update tap for each Tab
      tabs.each { |tab| tab.tap = new_tap }
      tabs.each(&:write)
    end
  end
end

#reportObject



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
# File 'Library/Homebrew/cmd/update-report.rb', line 340

def report
  return @report if @report

  @report = Hash.new { |h, k| h[k] = [] }
  return @report unless updated?

  diff.each_line do |line|
    status, *paths = line.split
    src = Pathname.new paths.first
    dst = Pathname.new paths.last

    next unless dst.extname == ".rb"

    if paths.any? { |p| tap.cask_file?(p) }
      # Currently only need to handle Cask deletion/migration.
      if status == "D"
        # Have a dedicated report array for deleted casks.
        @report[:DC] << tap.formula_file_to_name(src)
      end
    end

    next unless paths.any? { |p| tap.formula_file?(p) }

    case status
    when "A", "D"
      full_name = tap.formula_file_to_name(src)
      name = full_name.split("/").last
      new_tap = tap.tap_migrations[name]
      @report[status.to_sym] << full_name unless new_tap
    when "M"
      begin
        formula = Formulary.factory(tap.path/src)
        new_version = formula.pkg_version
        old_version = FormulaVersions.new(formula).formula_at_revision(@initial_revision, &:pkg_version)
        next if new_version == old_version
      rescue Exception => e # rubocop:disable Lint/RescueException
        onoe "#{e.message}\n#{e.backtrace.join "\n"}" if ARGV.homebrew_developer?
      end
      @report[:M] << tap.formula_file_to_name(src)
    when /^R\d{0,3}/
      src_full_name = tap.formula_file_to_name(src)
      dst_full_name = tap.formula_file_to_name(dst)
      # Don't report formulae that are moved within a tap but not renamed
      next if src_full_name == dst_full_name
      @report[:D] << src_full_name
      @report[:A] << dst_full_name
    end
  end

  renamed_formulae = Set.new
  @report[:D].each do |old_full_name|
    old_name = old_full_name.split("/").last
    new_name = tap.formula_renames[old_name]
    next unless new_name

    if tap.core_tap?
      new_full_name = new_name
    else
      new_full_name = "#{tap}/#{new_name}"
    end

    renamed_formulae << [old_full_name, new_full_name] if @report[:A].include? new_full_name
  end

  @report[:A].each do |new_full_name|
    new_name = new_full_name.split("/").last
    old_name = tap.formula_renames.key(new_name)
    next unless old_name

    if tap.core_tap?
      old_full_name = old_name
    else
      old_full_name = "#{tap}/#{old_name}"
    end

    renamed_formulae << [old_full_name, new_full_name]
  end

  unless renamed_formulae.empty?
    @report[:A] -= renamed_formulae.map(&:last)
    @report[:D] -= renamed_formulae.map(&:first)
    @report[:R] = renamed_formulae.to_a
  end

  @report
end

#updated?Boolean

Returns:

  • (Boolean)


427
428
429
# File 'Library/Homebrew/cmd/update-report.rb', line 427

def updated?
  initial_revision != current_revision
end