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.

[:bash, :zsh, :fish, :pwsh].freeze

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

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

Methods included from Utils::Output::Mixin

#issue_reporting_message, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_outside_github_actions, #opoo_without_github_actions_annotation, #pretty_deprecated, #pretty_disabled, #pretty_duration, #pretty_install_status, #pretty_installed, #pretty_uninstalled, #pretty_unmarked, #pretty_upgradable, #pretty_warning

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:



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

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

  @commands = commands
  @base_name = base_name
  @shell_parameter_format = shell_parameter_format
  @shells = shells
  @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:



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

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:



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

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:



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

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:



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

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
53
# 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))
                    .map(&:to_sym)

  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)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'cask/artifact/generated_completion.rb', line 92

def install_phase(**_options)
  executable = staged_path_join_executable(commands.fetch(0))
  completion_commands = [executable, *commands[1..]]
  completions = shells.map do |shell|
    popen_read_env = { "SHELL" => shell.to_s }
    {
      "shell"           => shell.to_s,
      "commands"        => completion_commands.map(&:to_s),
      "shell_parameter" => ::Utils::ShellCompletion.completion_shell_parameter(
        shell_parameter_format, shell, executable.to_s, popen_read_env
      ),
      "env"             => popen_read_env,
      "output_path"     => completion_script_path(shell).to_s,
    }
  end

  if (sandbox = cask_sandbox)
    completions.map { |completion| Pathname(completion.fetch("output_path")).dirname }.uniq.each do |directory|
      sandbox.allow_write_path directory
    end
    begin
      run_cask_sandbox(sandbox, { "action" => "generated_completions", "completions" => completions })
    rescue => e
      opoo e
    end
    return
  end

  completions.each { |completion| write_completion(completion, executable) }
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:



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

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)


124
125
126
127
128
129
130
131
132
133
# File 'cask/artifact/generated_completion.rb', line 124

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