-
Notifications
You must be signed in to change notification settings - Fork 10
/
install.rb
175 lines (135 loc) · 4.34 KB
/
install.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
require "erb"
require "fileutils"
require "yaml"
require "optparse"
abort "Ruby >= 1.9 and < 2.5 is required, you're running #{RUBY_VERSION}" if RUBY_VERSION < "1.9" || RUBY_VERSION >= "2.5.0"
DBNAME = "itmsweb"
RAKE = "ruby bin/padrino rake -e production"
ROOT = File.dirname(__FILE__)
DBDependency = Struct.new(:name, :gem, :adapter, :version) do
def to_gem
s = "gem '#{gem}'"
s << ", '#{version}'" if version
s
end
def adapter
self[:adapter] || gem
end
end
$config = {
:db_name => DBNAME,
:prompt => true
}
$supported_drivers = [
DBDependency.new("SQLite", "sqlite3", nil, "~> 1.3.0"),
DBDependency.new("MySQL", "mysql2", nil, "~> 0.3.18"),
DBDependency.new("PostgreSQL", "postgresql", "pg")
]
$default_driver = $supported_drivers[0]
parser = OptionParser.new do |opts|
opts.banner = "usage: #{File.basename($0)} [--db-driver=name] [--db-user=name] [--db-password=name] [--db-host=name] [--no-prompt]"
opts.on "-h", "--help", "Show this message" do
puts opts
exit
end
opts.on "--[no-]prompt", "Prompt or don't prompt user for config options" do |prompt|
$config[:prompt] = prompt
end
opts.on "--[no-]config", "Do not create a config file" do |config|
$config[:config] = config
end
opts.on "--db-driver=NAME", $supported_drivers.map { |d| d.name.downcase }, "DB driver" do |name|
$config[:db_driver] = $supported_drivers.find { |d| d.name.downcase == name }
end
opts.on "--db-password=NAME", "DB password" do |name|
$config[:db_password] = name
end
opts.on "--db-host=NAME", "DB hostname" do |name|
$config[:db_host] = name
end
opts.on "--db-user=NAME", "DB username" do |name|
$config[:db_user] = name
end
end.parse!
def prompt_for_config
unless $config[:db_driver]
puts "Select your database: "
$supported_drivers.each_with_index { |dep, i| puts "#{i + 1}: #{dep.name}" }
i = gets
exit unless i # EOF
abort "Unknown choice '#{i.chomp}'" unless i =~ /\A\d+\Z/ && $supported_drivers[pos = i.to_i - 1]
$config[:db_driver] = $supported_drivers[pos]
end
return if $config[:db_driver].name == "SQLite"
unless $config[:db_host]
print "Database host [localhost]: "
host = gets
exit unless host
$config[:db_host] = host.chomp if host =~ /\w/
end
%w[user password].each do |opt|
key = :"db_#{opt}"
next if $config[key]
print "Database #{opt}: "
choice = gets
exit unless choice
$config[key] = choice.chomp
end
end
def install
puts "Installing..."
db_driver = $config[:db_driver] || $default_driver
db_config = {
"name" => $config[:db_name],
"adapter" => db_driver.adapter,
"host" => $config[:db_host] || "localhost",
"username" => $config[:db_user],
"password" => $config[:db_password],
}
if db_config["adapter"] == "sqlite3"
path = "#{ROOT}/db"
Dir.mkdir(path) unless File.directory?(path)
db_config["name"] = "#{path}/#{$config[:db_name]}.sqlite3"
db_config["timeout"] = 5000
end
unless $config[:config]
File.open("#{ROOT}/config/itmsweb.yml", "w") do |io|
io.puts YAML.dump("database" => db_config)
end
end
gemfile = "#{ROOT}/Gemfile.#{RUBY_PLATFORM}"
FileUtils.cp("#{ROOT}/Gemfile", gemfile)
# SQLite is in the Gemfile by default
File.open(gemfile, "a") { |io| io.puts db_driver.to_gem }
ENV["BUNDLE_GEMFILE"] = gemfile
commands = [
"gem install bundler -v 1.16.4 --no-rdoc",
# TODO: update these in 0.2.0 to use new Bundler options
"bundle _1.16.4_ install --path vendor/bundle --without=test development --binstubs",
"#{RAKE} ar:setup"
]
commands.each do |cmd|
puts cmd
abort "Installation failed" unless system cmd
end
# Remove unneeded files created by bundle --binstubs
File.delete(*Dir["bin/*"].reject { |path|
%w[padrino itmsweb itmsworker].include?(File.basename(path).sub(/\.\w+\Z/, ""))
})
# iTMSTransporter logs go here by default
FileUtils.mkdir_p("#{ROOT}/var/lib/output")
end
prompt_for_config if $config[:prompt]
install
puts(<<MSG)
Installation successful!
You can now start the website and the queue worker using the following commands:
bin/itmsweb start
bin/itmsworker
Please report any issues at https://github.com/sshaw/itunes_store_transporter_web/issues.
Thanks for using the iTunes Store Transporter: GUI.
http://transportergui.com
Made by ScreenStaring
http://screenstaring.com
---
MSG