Class: Completely::Installer

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Installer.



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

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

Instance Attribute Details

#programObject (readonly)

Returns the value of attribute program.



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

def program
  @program
end

#script_pathObject (readonly)

Returns the value of attribute script_path.



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

def script_path
  @script_path
end

Class Method Details

.create_tempfileObject



25
26
27
28
29
# File 'lib/completely/installer.rb', line 25

def create_tempfile
  tempfile = Tempfile.new ['completely-', '.bash']
  tempfiles.push tempfile
  tempfile
end

.from_io(program:, io: nil) ⇒ Object

Raises:



4
5
6
7
8
9
10
11
# File 'lib/completely/installer.rb', line 4

def from_io(program:, io: nil)
  io ||= $stdin

  raise InstallError, 'io must respond to #read' unless io.respond_to?(:read)
  raise InstallError, 'io is closed' if io.respond_to?(:closed?) && io.closed?

  from_string program:, string: io.read
end

.from_string(program:, string:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/completely/installer.rb', line 13

def from_string(program:, string:)
  tempfile = create_tempfile
  script_path = tempfile.path
  begin
    File.write script_path, string
  ensure
    tempfile.close
  end

  new program:, script_path:
end

.tempfilesObject



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

def tempfiles = @tempfiles ||= []

Instance Method Details

#install(force: false) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/completely/installer.rb', line 72

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



50
51
52
53
# File 'lib/completely/installer.rb', line 50

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

#install_command_stringObject



55
56
57
# File 'lib/completely/installer.rb', line 55

def install_command_string
  install_command.join ' '
end

#target_directoriesObject



41
42
43
44
45
46
47
48
# File 'lib/completely/installer.rb', line 41

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



68
69
70
# File 'lib/completely/installer.rb', line 68

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

#uninstallObject



88
89
90
# File 'lib/completely/installer.rb', line 88

def uninstall
  system(*uninstall_command)
end

#uninstall_commandObject



59
60
61
62
# File 'lib/completely/installer.rb', line 59

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

#uninstall_command_stringObject



64
65
66
# File 'lib/completely/installer.rb', line 64

def uninstall_command_string
  uninstall_command.join ' '
end