command_kit 0.4.1 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +1 -1
  3. data/.rubocop.yml +7 -1
  4. data/ChangeLog.md +11 -0
  5. data/Gemfile +2 -0
  6. data/README.md +3 -0
  7. data/Rakefile +2 -0
  8. data/command_kit.gemspec +8 -6
  9. data/lib/command_kit/arguments/argument.rb +3 -1
  10. data/lib/command_kit/arguments/argument_value.rb +2 -0
  11. data/lib/command_kit/arguments.rb +5 -5
  12. data/lib/command_kit/bug_report.rb +4 -2
  13. data/lib/command_kit/colors.rb +2 -2
  14. data/lib/command_kit/command.rb +13 -11
  15. data/lib/command_kit/command_name.rb +3 -1
  16. data/lib/command_kit/commands/auto_load/subcommand.rb +3 -1
  17. data/lib/command_kit/commands/auto_load.rb +3 -3
  18. data/lib/command_kit/commands/auto_require.rb +5 -3
  19. data/lib/command_kit/commands/command.rb +4 -2
  20. data/lib/command_kit/commands/help.rb +2 -2
  21. data/lib/command_kit/commands/parent_command.rb +2 -0
  22. data/lib/command_kit/commands/subcommand.rb +2 -0
  23. data/lib/command_kit/commands.rb +8 -8
  24. data/lib/command_kit/completion/install.rb +277 -0
  25. data/lib/command_kit/description.rb +3 -1
  26. data/lib/command_kit/edit.rb +1 -1
  27. data/lib/command_kit/env/home.rb +1 -1
  28. data/lib/command_kit/env/path.rb +1 -1
  29. data/lib/command_kit/env/prefix.rb +41 -0
  30. data/lib/command_kit/env/shell.rb +58 -0
  31. data/lib/command_kit/env.rb +2 -0
  32. data/lib/command_kit/examples.rb +4 -2
  33. data/lib/command_kit/exception_handler.rb +4 -2
  34. data/lib/command_kit/file_utils.rb +2 -0
  35. data/lib/command_kit/help/man.rb +4 -4
  36. data/lib/command_kit/help.rb +2 -0
  37. data/lib/command_kit/interactive.rb +1 -1
  38. data/lib/command_kit/main.rb +2 -0
  39. data/lib/command_kit/open_app.rb +4 -2
  40. data/lib/command_kit/options/option.rb +3 -1
  41. data/lib/command_kit/options/option_value.rb +2 -2
  42. data/lib/command_kit/options/parser.rb +3 -3
  43. data/lib/command_kit/options/quiet.rb +3 -1
  44. data/lib/command_kit/options/verbose.rb +3 -1
  45. data/lib/command_kit/options/version.rb +3 -1
  46. data/lib/command_kit/options.rb +5 -3
  47. data/lib/command_kit/os/linux.rb +3 -1
  48. data/lib/command_kit/os.rb +2 -0
  49. data/lib/command_kit/package_manager.rb +6 -4
  50. data/lib/command_kit/pager.rb +4 -4
  51. data/lib/command_kit/printing/fields.rb +3 -1
  52. data/lib/command_kit/printing/indent.rb +2 -0
  53. data/lib/command_kit/printing/lists.rb +1 -1
  54. data/lib/command_kit/printing/tables/border_style.rb +2 -0
  55. data/lib/command_kit/printing/tables/row_builder.rb +3 -1
  56. data/lib/command_kit/printing/tables/style.rb +1 -1
  57. data/lib/command_kit/printing/tables/table_builder.rb +3 -1
  58. data/lib/command_kit/printing/tables.rb +4 -4
  59. data/lib/command_kit/printing.rb +1 -1
  60. data/lib/command_kit/program_name.rb +2 -0
  61. data/lib/command_kit/stdio.rb +2 -0
  62. data/lib/command_kit/sudo.rb +1 -1
  63. data/lib/command_kit/terminal.rb +4 -2
  64. data/lib/command_kit/usage.rb +4 -2
  65. data/lib/command_kit/version.rb +3 -1
  66. data/lib/command_kit/xdg.rb +4 -2
  67. data/lib/command_kit.rb +4 -2
  68. metadata +5 -65
  69. data/spec/arguments/argument_spec.rb +0 -133
  70. data/spec/arguments/argument_value_spec.rb +0 -66
  71. data/spec/arguments_spec.rb +0 -279
  72. data/spec/bug_report_spec.rb +0 -266
  73. data/spec/colors_spec.rb +0 -771
  74. data/spec/command_kit_spec.rb +0 -8
  75. data/spec/command_name_spec.rb +0 -130
  76. data/spec/command_spec.rb +0 -123
  77. data/spec/commands/auto_load/subcommand_spec.rb +0 -82
  78. data/spec/commands/auto_load_spec.rb +0 -159
  79. data/spec/commands/auto_require_spec.rb +0 -142
  80. data/spec/commands/fixtures/test_auto_load/cli/commands/test1.rb +0 -10
  81. data/spec/commands/fixtures/test_auto_load/cli/commands/test2.rb +0 -10
  82. data/spec/commands/fixtures/test_auto_require/lib/test_auto_require/cli/commands/test1.rb +0 -10
  83. data/spec/commands/help_spec.rb +0 -66
  84. data/spec/commands/parent_command_spec.rb +0 -40
  85. data/spec/commands/subcommand_spec.rb +0 -99
  86. data/spec/commands_spec.rb +0 -865
  87. data/spec/description_spec.rb +0 -179
  88. data/spec/edit_spec.rb +0 -72
  89. data/spec/env/home_spec.rb +0 -46
  90. data/spec/env/path_spec.rb +0 -84
  91. data/spec/env_spec.rb +0 -123
  92. data/spec/examples_spec.rb +0 -211
  93. data/spec/exception_handler_spec.rb +0 -103
  94. data/spec/file_utils_spec.rb +0 -59
  95. data/spec/fixtures/template.erb +0 -5
  96. data/spec/help/man_spec.rb +0 -345
  97. data/spec/help_spec.rb +0 -94
  98. data/spec/inflector_spec.rb +0 -166
  99. data/spec/interactive_spec.rb +0 -415
  100. data/spec/main_spec.rb +0 -179
  101. data/spec/man_spec.rb +0 -46
  102. data/spec/open_app_spec.rb +0 -85
  103. data/spec/options/option_spec.rb +0 -343
  104. data/spec/options/option_value_spec.rb +0 -171
  105. data/spec/options/parser_spec.rb +0 -274
  106. data/spec/options/quiet_spec.rb +0 -51
  107. data/spec/options/verbose_spec.rb +0 -51
  108. data/spec/options/version_spec.rb +0 -146
  109. data/spec/options_spec.rb +0 -465
  110. data/spec/os/linux_spec.rb +0 -164
  111. data/spec/os_spec.rb +0 -233
  112. data/spec/package_manager_spec.rb +0 -806
  113. data/spec/pager_spec.rb +0 -217
  114. data/spec/printing/fields_spec.rb +0 -167
  115. data/spec/printing/indent_spec.rb +0 -132
  116. data/spec/printing/lists_spec.rb +0 -99
  117. data/spec/printing/tables/border_style.rb +0 -43
  118. data/spec/printing/tables/cell_builer_spec.rb +0 -135
  119. data/spec/printing/tables/row_builder_spec.rb +0 -165
  120. data/spec/printing/tables/style_spec.rb +0 -377
  121. data/spec/printing/tables/table_builder_spec.rb +0 -252
  122. data/spec/printing/tables/table_formatter_spec.rb +0 -1190
  123. data/spec/printing/tables_spec.rb +0 -1069
  124. data/spec/printing_spec.rb +0 -106
  125. data/spec/program_name_spec.rb +0 -70
  126. data/spec/spec_helper.rb +0 -3
  127. data/spec/stdio_spec.rb +0 -264
  128. data/spec/sudo_spec.rb +0 -51
  129. data/spec/terminal_spec.rb +0 -231
  130. data/spec/usage_spec.rb +0 -237
  131. data/spec/xdg_spec.rb +0 -191
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3a35e836c936c91aa916f33e691260a591d2e363de6a0fd3d9259150b86add15
4
- data.tar.gz: fe2e2cc87a8a0fe889cba23b5567b307bb088fdf3ee4eff7d681694a981b5143
3
+ metadata.gz: 2f84fffcb26bb3b32c2d87e9e881ce6a29d8632b62aba64d6b47c7aef45ee170
4
+ data.tar.gz: 2c0de71142b1f4f7c28986f5b1fcc5bc15939f438daeb00f726809378feb42f8
5
5
  SHA512:
