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

#odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_outside_github_actions, #pretty_duration, #pretty_installed, #pretty_outdated, #pretty_uninstalled

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:



19
20
21
22
23
24
25
26
27
28
29
30
# File 'aliases/alias.rb', line 19

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:



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

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:



13
14
15
# File 'aliases/alias.rb', line 13

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)


38
39
40
41
# File 'aliases/alias.rb', line 38

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.



114
115
116
117
# File 'aliases/alias.rb', line 114

def edit
  write(override: false)
  exec_editor 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.



61
62
63
64
# File 'aliases/alias.rb', line 61

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.



106
107
108
109
110
111
# File 'aliases/alias.rb', line 106

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)


33
34
35
# File 'aliases/alias.rb', line 33

def reserved?
  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:



44
45
46
# File 'aliases/alias.rb', line 44

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:



49
50
51
# File 'aliases/alias.rb', line 49

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)


54
55
56
57
58
# File 'aliases/alias.rb', line 54

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: {})


67
68
69
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
# File 'aliases/alias.rb', line 67

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
      #! #{`which bash`.chomp}
      # alias: brew #{name}
      #{content}
    EOS
  end
  script.chmod 0744
  link
end