-
Notifications
You must be signed in to change notification settings - Fork 14
/
build.gradle
196 lines (162 loc) · 5.55 KB
/
build.gradle
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
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'junit:junit:4.12'
}
}
import static org.junit.Assert.assertEquals
// Empty catch all tasks
task compile {}
task clean {}
task test {}
// Catch all tasks
task build {}
build.dependsOn compile, test
def createCodeDirCleanTask(lessonDir, codeDir) {
task "clean${lessonDir.name}${codeDir.name}" (type:Delete) {
delete getCompileDir(lessonDir, codeDir)
}
tasks["clean${lessonDir.name}"].dependsOn "clean${lessonDir.name}${codeDir.name}"
}
def createCodeDirCompileTask(lessonDir, codeDir) {
def javaFiles = []
codeDir.eachFile {
if (it.name.endsWith('.java')) {
javaFiles.add(it)
}
}
javaFiles.addAll(findDependencies(codeDir))
task "compile${lessonDir.name}${codeDir.name}" (type:JavaCompile) {
classpath = files(codeDir)
destinationDir = getCompileDir(lessonDir, codeDir)
source = files(javaFiles)
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
inputs.files files(codeDir)
}
tasks["compile${lessonDir.name}"].dependsOn "compile${lessonDir.name}${codeDir.name}"
}
def createCodeDirTestTask(lessonDir, codeDir) {
findExecutions(lessonDir, codeDir).eachWithIndex { execution, index ->
def testTask = task "test${lessonDir.name}${codeDir.name}_${index + 1}" (type: JavaExec) {
args = execution.args
classpath = files(getCompileDir(lessonDir, codeDir))
dependsOn tasks["compile${lessonDir.name}${codeDir.name}"]
main = execution.className
standardInput = new ByteArrayInputStream(execution.inputs.join(System.lineSeparator()).getBytes())
standardOutput = new ByteArrayOutputStream()
doLast {
def cleanedExpectedOutput = execution.output
.replaceAll("\\d+ms", "dms")
.replace("\t", " ")
.replaceAll("\\.+",'.')
.replaceAll("[\n\r]+", "")
def cleanedActualOutput = standardOutput.toString()
.replaceAll("\\d+ms", "dms")
.replace("\t", " ")
.replaceAll("\\.+",'.')
.replaceAll("[\n\r]+", "")
assertEquals(cleanedExpectedOutput, cleanedActualOutput)
}
}
tasks["test${lessonDir.name}"].dependsOn testTask
}
}
def createLessonCleanTask(lessonDir) {
task "clean${lessonDir.name}" {}
clean.dependsOn "clean${lessonDir.name}"
}
def createLessonCompileTask(lessonDir) {
task "compile${lessonDir.name}" {}
compile.dependsOn "compile${lessonDir.name}"
}
def createLessonTestTask(lessonDir) {
task "test${lessonDir.name}" {}
test.dependsOn "test${lessonDir.name}"
}
def findDependencies(codeDir) {
def result = []
def readmeFile = new File(codeDir, 'README.md')
findDependenciesFromReadme(readmeFile).each { dependency ->
result << dependency
result << findDependencies(new File(dependency))
}
return result
}
def findDependenciesFromReadme(readMeFile) {
if (!readMeFile.exists()) {
return []
}
def result = []
def isDependencies = false
readMeFile.readLines().each { line ->
if (isDependencies && line.startsWith('###')) {
isDependencies = false
}
if (line == '### Depends On') {
isDependencies = true
}
if (isDependencies && line.startsWith('- ')) {
result << line.substring('- '.length())
}
}
return result
}
def findExecutions(lessonDir, codeDir) {
def readMe = new File(codeDir, 'README.md')
def executions = []
if (readMe.exists()) {
def execution = null
def isOutput = false
readMe.readLines().each { line ->
if (execution != null && line == "```") {
execution = null
isOutput = false
return
}
if (execution == null && line == "```") {
execution = ['args': [], 'inputs': [], 'output': '']
executions << execution
isOutput = false
}
if (line.startsWith('args')) {
execution['args'] << line.substring('args:'.length()).trim()
}
if (line.startsWith('class:')) {
execution['className'] = line.substring('class:'.length()).trim()
}
if (line.startsWith('inputs:')) {
execution['inputs'] << line.substring('inputs:'.length()).trim()
}
if (isOutput) {
execution['output'] += line
}
if (line == 'output:') {
isOutput = true
}
}
}
return executions
}
def getCompileDir(lessonDir, codeDir) {
return file("${buildDir}/compiled/${lessonDir.name}/${codeDir.name}")
}
new File(".").eachDir { lessonDir ->
// For each Lesson Directory
if (lessonDir.name.startsWith("Lesson")) {
createLessonCleanTask(lessonDir)
createLessonCompileTask(lessonDir)
createLessonTestTask(lessonDir)
lessonDir.eachDir { codeDir ->
if (codeDir.name.startsWith("Exercise")
|| codeDir.name.startsWith("Activity")
|| codeDir.name.startsWith("Snippet")) {
createCodeDirCleanTask(lessonDir, codeDir)
createCodeDirCompileTask(lessonDir, codeDir)
createCodeDirTestTask(lessonDir, codeDir)
}
}
}
}