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:



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

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:



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

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:



8
9
10
# File 'aliases/alias.rb', line 8

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)


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

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.



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

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.



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

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.



99
100
101
102
103
104
# File 'aliases/alias.rb', line 99

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)


28
29
30
# File 'aliases/alias.rb', line 28

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:



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

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:



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

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)


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

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


62
63
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
# File 'aliases/alias.rb', line 62

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