forked from ruby-china/homeland
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.rb
105 lines (91 loc) · 2.24 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env ruby
ROW_SIZE = 80
class String
COLORS = {
:red => "\033[31m",
:green => "\033[32m",
:yellow => "\033[33m",
:blue => "\033[34m"
}
def colorize(color)
"#{COLORS[color]}#{self}\033[0m"
end
end
def puts_section(info, &block)
puts info
puts "-"*ROW_SIZE
yield block
puts "-"*ROW_SIZE
puts ""
end
def puts_line(info, &block)
print info
rsize = ROW_SIZE - info.length
success = yield block
if success == false
puts "[Failed]".rjust(rsize).colorize(:red)
else
puts "[Done]".rjust(rsize).colorize(:green)
end
end
def puts_line_with_yn(info, &block)
print info
rsize = ROW_SIZE - info.length
success = yield block
if success == false
puts "[No]".rjust(rsize).colorize(:red)
else
puts "[Yes]".rjust(rsize).colorize(:green)
end
end
def replace_file(file_name, from, to)
File.open(file_name, "r+") do |f|
out = ""
f.each do |line|
out << line.gsub(from, to)
end
f.pos = 0
f.print out
f.truncate(f.pos)
end
end
puts "Now Installing Ruby China..."
puts "="*ROW_SIZE
puts ""
puts_section "Checking Package Dependencies..." do
pkg_exist = true
[["bundle","Bundler"],["mongod","MongoDB 2.0+"],["redis-server","Redis 2.0+"],["memcached","Memcached 1.4+"],["convert","ImageMagick 6.5+"]].each do |item|
puts_line_with_yn item[1] do
if `which #{item[0]}` == ""
pkg_exist = false
false
else
true
end
end
end
exit(0) if pkg_exist == false
end
# Config files
puts_section "Configure" do
%w(config mongoid redis thin).each do |fname|
`cp config/#{fname}.yml.default config/#{fname}.yml`
end
print "Your MongoDB host (default: 127.0.0.1:27017):"
host = gets.strip
host = "127.0.0.1:27017" if host == ""
replace_file('config/mongoid.yml','SETUP_DEVELOPMENT_HOST',host)
print "Your Redis host (default: 127.0.0.1:6379):"
host = gets.strip
host = "127.0.0.1:6379" if host == ""
replace_file('config/redis.yml','SETUP_REDIS_HOST',host.split(":")[0])
replace_file('config/redis.yml','SETUP_REDIS_PORT',host.split(":")[1])
end
puts_line "Install gems..." do
`bundle install`
end
puts_line "Seed default data..." do
`bundle exec rake db:seed`
end
puts ""
puts "Ruby China Successfully Installed."