Class: Homebrew::Aliases::Alias Private

Inherits:
Object
  • Object
show all
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

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:



16
17
18
19
20
21
22
23
24
25
26
27
# File 'aliases/alias.rb', line 16

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:



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

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:



10
11
12
# File 'aliases/alias.rb', line 10

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)


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

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.



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

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.



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

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.



101
102
103
104
105
106
# File 'aliases/alias.rb', line 101

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)


30
31
32
# File 'aliases/alias.rb', line 30

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:



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

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:



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

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)


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

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


64
65
66
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
# File 'aliases/alias.rb', line 64

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
      #
      # 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 alias "#{name}" and my args are:" $1
    EOS
  end

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