-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.gradle.kts
88 lines (78 loc) · 3.05 KB
/
build.gradle.kts
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
import java.io.ByteArrayOutputStream
plugins {
kotlin("jvm") version "1.7.22"
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.jetbrains.kotlinx:multik-core:0.2.0")
implementation("org.jetbrains.kotlinx:multik-default:0.2.0")
}
tasks {
sourceSets {
main {
java.srcDirs("src")
}
}
wrapper {
gradleVersion = "7.6"
}
}
customAdventOfCodeTasks()
fun customAdventOfCodeTasks() {
val testTask = tasks["test"]
val solveTask = tasks.register("solve").get()
fileTree("src") {
include("day*/*.in")
}.map {
val day = it.parent.split("/").last().removePrefix("day")
val inputName = it.name.removeSuffix(".in")
day to inputName
}.groupBy { it.first }.map { entry ->
entry.key to entry.value.map { it.second }
}.forEach { (day, inputs) ->
val solveDayTask = tasks.register("solveDay$day").get()
solveTask.dependsOn(solveDayTask)
val testDayTask = tasks.register("testDay$day").get()
testTask.dependsOn(testDayTask)
inputs.forEach { inputName ->
val inputFile = File("src/day$day/$inputName.in")
val outputFile = layout.buildDirectory.file("answers/day$day/$inputName.out").get().asFile
val expectedOutputFile = File("src/day$day/$inputName.out")
val executeTask = tasks.register<JavaExec>("solveDay$day-$inputName") {
description = "Calculates the solution for day $day for input '$inputName'"
dependsOn("compileKotlin")
this.inputs.file(inputFile)
this.outputs.file(outputFile)
// Don't cache response, always run.
this.outputs.upToDateWhen { false }
classpath(sourceSets["main"].runtimeClasspath)
mainClass.set("aoc2022/day$day/Day" + day + "Kt")
standardInput = inputFile.inputStream()
standardOutput = ByteArrayOutputStream()
doLast {
val result = (standardOutput as ByteArrayOutputStream).toString()
outputFile.parentFile.mkdirs()
if (!outputFile.exists()) {
outputFile.createNewFile()
}
val fout = outputFile.outputStream()
fout.write(result.toByteArray())
fout.close()
print(result)
}
}.get()
solveDayTask.dependsOn(executeTask)
if (expectedOutputFile.exists()) {
val testDayInputTask = tasks.register<Exec>("testDay$day-$inputName") {
description = "Tests the solution for day $day using input/output for '$inputName'"
dependsOn(executeTask)
this.inputs.files(expectedOutputFile, outputFile)
commandLine = listOf("diff", expectedOutputFile.path, outputFile.path)
}.get()
testDayTask.dependsOn(testDayInputTask)
}
}
}
}