Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions lib/completely/commands/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions spec/approvals/cli/generate/help
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down
2 changes: 2 additions & 0 deletions spec/approvals/cli/generate/install
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Saved stubbed target_path
You may need to restart your session to test it
23 changes: 22 additions & 1 deletion spec/completely/commands/generate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down