6
- metadata.gz: da2676ab635edc9898457b1c532af6ac4c478c6f3a0ab19dfc42f0abc4ddd73ef4f473fe7699f8bed3ac5abec3381a7a9ca9c50736c4cc3697612ef36f3d6fea
7
- data.tar.gz: d30a04ab5274f489ef3e534950301669d47112ee56d43b59e51c5e816f7de21fba72197d43f3362331c40f3e75a70e0c316d53fe12bf8960daf79efec270a372
6
+ metadata.gz: 3db3a4eab3b425d5d516d39275b71ea552b75e5c41d17e070b8e252b9ecfafa606a0355c32a489ee56549a60f14a40a97ed47f0816e3d5a4581068aede96b44f
7
+ data.tar.gz: cc9b154d81e142f46be9452e8b8c98b435e582319e572b1ebb059c2ba03140486b6c8a1a33988f16725e6baabc3451f96d43cb5c9c006c298e275996eb73222e
@@ -19,7 +19,7 @@ jobs:
19
19
  - truffleruby
20
20
  name: Ruby ${{ matrix.ruby }}
21
21
  steps:
22
- - uses: actions/checkout@v2
22
+ - uses: actions/checkout@v4
23
23
  - name: Set up Ruby
24
24
  uses: ruby/setup-ruby@v1
