Class: Reporter

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

Defined Under Namespace

Classes: ReporterRevisionUnsetError

Instance Method Summary collapse

Constructor Details

#initialize(tap, api_names_txt: nil, api_names_before_txt: nil, api_dir_prefix: nil) ⇒ Reporter

Returns a new instance of Reporter.



423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'cmd/update-report.rb', line 423

def initialize(tap, api_names_txt: nil, api_names_before_txt: nil, api_dir_prefix: nil)
  @tap = tap

  # This is slightly involved/weird but all the #report logic is shared so it's worth it.
  if installed_from_api?(api_names_txt, api_names_before_txt, api_dir_prefix)
    @api_names_txt = api_names_txt
    @api_names_before_txt = api_names_before_txt
    @api_dir_prefix = api_dir_prefix
  else
    initial_revision_var = "HOMEBREW_UPDATE_BEFORE#{tap.repo_var_suffix}"
    @initial_revision = ENV[initial_revision_var].to_s
    raise ReporterRevisionUnsetError, initial_revision_var if @initial_revision.empty?

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

Instance Method Details

#migrate_cask_renameObject



671
672
673
674
675
# File 'cmd/update-report.rb', line 671

def migrate_cask_rename
  Cask::Caskroom.casks.each do |cask|
    Cask::Migrator.migrate_if_needed(cask)
  end
end

#migrate_formula_rename(force:, verbose:) ⇒ Object



677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
# File 'cmd/update-report.rb', line 677

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

    oldnames_to_migrate = formula.oldnames.select do |oldname|
      oldname_rack = HOMEBREW_CELLAR/oldname
      next false unless oldname_rack.exist?

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

      true
    end
    next if oldnames_to_migrate.empty?

    Migrator.migrate_if_needed(formula, force:)
  end
end

#migrate_tap_migrationObject



592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
# File 'cmd/update-report.rb', line 592

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.ensure_installed!
      ohai "#{name} has been moved to Homebrew.", <<~EOS
        To uninstall the cask, run:
          brew uninstall --cask --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#{Utils::Backtrace.clean(e)&.join("\n")}" if Homebrew::EnvConfig.developer?
      end
      next
    end

    next unless (dir = HOMEBREW_CELLAR/name).exist? # skip if formula is not installed.

    tabs = dir.subdirs.map { |d| Keg.new(d).tab }
    next if 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.start_with?("homebrew/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 cleanup"
        system HOMEBREW_BREW_FILE, "cleanup"
        ohai "brew install --cask #{new_name}"
        system HOMEBREW_BREW_FILE, "install", "--cask", 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 tap #{new_tap_name}
            brew install --cask #{new_name}
        EOS
      end
    else
      new_tap.ensure_installed!
      # update tap for each Tab
      tabs.each { |tab| tab.tap = new_tap }
      tabs.each(&:write)
    end
  end
end

#report(auto_update: false) ⇒ Object



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
504
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'cmd/update-report.rb', line 442

def report(auto_update: false)
  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 if dst.extname != ".rb"

    if paths.any? { |p| tap.cask_file?(p) }
      case status
      when "A"
        # Have a dedicated report array for new casks.
        @report[:AC] << tap.formula_file_to_name(src)
      when "D"
        # Have a dedicated report array for deleted casks.
        @report[:DC] << tap.formula_file_to_name(src)
      when "M"
        # Report updated casks
        @report[:MC] << 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[:DC] << src_full_name
        @report[:AC] << dst_full_name
      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"
      name = tap.formula_file_to_name(src)

      @report[:M] << name
    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_casks = Set.new
  @report[:DC].each do |old_full_name|
    old_name = old_full_name.split("/").last
    new_name = tap.cask_renames[old_name]
    next unless new_name

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

    renamed_casks << [old_full_name, new_full_name] if @report[:AC].include?(new_full_name)
  end

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

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

    renamed_casks << [old_full_name, new_full_name]
  end

  if renamed_casks.any?
    @report[:AC] -= renamed_casks.map(&:last)
    @report[:DC] -= renamed_casks.map(&:first)
    @report[:RC] = renamed_casks.to_a
  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

    new_full_name = if tap.core_tap?
      new_name
    else
      "#{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

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

    renamed_formulae << [old_full_name, new_full_name]
  end

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

  # If any formulae/casks are marked as added and deleted, remove them from
  # the report as we've not detected things correctly.
  if (added_and_deleted_formulae = (@report[:A] & @report[:D]).presence)
    @report[:A] -= added_and_deleted_formulae
    @report[:D] -= added_and_deleted_formulae
  end
  if (added_and_deleted_casks = (@report[:AC] & @report[:DC]).presence)
    @report[:AC] -= added_and_deleted_casks
    @report[:DC] -= added_and_deleted_casks
  end

  @report
end

#updated?Boolean

Returns:

  • (Boolean)


584
585
586
587
588
589
590
# File 'cmd/update-report.rb', line 584

def updated?
  if installed_from_api?
    diff.present?
  else
    initial_revision != current_revision
  end
end