Class: Envo::Cli::InstallerBash

Inherits:
Object
  • Object
show all
Defined in:
lib/envo/cli/installer_bash.rb

Constant Summary collapse

ENVO_INSTALLATION_BEGIN =

def detect_installed_envo?()

stdout, stderr, code = Open3.capture3("bash -ic 'command -v envo'")
code.success?

end

'### BEGIN envo installation (don\'t remove line)'
ENVO_INSTALLATION_END =
'### END envo installation (don\'t remove line)'
SOURCE_FILE =
'envo.sh'
DEFAULT_DOTFILE =
File.join(Dir.home, '.bashrc')
USAGE =
<<~EOF
  usage: envo-install [u] [--dotfile <path>]
EOF

Instance Method Summary collapse

Instance Method Details

#find_existing_installation_data(dotfile) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/envo/cli/installer_bash.rb', line 14

def find_existing_installation_data(dotfile)
  return nil if !File.exist?(dotfile)

  raise Envo::Error.new "'#{dotfile}' exists but is not a file. You need to choose a file." if !File.file?(dotfile)

  lines = File.readlines(dotfile)
  first = nil
  last = nil
  lines.each_with_index do |l, i|
    lc = l.chomp
    if lc == ENVO_INSTALLATION_BEGIN
      first = i
    elsif lc == ENVO_INSTALLATION_END
      last = i
    end
  end

  return nil if !first && !last

  if !first || !last
    raise Envo::Error.new <<~EOF
      #{dotfile}' contains a broken confy insallation.
      You need to remove it manually
    EOF
  end

  num = last - first + 1
  return {first: first, num: num, lines: lines}
end

#run(argv) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/envo/cli/installer_bash.rb', line 86

def run(argv)
  if argv.empty?
    try_install(DEFAULT_DOTFILE)
    return 0
  end

  if argv[0] == '--help' || argv[0] == '-?'
    puts "installer for envo v#{Envo::VERSION} #{Envo::VERSION_TYPE}"
    puts USAGE
    puts
    puts '                u - uninstall envo'
    puts ' --dotfile <file> - install to or uninstall form a specified dotfile'
    return 0
  end

  if argv[0] == 'u'
    @uninstalling = true
    argv.shift
  end

  dotfile = DEFAULT_DOTFILE
  if !argv.empty?
    arg = argv.shift
    if arg != '--dotfile'
      STDERR.puts "Unknown argument #{arg}"
      STDERR.puts USAGE
      return 1
    end
    if argv.empty?
      STDERR.puts "Missing dotfile path"
      STDERR.puts USAGE
      return 1
    end
    dotfile = argv.shift
  end

  # if detect_installed_envo?
  #   puts "It seems that you already have envo installed"
  #   puts "Do you want to reinstall it? (y/n)"
  # end
  @uninstalling ? try_uninstall(dotfile) : try_install(dotfile)
  return 0
end

#try_install(dotfile) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/envo/cli/installer_bash.rb', line 45

def try_install(dotfile)
  install_lines = [
    ENVO_INSTALLATION_BEGIN,
    "### envo #{VERSION}",
    File.read(File.join(__dir__, SOURCE_FILE)),
    ENVO_INSTALLATION_END
  ]

  found = find_existing_installation_data(dotfile)

  openmode = 'a'
  if found
    lines = found[:lines]
    lines[found[:first], found[:num]] = install_lines
    install_lines = lines
    openmode = 'w'
  end

  File.open(dotfile, openmode) { |f| f.puts install_lines }
  puts <<~EOF
    Sucessfully installed confy to '#{dotfile}'
    Source the file, or restart the bash session if the file is auto-sourced.
  EOF
end

#try_uninstall(dotfile) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/envo/cli/installer_bash.rb', line 70

def try_uninstall(dotfile)
  found = find_existing_installation_data(dotfile)
  if !found || found[:num] == 0
    raise Envo::Error.new "'#{dotfile}' doesn't seem to contain an envo installation"
  end

  lines = found[:lines]
  lines[found[:first], found[:num]] = []
  File.open(dotfile, 'w') { |f| f.puts lines }
  puts "Suncessfully uninstalled confy from '#{dotfile}'"
end