-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathRakefile
287 lines (217 loc) · 9.39 KB
/
Rakefile
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
require 'bundler/gem_tasks'
require 'rake/testtask'
Rake::TestTask.new do |t|
t.pattern = "test/**/*_test.rb"
end
task :default => :test
desc "Creates a (test) MySQL database"
task 'db:create:mysql' do
fail "could not create database: mysql executable not found" unless mysql = _which('mysql')
ENV['Rake'] = true.to_s; ENV['AR_ADAPTER'] ||= 'mysql'
require File.expand_path('test/test_helper.rb', File.dirname(__FILE__))
script = "DROP DATABASE IF EXISTS `#{AR_CONFIG[:database]}`;"
script << "CREATE DATABASE `#{AR_CONFIG[:database]}` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_general_ci`;"
if AR_CONFIG[:username]
script << "GRANT ALL PRIVILEGES ON `#{AR_CONFIG[:database]}`.* TO #{AR_CONFIG[:username]}@localhost;"
script << "SET PASSWORD FOR #{AR_CONFIG[:username]}@localhost = PASSWORD('#{AR_CONFIG[:password]}');"
end
params = { '-u' => 'root' }
if ENV['DATABASE_YML']; require 'yaml'
password = YAML.load(File.new(ENV['DATABASE_YML']))["production"]["password"]
params['--password'] = password
end
puts "Creating MySQL database: #{AR_CONFIG[:database]}"
sh "cat #{_sql_script(script).path} | #{mysql} #{params.to_a.join(' ')}", :verbose => $VERBOSE
# puts "... run tests with MySQL using: `AR_ADAPTER=mysql rake test `"
end
task 'db:drop:mysql' do
fail "could not create database: mysql executable not found" unless mysql = _which('mysql')
ENV['Rake'] = true.to_s; ENV['AR_ADAPTER'] ||= 'mysql'
require File.expand_path('test/test_helper.rb', File.dirname(__FILE__))
script = "DROP DATABASE `#{AR_CONFIG[:database]}`;"
params = { '-u' => 'root' }
if ENV['DATABASE_YML']; require 'yaml'
password = YAML.load(File.new(ENV['DATABASE_YML']))["production"]["password"]
params['--password'] = password
end
puts "Creating MySQL database: #{AR_CONFIG[:database]}"
sh "cat #{_sql_script(script).path} | #{mysql} #{params.to_a.join(' ')}", :verbose => $VERBOSE
end
desc "Creates a (test) PostgreSQL database"
task 'db:create:postgresql' do
fail 'could not create database: psql executable not found' unless psql = _which('psql')
ENV['Rake'] = true.to_s; ENV['AR_ADAPTER'] ||= 'postgresql'
require File.expand_path('test/test_helper.rb', File.dirname(__FILE__))
script = "DROP DATABASE IF EXISTS #{AR_CONFIG[:database]};"
if pg_user = AR_CONFIG[:username] || ENV['PGUSER'] || ENV_JAVA['user.name']
script << "DROP USER IF EXISTS #{pg_user};"
pg_password = AR_CONFIG[:password] || ENV['PGPASSWORD']
script << "CREATE USER #{pg_user} CREATEDB SUPERUSER LOGIN PASSWORD '#{pg_password}';"
end
script << "CREATE DATABASE #{AR_CONFIG[:database]} OWNER #{pg_user || 'postgres'};"
psql_params = "-U #{ENV['PSQL_USER'] || 'postgres'}"
psql_params << " -h #{ENV['PGHOST']}" if ENV['PGHOST']
psql_params << " -p #{ENV['PGPORT']}" if ENV['PGPORT']
psql_params << " -q " unless $VERBOSE
puts "Creating PostgreSQL database: #{AR_CONFIG[:database]}"
sh "cat #{_sql_script(script).path} | #{psql} #{psql_params}", :verbose => $VERBOSE
# puts "... run tests with PostgreSQL using: `AR_ADAPTER=postgresql rake test `"
end
def _sql_script(content, name = '_sql_script')
require 'tempfile'
script = Tempfile.new(name)
script.puts content
yield(script) if block_given?
script.close
at_exit { script.unlink }
script
end
def _which(cmd)
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
( ENV['PATH'] || '' ).split(File::PATH_SEPARATOR).each do |path|
exts.each do |ext|
exe = File.join(path, "#{cmd}#{ext}")
return exe if File.executable? exe
end
end
nil
end
def _download(uri, download_dir, as_file_name)
require 'open-uri'; require 'tmpdir'
temp_dir = File.join(Dir.tmpdir, (Time.now.to_f * 1000).to_i.to_s)
FileUtils.mkdir temp_dir
Dir.chdir(temp_dir) do
FileUtils.mkdir download_dir unless File.exist?(download_dir)
puts "downloading #{uri}"
file = open(uri)
FileUtils.cp file.path, File.join(download_dir, as_file_name)
end
FileUtils.rm_r temp_dir
end
MAVEN_BASE_URL = 'https://repo.maven.apache.org/maven2'
namespace :tomcat do
tomcat_maven_repo = "#{MAVEN_BASE_URL}/org/apache/tomcat"
download_dir = File.expand_path('test/jars', File.dirname(__FILE__))
version_default = '7.0.64'
[ 'tomcat-jdbc', 'tomcat-dbcp' ].each do |tomcat_pool|
namespace tomcat_pool.sub('tomcat-', '') do # rake tomcat:dbcp:download
tomcat_pool_jar = "#{tomcat_pool}.jar"
task :download, :version do |_,args| # rake tomcat:jdbc:download[7.0.64]
env_key = "#{tomcat_pool.upcase.sub('-', '_')}_VERSION" # 'TOMCAT_JDBC_VERSION'
version = args[:version] || ENV[env_key] || version_default
uri = "#{tomcat_maven_repo}/#{tomcat_pool}/#{version}/#{tomcat_pool}-#{version}.jar"
_download(uri, download_dir, tomcat_pool_jar)
end
task :check do
jar_path = File.join(download_dir, tomcat_pool_jar)
unless File.exist?(jar_path)
Rake::Task["#{tomcat_pool.sub('-',':')}:download"].invoke
end
end
task :clear do
jar_path = File.join(download_dir, tomcat_pool_jar)
rm jar_path if File.exist?(jar_path)
end
end
end
namespace 'jndi' do # contains a FS JNDI impl (for tests)
catalina_jar = 'tomcat-catalina.jar'; juli_jar = 'tomcat-juli.jar'
task :download, :version do |_,args|
version = args[:version] || version_default
catalina_uri = "#{tomcat_maven_repo}/tomcat-catalina/#{version}/tomcat-catalina-#{version}.jar"
juli_uri = "#{tomcat_maven_repo}/tomcat-juli/#{version}/tomcat-juli-#{version}.jar"
require 'open-uri'; require 'tmpdir'
temp_dir = File.join(Dir.tmpdir, (Time.now.to_f * 1000).to_i.to_s)
FileUtils.mkdir temp_dir
downloads = Hash.new
downloads[catalina_jar] = catalina_uri
downloads[juli_jar] = juli_uri
Dir.chdir(temp_dir) do
FileUtils.mkdir download_dir unless File.exist?(download_dir)
downloads.each do |jar, uri|
puts "downloading #{uri}"
file = open(uri)
FileUtils.cp file.path, File.join(download_dir, jar)
end
end
FileUtils.rm_r temp_dir
end
task :check do
jar_path = File.join(download_dir, catalina_jar)
unless File.exist?(jar_path)
Rake::Task['tomcat:jndi:download'].invoke
end
end
task :clear do
jar_path = File.join(download_dir, catalina_jar)
rm jar_path if File.exist?(jar_path)
jar_path = File.join(download_dir, juli_jar)
rm jar_path if File.exist?(jar_path)
end
end
end
namespace :dbcp do # a.k.a DBCP2
commons_repo = "#{MAVEN_BASE_URL}/org/apache/commons"
download_dir = File.expand_path('test/jars', File.dirname(__FILE__))
version_default = '2.0.1'
task :download, :version do |_,args| # rake dbcp:download
version = args[:version] || ENV['DBCP_VERSION'] || version_default
dbcp_jar = "commons-dbcp2-#{version}.jar"
uri = "#{commons_repo}/commons-dbcp2/#{version}/#{dbcp_jar}"
_download(uri, download_dir, dbcp_jar)
end
task :clear do
Dir.glob( File.join(download_dir, '{commons-dbcp2}*.jar') ).each { |jar| rm jar }
end
end
namespace :c3p0 do
mchange_base_repo = "#{MAVEN_BASE_URL}/com/mchange"
download_dir = File.expand_path('test/jars', File.dirname(__FILE__))
c3p0_version = '0.9.5'
mchange_commons_version = '0.2.9'
mchange_commons_jar = "mchange-commons-java-#{mchange_commons_version}.jar"
task :download, :version do |_,args| # rake c3p0:download
version = args[:version] || ENV['C3P0_VERSION'] || c3p0_version
c3p0_jar = "c3p0-#{version}.jar"
uri = "#{mchange_base_repo}/mchange-commons-java/#{mchange_commons_version}/#{mchange_commons_jar}"
_download(uri, download_dir, mchange_commons_jar)
uri = "#{mchange_base_repo}/c3p0/#{version}/#{c3p0_jar}"
_download(uri, download_dir, c3p0_jar)
end
task :clear do
Dir.glob( File.join(download_dir, '{c3p0,mchange-commons}*.jar') ).each { |jar| rm jar }
end
end
namespace :hikari do
hikari_base_repo = "#{MAVEN_BASE_URL}/com/zaxxer"
slf4j_base_repo = "#{MAVEN_BASE_URL}/org/slf4j"
javassist_base_repo = "#{MAVEN_BASE_URL}/org/javassist"
download_dir = File.expand_path('test/jars', File.dirname(__FILE__))
hikari_version = '1.4.0'
slf4j_version = '1.7.10'
javassist_version = '3.18.2-GA'
slf4j_api_jar = "slf4j-api-#{slf4j_version}.jar"
slf4j_simple_jar = "slf4j-simple-#{slf4j_version}.jar"
javassist_jar = "javassist-#{javassist_version}.jar"
task :download, :version do |_,args| # rake c3p0:download
version = args[:version] || ENV['HIKARI_VERSION'] || hikari_version
version = version.dup
if version.sub!('-java6', '') # e.g. '2.3.2-java6'
hikari_jar = "HikariCP-java6-#{version}.jar"
uri = "#{hikari_base_repo}/HikariCP-java6/#{version}/#{hikari_jar}"
else
hikari_jar = "HikariCP-#{version}.jar"
uri = "#{hikari_base_repo}/HikariCP/#{version}/#{hikari_jar}"
end
_download(uri, download_dir, hikari_jar)
uri = "#{slf4j_base_repo}/slf4j-api/#{slf4j_version}/#{slf4j_api_jar}"
_download(uri, download_dir, slf4j_api_jar)
uri = "#{slf4j_base_repo}/slf4j-simple/#{slf4j_version}/#{slf4j_simple_jar}"
_download(uri, download_dir, slf4j_simple_jar)
uri = "#{javassist_base_repo}/javassist/#{javassist_version}/#{javassist_jar}"
_download(uri, download_dir, javassist_jar)
end
task :clear do
Dir.glob( File.join(download_dir, '{HikariCP,slf4j,javassist}*.jar') ).each { |jar| rm jar }
end
end