25
25
  with:
data/.rubocop.yml CHANGED
@@ -79,7 +79,6 @@ Naming/RescuedExceptionsVariableName: { Enabled: false } # Offense count: 11
79
79
  Style/BlockDelimiters: { Enabled: false } # Offense count: 17
80
80
  Style/ClassCheck: { Enabled: false } # Offense count: 10
81
81
  Style/ClassEqualityComparison: { Enabled: false } # Offense count: 16
82
- Style/FrozenStringLiteralComment: { Enabled: false } # Offense count: 77
83
82
  Style/GlobalStdStream: { Enabled: false } # Offense count: 13
84
83
  Style/GuardClause: { Enabled: false } # Offense count: 10
85
84
  Style/IfUnlessModifier: { Enabled: false } # Offense count: 13
@@ -138,6 +137,13 @@ Style/RescueStandardError: { Enabled: false } # Offense count: 1
138
137
  Style/SoleNestedConditional: { Enabled: false } # Offense count: 1
139
138
  Style/TrailingCommaInHashLiteral: { Enabled: false } # Offense count: 2
140
139
 
140
+ # enforce frozen_string_literal for lib/, but not examples/ or spec/.
141
+ Style/FrozenStringLiteralComment:
142
+ Exclude:
143
+ - 'examples/{**/,}*.rb'
144
+ - 'spec/spec_helper.rb'
145
+ - 'spec/{**/,}*_spec.rb'
146
+
141
147
  # rubocop cannot tell that rubygems_mfa_required is enabled in gemspec.yml
142
148
  Gemspec/RequireMFA: { Enabled: false }
143
149
 
data/ChangeLog.md CHANGED
@@ -1,3 +1,14 @@
1
+ ### 0.5.1 / 2024-01-24
2
+
3
+ * Switched to using `require_relative` to improve load-times.
4
+ * Added `# frozen_string_literal: true` to all files.
5
+
6
+ ### 0.5.0 / 2024-01-04
7
+
8
+ * Added {CommandKit::Env::Shell}.
9
+ * Added {CommandKit::Env::Prefix}.
10
+ * Added {CommandKit::Completion::Install}.
11
+
1
12
  ### 0.4.1 / 2024-01-03
2
13
 
