forked from petejkim/autoparts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.rb
64 lines (53 loc) · 2.01 KB
/
setup.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Copyright (c) 2013-2014 Irrational Industries Inc. d.b.a. Nitrous.IO
# This software is licensed under the [BSD 2-Clause license](https://raw.github.com/nitrous-io/autoparts/master/LICENSE).
require 'pathname'
HOME_PATH = Pathname.new(Dir.home)
AUTOPARTS_ROOT_PATH = HOME_PATH + '.parts'
AUTOPARTS_APP_PATH = AUTOPARTS_ROOT_PATH + 'autoparts'
class ExecutionFailedError < StandardError
def initialize(cmd)
super("\"#{cmd}\" failed")
end
end
def execute(*args)
args = args.map(&:to_s)
unless system(*args)
raise ExecutionFailedError.new args.join(' ')
end
end
def inject_parts_init(path)
puts "=> Injecting init script into ~/#{path.basename}"
relative_autoparts_bin_path = AUTOPARTS_APP_PATH.relative_path_from(HOME_PATH) + 'bin'
file = File.read(path)
File.open(path, 'a') do |f|
export_path = "export PATH=\"$HOME/#{relative_autoparts_bin_path}:$PATH\"\n"
parts_init = "eval \"$(parts env)\"\n"
f.write "\n"
f.write export_path unless file.include? export_path
f.write parts_init unless file.include? parts_init
end
end
if AUTOPARTS_APP_PATH.exist? && AUTOPARTS_APP_PATH.children.any?
abort "setup: It appears that Autoparts is already installed on your box. If you want to reinstall Autoparts, please make sure that your \"#{AUTOPARTS_APP_PATH}\" directory is empty."
end
begin
AUTOPARTS_ROOT_PATH.mkpath
puts "=> Downloading Autoparts..."
execute 'git', 'clone', 'https://github.com/action-io/autoparts.git', AUTOPARTS_APP_PATH
bash_profile_path = HOME_PATH + '.bash_profile'
bashrc_path = HOME_PATH + '.bashrc'
zshrc_path = HOME_PATH + '.zshrc'
if bash_profile_path.exist?
inject_parts_init(bash_profile_path)
elsif bashrc_path.exist?
inject_parts_init(bashrc_path)
end
if zshrc_path.exist?
inject_parts_init(zshrc_path)
end
puts "=> Installation complete!"
puts "\nPlease reopen this shell or enter the following command:\n exec $SHELL -l"
rescue => e
AUTOPARTS_APP_PATH.rmtree if AUTOPARTS_APP_PATH.exist?
abort "setup: ERROR: #{e}\nAborting!"
end