Class: Homebrew::Aliases::Alias Private

Inherits:
Object
  • Object
show all
Includes:
Utils::Output::Mixin
Defined in:
aliases/alias.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

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(name, command = nil) ⇒ 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:



22
23
24
25
26
27
28
29
30
31
32
33
# File 'aliases/alias.rb', line 22

def initialize(name, command = nil)
  @name = T.let(name.strip, String)
  @command = T.let(nil, T.nilable(String))
  @script = T.let(nil, T.nilable(Pathname))
  @symlink = T.let(nil, T.nilable(Pathname))

  @command = if command&.start_with?("!", "%")
    command[1..]
  elsif command
    "brew #{command}"
  end
end

Instance Attribute Details

#commandString?

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:



19
20
21
# File 'aliases/alias.rb', line 19

def command
  @command
end

#nameString

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:



16
17
18
# File 'aliases/alias.rb', line 16

def name
  @name
end

Instance Method Details

#cmd_exists?Boolean

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:

  • (Boolean)


41
42
43
44
# File 'aliases/alias.rb', line 41

def cmd_exists?
  path = which("brew-#{name}.rb") || which("brew-#{name}")
  !path.nil? && path.realpath.parent != HOMEBREW_ALIASES
end

#editvoid

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.



117
118
119
120
# File 'aliases/alias.rb', line 117

def edit
  write(override: false)
  Utils::Editor.open script.to_s
end

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.



64
65
66
67
# File 'aliases/alias.rb', line 64

def link
  FileUtils.rm symlink if File.symlink? symlink
  FileUtils.ln_s script, symlink
end

#removevoid

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.



109
110
111
112
113
114
# File 'aliases/alias.rb', line 109

def remove
  odie "'brew #{name}' is not aliased to anything." if !symlink.exist? || !valid_symlink?

  script.unlink
  symlink.unlink
end

#reserved?Boolean

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:

  • (Boolean)


36
37
38
# File 'aliases/alias.rb', line 36

def reserved?
  Aliases.reserved.include? name
end

#scriptPathname

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:



47
48
49
# File 'aliases/alias.rb', line 47

def script
  @script ||= Pathname.new("#{HOMEBREW_ALIASES}/#{name.gsub(/\W/, "_")}")
end

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:



52
53
54
# File 'aliases/alias.rb', line 52

def symlink
  @symlink ||= Pathname.new("#{HOMEBREW_PREFIX}/bin/brew-#{name}")
end

#valid_symlink?Boolean

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:

  • (Boolean)


57
58
59
60
61
# File 'aliases/alias.rb', line 57

def valid_symlink?
  symlink.realpath.parent == HOMEBREW_ALIASES.realpath
rescue NameError
  false
end

#write(opts = {}) ⇒ 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:

  • opts (Hash{Symbol => Boolean}) (defaults to: {})


70
71
72
73
74
75
76
77
78
79
80
81
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
# File 'aliases/alias.rb', line 70

def write(opts = {})
  odie "'#{name}' is a reserved command. Sorry." if reserved?
  odie "'brew #{name}' already exists. Sorry." if cmd_exists?

  return if !opts[:override] && script.exist?

  content = if command
    <<~EOS
      #:  * `#{name}` [args...]
      #:    `brew #{name}` is an alias for `#{command}`
      #{command} $*
    EOS
  else
    <<~EOS
      #:  * `#{name}` [args...]
      #:    `brew #{name}` is an alias for *command*

      # This is a Homebrew alias script. It'll be called when the user
      # types `brew #{name}`. Any remaining arguments are passed to
      # this script. You can retrieve those with $*, or only the first
      # one with $1. Please keep your script on one line.

      # TODO: Replace the line below with your script
      echo "Hello I'm 'brew "#{name}"' and my args are:" $*
    EOS
  end

  script.open("w") do |f|
    f.write <<~EOS
      #! #{Utils::Shell.which("bash")}
      # alias: brew #{name}
      #{content}
    EOS
  end
  script.chmod 0744
  link
end