Class: Completely::Installer

Inherits:
Object
  • Object
show all
Defined in:
lib/completely/installer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(program:, script_path: nil) ⇒ Installer

Returns a new instance of Installer.



5
6
7
8
# File 'lib/completely/installer.rb', line 5

def initialize(program:, script_path: nil)
  @program = program
  @script_path = script_path
end

Instance Attribute Details

#programObject (readonly)

Returns the value of attribute program.



3
4
5
# File 'lib/completely/installer.rb', line 3

def program
  @program
end

#script_pathObject (readonly)

Returns the value of attribute script_path.



3
4
5
# File 'lib/completely/installer.rb', line 3

def script_path
  @script_path
end

Instance Method Details

#install(force: false) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/completely/installer.rb', line 41

def install(force: false)
  unless completions_path
    raise InstallError, 'Cannot determine system completions directory'
  end

  unless script_exist?
    raise InstallError, "Cannot find script: m`#{script_path}`"
  end

  if target_exist? && !force
    raise InstallError, "File exists: m`#{target_path}`"
  end

  system(*install_command)
end

#install_commandObject



19
20
21
22
# File 'lib/completely/installer.rb', line 19

def install_command
  result = root_user? ? [] : %w[sudo]
  result + %W[cp #{script_path} #{target_path}]
end

#install_command_stringObject



24
25
26
# File 'lib/completely/installer.rb', line 24

def install_command_string
  install_command.join ' '
end

#target_directoriesObject



10
11
12
13
14
15
16
17
# File 'lib/completely/installer.rb', line 10

def target_directories
  @target_directories ||= %W[
    /usr/share/bash-completion/completions
    /usr/local/etc/bash_completion.d
    #{Dir.home}/.local/share/bash-completion/completions
    #{Dir.home}/.bash_completion.d
  ]
end

#target_pathObject



37
38
39
# File 'lib/completely/installer.rb', line 37

def target_path
  "#{completions_path}/#{program}"
end

#uninstallObject



57
58
59
# File 'lib/completely/installer.rb', line 57

def uninstall
  system(*uninstall_command)
end

#uninstall_commandObject



28
29
30
31
# File 'lib/completely/installer.rb', line 28

def uninstall_command
  result = root_user? ? [] : %w[sudo]
  result + %w[rm -f] + target_directories.map { |dir| "#{dir}/#{program}" }
end

#uninstall_command_stringObject



33
34
35
# File 'lib/completely/installer.rb', line 33

def uninstall_command_string
  uninstall_command.join ' '
end