Class: Cask::Artifact::GeneratedCompletion Private

Inherits:
AbstractArtifact show all
Defined in:
cask/artifact/generated_completion.rb

Overview

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.

Artifact corresponding to the generate_completions_from_executable stanza.

Constant Summary collapse

SUPPORTED_SHELLS =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

T.let([:bash, :zsh, :fish, :pwsh].freeze, T::Array[Symbol])

Constants inherited from AbstractArtifact

AbstractArtifact::DirectivesType

Instance Attribute Summary collapse

Attributes inherited from AbstractArtifact

#cask

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractArtifact

#config, dirmethod, english_article, english_name, read_script_arguments, #sort_order, #staged_path_join_executable, #to_args

Methods included from Utils::Output::Mixin

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

Constructor Details

#initialize(cask, commands, base_name:, shell_parameter_format:, shells:) ⇒ 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:



63
64
65
66
67
68
69
70
71
# File 'cask/artifact/generated_completion.rb', line 63

def initialize(cask, commands, base_name:, shell_parameter_format:, shells:)
  super(cask, *commands, base_name:, shell_parameter_format:, shells:)

  @commands = T.let(commands, T::Array[T.any(Pathname, String)])
  @base_name = T.let(base_name, T.nilable(String))
  @shell_parameter_format = T.let(shell_parameter_format, T.nilable(T.any(Symbol, String)))
  @shells = T.let(shells, T::Array[Symbol])
  @resolved_base_name = T.let(nil, T.nilable(String))
end

Instance Attribute Details

#base_nameString? (readonly)

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:



77
78
79
# File 'cask/artifact/generated_completion.rb', line 77

def base_name
  @base_name
end

#commandsArray<Pathname, String> (readonly)

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:



74
75
76
# File 'cask/artifact/generated_completion.rb', line 74

def commands
  @commands
end

#shell_parameter_formatSymbol, ... (readonly)

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:



80
81
82
# File 'cask/artifact/generated_completion.rb', line 80

def shell_parameter_format
  @shell_parameter_format
end

#shellsArray<Symbol> (readonly)

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:



83
84
85
# File 'cask/artifact/generated_completion.rb', line 83

def shells
  @shells
end

Class Method Details

.dsl_keySymbol

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:



18
19
20
# File 'cask/artifact/generated_completion.rb', line 18

def self.dsl_key
  :generate_completions_from_executable
end

.from_args(cask, *args, base_name: nil, shell_parameter_format: nil, shells: nil) ⇒ T.attached_class

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:

Returns:

  • (T.attached_class)

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'cask/artifact/generated_completion.rb', line 31

def self.from_args(cask, *args, base_name: nil, shell_parameter_format: nil, shells: nil)
  raise CaskInvalidError.new(cask.token, "'#{dsl_key}' requires at least one command") if args.empty?

  commands = args.to_a
  resolved_shells = shells || ::Utils::ShellCompletion.default_completion_shells(shell_parameter_format)

  unsupported_shells = resolved_shells - SUPPORTED_SHELLS
  unless unsupported_shells.empty?
    raise CaskInvalidError.new(
      cask.token,
      "'#{dsl_key}' does not support shell(s): #{unsupported_shells.join(", ")}",
    )
  end

  new(
    cask,
    commands,
    base_name:,
    shell_parameter_format:,
    shells:                 resolved_shells,
  )
end

Instance Method Details

#install_phase(**_options) ⇒ 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:

  • _options (T.untyped)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'cask/artifact/generated_completion.rb', line 91

def install_phase(**_options)
  executable = staged_path_join_executable(T.must(commands.first))

  shells.each do |shell|
    popen_read_env = { "SHELL" => shell.to_s }
    shell_parameter = ::Utils::ShellCompletion.completion_shell_parameter(
      shell_parameter_format, shell, executable.to_s, popen_read_env
    )

    script_path = completion_script_path(shell)
    script_path.dirname.mkpath
    script_path.write(::Utils::ShellCompletion.generate_completion_output(
                        [executable, *commands[1..]],
                        shell_parameter,
                        popen_read_env,
                      ))
  rescue => e
    opoo "Failed to generate #{shell} completions from #{executable}: #{e}"
  end
end

#summarizeString

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:



86
87
88
# File 'cask/artifact/generated_completion.rb', line 86

def summarize
  "#{commands.join(" ")} (base_name: #{resolved_base_name}, shells: #{shells.join(", ")})"
end

#uninstall_phase(command: SystemCommand, **_options) ⇒ 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:

  • command (T.class_of(SystemCommand)) (defaults to: SystemCommand)
  • _options (T.untyped)


113
114
115
116
117
118
119
120
121
122
# File 'cask/artifact/generated_completion.rb', line 113

def uninstall_phase(command: SystemCommand, **_options)
  shells.each do |shell|
    path = completion_script_path(shell)
    next unless path.exist?

    Utils.gain_permissions_remove(path, command:)
  rescue => e
    opoo "Failed to remove #{shell} generated completions: #{e}"
  end
end