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.



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

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

Instance Attribute Details

#programObject (readonly)

Returns the value of attribute program.



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

def program
  @program
end

#script_pathObject (readonly)

Returns the value of attribute script_path.



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

def script_path
  @script_path
end

Class Method Details

.create_tempfileObject



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

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

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

Raises:



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

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



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

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



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

def tempfiles = @tempfiles ||= []

Instance Method Details

#install(force: false) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/completely/installer.rb', line 63

def install(force: false)
  unless script_exist?
    raise InstallError, "Cannot find script: m`#{script_path}`"
  end

  FileUtils.mkdir_p completions_path

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

  system(*install_command)
end

#install_commandObject



43
44
45
# File 'lib/completely/installer.rb', line 43

def install_command
  %W[cp #{script_path} #{target_path}]
end

#install_command_stringObject



47
48
49
# File 'lib/completely/installer.rb', line 47

def install_command_string
  install_command.join ' '
end

#target_pathObject



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

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

#uninstallObject



77
78
79
# File 'lib/completely/installer.rb', line 77

def uninstall
  system(*uninstall_command)
end

#uninstall_commandObject



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

def uninstall_command
  %W[rm -f #{target_path}]
end

#uninstall_command_stringObject



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

def uninstall_command_string
  uninstall_command.join ' '
end