From c5a877ef5e8e4ea35f30642db9e9af0c2b443066 Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Sun, 14 Sep 2025 11:17:00 +0000 Subject: [PATCH 1/3] - Add support for `generate --install` --- lib/completely/commands/generate.rb | 28 ++++++++++++++++++++++++---- spec/approvals/cli/generate/help | 4 ++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/completely/commands/generate.rb b/lib/completely/commands/generate.rb index 6fea310..8da6c87 100644 --- a/lib/completely/commands/generate.rb +++ b/lib/completely/commands/generate.rb @@ -6,12 +6,15 @@ class Generate < Base help 'Generate the bash completion script to file or stdout' usage 'completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]' + usage 'completely generate [CONFIG_PATH --install PROGRAM --function NAME]' usage 'completely generate (-h|--help)' option_function option '-w --wrap NAME', 'Wrap the completion script inside a function that echos the ' \ 'script. This is useful if you wish to embed it directly in your script.' + option '-i --install PROGRAM', 'Install the generated script as completions for PROGRAM.' + param 'CONFIG_PATH', <<~USAGE Path to the YAML configuration file [default: completely.yaml]. Use '-' to read from stdin. @@ -35,17 +38,34 @@ class Generate < Base def run wrap = args['--wrap'] output = wrap ? wrapper_function(wrap) : script + + if args['--install'] + install output + else + show_or_save output + end + end + + private + + def install(content) + installer = Installer.from_string program: args['--install'], string: content + success = installer.install force: true + raise InstallError, "Failed running command:\nnb`#{installer.install_command_string}`" unless success + say "Saved m`#{installer.target_path}`" + say 'You may need to restart your session to test it' + end + + def show_or_save(content) if output_path == '-' - puts output + puts content else - File.write output_path, output + File.write output_path, content say "Saved m`#{output_path}`" end syntax_warning unless completions.valid? end - private - def wrapper_function(wrapper_name) completions.wrapper_function wrapper_name end diff --git a/spec/approvals/cli/generate/help b/spec/approvals/cli/generate/help index 5bbb67f..0cf96a1 100644 --- a/spec/approvals/cli/generate/help +++ b/spec/approvals/cli/generate/help @@ -2,6 +2,7 @@ Generate the bash completion script to file or stdout Usage: completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME] + completely generate [CONFIG_PATH --install PROGRAM --function NAME] completely generate (-h|--help) Options: @@ -12,6 +13,9 @@ Options: Wrap the completion script inside a function that echos the script. This is useful if you wish to embed it directly in your script. + -i --install PROGRAM + Install the generated script as completions for PROGRAM. + -h --help Show this help From 63eac420806e17069a1fdca0fd9840a0633925f3 Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Sun, 14 Sep 2025 11:21:38 +0000 Subject: [PATCH 2/3] normalize mode condition --- lib/completely/commands/generate.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/completely/commands/generate.rb b/lib/completely/commands/generate.rb index 8da6c87..46ca14f 100644 --- a/lib/completely/commands/generate.rb +++ b/lib/completely/commands/generate.rb @@ -41,8 +41,10 @@ def run if args['--install'] install output + elsif output_path == '-' + show output else - show_or_save output + save output end end @@ -56,13 +58,11 @@ def install(content) say 'You may need to restart your session to test it' end - def show_or_save(content) - if output_path == '-' - puts content - else - File.write output_path, content - say "Saved m`#{output_path}`" - end + def show(content) = puts content + + def save(content) + File.write output_path, content + say "Saved m`#{output_path}`" syntax_warning unless completions.valid? end From b6a006b101301367a03135480c6edfd13790bbb2 Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Sun, 14 Sep 2025 11:38:55 +0000 Subject: [PATCH 3/3] add specs for `generate --install` --- lib/completely/commands/generate.rb | 1 + spec/approvals/cli/generate/install | 2 ++ spec/completely/commands/generate_spec.rb | 23 ++++++++++++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 spec/approvals/cli/generate/install diff --git a/lib/completely/commands/generate.rb b/lib/completely/commands/generate.rb index 46ca14f..091bd06 100644 --- a/lib/completely/commands/generate.rb +++ b/lib/completely/commands/generate.rb @@ -54,6 +54,7 @@ def install(content) installer = Installer.from_string program: args['--install'], string: content success = installer.install force: true raise InstallError, "Failed running command:\nnb`#{installer.install_command_string}`" unless success + say "Saved m`#{installer.target_path}`" say 'You may need to restart your session to test it' end diff --git a/spec/approvals/cli/generate/install b/spec/approvals/cli/generate/install new file mode 100644 index 0000000..0a7dd76 --- /dev/null +++ b/spec/approvals/cli/generate/install @@ -0,0 +1,2 @@ +Saved stubbed target_path +You may need to restart your session to test it diff --git a/spec/completely/commands/generate_spec.rb b/spec/completely/commands/generate_spec.rb index 748c037..fb6a198 100644 --- a/spec/completely/commands/generate_spec.rb +++ b/spec/completely/commands/generate_spec.rb @@ -116,7 +116,28 @@ end end - context 'with --wrapper NAME' do + context 'with --install PROGRAM' do + let(:mock_installer) do + instance_double Installer, + install: true, + install_command_string: 'stubbed install_command_string', + target_path: 'stubbed target_path' + end + + it 'passes the generated script to the installer' do + allow(Installer).to receive(:from_string) + .with( + program: 'mycli', + string: a_string_matching(/bash completions script/) + ).and_return(mock_installer) + + expect(mock_installer).to receive(:install) + + expect { subject.execute %w[generate --install mycli] }.to output_approval('cli/generate/install') + end + end + + context 'with --wrap NAME' do after { system 'rm -f completely.bash' } it 'wraps the script in a function' do