Skip to content

Commit 898962c

Browse files
authored
Merge pull request #90 from bashly-framework/add/generate-and-install
Add support for `generate --install`
2 parents 85615b7 + b6a006b commit 898962c

File tree

4 files changed

+54
-6
lines changed

4 files changed

+54
-6
lines changed

lib/completely/commands/generate.rb

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ class Generate < Base
66
help 'Generate the bash completion script to file or stdout'
77

88
usage 'completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]'
9+
usage 'completely generate [CONFIG_PATH --install PROGRAM --function NAME]'
910
usage 'completely generate (-h|--help)'
1011

1112
option_function
1213
option '-w --wrap NAME', 'Wrap the completion script inside a function that echos the ' \
1314
'script. This is useful if you wish to embed it directly in your script.'
1415

16+
option '-i --install PROGRAM', 'Install the generated script as completions for PROGRAM.'
17+
1518
param 'CONFIG_PATH', <<~USAGE
1619
Path to the YAML configuration file [default: completely.yaml].
1720
Use '-' to read from stdin.
@@ -35,17 +38,35 @@ class Generate < Base
3538
def run
3639
wrap = args['--wrap']
3740
output = wrap ? wrapper_function(wrap) : script
38-
if output_path == '-'
39-
puts output
41+
42+
if args['--install']
43+
install output
44+
elsif output_path == '-'
45+
show output
4046
else
41-
File.write output_path, output
42-
say "Saved m`#{output_path}`"
47+
save output
4348
end
44-
syntax_warning unless completions.valid?
4549
end
4650

4751
private
4852

53+
def install(content)
54+
installer = Installer.from_string program: args['--install'], string: content
55+
success = installer.install force: true
56+
raise InstallError, "Failed running command:\nnb`#{installer.install_command_string}`" unless success
57+
58+
say "Saved m`#{installer.target_path}`"
59+
say 'You may need to restart your session to test it'
60+
end
61+
62+
def show(content) = puts content
63+
64+
def save(content)
65+
File.write output_path, content
66+
say "Saved m`#{output_path}`"
67+
syntax_warning unless completions.valid?
68+
end
69+
4970
def wrapper_function(wrapper_name)
5071
completions.wrapper_function wrapper_name
5172
end

spec/approvals/cli/generate/help

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Generate the bash completion script to file or stdout
22

33
Usage:
44
completely generate [CONFIG_PATH OUTPUT_PATH --function NAME --wrap NAME]
5+
completely generate [CONFIG_PATH --install PROGRAM --function NAME]
56
completely generate (-h|--help)
67

78
Options:
@@ -12,6 +13,9 @@ Options:
1213
Wrap the completion script inside a function that echos the script. This is
1314
useful if you wish to embed it directly in your script.
1415

16+
-i --install PROGRAM
17+
Install the generated script as completions for PROGRAM.
18+
1519
-h --help
1620
Show this help
1721

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Saved stubbed target_path
2+
You may need to restart your session to test it

spec/completely/commands/generate_spec.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,28 @@
116116
end
117117
end
118118

119-
context 'with --wrapper NAME' do
119+
context 'with --install PROGRAM' do
120+
let(:mock_installer) do
121+
instance_double Installer,
122+
install: true,
123+
install_command_string: 'stubbed install_command_string',
124+
target_path: 'stubbed target_path'
125+
end
126+
127+
it 'passes the generated script to the installer' do
128+
allow(Installer).to receive(:from_string)
129+
.with(
130+
program: 'mycli',
131+
string: a_string_matching(/bash completions script/)
132+
).and_return(mock_installer)
133+
134+
expect(mock_installer).to receive(:install)
135+
136+
expect { subject.execute %w[generate --install mycli] }.to output_approval('cli/generate/install')
137+
end
138+
end
139+
140+
context 'with --wrap NAME' do
120141
after { system 'rm -f completely.bash' }
121142

122143
it 'wraps the script in a function' do

0 commit comments

Comments
 (0)