This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Rakefile.rb
341 lines (292 loc) · 10.1 KB
/
Rakefile.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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#########################################################################################################
# VARIABLES
#########################################################################################################
BANNER = """
=========================================================================================================
______ ____ ____ _______. _______ ___________ ____ __ ___ __ .___________.
/ |\\ \\ / / / | | \\ | ____\\ \\ / / | |/ / | | | |
| ,----' \\ \\/ / | (----` | .--. || |__ \\ \\/ / | ' / | | `---| |----`
| | \\ / \\ \\ | | | || __| \\ / | < | | | |
| `----. \\ / .----) | | '--' || |____ \\ / | . \\ | | | |
\\______| \\__/ |_______/ |_______/ |_______| \\__/ |__|\\__\\ |__| |__|
=========================================================================================================
"""
RED = "\033[0;31m"
YELLOW = "\033[1;33m"
GREEN = "\033[1;32m"
BLUE = "\033[1;34m"
NC = "\033[0m"
REPOSITORIES = ["cvs-svc-defects",
"cvs-svc-test-stations",
"cvs-svc-technical-records",
"cvs-svc-test-types",
"cvs-svc-preparers",
"cvs-svc-test-results",
"cvs-svc-activities",
"cvs-svc-test-number",
"cvs-tsk-cert-gen",
"cvs-tsk-cert-gen-init",
"cvs-svc-authoriser"]
SERVICE = ["cvs-svc-defects",
"cvs-svc-test-stations",
"cvs-svc-technical-records",
"cvs-svc-test-types",
"cvs-svc-preparers",
"cvs-svc-activities",
"cvs-svc-test-results",
"cvs-svc-test-number",
"cvs-tsk-cert-gen",
"cvs-tsk-cert-gen-init",
"cvs-svc-authoriser"]
GIT = "git@github.com:dvsa"
#########################################################################################################
# TASKS
#########################################################################################################
###################
# default
###################
task :default => :help
###################
# help
###################
task :help do
print "#{BLUE}#{BANNER}#{NC}\n"
print "rake #{RED}help#{NC} -- display help\n"
print "rake #{RED}branch#{NC} -- display current branch of all repos\n"
print "rake #{RED}clone#{NC} -- run 'git clone' on all repos\n"
print "rake #{RED}pull#{NC} -- run 'git pull --rebase' on all repos\n"
print "rake #{RED}create_branch 'BRANCH'#{NC} -- run 'git checkout -b \"BRANCH\"' on all repos\n"
print "rake #{RED}checkout_master#{NC} -- run 'git checkout master' on all repos\n"
print "rake #{RED}checkout_develop#{NC} -- run 'git checkout develop' on all repos\n"
print "rake #{RED}needs_commit#{NC} -- checks if any of the repos need a commit\n\n"
print "rake #{RED}install#{NC} -- run npm install in all repositories\n"
print "rake #{RED}start#{NC} -- run npm start in all repositories\n\n"
end
###################
# branch
###################
task :branch do
print "\nChecking branch in all repositories\n\n"
REPOSITORIES.each { |repo|
if directory_exists? repo
dir = Dir.pwd + "/" + repo
Dir.chdir(dir) do
branch = `git rev-parse --abbrev-ref HEAD`
print "Branch in #{YELLOW}#{repo} is #{BLUE}#{branch}#{NC}\n"
end
else
print "#{RED}ERROR #{YELLOW}#{repo} #{NC}repository does not exist locally\n\n"
end
}
end
###################
# clone
###################
task :clone do
REPOSITORIES.each { |repo|
if directory_exists? repo
print "#{RED}ERROR#{NC} Repository #{YELLOW}#{repo} #{NC}has already been cloned\n"
else
print "#{RED}Cloning #{YELLOW}#{repo} #{NC}"
sh "git clone --quiet #{GIT}/#{repo}.git"
print "#{GREEN}Cloned\n"
end
}
end
###################
# pull
###################
task :pull do
REPOSITORIES.each { |repo|
if directory_exists? repo
print "#{RED}Pulling #{YELLOW}#{repo} #{NC}"
sh "git --git-dir=#{repo}/.git pull --quiet --rebase --autostash"
print "#{GREEN}Pulled\n"
else
print "#{RED}ERROR #{YELLOW}#{repo} #{NC}repository does not exist locally\n"
end
}
end
###################
# checkout
###################
task :checkout, [:branch] do |task, args|
print "\nChecking out branch #{RED}#{args.branch}#{NC} in all repositories\n\n"
REPOSITORIES.each { |repo|
if directory_exists? repo
dir = Dir.pwd + "/" + repo
Dir.chdir(dir) do
print "Checking out #{YELLOW}#{repo} #{NC}"
sh "git checkout --quiet #{args.branch}"
print "#{GREEN}Done#{NC}\n"
end
else
print "#{RED}ERROR #{YELLOW}#{repo} #{NC}repository does not exist locally\n"
end
}
end
###################
# checkout_master
###################
task :checkout_master do
Rake::Task["checkout"].invoke("master")
end
###################
# checkout_develop
###################
task :checkout_develop do
Rake::Task["checkout"].invoke("develop")
end
###################
# create_branch
###################
task :create_branch do
ARGV.each { |a| task a.to_sym do ; end }
create_git_branch ARGV[1]
end
###################
# needs_commit
###################
task :needs_commit do
hasChanged = false
REPOSITORIES.each { |repo|
if directory_exists? repo
dir = Dir.pwd + "/" + repo
if !needs_commit? dir
hasChanged =true
end
else
print "#{RED}ERROR #{YELLOW}#{repo} #{NC}repository does not exist locally\n"
end
}
if !hasChanged
print "#{GREEN}No unstaged files#{NC}\n"
end
end
###################
# revert changes
###################
task :revert do
REPOSITORIES.each { |repo|
if directory_exists? repo
print "#{RED}Reverting #{YELLOW}#{repo} #{NC}"
sh "cd #{repo} git reset --hard"
print "#{GREEN}Reverted\n"
else
print "#{RED}ERROR #{YELLOW}#{repo} #{NC}no revert was made\n"
end
}
end
###################
# install
###################
task :install do
SERVICE.each { |service|
if directory_exists? service
dir = Dir.pwd + "/" + service
Dir.chdir(dir) do
print "#{RED}Installing #{YELLOW}#{service} #{NC}"
sh "npm install"
print "#{GREEN}Done#{NC}\n"
end
else
print "#{RED}ERROR#{NC} Node modules for #{YELLOW}#{service} #{NC}has already been installed\n"
end
}
end
###################
# start
###################
task :start do
Rake::Task["stop"].invoke()
SERVICE.each { |service|
if directory_exists? service
dir = Dir.pwd + "/" + service
Dir.chdir(dir) do
print "#{RED}Starting #{YELLOW}#{service} #{NC}"
sh "npm run start &"
print "#{GREEN}Done#{NC}\n"
end
else
print "#{RED}ERROR#{NC} #{YELLOW}#{service} #{NC}has already been started\n"
end
}
end
###################
# stop
###################
task :stop do
kill_port 3000
kill_port 3001
kill_port 3002
kill_port 3003
kill_port 3004
kill_port 3005
kill_port 3006
kill_port 3007
kill_port 3008
kill_port 3009
kill_port 8000
kill_port 8001
kill_port 8002
kill_port 8003
kill_port 8004
kill_port 8005
kill_port 8006
kill_port 8007
kill_port 8008
kill_port 8009
end
#########################################################################################################
# HELPERS
#########################################################################################################
def directory_exists?(directory_name)
File.directory?(directory_name)
end
def needs_commit?(dir = Dir.pwd, file = nil)
rval = false
Dir.chdir(dir) do
status = %x{git status}
if file.nil?
rval = true unless status =~ /nothing to commit \(working directory clean\)|nothing added to commit but untracked files present/
if status =~ /nothing added to commit but untracked files present/
puts "#{YELLOW}WARNING#{NC}: untracked files present in #{dir}"
untracked_files = `git ls-files --others --exclude-standard`
show_changed_files(untracked_files)
end
else
rval = true if status =~ /^#\t.*modified: #{file}/
end
end
rval
end
def show_changed_files(status)
status.each_line do |line|
puts "#{BLUE}=====>> #{line}"
end
end
def create_git_branch(branch)
print "\nChecking out branch #{RED}#{branch}#{NC} in all repositories\n\n"
REPOSITORIES.each { |repo|
if directory_exists? repo
dir = Dir.pwd + "/" + repo
Dir.chdir(dir) do
if !check_branch_exist? branch
print "Checking out #{YELLOW}#{repo} #{NC}"
sh "git checkout --quiet -b #{branch}"
print "#{GREEN}Done#{NC}\n"
else
print "#{RED}ERROR#{NC} in repository #{YELLOW}#{repo} #{NC}branch already exists locally\n"
end
end
else
print "#{RED}ERROR #{YELLOW}#{repo} #{NC}repository does not exist locally\n"
end
}
end
def check_branch_exist?(branch)
system("git rev-parse --quiet --verify #{branch} > /dev/null")
end
def kill_port(port)
sh "lsof -i tcp:#{port} | awk 'NR!=1 {print $2}' | xargs kill || true"
end