Skip to content

Commit b1341fa

Browse files
committed
- Refactor install command and add an Installer model
1 parent 78245ac commit b1341fa

File tree

5 files changed

+81
-64
lines changed

5 files changed

+81
-64
lines changed

lib/completely.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
require 'completely/pattern'
77
require 'completely/completions'
88
require 'completely/tester'
9+
require 'completely/installer'

lib/completely/commands/install.rb

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,10 @@
33
module 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+
installer.command_string
3625
return
3726
end
3827

39-
success = system(*command)
40-
raise "Failed running command:\nnb`#{command.join ' '}`" unless success
28+
success = installer.install force: args['--force']
29+
raise "Failed running command:\nnb`#{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}`\nUse 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
9646
end

lib/completely/installer.rb

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
module Completely
2+
class Installer
3+
attr_reader :program, :script_path
4+
5+
def initialize(program:, script_path: nil)
6+
@program = program
7+
@script_path = script_path
8+
end
9+
10+
def target_directories
11+
@target_directories ||= %W[
12+
/usr/share/bash-completion/completions
13+
/usr/local/etc/bash_completion.d
14+
#{Dir.home}/.bash_completion.d
15+
]
16+
end
17+
18+
def install(force: false)
19+
unless completions_path
20+
raise 'Cannot determine system completions directory'
21+
end
22+
23+
unless File.exist? script_path
24+
raise "Cannot find script: m`#{script_path}`"
25+
end
26+
27+
if target_exist? && !force
28+
raise "File exists: m`#{target_path}`"
29+
end
30+
31+
system(*command)
32+
end
33+
34+
def command
35+
result = root? ? [] : %w[sudo]
36+
result + %W[cp #{script_path} #{target_path}]
37+
end
38+
39+
def command_string
40+
command.join ' '
41+
end
42+
43+
def target_path
44+
"#{completions_path}/#{program}"
45+
end
46+
47+
private
48+
49+
def target_exist?
50+
File.exist? target_path
51+
end
52+
53+
def root?
54+
Process.uid.zero?
55+
end
56+
57+
def completions_path
58+
@completions_path ||= completions_path!
59+
end
60+
61+
def completions_path!
62+
target_directories.each do |target|
63+
return target if Dir.exist? target
64+
end
65+
66+
nil
67+
end
68+
end
69+
end

spec/approvals/cli/install/help

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
Install a bash completion script
22

3-
This command will copy the specified file to one of the following directories:
4-
5-
- /usr/share/bash-completion/completions
6-
- /usr/local/etc/bash_completion.d
7-
- /home/vagrant/.bash_completion.d
8-
3+
This command will copy the specified file to one of the bash completion
4+
directories.
95
The target filename will be the program name, and sudo will be used if
106
necessary.
117

spec/completely/commands/install_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@
4040
File.write 'spec/tmp/completely.bash', 'not-important'
4141
end
4242

43-
it 'copies the script' do
43+
it 'copies the script', :focus do
4444
Dir.chdir 'spec/tmp' do
4545
allow(subject).to receive(:system).with(*expected_args).and_return true
46+
subject.execute %w[install completely-test]
4647
expect { subject.execute %w[install completely-test] }
4748
.to output_approval('cli/install/install-default')
4849
end

0 commit comments

Comments
 (0)