Skip to content

Commit 3f4b1a3

Browse files
committed
- Add bashly completions --install command
1 parent a3b2d38 commit 3f4b1a3

File tree

7 files changed

+99
-10
lines changed

7 files changed

+99
-10
lines changed

lib/bashly/cli.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ def self.runner
99
header: 'Bashly - Bash CLI Generator',
1010
footer: "Help: m`bashly COMMAND --help`\nDocs: bu`https://bashly.dannyb.co`"
1111

12-
runner.route 'init', to: Commands::Init
13-
runner.route 'preview', to: Commands::Preview
14-
runner.route 'validate', to: Commands::Validate
15-
runner.route 'generate', to: Commands::Generate
16-
runner.route 'add', to: Commands::Add
17-
runner.route 'doc', to: Commands::Doc
18-
runner.route 'shell', to: Commands::Shell unless ENV['BASHLY_SHELL']
12+
runner.route 'init', to: Commands::Init
13+
runner.route 'preview', to: Commands::Preview
14+
runner.route 'validate', to: Commands::Validate
15+
runner.route 'generate', to: Commands::Generate
16+
runner.route 'add', to: Commands::Add
17+
runner.route 'doc', to: Commands::Doc
18+
runner.route 'completions', to: Commands::Completions
19+
runner.route 'shell', to: Commands::Shell unless ENV['BASHLY_SHELL']
1920

2021
runner
2122
end

lib/bashly/commands/completions.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
module Bashly
2+
module Commands
3+
class Completions < Base
4+
summary 'Install bash completions for bashly itself'
5+
help 'Display the bash completions script or install it directly to your bash completions directory'
6+
7+
usage 'bashly completions [--install]'
8+
usage 'bashly completions (-h|--help)'
9+
10+
option '-i --install', 'Install the completions script to your bash completions directory'
11+
12+
def run
13+
if args['--install']
14+
install_completions
15+
else
16+
show_completions
17+
end
18+
end
19+
20+
private
21+
22+
def install_completions
23+
raise Error, "Cannot find completions directory" unless compdir
24+
25+
target = "#{compdir}/bashly"
26+
27+
say "Installing completions to m`#{target}`"
28+
command = %Q[cp "#{completions_path}" "#{target}"]
29+
command = "sudo #{command}" unless root_user?
30+
system command
31+
32+
say 'Restart your session for the changes to take effect.'
33+
end
34+
35+
def show_completions
36+
puts completions_script
37+
end
38+
39+
def completions_path
40+
@completions_path ||= asset('completions/bashly-completions.bash')
41+
end
42+
43+
def completions_script
44+
@completions_script ||= asset_content('completions/bashly-completions.bash')
45+
end
46+
47+
def compdir
48+
@compdir ||= begin
49+
candidates = [
50+
'/usr/share/bash-completion/completions',
51+
'/usr/local/etc/bash_completion.d'
52+
]
53+
54+
candidates.each { |dir| return dir if Dir.exist? dir }
55+
nil
56+
end
57+
end
58+
59+
def root_user?
60+
Process.uid == 0
61+
end
62+
end
63+
64+
end
65+
end
File renamed without changes.

support/completions/bashly-completions.bash renamed to lib/bashly/completions/bashly-completions.bash

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ _bashly_completions() {
3636
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "development production")" -- "$cur" )
3737
;;
3838

39+
'completions'*)
40+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "--help -h --install -i")" -- "$cur" )
41+
;;
42+
3943
'validate'*)
4044
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "--help -h --verbose -v")" -- "$cur" )
4145
;;
@@ -92,12 +96,16 @@ _bashly_completions() {
9296
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "--help -h --force --list --source -f -l -s colors completions completions_script completions_yaml config help hooks lib settings strings test validations yaml")" -- "$cur" )
9397
;;
9498

99+
'c'*)
100+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "--help -h --install -i")" -- "$cur" )
101+
;;
102+
95103
's'*)
96104
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "--help -h")" -- "$cur" )
97105
;;
98106

99107
*)
100-
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "--help -h --version -v init preview validate generate add doc shell")" -- "$cur" )
108+
while read -r; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "$(_bashly_completions_filter "--help -h --version -v init preview validate generate add doc completions shell")" -- "$cur" )
101109
;;
102110

103111
esac
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ bashly:
99
- generate
1010
- add
1111
- doc
12+
- completions
1213
- shell
1314

1415
bashly init: &init
@@ -138,6 +139,13 @@ bashly doc: &doc
138139
- flag.required
139140
- flag.short
140141
- flag.validate
142+
bashly completions: &completions
143+
- --help
144+
- -h
145+
- --install
146+
- -i
147+
148+
bashly c: *completions
141149
bashly shell: &shell
142150
- --help
143151
- -h

support/completions/completely.yaml.gtx renamed to lib/bashly/completions/completely.yaml.gtx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ docs.each do |doc|
7373
= "- #{doc}"
7474
end
7575

76+
> bashly completions: &completions
77+
= help_flags
78+
> - --install
79+
> - -i
80+
>
81+
> bashly c: *completions
82+
7683
> bashly shell: &shell
7784
= help_flags
7885
>

support/runfile/completions.runfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ require 'bashly'
33
summary 'Generate bash completions for bashly itself'
44

55
action do
6-
completely_yaml_oath = 'support/completions/completely.yaml'
7-
completions_script_path = 'support/completions/bashly-completions.bash'
6+
completely_yaml_oath = 'lib/bashly/completions/completely.yaml'
7+
completions_script_path = 'lib/bashly/completions/bashly-completions.bash'
88
gtx_path = "#{completely_yaml_oath}.gtx"
99
template = File.read gtx_path
1010

0 commit comments

Comments
 (0)