3
14
  * Added more examples of how to define sub-commands and sub-sub-commands.
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  gemspec
data/README.md CHANGED
@@ -214,11 +214,14 @@ end
214
214
  * [CommandKit::Commands](https://rubydoc.info/gems/command_kit/CommandKit/Commands)
215
215
  * [CommandKit::Commands::AutoLoad](https://rubydoc.info/gems/command_kit/CommandKit/Commands/AutoLoad)
216
216
  * [CommandKit::Commands::AutoRequire](https://rubydoc.info/gems/command_kit/CommandKit/Commands/AutoRequire)
217
+ * [CommandKit::Completion::Install](https://rubydoc.info/gems/command_kit/CommandKit/Completion/Install)
217
218
  * [CommandKit::Description](https://rubydoc.info/gems/command_kit/CommandKit/Description)
218
219
  * [CommandKit::Edit](https://rubydoc.info/gems/command_kit/CommandKit/Edit)
219
220
  * [CommandKit::Env](https://rubydoc.info/gems/command_kit/CommandKit/Env)
220
221
  * [CommandKit::Env::Home](https://rubydoc.info/gems/command_kit/CommandKit/Env/Home)
221
222
  * [CommandKit::Env::Path](https://rubydoc.info/gems/command_kit/CommandKit/Env/Path)
223
+ * [CommandKit::Env::Prefix](https://rubydoc.info/gems/command_kit/CommandKit/Env/Prefix)
224
+ * [CommandKit::Env::Shell](https://rubydoc.info/gems/command_kit/CommandKit/Env/Shell)
222
225
  * [CommandKit::Examples](https://rubydoc.info/gems/command_kit/CommandKit/Examples)
223
226
  * [CommandKit::ExceptionHandler](https://rubydoc.info/gems/command_kit/CommandKit/ExceptionHandler)
224
227
  * [CommandKit::FileUtils](https://rubydoc.info/gems/command_kit/CommandKit/FileUtils)
data/Rakefile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rubygems'
2
4
 
3
5
  begin
data/command_kit.gemspec CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'yaml'
2
4
 
3
5
  Gem::Specification.new do |gem|
@@ -5,10 +7,7 @@ Gem::Specification.new do |gem|
5
7
 
6
8
  gem.name = gemspec.fetch('name')
7
9
  gem.version = gemspec.fetch('version') do
8
- lib_dir = File.join(File.dirname(__FILE__),'lib')
9
- $LOAD_PATH << lib_dir unless $LOAD_PATH.include?(lib_dir)
10
-
11
- require 'command_kit/version'
10
+ require_relative 'lib/command_kit/version'
12
11
  CommandKit::VERSION
13
12
  end
14
13
 
@@ -20,12 +19,15 @@ Gem::Specification.new do |gem|
20
19
  gem.homepage = gemspec['homepage']
21
20
  gem.metadata = gemspec['metadata'] if gemspec['metadata']
22
21
 
23
- glob = lambda { |patterns| gem.files & Dir[*patterns] }
22
+ glob = ->(patterns) { gem.files & Dir[*patterns] }
24
23
 
25
24
  gem.files = if gemspec['files'] then glob[gemspec['files']]
26
25
  else `git ls-files`.split($/)
27
26
  end
28
27
 
28
+ # exclude test files from the packages gem
29
+ gem.files -= glob[gemspec['test_files'] || 'spec/{**/}*']
30
+
29
31
  gem.executables = gemspec.fetch('executables') do
30
32
  glob['bin/*'].map { |path| File.basename(path) }
31
33
  end
@@ -43,7 +45,7 @@ Gem::Specification.new do |gem|
43
45
  gem.required_rubygems_version = gemspec['required_rubygems_version']
44
46
  gem.post_install_message = gemspec['post_install_message']
45
47
 
46
- split = lambda { |string| string.split(/,\s*/) }
48
+ split = ->(string) { string.split(/,\s*/) }
47
49
 
48
50
  if gemspec['dependencies']
49
51
  gemspec['dependencies'].each do |name,versions|
@@ -1,4 +1,6 @@
1
- require 'command_kit/arguments/argument_value'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'argument_value'
2
4
 
3
5
  module CommandKit
4
6
  module Arguments
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CommandKit
2
4
  module Arguments
3
5
  #
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'command_kit/arguments/argument'
4
- require 'command_kit/usage'
5
- require 'command_kit/main'
6
- require 'command_kit/help'
7
- require 'command_kit/printing'
3
+ require_relative 'arguments/argument'
4
+ require_relative 'usage'
5
+ require_relative 'main'
6
+ require_relative 'help'
7
+ require_relative 'printing'
8
8
 
9
9
  module CommandKit
10
10
  #
@@ -1,5 +1,7 @@
1
- require 'command_kit/exception_handler'
2
- require 'command_kit/printing'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'exception_handler'
4
+ require_relative 'printing'
3
5
 
4
6
  module CommandKit
5
7
  #
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'command_kit/stdio'
4
- require 'command_kit/env'
3
+ require_relative 'stdio'
4
+ require_relative 'env'
5
5
 
6
6
  module CommandKit
7
7
  #
@@ -1,14 +1,16 @@
1
- require 'command_kit/main'
2
- require 'command_kit/env'
3
- require 'command_kit/stdio'
4
- require 'command_kit/printing'
5
- require 'command_kit/usage'
6
- require 'command_kit/arguments'
7
- require 'command_kit/options'
8
- require 'command_kit/examples'
9
- require 'command_kit/description'
10
- require 'command_kit/exception_handler'
11
- require 'command_kit/file_utils'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'main'
4
+ require_relative 'env'
5
+ require_relative 'stdio'
6
+ require_relative 'printing'
7
+ require_relative 'usage'
8
+ require_relative 'arguments'
9
+ require_relative 'options'
10
+ require_relative 'examples'
11
+ require_relative 'description'
12
+ require_relative 'exception_handler'
13
+ require_relative 'file_utils'
12
14
 
13
15
  module CommandKit
14
16
  #
@@ -1,4 +1,6 @@
1
- require 'command_kit/inflector'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'inflector'
2
4
 
3
5
  module CommandKit
4
6
  #
@@ -1,4 +1,6 @@
1
- require 'command_kit/commands/subcommand'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../subcommand'
2
4
 
3
5
  module CommandKit
4
6
  module Commands
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'command_kit/commands'
4
- require 'command_kit/commands/auto_load/subcommand'
5
- require 'command_kit/inflector'
3
+ require_relative '../commands'
4
+ require_relative 'auto_load/subcommand'
5
+ require_relative '../inflector'
6
6
 
7
7
  module CommandKit
8
8
  module Commands
@@ -1,6 +1,8 @@
1
- require 'command_kit/commands'
2
- require 'command_kit/commands/subcommand'
3
- require 'command_kit/inflector'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../commands'
4
+ require_relative 'subcommand'
5
+ require_relative '../inflector'
4
6
 
5
7
  module CommandKit
6
8
  module Commands
@@ -1,5 +1,7 @@
1
- require 'command_kit/command'
2
- require 'command_kit/commands/parent_command'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../command'
4
+ require_relative 'parent_command'
3
5
 
4
6
  module CommandKit
5
7
  module Commands
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'command_kit/command'
4
- require 'command_kit/commands/parent_command'
3
+ require_relative '../command'
4
+ require_relative 'parent_command'
5
5
 
6
6
  module CommandKit
7
7
  module Commands
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CommandKit
2
4
  module Commands
3
5
  #
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module CommandKit
2
4
  module Commands
3
5
  #
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'command_kit/commands/subcommand'
4
- require 'command_kit/commands/parent_command'
5
- require 'command_kit/commands/help'
6
- require 'command_kit/command_name'
7
- require 'command_kit/usage'
8
- require 'command_kit/options'
9
- require 'command_kit/stdio'
10
- require 'command_kit/env'
3
+ require_relative 'commands/subcommand'
4
+ require_relative 'commands/parent_command'
5
+ require_relative 'commands/help'
6
+ require_relative 'command_name'
7
+ require_relative 'usage'
8
+ require_relative 'options'
9
+ require_relative 'stdio'
10
+ require_relative 'env'
11
11
 
12
12
  module CommandKit
13
13
  #
@@ -0,0 +1,277 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../printing'
4
+ require_relative '../env/home'
5
+ require_relative '../env/shell'
6
+ require_relative '../env/prefix'
7
+
8
+ require 'fileutils'
9
+
10
+ module CommandKit
11
+ module Completion
12
+ #
13
+ # Mixins that adds methods for installing shell completion files.
14
+ #
15
+ # ## Environment Variables
16
+ #
17
+ # * `SHELL` - The current shell.
18
+ # * `PREFIX` - The optional root prefix of the file-system.
19
+ #
20
+ # @api public
21
+ #
22
+ # @since 0.5.0
23
+ #
24
+ module Install
25
+ include Printing
26
+ include Env::Home
27
+ include Env::Shell
28
+ include Env::Prefix
29
+
30
+ # The installation directory for completion files for the current shell.
31
+ #
32
+ # @return [String, nil]
33
+ # * Bash
34
+ # * Regular users: `~/.local/share/bash-completion/completions`
35
+ # * Root users:`$PREFIX/usr/local/share/bash-completion/completions`
36
+ # * Zsh: `$PREFIX/usr/local/share/zsh/site-functions`
37
+ # * Fish:
38
+ # * Regular users: `~/.config/fish/completions`
39
+ # * Root users: `$PREFIX/usr/local/share/fish/completions`
40
+ #
41
+ # @api private
42
+ attr_reader :completions_dir
43
+
44
+ #
45
+ # Initialize {#completions_dir} based on the `SHELL` environment variable
46
+ # and the UID of the process.
47
+ #
48
+ # @param [Hash{Symbol => Object}] kwargs
49
+ # Additional keyword arguments.
50
+ #
51
+ # @api public
52
+ #
53
+ def initialize(**kwargs)
54
+ super(**kwargs)
55
+
56
+ @completions_dir = case shell_type
57
+ when :bash
58
+ if Process.uid == 0
59
+ File.join(root,'usr','local','share','bash-completion','completions')
60
+ else
61
+ xdg_data_home = env.fetch('XDG_DATA_HOME') do
62
+ File.join(home_dir,'.local','share')
63
+ end
64
+
65
+ File.join(xdg_data_home,'bash-completion','completions')
66
+ end
67
+ when :zsh
68
+ File.join(root,'usr','local','share','zsh','site-functions')
69
+ when :fish
70
+ if Process.uid == 0
71
+ File.join(root,'usr','local','share','fish','completions')
72
+ else
73
+ xdg_config_home = env.fetch('XDG_CONFIG_HOME') do
74
+ File.join(home_dir,'.config')
75
+ end
76
+
77
+ File.join(xdg_config_home,'fish','completions')
78
+ end
79
+ end
80
+ end
81
+
82
+ #
83
+ # Prints the shell completion file to stdout.
84
+ #
85
+ # @param [String] path
86
+ # The path to the shell completion file.
87
+ #
88
+ # @param [Hash{Symbol => Object}] kwargs
89
+ # Additional keyword arguments.
90
+ #
91
+ # @option kwargs [:bash, :zsh, :fish] :type (:bash)
92
+ # The type of the completion file.
93
+ #
94
+ # @example Prints a Bash completion file:
95
+ # print_completion_file 'path/to/completions/foo'
96
+ #
97
+ # @example Prints a Zsh completion file:
98
+ # print_completion_file 'path/to/completions/foo', type: :zsh
99
+ #
100
+ # @example Prints a Fish completion file:
101
+ # print_completion_file 'path/to/completions/foo', type: :fish
102
+ #
103
+ # @api public
104
+ #
105
+ def print_completion_file(path,**kwargs)
106
+ write_completion_file(path,stdout,**kwargs)
107
+ end
108
+
109
+ #
110
+ # Installs the shell completion file into {#completions_dir}.
111
+ #
112
+ # @param [String] path
113
+ # The path to the shell completion file.
114
+ #
115
+ # @param [:bash, :zsh, :fish] type
116
+ # The type of the shell completion file.
117
+ #
118
+ # @example Install a Bash completion file:
119
+ # install_completion_file 'path/to/completions/foo'
120
+ #
121
+ # @example Install a Zsh completion file:
122
+ # install_completion_file 'path/to/completions/foo', type: :zsh
123
+ #
124
+ # @example Install a Fish completion file:
125
+ # install_completion_file 'path/to/completions/foo', type: :fish
126
+ #
127
+ # @api public
128
+ #
129
+ def install_completion_file(path, type: :bash)
130
+ completion_file = normalize_completion_file(path, type: type)
131
+ completion_path = File.join(@completions_dir,completion_file)
132
+
133
+ begin
134
+ ::FileUtils.mkdir_p(@completions_dir)
135
+ rescue Errno::EACCES
136
+ print_error "cannot write to #{shell_type} completions directory: #{@completions_dir}"
137
+ exit(-1)
138
+ end
139
+
140
+ begin
141
+ File.open(completion_path,'w') do |output|
142
+ write_completion_file(path,output, type: type)
143
+ end
144
+ rescue Errno::EACCES
145
+ print_error "cannot write to #{shell_type} completion file: #{completion_path}"
146
+ exit(-1)
147
+ end
148
+ end
149
+
150
+ #
151
+ # Uninstalls a shell completion file for the specified command.
152
+ #
153
+ # @param [String] command
154
+ # The command to uninstall the completions for.
155
+ #
156
+ # @example Removes the completion file for the command 'foo':
157
+ # uninstall_completion_file_for 'foo'
158
+ #
159
+ # @api public
160
+ #
161
+ def uninstall_completion_file_for(command)
162
+ completion_file = completion_file_for_command(command)
163
+ completion_path = File.join(@completions_dir,completion_file)
164
+
165
+ begin
166
+ ::FileUtils.rm_f(completion_path)
167
+ rescue Errno::EACCES
168
+ print_error "cannot remove #{shell_type} completion file: #{completion_path}"
169
+ exit(-1)
170
+ end
171
+ end
172
+
173
+ private
174
+
175
+ #
176
+ # Calculates the installed completion file for the given command.
177
+ #
178
+ # @param [String] command
179
+ # The command name.
180
+ #
181
+ # @return [String]
182
+ # The path to the completion file for the command.
183
+ #
184
+ # @api private
185
+ #
186
+ def completion_file_for_command(command)
187
+ case shell_type
188
+ when :bash then command
189
+ when :zsh then "_#{command}"
190
+ when :fish then "#{command}.fish"
191
+ when nil
192
+ if shell then print_error("cannot identify shell: #{shell}")
193
+ else print_error("cannot identify shell")
194
+ end
195
+
196
+ exit(-1)
197
+ else
198
+ print_error("completions not support for the #{shell_type} shell: #{shell}")
199
+ exit(-1)
200
+ end
201
+ end
202
+
203
+ #
204
+ # Calculates the installation path for the given completion file.
205
+ #
206
+ # @param [String] path
207
+ # The path to the completion file that will be installed.
208
+ #
209
+ # @param [:bash, :zsh, :fish] type
210
+ # The type of the shell completion file.
211
+ #
212
+ # @return [String]
213
+ # The installation path for the completion file.
214
+ #
215
+ # @api private
216
+ #
217
+ def normalize_completion_file(path, type: :bash)
218
+ ext = File.extname(path)
219
+ file = File.basename(path,ext)
220
+
221
+ case [shell_type, type]
222
+ when [:bash, :bash] # no-op
223
+ when [:zsh, :zsh],
224
+ [:zsh, :bash]
225
+ unless file.start_with?('_')
226
+ file = "_#{file}"
227
+ end
228
+ when [:fish, :fish]
229
+ file = "#{file}.fish"
230
+ else
231
+ if shell_type
232
+ print_error("cannot install #{type} completion file into the #{shell_type} shell: #{path}")
233
+ elsif shell
234
+ print_error("cannot identify shell: #{shell}")
235
+ else
236
+ print_error("cannot identify shell")
237
+ end
238
+
239
+ exit(-1)
240
+ end
241
+
242
+ return file
243
+ end
244
+
245
+ #
246
+ # Writes the shell completion file to the output
247
+ #
248
+ # @param [String] path
249
+ # The path to the shell completion file.
250
+ #
251
+ # @param [IO] output
252
+ # The output stream to write to.
253
+ #
254
+ # @param [:bash, :zsh, :fish] type
255
+ # The type of the shell completion file.
256
+ #
257
+ # @api private
258
+ #
259
+ def write_completion_file(path,output, type: :bash)
260
+ if shell_type == :zsh && type == :bash
261
+ file_name = File.basename(path,File.extname(path))
262
+ command = file_name.sub(/\A_/,'')
263
+
264
+ # add the #compdef magic comments for zsh
265
+ output.puts "#compdef #{command}"
266
+ output.puts
267
+ end
268
+
269
+ File.open(path) do |file|
270
+ file.each_line do |line|
271
+ output.write(line)
272
+ end
273
+ end
274
+ end
275
+ end
276
+ end
277
+ end
@@ -1,4 +1,6 @@
1
- require 'command_kit/help'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'help'
2
4
 
3
5
  module CommandKit
4
6
  #
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'command_kit/env'
3
+ require_relative 'env'
4
4
 
5
5
  module CommandKit
6
6
  #
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'command_kit/env'
3
+ require_relative '../env'
4
4
 
5
5
  module CommandKit
6
6
  module Env
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'command_kit/env'
3
+ require_relative '../env'
4
4
 
5
5
  module CommandKit
6
6
  module Env