Skip to content

Commit 77d22cd

Browse files
authored
Merge pull request #51 from DannyBen/add/installer-model
Refactor installer
2 parents 78245ac + 6f84312 commit 77d22cd

32 files changed

+234
-164
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
runs-on: ubuntu-latest
1111

1212
strategy:
13-
matrix: { ruby: ['2.7', '3.0', '3.1', '3.2', head] }
13+
matrix: { ruby: ['3.0', '3.1', '3.2', head] }
1414

1515
steps:
1616
- name: Checkout code

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
/completely.bash
44
/completely.yaml
55
/coverage
6+
/debug.runfile
67
/dev
78
/doc
89
/Gemfile.lock
910
/gems
11+
/out.sh
12+
/spec/status.txt
1013
/spec/tmp
11-
/debug.runfile
1214
/tmp
13-
out.sh

.rubocop.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ inherit_gem:
88
- rspec.yml
99

1010
AllCops:
11-
TargetRubyVersion: 2.7
11+
TargetRubyVersion: 3.0
1212
Exclude:
1313
- 'spec/approvals/**/*'
1414
- 'debug.rb'
@@ -18,8 +18,3 @@ AllCops:
1818
Naming/AccessorMethodName:
1919
Exclude:
2020
- 'lib/completely/tester.rb'
21-
22-
# The `bounce` method here is more readable without this cop
23-
Style/GuardClause:
24-
Exclude:
25-
- 'lib/completely/commands/install.rb'

completely.gemspec

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@ Gem::Specification.new do |s|
1111
s.email = '[email protected]'
1212
s.files = Dir['README.md', 'lib/**/*.*']
1313
s.executables = ['completely']
14-
s.homepage = 'https://github.com/dannyben/completely'
14+
s.homepage = 'https://github.com/DannyBen/completely'
1515
s.license = 'MIT'
1616

17-
s.required_ruby_version = '>= 2.7.0'
17+
s.required_ruby_version = '>= 3.0'
1818

1919
s.add_runtime_dependency 'colsole', '>= 0.8.1', '< 2'
2020
s.add_runtime_dependency 'mister_bin', '~> 0.7'
2121

22-
s.metadata['rubygems_mfa_required'] = 'true'
22+
s.metadata = {
23+
'bug_tracker_uri' => 'https://github.com/DannyBen/completely/issues',
24+
'changelog_uri' => 'https://github.com/DannyBen/completely/blob/master/CHANGELOG.md',
25+
'source_code_uri' => 'https://github.com/DannyBen/completely',
26+
'rubygems_mfa_required' => 'true',
27+
}
2328
end

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+
puts 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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 command
19+
result = root_user? ? [] : %w[sudo]
20+
result + %W[cp #{script_path} #{target_path}]
21+
end
22+
23+
def command_string
24+
command.join ' '
25+
end
26+
27+
def target_path
28+
"#{completions_path}/#{program}"
29+
end
30+
31+
def install(force: false)
32+
unless completions_path
33+
raise 'Cannot determine system completions directory'
34+
end
35+
36+
unless script_exist?
37+
raise "Cannot find script: m`#{script_path}`"
38+
end
39+
40+
if target_exist? && !force
41+
raise "File exists: m`#{target_path}`"
42+
end
43+
44+
system(*command)
45+
end
46+
47+
private
48+
49+
def target_exist?
50+
File.exist? target_path
51+
end
52+
53+
def script_exist?
54+
File.exist? script_path
55+
end
56+
57+
def root_user?
58+
Process.uid.zero?
59+
end
60+
61+
def completions_path
62+
@completions_path ||= completions_path!
63+
end
64+
65+
def completions_path!
66+
target_directories.each do |target|
67+
return target if Dir.exist? target
68+
end
69+
70+
nil
71+
end
72+
end
73+
end

spec/approvals/cli/install/dry

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sudo cp README.md /usr/share/bash-completion/completions/completely-test
1+
sudo cp completely.bash /usr/share/bash-completion/completions/completely-test

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/approvals/cli/install/install

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

0 commit comments

Comments
 (0)