-
Notifications
You must be signed in to change notification settings - Fork 81
/
run.ps1
329 lines (254 loc) · 7.97 KB
/
run.ps1
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
$languages = @{ "js"="origin/js/v2.1"; "csharp"="origin/csharp/v2.3";"csharp-core"="origin/csharp-core/v2.1"; "java"="origin/java/v2.2"; "scala"="origin/scala/v2.2"; "php"="origin/php/v2.1"; "fsharp"="origin/fsharp/v2.2" }
$testsNbByStep = @{ 1=4; 2=1; 3=4; 4=4; 5=2 }
$masterBranch = "origin/master"
$testBranch = "test"
$solutionBranch = "solution"
$workshopBranch = "workshop"
$batWrapperTemplate = @"
@echo off
PowerShell -ExecutionPolicy Bypass -File @@scriptname@@.ps1
"@
$nextCommandTemplate = @"
git add -A
git commit -m "Resolve test"
git merge --no-edit @@nexttag@@
"@
$jumpToNextStepCommandTemplate = @"
git add -A
git commit -m "Abort test"
git checkout -b $workshopBranch-@@nexttag@@ @@nexttag@@
git merge --no-edit @@nexttag@@-test1 *> `$null`
git checkout --ours .
git add .
git commit -m "Merge with test branch"
"@
$displayNextStepMessageTemplate = @"
Write-Host ""
Write-Host ""
Get-Content stepsDoc/step@@stepNum@@.txt | Write-Host -f green
Write-Host ""
Write-Host ""
"@
$displayNextTestMessageTemplate = @"
Write-Host ""
Write-Host ""
Write-Host -f green "========================"
Write-Host -f green "=== STEP @@stepNum@@ - Test @@testNum@@ ==="
Write-Host -f green "========================"
Write-Host ""
Write-Host ""
"@
$jumpToEndStepCommandTemplate = @"
git add -A
git commit -m "Abort test"
git checkout -b $workshopBranch-end $solutionBranch
"@
$displayEndMessageTemplate = @"
Write-Host ""
Write-Host ""
Get-Content stepsDoc/end.txt | Write-Host -f green
Write-Host ""
Write-Host ""
"@
function AskParametreWithValues($name, $values){
do {
$value = Read-Host ($name + " (" + ($values -join ", ") + ")")
} while(-Not ($values -contains $value))
return $value
}
function askLanguage(){
AskParametreWithValues "Language" $languages.Keys
}
function generateTagsForStep($stepNum){
[array]$testTags = 1..$testsNbByStep.$stepNum | %{ "step" + $stepNum + "-test" + $_ }
[array]$stepTags = ("step" + $stepNum)
$testTags + $stepTags
}
function generateTags(){
1..$testsNbByStep.Count | %{ generateTagsForStep $_ }
}
function getCurrentTestTag(){
"step" + $currentStep + "-test" + $currentTestOfStep
}
function getNextTestTag(){
if($testsNbByStep.$currentStep -le $currentTestOfStep){
"step" + ($currentStep + 1) + "-test1"
} else {
"step" + $currentStep + "-test" + ($currentTestOfStep + 1)
}
}
function getNextTestNum(){
if($testsNbByStep.$currentStep -le $currentTestOfStep){
return 1
} else {
return ($currentTestOfStep + 1)
}
}
function getCurrentStepTag(){
"step" + $currentStep
}
function getNextStepTag(){
"step" + ($currentStep + 1)
}
function resetTestCounter(){
$script:currentStep = 1
$script:currentTestOfStep = 1
}
function nextTest(){
if($testsNbByStep.$currentStep -le $currentTestOfStep){
$script:currentStep++
$script:currentTestOfStep = 1
} else {
$script:currentTestOfStep++
}
}
function hasNextTestForCurrentStep(){
$currentTestOfStep -lt $testsNbByStep.$currentStep
}
function hasNextStep(){
$currentStep -lt $testsNbByStep.Count
}
function clean(){
Write-Host "Clean repository..."
git clean -d -x -f > $null
git reset --hard HEAD > $null
git checkout master > $null
git branch -D $testBranch *> $null
git branch -D $solutionBranch *> $null
git branch -D $workshopBranch *> $null
git branch -D ($workshopBranch + "-end") *> $null
generateTags | %{ git branch -D ($workshopBranch + "-" + $_) } *> $null
generateTags | %{ git tag -d $_ } *> $null
}
function extraCommitHashOfLog($line){
$parts = $line.split(' ')
$parts[0]
}
function isFailedTestCommit($line){
$line -like '* KO]*'
}
function addStepNavigationCommand($nextStepTag, $nextStepNum){
$nextCommandContent = $jumpToNextStepCommandTemplate.Replace("@@nexttag@@", $nextStepTag) + "`r`n" + $displayNextStepMessageTemplate.Replace("@@stepNum@@", $nextStepNum)
$nextCommandContent | out-file 'jumpToNextStep.ps1' -enc ascii
git add jumpToNextStep.ps1 > $null
git commit -m "Add step navigation commands" > $null
}
function addEndStepNavigationCommand(){
$nextCommandContent = $jumpToEndStepCommandTemplate + "`r`n" + $displayEndMessageTemplate
$nextCommandContent | out-file 'jumpToNextStep.ps1' -enc ascii
git add jumpToNextStep.ps1 > $null
git commit -m "Add end step navigation commands" > $null
}
function pickCommitForSolution($line){
$hash = extraCommitHashOfLog $line
Write-Host -NoNewline "."
$isKoTest = isFailedTestCommit $line
$isFirstTestOfStep = $currentTestOfStep -eq 1
if($isKoTest -and $isFirstTestOfStep){
if((hasNextStep)) {
addStepNavigationCommand (getNextStepTag) ($currentStep + 1)
} else {
addEndStepNavigationCommand
}
}
git cherry-pick $hash > $null
if($isKoTest -and $isFirstTestOfStep){
git tag (getCurrentStepTag) > $null
Write-Host ((getCurrentStepTag) + " Ok")
}
if($isKoTest){
nextTest
}
}
function getCommitLog($branch){
git log $branch --pretty=tformat:'%h %s' --reverse -E HEAD..
}
function initializeNavigationScript(){
$batWrapperTemplate.Replace("@@scriptname@@", 'jumpToNextStep') | out-file 'jumpToNextStep.bat' -enc ascii
$batWrapperTemplate.Replace("@@scriptname@@", 'next') | out-file 'next.bat' -enc ascii
git add jumpToNextStep.bat > $null
git add next.bat > $null
git commit -m "Add bat wrapper to navigation commands" > $null
}
function initializeSolutionBranch($referenceBranch){
Write-Host "Initialize solution branch"
resetTestCounter
git checkout -b $solutionBranch $masterBranch > $null
initializeNavigationScript
getCommitLog $referenceBranch | %{ pickCommitForSolution $_ }
Write-Host "Done"
}
function addNavigationCommand($nextTestTag, $nextTestNum, $currentStepNum){
$nextCommandContent = $nextCommandTemplate.Replace("@@nexttag@@", $nextTestTag)
if((getNextTestNum) -eq 1) {
$nextCommandContent += "`r`n" + $displayNextStepMessageTemplate.Replace("@@stepNum@@", $currentStepNum + 1)
} else {
$nextCommandContent += "`r`n" + $displayNextTestMessageTemplate.Replace("@@stepNum@@", $currentStepNum).Replace("@@testNum@@", $nextTestNum)
}
$nextCommandContent | out-file 'next.ps1' -enc ascii
git add next.ps1 > $null
git commit -m "Add test navigation commands" > $null
}
function addEndNavigationCommand(){
$nextCommandContent = $nextCommandTemplate.Replace("@@nexttag@@", $testBranch) + "`r`n" + $displayEndMessageTemplate
$nextCommandContent | out-file 'next.ps1' -enc ascii
git add next.ps1 > $null
git commit -m "Add end test navigation commands" > $null
}
function pickCommitForTest($line){
$hash = extraCommitHashOfLog $line
Write-Host -NoNewline "."
git cherry-pick $hash > $null
if (isFailedTestCommit $line) {
if((hasNextStep) -or (hasNextTestForCurrentStep)) {
addNavigationCommand (getNextTestTag) (getNextTestNum) ($currentStep)
} else {
addEndNavigationCommand
}
git tag (getCurrentTestTag) > $null
Write-Host ((getCurrentTestTag) + " Ok")
nextTest
}
}
function getCommitLogWithoutGreenTest($branch){
git log $branch --pretty=tformat:'%h %s' --grep '[^K]\][^\[]' --grep '^[^\[]' --reverse -E HEAD..
}
function initializeTestBranch(){
Write-Host "Initialize test branch"
resetTestCounter
git checkout -b $testBranch $masterBranch > $null
getCommitLogWithoutGreenTest $solutionBranch | %{ pickCommitForTest $_ }
Write-Host "Done"
}
function initializeWorkflow(){
Write-Host "Initialize workspace"
resetTestCounter
git checkout -b $workshopBranch (getCurrentTestTag)
}
function customInitialize(){
if (Test-Path ./initialize.bat) {
& ./initialize.bat
}
}
function displayWarningIfNotMaster(){
$currentBranch = git name-rev --name-only HEAD
if($currentBranch -ne "master") {
$confirmation = Read-Host "Are you sure you want to reinit everything? All your work will be lost! (y/N)"
if ($confirmation.ToLower() -ne 'y') {
exit
}
}
}
displayWarningIfNotMaster
$selectedLanguage = askLanguage
clean
initializeSolutionBranch $languages.$selectedLanguage
initializeTestBranch
initializeWorkflow
customInitialize
Write-Host "Koan OK"
Write-Host ""
Write-Host ""
Get-Content stepsDoc/step1.txt | Write-Host -f green
Write-Host ""
Write-Host ""