Skip to content

Extract remote schema generation into a task in buildSrc #274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions buildSrc/src/main/kotlin/tasks/GenerateRemoteSchemas.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package tasks

import org.gradle.api.DefaultTask
import org.gradle.api.file.Directory
import org.gradle.api.file.RegularFile
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.process.ExecOperations
import javax.inject.Inject

/**
* Generates remote schemas file for JSON schema test-suite
*/
abstract class GenerateRemoteSchemas : DefaultTask() {
@InputDirectory
val remotes: Provider<Directory> =
project.objects.directoryProperty()
.convention(
project.layout.projectDirectory.dir("schema-test-suite/remotes"),
)

@InputFile
val script: Provider<RegularFile> =
project.objects.fileProperty()
.convention(
project.layout.projectDirectory.file("schema-test-suite/bin/jsonschema_suite"),
)

@OutputFile
val remotesFile: Provider<RegularFile> =
project.objects.fileProperty()
.convention(
project.layout.buildDirectory.file("remotes.json"),
)

@get:Inject
protected abstract val execService: ExecOperations

init {
group = "generation"
description = "Generates remote schema files for test suites"
}

@TaskAction
protected fun generate() {
remotesFile.get().asFile.outputStream().use { out ->
execService.exec {
standardOutput = out
executable = "python3"
args(
script.get().asFile.path,
"remotes",
)
}
}
}
}
53 changes: 23 additions & 30 deletions test-suites/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeSimulatorTest
import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeTest
import org.jlleitschuh.gradle.ktlint.reporter.ReporterType
import tasks.GenerateRemoteSchemas

plugins {
convention.kotlin
Expand Down Expand Up @@ -68,52 +69,44 @@ dependencies {
kover(projects.jsonSchemaValidator)
}

private val remotesFile =
layout.buildDirectory
.file("remotes.json")
.get()
.asFile

val generateRemoteSchemas =
tasks.register("generateRemoteSchemas") {
inputs.dir("$projectDir/schema-test-suite/remotes")
outputs.files(remotesFile)
doLast {
remotesFile.outputStream().use { out ->
exec {
standardOutput = out
executable = "python3"
args(
"$projectDir/schema-test-suite/bin/jsonschema_suite",
"remotes",
)
}
}
}
}
tasks.register<GenerateRemoteSchemas>("generateRemoteSchemas")

tasks.withType<AbstractTestTask> {
dependsOn(generateRemoteSchemas)
}

tasks.withType<KotlinJsTest> {
// This is used to pass the right location for Node.js test
environment("TEST_SUITES_DIR", "$projectDir/schema-test-suite/tests")
environment("REMOTES_SCHEMAS_JSON", remotesFile.absolutePath)
doFirst {
// This is used to pass the right location for Node.js test
environment("TEST_SUITES_DIR", "$projectDir/schema-test-suite/tests")
environment("REMOTES_SCHEMAS_JSON", generateRemoteSchemas.flatMap { it.remotesFile }.get().asFile.absolutePath)
}
}

tasks.withType<KotlinNativeSimulatorTest> {
// prefix SIMCTL_CHILD_ is used to pass the env variable to the simulator
environment("SIMCTL_CHILD_TEST_SUITES_DIR", "$projectDir/schema-test-suite/tests")
environment("SIMCTL_CHILD_REMOTES_SCHEMAS_JSON", remotesFile.absolutePath)
doFirst {
// prefix SIMCTL_CHILD_ is used to pass the env variable to the simulator
environment("SIMCTL_CHILD_TEST_SUITES_DIR", "$projectDir/schema-test-suite/tests")
environment(
"SIMCTL_CHILD_REMOTES_SCHEMAS_JSON",
generateRemoteSchemas.flatMap {
it.remotesFile
}.get().asFile.absolutePath,
)
}
}

tasks.withType<KotlinNativeTest> {
environment("REMOTES_SCHEMAS_JSON", remotesFile.absolutePath)
doFirst {
environment("REMOTES_SCHEMAS_JSON", generateRemoteSchemas.flatMap { it.remotesFile }.get().asFile.absolutePath)
}
}

tasks.withType<Test> {
environment("REMOTES_SCHEMAS_JSON", remotesFile.absolutePath)
doFirst {
environment("REMOTES_SCHEMAS_JSON", generateRemoteSchemas.flatMap { it.remotesFile }.get().asFile.absolutePath)
}
}

ktlint {
Expand Down
Loading