-
Notifications
You must be signed in to change notification settings - Fork 798
/
Rakefile
309 lines (260 loc) · 9.52 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
require 'pathname'
require 'elasticsearch'
subprojects = ['elasticsearch-rails', 'elasticsearch-persistence']
subprojects << 'elasticsearch-model' unless defined?(JRUBY_VERSION)
__current__ = Pathname(File.expand_path(__dir__))
def admin_client
$admin_client ||= begin
transport_options = {}
test_suite = ENV['TEST_SUITE'].freeze
if hosts = ENV['TEST_ES_SERVER'] || ENV['ELASTICSEARCH_HOSTS']
split_hosts = hosts.split(',').map do |host|
/(http\:\/\/)?(\S+)/.match(host)[2]
end
host, port = split_hosts.first.split(':')
end
if test_suite == 'security'
transport_options.merge!(:ssl => { verify: false,
ca_path: defined?(CERT_DIR) ? CERT_DIR : nil
}.compact)
password = ENV['ELASTIC_PASSWORD']
user = ENV['ELASTIC_USER'] || 'elastic'
url = "https://#{user}:#{password}@#{host || 'localhost'}:#{port || 9200}"
else
url = "http://#{host || 'localhost'}:#{port || 9200}"
end
ENV['ELASTICSEARCH_URL'] ||= url
Elasticsearch::Client.new(host: url, transport_options: transport_options)
end
end
task :default do
system 'rake --tasks'
end
desc 'Show subprojects information'
task :subprojects do
puts '-' * 80
subprojects.each do |project|
commit = `git log --pretty=format:'%h %ar: %s' -1 #{project}`
version = Gem::Specification.load(__current__.join(project, "#{project}.gemspec").to_s).version.to_s
puts "[#{version}] \e[1m#{project.ljust(subprojects.map(&:length).max)}\e[0m | #{commit[0..80]}..."
end
end
desc 'Alias for `bundle:install`'
task bundle: 'bundle:install'
namespace :bundle do
desc 'Run `bundle install` in all subprojects'
task :install do
subprojects.each do |project|
puts '-'*80
sh "cd #{__current__.join(project)} && bundle exec rake bundle:install"
puts
end
end
desc "Remove Gemfile.lock in all subprojects"
task :clean do
subprojects.each do |project|
sh "rm -f #{__current__.join(project)}/Gemfile.lock"
end
sh "rm -f #{__current__.join('elasticsearch-model/gemfiles')}/*.lock"
end
sh "rm -f Gemfile.lock"
end
namespace :test do
task bundle: 'bundle:install'
desc "Run unit tests in all subprojects"
task :unit do
subprojects.each do |project|
puts '-'*80
sh "cd #{__current__.join(project)} && unset BUNDLE_GEMFILE && bundle exec rake test:unit"
puts "\n"
end
end
desc "Setup MongoDB (Docker)"
task :setup_mongodb_docker do
begin
if ENV['MONGODB_VERSION']
sh <<-COMMAND.gsub(/^\s*/, '').gsub(/\s{1,}/, ' ')
wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-${MONGODB_VERSION}.tgz -O /tmp/mongodb.tgz &&
tar -xvf /tmp/mongodb.tgz &&
mkdir /tmp/data &&
${PWD}/mongodb-linux-x86_64-${MONGODB_VERSION}/bin/mongod --setParameter enableTestCommands=1 --dbpath /tmp/data --bind_ip 127.0.0.1 --auth &> /dev/null &
COMMAND
end
rescue
end
end
desc "Run integration tests in all subprojects"
task integration: :setup_elasticsearch do
# 1/ elasticsearch-model
#
puts '-'*80
sh "cd #{__current__.join('elasticsearch-model')} && unset BUNDLE_GEMFILE &&" +
%Q| #{ ENV['TEST_BUNDLE_GEMFILE'] ? "BUNDLE_GEMFILE='#{ENV['TEST_BUNDLE_GEMFILE']}'" : '' }| +
" bundle exec rake test:integration"
puts "\n"
# 2/ elasticsearch-persistence
#
puts '-'*80
sh "cd #{__current__.join('elasticsearch-persistence')} && unset BUNDLE_GEMFILE &&" +
" bundle exec rake test:integration"
puts "\n"
# 3/ elasticsearch-rails
#
puts '-'*80
sh "cd #{__current__.join('elasticsearch-rails')} && unset BUNDLE_GEMFILE &&" +
" bundle exec rake test:integration"
puts "\n"
end
desc "Run all tests in all subprojects"
task all: :wait_for_green_or_yellow do
subprojects.each do |project|
puts '-'*80
sh "cd #{project} && " +
"unset BUNDLE_GEMFILE && " +
"bundle exec rake test:all"
puts "\n"
end
end
end
desc "Wait for elasticsearch cluster to be in green or yellow state"
task :wait_for_green_or_yellow do
require 'elasticsearch'
ready = nil
5.times do |i|
begin
puts "Attempting to wait for green or yellow status: #{i + 1}"
if admin_client.cluster.health(wait_for_status: 'yellow', timeout: '50s')
ready = true
break
end
rescue Elastic::Transport::Transport::Errors::RequestTimeout => ex
puts "Couldn't confirm green or yellow status.\n#{ex.inspect}."
rescue Faraday::ConnectionFailed => ex
puts "Couldn't connect to Elasticsearch.\n#{ex.inspect}."
sleep(30)
end
end
unless ready
puts "Couldn't connect to Elasticsearch, aborting program."
exit(1)
end
end
desc 'Generate documentation for all subprojects'
task :doc do
subprojects.each do |project|
sh "cd #{__current__.join(project)} && rake doc"
puts '-'*80
end
end
desc 'Release all subprojects to Rubygems'
task :release do
subprojects.each do |project|
sh "cd #{__current__.join(project)} && rake release"
puts '-' * 80
end
end
desc <<-DESC
Update Rubygems versions in version.rb and *.gemspec files
Example:
$ rake update_version[5.0.0,5.0.1]
DESC
task :update_version, :old, :new do |_, args|
require 'ansi'
puts '[!!!] Required argument [old] missing'.ansi(:red) unless args[:old]
puts '[!!!] Required argument [new] missing'.ansi(:red) unless args[:new]
files = Dir['**/**/version.rb', '**/**/*.gemspec']
longest_line = files.map(&:size).max
puts "\n", '= FILES '.ansi(:faint) + ('=' * 92).ansi(:faint), "\n"
files.each do |file|
begin
File.open(file, 'r+') do |f|
content = f.read
if content.match Regexp.new(args[:old])
content.gsub! Regexp.new(args[:old]), args[:new]
puts "+ [#{file}]".ansi(:green).ljust(longest_line + 20) + " [#{args[:old]}] -> [#{args[:new]}]".ansi(:green, :bold)
f.rewind
f.write content
else
puts "- [#{file}]".ansi(:yellow).ljust(longest_line+20) + " -".ansi(:faint,:strike)
end
end
rescue Exception => e
puts "[!!!] #{e.class} : #{e.message}".ansi(:red,:bold)
raise e
end
end
puts "\n\n", "= CHANGELOG ".ansi(:faint) + ('='*88).ansi(:faint), "\n"
log = `git --no-pager log --reverse --no-color --pretty='* %s' HEAD --not v#{args[:old]} elasticsearch-*`.split("\n")
puts log.join("\n")
log_entries = {}
log_entries[:common] = log.reject { |l| l =~ /^* \[/ }
log_entries[:model] = log.select { |l| l =~ /^* \[MODEL\]/ }
log_entries[:store] = log.select { |l| l =~ /^* \[STORE\]/ }
log_entries[:rails] = log.select { |l| l =~ /^* \[RAILS\]/ }
changelog = File.read(File.open('CHANGELOG.md', 'r'))
changelog_update = ''
changelog_update << "## #{args[:new]}\n\n"
unless log_entries[:common].empty?
changelog_update << log_entries[:common]
.map { |l| "#{l}" }
.join("\n")
changelog_update << "\n\n"
end
unless log_entries[:model].empty?
changelog_update << "### ActiveModel\n\n"
changelog_update << log_entries[:model]
.map { |l| l.gsub /\[.+\] /, '' }
.map { |l| "#{l}" }
.join("\n")
changelog_update << "\n\n"
end
unless log_entries[:store].empty?
changelog_update << "### Persistence\n\n"
changelog_update << log_entries[:store]
.map { |l| l.gsub /\[.+\] /, '' }
.map { |l| "#{l}" }
.join("\n")
changelog_update << "\n\n"
end
unless log_entries[:rails].empty?
changelog_update << "### Ruby on Rails\n\n"
changelog_update << log_entries[:rails]
.map { |l| l.gsub /\[.+\] /, '' }
.map { |l| "#{l}" }
.join("\n")
changelog_update << "\n\n"
end
unless changelog =~ /^## #{args[:new]}/
File.open('CHANGELOG.md', 'w+') { |f| f.write changelog_update and f.write changelog }
end
puts "\n\n", "= DIFF ".ansi(:faint) + ('='*93).ansi(:faint)
diff = `git --no-pager diff --patch --word-diff=color --minimal elasticsearch-*`.split("\n")
puts diff
.reject { |l| l =~ /^\e\[1mdiff \-\-git/ }
.reject { |l| l =~ /^\e\[1mindex [a-z0-9]{7}/ }
.reject { |l| l =~ /^\e\[1m\-\-\- i/ }
.reject { |l| l =~ /^\e\[36m@@/ }
.map { |l| l =~ /^\e\[1m\+\+\+ w/ ? "\n#{l} " + '-'*(104-l.size) : l }
.join("\n")
puts "\n\n", "= COMMIT ".ansi(:faint) + ('='*91).ansi(:faint), "\n"
puts 'git add CHANGELOG.md elasticsearch-*',
"git commit --verbose --message='Release #{args[:new]}' --edit",
'rake release'
"\n"
end