diff --git a/lib/completely/commands/generate.rb b/lib/completely/commands/generate.rb index 6fea310..091bd06 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,35 @@ class Generate < Base def run wrap = args['--wrap'] output = wrap ? wrapper_function(wrap) : script - if output_path == '-' - puts output + + if args['--install'] + install output + elsif output_path == '-' + show output else - File.write output_path, output - say "Saved m`#{output_path}`" + save output end - syntax_warning unless completions.valid? 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(content) = puts content + + def save(content) + File.write output_path, content + say "Saved m`#{output_path}`" + syntax_warning unless completions.valid? + end + 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 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