33module Completely
44 module Commands
55 class Install < Base
6- TARGETS = %W[
7- /usr/share/bash-completion/completions
8- /usr/local/etc/bash_completion.d
9- #{ Dir . home } /.bash_completion.d
10- ]
11-
126 summary 'Install a bash completion script'
137
148 help <<~HELP
15- This command will copy the specified file to one of the following directories:
16-
17- #{ TARGETS . map { |c | " - #{ c } " } . join "\n " }
18-
9+ This command will copy the specified file to one of the bash completion directories.
1910 The target filename will be the program name, and sudo will be used if necessary.
2011 HELP
2112
@@ -29,68 +20,27 @@ class Install < Base
2920 param 'SCRIPT_PATH' , 'Path to the source bash script [default: completely.bash].'
3021
3122 def run
32- bounce
33-
3423 if args [ '--dry' ]
35- puts command . join ' '
24+ puts installer . command_string
3625 return
3726 end
3827
39- success = system ( * command )
40- raise "Failed running command:\n nb`#{ command . join ' ' } `" unless success
28+ success = installer . install force : args [ '--force' ]
29+ raise "Failed running command:\n nb`#{ installer . command_string } `" unless success
4130
42- say "Saved m`#{ target_path } `"
31+ say "Saved m`#{ installer . target_path } `"
4332 say 'You may need to restart your session to test it'
4433 end
4534
4635 private
4736
48- def bounce
49- unless completions_path
50- raise 'Cannot determine system completions directory'
51- end
52-
53- unless File . exist? script_path
54- raise "Cannot find script: m`#{ script_path } `"
55- end
56-
57- if target_exist? && !args [ '--force' ]
58- raise "File exists: m`#{ target_path } `\n Use nb`--force` to overwrite"
59- end
60- end
61-
62- def target_exist?
63- File . exist? target_path
64- end
65-
66- def command
67- result = root? ? [ ] : %w[ sudo ]
68- result + %W[ cp #{ script_path } #{ target_path } ]
37+ def installer
38+ Installer . new program : args [ 'PROGRAM' ] , script_path : script_path
6939 end
7040
7141 def script_path
7242 args [ 'SCRIPT_PATH' ] || 'completely.bash'
7343 end
74-
75- def target_path
76- "#{ completions_path } /#{ args [ 'PROGRAM' ] } "
77- end
78-
79- def root?
80- Process . uid . zero?
81- end
82-
83- def completions_path
84- @completions_path ||= completions_path!
85- end
86-
87- def completions_path!
88- TARGETS . each do |tarnet |
89- return tarnet if Dir . exist? tarnet
90- end
91-
92- nil
93- end
9444 end
9545 end
9646end
0 commit comments