Module: CommandKit::Completion::Install
- Includes:
- Env::Home, Env::Prefix, Env::Shell, Printing
- Defined in:
- lib/command_kit/completion/install.rb
Overview
Mixins that adds methods for installing shell completion files.
Environment Variables
SHELL
- The current shell.PREFIX
- The optional root prefix of the file-system.
Constant Summary
Constants included from Printing
Instance Attribute Summary collapse
-
#completions_dir ⇒ String?
readonly
private
The installation directory for completion files for the current shell.
Attributes included from Env::Prefix
Attributes included from Env
Attributes included from Env::Shell
Attributes included from Env::Home
Instance Method Summary collapse
-
#initialize(**kwargs) ⇒ Object
Initialize #completions_dir based on the
SHELL
environment variable and the UID of the process. -
#install_completion_file(path, type: :bash) ⇒ Object
Installs the shell completion file into #completions_dir.
-
#print_completion_file(path, **kwargs) ⇒ Object
Prints the shell completion file to stdout.
-
#uninstall_completion_file_for(command) ⇒ Object
Uninstalls a shell completion file for the specified command.
Methods included from Env::Home::ModuleMethods
Methods included from Printing
#print_error, #print_exception
Methods included from Stdio
#abort, #gets, #print, #printf, #putc, #puts, #readline, #readlines, #stderr, #stdin, #stdout
Instance Attribute Details
#completions_dir ⇒ String? (readonly)
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.
The installation directory for completion files for the current shell.
42 43 44 |
# File 'lib/command_kit/completion/install.rb', line 42 def completions_dir @completions_dir end |
Instance Method Details
#initialize(**kwargs) ⇒ Object
Initialize #completions_dir based on the SHELL
environment variable
and the UID of the process.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/command_kit/completion/install.rb', line 53 def initialize(**kwargs) super(**kwargs) @completions_dir = case shell_type when :bash if Process.uid == 0 File.join(root,'usr','local','share','bash-completion','completions') else xdg_data_home = env.fetch('XDG_DATA_HOME') do File.join(home_dir,'.local','share') end File.join(xdg_data_home,'bash-completion','completions') end when :zsh File.join(root,'usr','local','share','zsh','site-functions') when :fish if Process.uid == 0 File.join(root,'usr','local','share','fish','completions') else xdg_config_home = env.fetch('XDG_CONFIG_HOME') do File.join(home_dir,'.config') end File.join(xdg_config_home,'fish','completions') end end end |
#install_completion_file(path, type: :bash) ⇒ Object
Installs the shell completion file into #completions_dir.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/command_kit/completion/install.rb', line 129 def install_completion_file(path, type: :bash) completion_file = normalize_completion_file(path, type: type) completion_path = File.join(@completions_dir,completion_file) begin ::FileUtils.mkdir_p(@completions_dir) rescue Errno::EACCES print_error "cannot write to #{shell_type} completions directory: #{@completions_dir}" exit(-1) end begin File.open(completion_path,'w') do |output| write_completion_file(path,output, type: type) end rescue Errno::EACCES print_error "cannot write to #{shell_type} completion file: #{completion_path}" exit(-1) end end |
#print_completion_file(path, **kwargs) ⇒ Object
Prints the shell completion file to stdout.
105 106 107 |
# File 'lib/command_kit/completion/install.rb', line 105 def print_completion_file(path,**kwargs) write_completion_file(path,stdout,**kwargs) end |
#uninstall_completion_file_for(command) ⇒ Object
Uninstalls a shell completion file for the specified command.
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/command_kit/completion/install.rb', line 161 def uninstall_completion_file_for(command) completion_file = completion_file_for_command(command) completion_path = File.join(@completions_dir,completion_file) begin ::FileUtils.rm_f(completion_path) rescue Errno::EACCES print_error "cannot remove #{shell_type} completion file: #{completion_path}" exit(-1) end end |