Module: Commands Private

Defined in:
Library/Homebrew/commands.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Helper functions for commands.

Constant Summary collapse

HOMEBREW_CMD_PATH =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

(HOMEBREW_LIBRARY_PATH/"cmd").freeze
HOMEBREW_DEV_CMD_PATH =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

(HOMEBREW_LIBRARY_PATH/"dev-cmd").freeze
HOMEBREW_INTERNAL_COMMAND_ALIASES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

{
  "ls"          => "list",
  "homepage"    => "home",
  "-S"          => "search",
  "up"          => "update",
  "ln"          => "link",
  "instal"      => "install", # gem does the same
  "uninstal"    => "uninstall",
  "rm"          => "uninstall",
  "remove"      => "uninstall",
  "configure"   => "diy",
  "abv"         => "info",
  "dr"          => "doctor",
  "--repo"      => "--repository",
  "environment" => "--env",
  "--config"    => "config",
  "-v"          => "--version",
}.freeze

Class Method Summary collapse

Class Method Details

.args_method_name(cmd_path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


47
48
49
50
51
# File 'Library/Homebrew/commands.rb', line 47

def args_method_name(cmd_path)
  cmd_path_basename = basename_without_extension(cmd_path)
  cmd_method_prefix = method_name(cmd_path_basename)
  "#{cmd_method_prefix}_args".to_sym
end

.basename_without_extension(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


171
172
173
# File 'Library/Homebrew/commands.rb', line 171

def basename_without_extension(path)
  path.basename(path.extname)
end

.cask_commands(aliases: false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


143
144
145
146
147
148
# File 'Library/Homebrew/commands.rb', line 143

def cask_commands(aliases: false)
  cmds = cask_internal_commands
  cmds += cask_internal_command_aliases if aliases
  cmds += cask_external_commands
  cmds
end

.cask_external_commandsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


158
159
160
161
162
163
164
165
166
167
168
169
# File 'Library/Homebrew/commands.rb', line 158

def cask_external_commands
  PATH.new(Tap.cmd_directories, ENV["HOMEBREW_PATH"]).flat_map do |search_path|
    find_commands(search_path).map do |possible_command|
      path = possible_command.to_path
      command_name = path.match(/brewcask-(.*)\.rb/) { |data| data[1].delete_suffix(".rb") }
      if command_name.blank? && possible_command.executable?
        command_name = path.match(/brewcask-(.*)/) { |data| data[1] }
      end
      command_name
    end.compact
  end
end

.cask_internal_command_aliasesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


154
155
156
# File 'Library/Homebrew/commands.rb', line 154

def cask_internal_command_aliases
  Cask::Cmd.aliases.keys
end

.cask_internal_commandsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


150
151
152
# File 'Library/Homebrew/commands.rb', line 150

def cask_internal_commands
  Cask::Cmd.commands
end

.commands(aliases: false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


92
93
94
95
96
97
98
99
# File 'Library/Homebrew/commands.rb', line 92

def commands(aliases: false)
  cmds = internal_commands
  cmds += internal_developer_commands
  cmds += external_commands
  cmds += internal_commands_aliases if aliases
  cmds += cask_commands(aliases: aliases).map { |cmd| "cask #{cmd}" }
  cmds.sort
end

.external_cmd_path(cmd) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


78
79
80
# File 'Library/Homebrew/commands.rb', line 78

def external_cmd_path(cmd)
  which("brew-#{cmd}", PATH.new(ENV["PATH"]).append(Tap.cmd_directories))
end

.external_commandsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


134
135
136
137
138
139
140
141
# File 'Library/Homebrew/commands.rb', line 134

def external_commands
  Tap.cmd_directories.flat_map do |path|
    find_commands(path).select(&:executable?)
                       .map(&method(:basename_without_extension))
                       .map { |p| p.to_s.delete_prefix("brew-").strip }
  end.map(&:to_s)
     .sort
end

.external_ruby_cmd_path(cmd) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Ruby commands which are run by being required.


74
75
76
# File 'Library/Homebrew/commands.rb', line 74

def external_ruby_cmd_path(cmd)
  which("brew-#{cmd}.rb", PATH.new(ENV["PATH"]).append(Tap.cmd_directories))
end

.external_ruby_v2_cmd_path(cmd) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Ruby commands which can be required without being run.


68
69
70
71
# File 'Library/Homebrew/commands.rb', line 68

def external_ruby_v2_cmd_path(cmd)
  path = which("#{cmd}.rb", Tap.cmd_directories)
  path if require?(path)
end

.find_commands(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


175
176
177
178
179
# File 'Library/Homebrew/commands.rb', line 175

def find_commands(path)
  Pathname.glob("#{path}/*")
          .select(&:file?)
          .sort
end

.find_internal_commands(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


129
130
131
132
# File 'Library/Homebrew/commands.rb', line 129

def find_internal_commands(path)
  find_commands(path).map(&:basename)
                     .map(&method(:basename_without_extension))
end

.internal_cmd_path(cmd) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


53
54
55
56
57
58
# File 'Library/Homebrew/commands.rb', line 53

def internal_cmd_path(cmd)
  [
    HOMEBREW_CMD_PATH/"#{cmd}.rb",
    HOMEBREW_CMD_PATH/"#{cmd}.sh",
  ].find(&:exist?)
end

.internal_commandsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


117
118
119
# File 'Library/Homebrew/commands.rb', line 117

def internal_commands
  find_internal_commands(HOMEBREW_CMD_PATH).map(&:to_s)
end

.internal_commands_aliasesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


125
126
127
# File 'Library/Homebrew/commands.rb', line 125

def internal_commands_aliases
  HOMEBREW_INTERNAL_COMMAND_ALIASES.keys
end

.internal_commands_pathsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


101
102
103
# File 'Library/Homebrew/commands.rb', line 101

def internal_commands_paths
  find_commands HOMEBREW_CMD_PATH
end

.internal_dev_cmd_path(cmd) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


60
61
62
63
64
65
# File 'Library/Homebrew/commands.rb', line 60

def internal_dev_cmd_path(cmd)
  [
    HOMEBREW_DEV_CMD_PATH/"#{cmd}.rb",
    HOMEBREW_DEV_CMD_PATH/"#{cmd}.sh",
  ].find(&:exist?)
end

.internal_developer_commandsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


121
122
123
# File 'Library/Homebrew/commands.rb', line 121

def internal_developer_commands
  find_internal_commands(HOMEBREW_DEV_CMD_PATH).map(&:to_s)
end

.internal_developer_commands_pathsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


105
106
107
# File 'Library/Homebrew/commands.rb', line 105

def internal_developer_commands_paths
  find_commands HOMEBREW_DEV_CMD_PATH
end

.method_name(cmd) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


40
41
42
43
44
45
# File 'Library/Homebrew/commands.rb', line 40

def method_name(cmd)
  cmd.to_s
     .tr("-", "_")
     .downcase
     .to_sym
end

.official_external_commands_pathsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


109
110
111
112
113
114
115
# File 'Library/Homebrew/commands.rb', line 109

def official_external_commands_paths
  %w[bundle services test-bot].map do |cmd|
    tap = Tap.fetch("Homebrew/#{cmd}")
    tap.install unless tap.installed?
    external_ruby_v2_cmd_path(cmd)
  end
end

.path(cmd) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


82
83
84
85
86
87
88
89
90
# File 'Library/Homebrew/commands.rb', line 82

def path(cmd)
  internal_cmd = HOMEBREW_INTERNAL_COMMAND_ALIASES.fetch(cmd, cmd)
  path ||= internal_cmd_path(internal_cmd)
  path ||= internal_dev_cmd_path(internal_cmd)
  path ||= external_ruby_v2_cmd_path(cmd)
  path ||= external_ruby_cmd_path(cmd)
  path ||= external_cmd_path(cmd)
  path
end

.rebuild_commands_completion_listObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


188
189
190
191
192
193
194
# File 'Library/Homebrew/commands.rb', line 188

def rebuild_commands_completion_list
  # Ensure that the cache exists so we can build the commands list
  HOMEBREW_CACHE.mkpath

  file = HOMEBREW_CACHE/"all_commands_list.txt"
  file.atomic_write("#{commands(aliases: true).sort.join("\n")}\n")
end

.rebuild_internal_commands_completion_listObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


181
182
183
184
185
186
# File 'Library/Homebrew/commands.rb', line 181

def rebuild_internal_commands_completion_list
  cmds = internal_commands + internal_developer_commands + internal_commands_aliases

  file = HOMEBREW_REPOSITORY/"completions/internal_commands_list.txt"
  file.atomic_write("#{cmds.sort.join("\n")}\n")
end

.valid_internal_cmd?(cmd) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

32
33
34
# File 'Library/Homebrew/commands.rb', line 32

def valid_internal_cmd?(cmd)
  require?(HOMEBREW_CMD_PATH/cmd)
end

.valid_internal_dev_cmd?(cmd) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

36
37
38
# File 'Library/Homebrew/commands.rb', line 36

def valid_internal_dev_cmd?(cmd)
  require?(HOMEBREW_DEV_CMD_PATH/cmd)
end