Skip to content

Commit 5b57fee

Browse files
committed
Extract remote schema generation into a task in buildSrc
1 parent bec8547 commit 5b57fee

File tree

2 files changed

+83
-30
lines changed

2 files changed

+83
-30
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package tasks
2+
3+
import org.gradle.api.DefaultTask
4+
import org.gradle.api.file.Directory
5+
import org.gradle.api.file.RegularFile
6+
import org.gradle.api.provider.Provider
7+
import org.gradle.api.tasks.InputDirectory
8+
import org.gradle.api.tasks.InputFile
9+
import org.gradle.api.tasks.OutputFile
10+
import org.gradle.api.tasks.TaskAction
11+
import org.gradle.process.ExecOperations
12+
import javax.inject.Inject
13+
14+
/**
15+
* Generates remote schemas file for JSON schema test-suite
16+
*/
17+
abstract class GenerateRemoteSchemas : DefaultTask() {
18+
@InputDirectory
19+
val remotes: Provider<Directory> =
20+
project.objects.directoryProperty()
21+
.convention(
22+
project.layout.projectDirectory.dir("schema-test-suite/remotes"),
23+
)
24+
25+
@InputFile
26+
val script: Provider<RegularFile> =
27+
project.objects.fileProperty()
28+
.convention(
29+
project.layout.projectDirectory.file("schema-test-suite/bin/jsonschema_suite"),
30+
)
31+
32+
@OutputFile
33+
val remotesFile: Provider<RegularFile> =
34+
project.objects.fileProperty()
35+
.convention(
36+
project.layout.buildDirectory.file("remotes.json"),
37+
)
38+
39+
@get:Inject
40+
protected abstract val execService: ExecOperations
41+
42+
init {
43+
group = "generation"
44+
description = "Generates remote schema files for test suites"
45+
}
46+
47+
@TaskAction
48+
protected fun generate() {
49+
remotesFile.get().asFile.outputStream().use { out ->
50+
execService.exec {
51+
standardOutput = out
52+
executable = "python3"
53+
args(
54+
script.get().asFile.path,
55+
"remotes",
56+
)
57+
}
58+
}
59+
}
60+
}

test-suites/build.gradle.kts

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
33
import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeSimulatorTest
44
import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeTest
55
import org.jlleitschuh.gradle.ktlint.reporter.ReporterType
6+
import tasks.GenerateRemoteSchemas
67

78
plugins {
89
convention.kotlin
@@ -68,52 +69,44 @@ dependencies {
6869
kover(projects.jsonSchemaValidator)
6970
}
7071

71-
private val remotesFile =
72-
layout.buildDirectory
73-
.file("remotes.json")
74-
.get()
75-
.asFile
76-
7772
val generateRemoteSchemas =
78-
tasks.register("generateRemoteSchemas") {
79-
inputs.dir("$projectDir/schema-test-suite/remotes")
80-
outputs.files(remotesFile)
81-
doLast {
82-
remotesFile.outputStream().use { out ->
83-
exec {
84-
standardOutput = out
85-
executable = "python3"
86-
args(
87-
"$projectDir/schema-test-suite/bin/jsonschema_suite",
88-
"remotes",
89-
)
90-
}
91-
}
92-
}
93-
}
73+
tasks.register<GenerateRemoteSchemas>("generateRemoteSchemas")
9474

9575
tasks.withType<AbstractTestTask> {
9676
dependsOn(generateRemoteSchemas)
9777
}
9878

9979
tasks.withType<KotlinJsTest> {
100-
// This is used to pass the right location for Node.js test
101-
environment("TEST_SUITES_DIR", "$projectDir/schema-test-suite/tests")
102-
environment("REMOTES_SCHEMAS_JSON", remotesFile.absolutePath)
80+
doFirst {
81+
// This is used to pass the right location for Node.js test
82+
environment("TEST_SUITES_DIR", "$projectDir/schema-test-suite/tests")
83+
environment("REMOTES_SCHEMAS_JSON", generateRemoteSchemas.flatMap { it.remotesFile }.get().asFile.absolutePath)
84+
}
10385
}
10486

10587
tasks.withType<KotlinNativeSimulatorTest> {
106-
// prefix SIMCTL_CHILD_ is used to pass the env variable to the simulator
107-
environment("SIMCTL_CHILD_TEST_SUITES_DIR", "$projectDir/schema-test-suite/tests")
108-
environment("SIMCTL_CHILD_REMOTES_SCHEMAS_JSON", remotesFile.absolutePath)
88+
doFirst {
89+
// prefix SIMCTL_CHILD_ is used to pass the env variable to the simulator
90+
environment("SIMCTL_CHILD_TEST_SUITES_DIR", "$projectDir/schema-test-suite/tests")
91+
environment(
92+
"SIMCTL_CHILD_REMOTES_SCHEMAS_JSON",
93+
generateRemoteSchemas.flatMap {
94+
it.remotesFile
95+
}.get().asFile.absolutePath,
96+
)
97+
}
10998
}
11099

111100
tasks.withType<KotlinNativeTest> {
112-
environment("REMOTES_SCHEMAS_JSON", remotesFile.absolutePath)
101+
doFirst {
102+
environment("REMOTES_SCHEMAS_JSON", generateRemoteSchemas.flatMap { it.remotesFile }.get().asFile.absolutePath)
103+
}
113104
}
114105

115106
tasks.withType<Test> {
116-
environment("REMOTES_SCHEMAS_JSON", remotesFile.absolutePath)
107+
doFirst {
108+
environment("REMOTES_SCHEMAS_JSON", generateRemoteSchemas.flatMap { it.remotesFile }.get().asFile.absolutePath)
109+
}
117110
}
118111

119112
ktlint {

0 commit comments

Comments
 (0)