Class: Cask::Migrator Private

Inherits:
Object show all
Extended by:
Utils::Output::Mixin
Includes:
Utils::Output::Mixin
Defined in:
cask/migrator.rb

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_cannot_install, pretty_deprecated, pretty_disabled, pretty_duration, pretty_install_status, pretty_installed, pretty_uninstalled, pretty_unmarked, pretty_upgradable, pretty_warning

Constructor Details

#initialize(old_cask, new_cask) ⇒ 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.

Parameters:

Raises:



17
18
19
20
21
22
# File 'cask/migrator.rb', line 17

def initialize(old_cask, new_cask)
  raise CaskNotInstalledError, new_cask unless new_cask.installed?

  @old_cask = old_cask
  @new_cask = new_cask
end

Instance Attribute Details

#new_caskCask (readonly)

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:



14
15
16
# File 'cask/migrator.rb', line 14

def new_cask
  @new_cask
end

#old_caskCask (readonly)

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:



14
15
16
# File 'cask/migrator.rb', line 14

def old_cask
  @old_cask
end

Class Method Details

.migrate_if_needed(new_cask, dry_run: false) ⇒ 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.

Parameters:

  • new_cask (Cask)
  • dry_run (Boolean) (defaults to: false)


47
48
49
50
51
52
53
# File 'cask/migrator.rb', line 47

def self.migrate_if_needed(new_cask, dry_run: false)
  old_tokens_needing_migration(new_cask).each do |old_token|
    new(Cask.new(old_token), new_cask).migrate(dry_run:)
  rescue => e
    onoe e
  end
end

.old_tokens_needing_migration(new_cask, dry_run: false) ⇒ Array<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.

The old tokens of new_cask that are still installed in their own Caskroom directory. A symlinked directory means the cask has already been migrated.

Parameters:

  • new_cask (Cask)
  • dry_run (Boolean) (defaults to: false)

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'cask/migrator.rb', line 27

def self.old_tokens_needing_migration(new_cask, dry_run: false)
  new_cask.old_tokens
          .map { |old_token| Caskroom.token_from_full_token(old_token) }
          .uniq
          .select do |old_token|
    next false if old_token == new_cask.token

    old_caskroom_path = Caskroom.path/old_token
    next false if old_caskroom_path.symlink? || !old_caskroom_path.directory?

    if Caskroom.cask_installed_caskfile(old_token).nil?
      ::Utils::Path.rmdir_if_possible(old_caskroom_path) unless dry_run
      next false
    end

    true
  end
end

.replace_caskfile_token(path, old_token, new_token) ⇒ 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.

Parameters:



69
70
71
72
73
74
75
76
77
78
# File 'cask/migrator.rb', line 69

def self.replace_caskfile_token(path, old_token, new_token)
  case path.extname
  when ".rb"
    ::Utils::Inreplace.inreplace path, /\A\s*cask\s+"#{Regexp.escape(old_token)}"/, "cask #{new_token.inspect}"
  when ".json"
    json = JSON.parse(path.read)
    json["token"] = new_token
    path.atomic_write json.to_json
  end
end

Instance Method Details

#migrate(dry_run: false) ⇒ 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.

Parameters:

  • dry_run (Boolean) (defaults to: false)


56
57
58
59
60
61
62
63
64
65
66
# File 'cask/migrator.rb', line 56

def migrate(dry_run: false)
  old_caskfile = old_cask.installed_caskfile
  return if old_caskfile.nil?

  new_caskroom_path = new_cask.caskroom_path
  if new_caskroom_path.directory? && !new_caskroom_path.symlink?
    uninstall_old_cask(old_caskfile, dry_run:)
  else
    move_old_cask(old_caskfile, dry_run:)
  end
end