From 532d4101eb549b6226cb0aa2fa8476ae51de8cb1 Mon Sep 17 00:00:00 2001 From: Corbin McNeely-Smith <58151731+restingbull@users.noreply.github.com> Date: Fri, 26 Apr 2024 14:09:52 -0500 Subject: [PATCH] Integration test for KSP processing --- .bazelproject | 10 +- MODULE.bazel | 3 +- examples/ksp/BUILD | 5 +- kotlin_rules_maven_install.json | 126 ++++++++- .../io/bazel/kotlin/integration/BUILD.bazel | 18 ++ .../kotlin/integration/KspCompileTest.kt | 244 ++++++++++++++++++ .../kotlin/integration/WriteWorkspace.kt | 42 ++- 7 files changed, 417 insertions(+), 31 deletions(-) create mode 100644 src/test/kotlin/io/bazel/kotlin/integration/KspCompileTest.kt diff --git a/.bazelproject b/.bazelproject index 7e950212c..5835927c2 100644 --- a/.bazelproject +++ b/.bazelproject @@ -19,12 +19,6 @@ directories: targets: //:all_local_tests - # These targets are built for the ide only. Primary purpose is to ensure the builder can build the targets, but it's - # also a good way of testing the intellij plugin. - //src/main/kotlin/io/bazel/kotlin/builder/tasks:tasks_for_ide - //src/main/kotlin/io/bazel/kotlin/builder/utils:utils_for_ide - //src/main/kotlin/io/bazel/kotlin/builder/toolchain:toolchain_for_ide - //src/main/kotlin/io/bazel/kotlin/compiler:compiler_for_ide //kotlin:stardoc //src/main/starlark/core/repositories:all @@ -35,6 +29,4 @@ additional_languages: kotlin import_run_configurations: - src/test/Bazel_all_local_tests.xml - -android_sdk_platform: android-31 \ No newline at end of file + src/test/Bazel_all_local_tests.xml \ No newline at end of file diff --git a/MODULE.bazel b/MODULE.bazel index c125a9b56..a32e59dae 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -22,7 +22,7 @@ use_repo( "rules_android", ) -# Once the released rules_koltin is defined, configure it. +# Once the released rules_kotlin is defined, configure it. rules_kotlin_bootstrap_extensions = use_extension( "//src/main/starlark/core/repositories:bzlmod_bootstrap.bzl", "rules_kotlin_bootstrap_extensions", @@ -76,6 +76,7 @@ maven.install( "com.squareup.moshi:moshi-kotlin:1.15.0", "com.squareup.moshi:moshi-kotlin-codegen:1.15.0", "com.google.auto.service:auto-service-annotations:jar:1.1.1", + "org.apache.commons:commons-compress:1.26.1", ], fail_if_repin_required = True, fetch_sources = True, diff --git a/examples/ksp/BUILD b/examples/ksp/BUILD index 6722c96f2..eee4ba56d 100644 --- a/examples/ksp/BUILD +++ b/examples/ksp/BUILD @@ -1,6 +1,3 @@ -load("@bazel_skylib//rules:build_test.bzl", "build_test") -load("@rules_java//java:defs.bzl", "java_binary", "java_plugin") - # Copyright 2018 The Bazel Authors. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,6 +11,8 @@ load("@rules_java//java:defs.bzl", "java_binary", "java_plugin") # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +load("@bazel_skylib//rules:build_test.bzl", "build_test") +load("@rules_java//java:defs.bzl", "java_binary", "java_plugin") load("@rules_kotlin//kotlin:core.bzl", "define_kt_toolchain", "kt_compiler_plugin", "kt_ksp_plugin", "kt_plugin_cfg") load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library") diff --git a/kotlin_rules_maven_install.json b/kotlin_rules_maven_install.json index 8fb9c84dd..617766956 100755 --- a/kotlin_rules_maven_install.json +++ b/kotlin_rules_maven_install.json @@ -1,7 +1,7 @@ { "__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL", - "__INPUT_ARTIFACTS_HASH": -763710014, - "__RESOLVED_ARTIFACTS_HASH": -640184638, + "__INPUT_ARTIFACTS_HASH": 131470593, + "__RESOLVED_ARTIFACTS_HASH": -1372986127, "conflict_resolution": { "com.google.auto.service:auto-service-annotations:1.0.1": "com.google.auto.service:auto-service-annotations:1.1.1", "com.google.guava:guava:27.1-jre": "com.google.guava:guava:32.1.1-jre" @@ -217,6 +217,20 @@ }, "version": "1.13.2" }, + "commons-codec:commons-codec": { + "shasums": { + "jar": "ec87bfb55f22cbd1b21e2190eeda28b2b312ed2a431ee49fbdcc01812d04a5e4", + "sources": "1b9d7336bef950cd45dbefd5351222ee88e4efde09a9454e851a458c34f813be" + }, + "version": "1.16.1" + }, + "commons-io:commons-io": { + "shasums": { + "jar": "a58af12ee1b68cfd2ebb0c27caef164f084381a00ec81a48cc275fd7ea54e154", + "sources": "50cb24bb83c1edcb1c1007a4b6c7ea024c71ac0fa018b80a57391d7c7b5b8246" + }, + "version": "2.15.1" + }, "dev.zacsweers.autoservice:auto-service-ksp": { "shasums": { "jar": "290457e26274b41f625f96b24e033dd18639c6353a6f81304f0a15e2b22eec35", @@ -280,12 +294,19 @@ }, "version": "1.9.9" }, + "org.apache.commons:commons-compress": { + "shasums": { + "jar": "27bb5d40f37c3bb7205b4a0540247df057715e9f6cbbd97d626ab8b50318bb04", + "sources": "767c5e39eb93f7c460926278a2aa7a7cec146aa1f32f8b6664a56acfdcc2e812" + }, + "version": "1.26.1" + }, "org.apache.commons:commons-lang3": { "shasums": { - "jar": "734c8356420cc8e30c795d64fd1fcd5d44ea9d90342a2cc3262c5158fbc6d98b", - "sources": "4709f16a9e0f8fd83ae155083d63044e23045aac8f6f0183a2db09f492491b12" + "jar": "7b96bf3ee68949abb5bc465559ac270e0551596fa34523fddf890ec418dde13c", + "sources": "ab3b86afb898f1026dbe43aaf71e9c1d719ec52d6e41887b362d86777c299b6f" }, - "version": "3.4" + "version": "3.14.0" }, "org.apache.maven:maven-artifact": { "shasums": { @@ -593,6 +614,11 @@ "org.apache.ant:ant": [ "org.apache.ant:ant-launcher" ], + "org.apache.commons:commons-compress": [ + "commons-codec:commons-codec", + "commons-io:commons-io", + "org.apache.commons:commons-lang3" + ], "org.apache.maven:maven-artifact": [ "org.apache.commons:commons-lang3", "org.codehaus.plexus:plexus-utils" @@ -943,6 +969,32 @@ "com.squareup:kotlinpoet-ksp": [ "com.squareup.kotlinpoet.ksp" ], + "commons-codec:commons-codec": [ + "org.apache.commons.codec", + "org.apache.commons.codec.binary", + "org.apache.commons.codec.cli", + "org.apache.commons.codec.digest", + "org.apache.commons.codec.language", + "org.apache.commons.codec.language.bm", + "org.apache.commons.codec.net" + ], + "commons-io:commons-io": [ + "org.apache.commons.io", + "org.apache.commons.io.build", + "org.apache.commons.io.channels", + "org.apache.commons.io.charset", + "org.apache.commons.io.comparator", + "org.apache.commons.io.file", + "org.apache.commons.io.file.attribute", + "org.apache.commons.io.file.spi", + "org.apache.commons.io.filefilter", + "org.apache.commons.io.function", + "org.apache.commons.io.input", + "org.apache.commons.io.input.buffer", + "org.apache.commons.io.monitor", + "org.apache.commons.io.output", + "org.apache.commons.io.serialization" + ], "dev.zacsweers.autoservice:auto-service-ksp": [ "dev.zacsweers.autoservice.ksp" ], @@ -1070,19 +1122,63 @@ "org.apache.ant:ant-launcher": [ "org.apache.tools.ant.launch" ], + "org.apache.commons:commons-compress": [ + "org.apache.commons.compress", + "org.apache.commons.compress.archivers", + "org.apache.commons.compress.archivers.ar", + "org.apache.commons.compress.archivers.arj", + "org.apache.commons.compress.archivers.cpio", + "org.apache.commons.compress.archivers.dump", + "org.apache.commons.compress.archivers.examples", + "org.apache.commons.compress.archivers.jar", + "org.apache.commons.compress.archivers.sevenz", + "org.apache.commons.compress.archivers.tar", + "org.apache.commons.compress.archivers.zip", + "org.apache.commons.compress.changes", + "org.apache.commons.compress.compressors", + "org.apache.commons.compress.compressors.brotli", + "org.apache.commons.compress.compressors.bzip2", + "org.apache.commons.compress.compressors.deflate", + "org.apache.commons.compress.compressors.deflate64", + "org.apache.commons.compress.compressors.gzip", + "org.apache.commons.compress.compressors.lz4", + "org.apache.commons.compress.compressors.lz77support", + "org.apache.commons.compress.compressors.lzma", + "org.apache.commons.compress.compressors.lzw", + "org.apache.commons.compress.compressors.pack200", + "org.apache.commons.compress.compressors.snappy", + "org.apache.commons.compress.compressors.xz", + "org.apache.commons.compress.compressors.z", + "org.apache.commons.compress.compressors.zstandard", + "org.apache.commons.compress.harmony", + "org.apache.commons.compress.harmony.archive.internal.nls", + "org.apache.commons.compress.harmony.pack200", + "org.apache.commons.compress.harmony.unpack200", + "org.apache.commons.compress.harmony.unpack200.bytecode", + "org.apache.commons.compress.harmony.unpack200.bytecode.forms", + "org.apache.commons.compress.java.util.jar", + "org.apache.commons.compress.parallel", + "org.apache.commons.compress.utils" + ], "org.apache.commons:commons-lang3": [ "org.apache.commons.lang3", + "org.apache.commons.lang3.arch", "org.apache.commons.lang3.builder", + "org.apache.commons.lang3.compare", "org.apache.commons.lang3.concurrent", + "org.apache.commons.lang3.concurrent.locks", "org.apache.commons.lang3.event", "org.apache.commons.lang3.exception", + "org.apache.commons.lang3.function", "org.apache.commons.lang3.math", "org.apache.commons.lang3.mutable", "org.apache.commons.lang3.reflect", + "org.apache.commons.lang3.stream", "org.apache.commons.lang3.text", "org.apache.commons.lang3.text.translate", "org.apache.commons.lang3.time", - "org.apache.commons.lang3.tuple" + "org.apache.commons.lang3.tuple", + "org.apache.commons.lang3.util" ], "org.apache.maven:maven-artifact": [ "org.apache.maven.artifact", @@ -1423,6 +1519,10 @@ "com.squareup:kotlinpoet-ksp", "com.squareup:kotlinpoet-ksp:jar:sources", "com.squareup:kotlinpoet:jar:sources", + "commons-codec:commons-codec", + "commons-codec:commons-codec:jar:sources", + "commons-io:commons-io", + "commons-io:commons-io:jar:sources", "dev.zacsweers.autoservice:auto-service-ksp", "dev.zacsweers.autoservice:auto-service-ksp:jar:sources", "javax.annotation:javax.annotation-api", @@ -1441,6 +1541,8 @@ "org.apache.ant:ant-launcher", "org.apache.ant:ant-launcher:jar:sources", "org.apache.ant:ant:jar:sources", + "org.apache.commons:commons-compress", + "org.apache.commons:commons-compress:jar:sources", "org.apache.commons:commons-lang3", "org.apache.commons:commons-lang3:jar:sources", "org.apache.maven:maven-artifact", @@ -1556,6 +1658,10 @@ "com.squareup:kotlinpoet-ksp", "com.squareup:kotlinpoet-ksp:jar:sources", "com.squareup:kotlinpoet:jar:sources", + "commons-codec:commons-codec", + "commons-codec:commons-codec:jar:sources", + "commons-io:commons-io", + "commons-io:commons-io:jar:sources", "dev.zacsweers.autoservice:auto-service-ksp", "dev.zacsweers.autoservice:auto-service-ksp:jar:sources", "javax.annotation:javax.annotation-api", @@ -1574,6 +1680,8 @@ "org.apache.ant:ant-launcher", "org.apache.ant:ant-launcher:jar:sources", "org.apache.ant:ant:jar:sources", + "org.apache.commons:commons-compress", + "org.apache.commons:commons-compress:jar:sources", "org.apache.commons:commons-lang3", "org.apache.commons:commons-lang3:jar:sources", "org.apache.maven:maven-artifact", @@ -1689,6 +1797,10 @@ "com.squareup:kotlinpoet-ksp", "com.squareup:kotlinpoet-ksp:jar:sources", "com.squareup:kotlinpoet:jar:sources", + "commons-codec:commons-codec", + "commons-codec:commons-codec:jar:sources", + "commons-io:commons-io", + "commons-io:commons-io:jar:sources", "dev.zacsweers.autoservice:auto-service-ksp", "dev.zacsweers.autoservice:auto-service-ksp:jar:sources", "javax.annotation:javax.annotation-api", @@ -1707,6 +1819,8 @@ "org.apache.ant:ant-launcher", "org.apache.ant:ant-launcher:jar:sources", "org.apache.ant:ant:jar:sources", + "org.apache.commons:commons-compress", + "org.apache.commons:commons-compress:jar:sources", "org.apache.commons:commons-lang3", "org.apache.commons:commons-lang3:jar:sources", "org.apache.maven:maven-artifact", diff --git a/src/test/kotlin/io/bazel/kotlin/integration/BUILD.bazel b/src/test/kotlin/io/bazel/kotlin/integration/BUILD.bazel index 8ae74f48b..03b7c23b0 100644 --- a/src/test/kotlin/io/bazel/kotlin/integration/BUILD.bazel +++ b/src/test/kotlin/io/bazel/kotlin/integration/BUILD.bazel @@ -20,6 +20,24 @@ kt_jvm_test( ], ) +kt_jvm_test( + name = "KspCompileTest", + srcs = [ + "KspCompileTest.kt", + ], + resources = [ + "//:rules_kotlin_release.tgz", + ], + test_class = "io.bazel.kotlin.integration.KspCompileTest", + deps = [ + ":lib", + "//kotlin/compiler:kotlin-test", + "@kotlin_rules_maven//:com_google_truth_truth", + "@kotlin_rules_maven//:junit_junit", + "@kotlin_rules_maven//:org_apache_commons_commons_compress", + ], +) + kt_jvm_test( name = "MixedSourceCompileTest", srcs = ["MixedSourceCompileTest.kt"], diff --git a/src/test/kotlin/io/bazel/kotlin/integration/KspCompileTest.kt b/src/test/kotlin/io/bazel/kotlin/integration/KspCompileTest.kt new file mode 100644 index 000000000..69befccf1 --- /dev/null +++ b/src/test/kotlin/io/bazel/kotlin/integration/KspCompileTest.kt @@ -0,0 +1,244 @@ +package io.bazel.kotlin.integration + +import com.google.common.truth.Truth.assertWithMessage +import io.bazel.kotlin.integration.RulesKotlinWorkspace.Companion.build +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream +import org.junit.Test +import java.nio.file.Files.copy +import java.nio.file.Files.createTempDirectory +import java.nio.file.Path +import java.nio.file.StandardCopyOption +import java.util.zip.GZIPInputStream +import kotlin.io.path.createDirectories + +class KspCompileTest { + + private fun unpackReleaseArchive(): Path { + return RulesKotlinWorkspace::class.java.classLoader.getResourceAsStream("_release.tgz") + ?.let { stream -> + createTempDirectory("rules_kotlin_release-") + .also { tmp -> + TarArchiveInputStream(GZIPInputStream(stream)).use { archive -> + generateSequence(archive::getNextEntry).forEach { entry -> + if (!entry.isDirectory) { + copy( + archive, + tmp.resolve(entry.name).createDirectories(), + StandardCopyOption.REPLACE_EXISTING, + ) + } + } + } + } + } + ?: error("Cannot find release repo") + } + + @Test + fun `test ksp compile`() { + val workspace = WriteWorkspace.using { + module { + "bazel_dep"( + "name" `=` "rules_kotlin", + ) + "local_path_override"( + "module_name" `=` "rules_kotlin", + "path" `=` unpackReleaseArchive().toString(), + ) + "bazel_dep"("name" `=` "bazel_skylib", "version" `=` "1.4.2") + "bazel_dep"("name" `=` "rules_java", "version" `=` "7.4.0") + "bazel_dep"("name" `=` "rules_jvm_external", "version" `=` "5.3") + + "maven = use_extension"("@rules_jvm_external//:extensions.bzl", "maven") + "maven.install"( + "name" `=` "maven", + "artifacts".list( + "com.squareup.moshi:moshi:1.14.0", + "com.squareup.moshi:moshi-kotlin:1.14.0", + "com.squareup.moshi:moshi-kotlin-codegen:1.14.0", + "com.google.auto.service:auto-service-annotations:jar:1.1.1", + "com.google.auto.value:auto-value:1.10.1", + "com.google.auto.value:auto-value-annotations:1.10.1", + "dev.zacsweers.autoservice:auto-service-ksp:jar:1.1.0", + ), + "repositories".list( + "https://maven.google.com", + "https://repo1.maven.org/maven2", + ), + ) + "use_repo"(!"maven", "maven") + } + + build { + load("@rules_kotlin//kotlin:core.bzl", "define_kt_toolchain") + "define_kt_toolchain"("name" `=` "kotlin_toolchain") + } + + "coffee" { + kotlin("CoffeeAppModel.kt") { + +""" + package coffee + + import com.squareup.moshi.JsonClass + + @JsonClass(generateAdapter = true) + data class CoffeeAppModel(val id: String) + """.trimIndent() + } + + + java("CoffeeAppJavaModel.java") { + +""" + package coffee; + + import com.google.auto.value.AutoValue; + + @AutoValue + public abstract class CoffeeAppJavaModel { + + abstract CoffeeAppModel coffeeAppModel(); + + Builder builder() { + return new AutoValue_CoffeeAppJavaModel.Builder(); + } + + @AutoValue.Builder + abstract static class Builder { + + abstract Builder setCoffeeAppModel(CoffeeAppModel coffeeAppModel); + + abstract CoffeeAppJavaModel build(); + } + } + """.trimIndent() + } + java("CoffeeAppService.java") { + `package`("coffee") + +""" + import com.google.auto.service.AutoService; + + @AutoService(Object.class) + public class CoffeeAppService { + } + """.trimIndent() + } + + kotlin("CoffeeApp.kt") { + +""" + /* + * Copyright 2018 The Bazel Authors. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package coffee + + import com.squareup.moshi.Moshi + + class CoffeeApp { + + companion object { + + private val adapter = CoffeeAppModelJsonAdapter(Moshi.Builder().build()) + private val d = AutoValue_CoffeeAppJavaModel.Builder() + .setCoffeeAppModel(CoffeeAppModel("1")) + .build() + + @JvmStatic + fun main(args: Array) { + println( + adapter.toJson(d.coffeeAppModel()) + ) + } + } + } + """.trimIndent() + } + + build { + +""" + load("@bazel_skylib//rules:build_test.bzl", "build_test") + load("@rules_java//java:defs.bzl", "java_binary", "java_plugin") + load("@rules_kotlin//kotlin:core.bzl", "kt_compiler_plugin", "kt_ksp_plugin", "kt_plugin_cfg") + load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library") + + package(default_visibility = ["//visibility:public"]) + + java_plugin( + name = "autovalue", + generates_api = True, + processor_class = "com.google.auto.value.processor.AutoValueProcessor", + deps = ["@maven//:com_google_auto_value_auto_value"], + ) + + kt_ksp_plugin( + name = "moshi-kotlin-codegen", + processor_class = "com.squareup.moshi.kotlin.codegen.ksp.JsonClassSymbolProcessorProvider", + deps = [ + "@maven//:com_squareup_moshi_moshi", + "@maven//:com_squareup_moshi_moshi_kotlin", + "@maven//:com_squareup_moshi_moshi_kotlin_codegen", + ], + ) + + kt_ksp_plugin( + name = "autoservice", + processor_class = "dev.zacsweers.autoservice.ksp.AutoServiceSymbolProcessor${"$"}Provider", + deps = [ + "@maven//:com_google_auto_service_auto_service_annotations", + "@maven//:dev_zacsweers_autoservice_auto_service_ksp", + ], + ) + + kt_jvm_library( + name = "coffee_lib", + srcs = glob([ + "*.kt", + "*.java", + ]), + plugins = [ + ":moshi-kotlin-codegen", + ":autovalue", + ":autoservice", + ], + deps = [ + "@maven//:com_google_auto_service_auto_service_annotations", + "@maven//:com_google_auto_value_auto_value_annotations", + "@maven//:com_squareup_moshi_moshi", + "@maven//:com_squareup_moshi_moshi_kotlin", + ], + ) + + java_binary( + name = "coffee_app", + main_class = "coffee.CoffeeApp", + visibility = ["//visibility:public"], + runtime_deps = [":coffee_lib"], + ) + + build_test( + name = "force_build_app_test", + targets = [ + ":coffee_app", + # build_test doesn't actually fail unless you force the deploy jar to be built + ":coffee_app_deploy.jar", + ], + ) + """.trimIndent() + } + } + } + workspace.build("//coffee:force_build_app_test").run { + assertWithMessage("\noutput:\n $out\nerr:\n $err").that(exit).isEqualTo(0) + } + } +} diff --git a/src/test/kotlin/io/bazel/kotlin/integration/WriteWorkspace.kt b/src/test/kotlin/io/bazel/kotlin/integration/WriteWorkspace.kt index 4ec5d87a9..ccb22d945 100644 --- a/src/test/kotlin/io/bazel/kotlin/integration/WriteWorkspace.kt +++ b/src/test/kotlin/io/bazel/kotlin/integration/WriteWorkspace.kt @@ -19,9 +19,10 @@ object WriteWorkspace { } inline fun using(noinline contents: Workspace.() -> Unit): Path = using( - CLASS::class.run { qualifiedName ?: simpleName ?: error("Cannot use unnamed class") }, contents + CLASS::class.run { qualifiedName ?: simpleName ?: error("Cannot use unnamed class") }, contents, ) + /** * [open] the [root] of an existing [Workspace] and apply [contents] * @@ -99,7 +100,7 @@ object WriteWorkspace { fun Array.render( separator: String = " ", prefix: String = "", - postfix: String = "" + postfix: String = "", ): CharSequence = Rendered(joinToString(separator, prefix, postfix) { it.render() }) operator fun CharSequence.not(): CharSequence = Rendered(this) @@ -128,7 +129,7 @@ object WriteWorkspace { operator fun CharSequence.invoke( argument: CharSequence, vararg arguments: CharSequence, - contents: Define.() -> Unit + contents: Define.() -> Unit, ) { define(this, *(arrayOf(argument) + arguments), contents = contents) } @@ -167,8 +168,15 @@ object WriteWorkspace { call(toString(), *arguments) } - fun CharSequence.list(vararg elements:Any?) : Pair { - return this to !"[${elements.joinToString(","){ it.render() }}]" + fun CharSequence.`= list`(vararg elements: Any?): Pair { + return list(*elements) + } + + infix fun CharSequence.`=`(element: Any?): Pair { + return this to element.render() + } + fun CharSequence.list(vararg elements: Any?): Pair { + return this to !"[${elements.joinToString(",") { it.render() }}]" } } @@ -205,17 +213,20 @@ object WriteWorkspace { fun local_repository(name: CharSequence, location: Path) { "local_repository"( "name" to name, - "path" to location.toString() + "path" to location.toString(), ) } } + interface BzlModule : Starlark { + } + interface Bzl : Starlark, Define { override fun define( name: CharSequence, vararg arguments: CharSequence, - contents: Define.() -> Unit + contents: Define.() -> Unit, ) { block(arguments.render(prefix = "$name(", separator = ", ", postfix = ")")) { contents() @@ -225,7 +236,7 @@ object WriteWorkspace { fun rule( name: String, implementation: CharSequence, - vararg attributes: Pair + vararg attributes: Pair, ) { +"$name = rule(" indent { @@ -251,7 +262,7 @@ object WriteWorkspace { override fun define( name: CharSequence, vararg arguments: CharSequence, - contents: Define.() -> Unit + contents: Define.() -> Unit, ) { block(arguments.render(separator = ", ", prefix = "$name(", postfix = ")")) { contents() @@ -325,6 +336,7 @@ object WriteWorkspace { .sorted() .reversed() .forEach(Files::deleteIfExists) + else -> Files.deleteIfExists(location) } } @@ -372,6 +384,12 @@ object WriteWorkspace { } } + fun module(contents: BzlModule.() -> Unit) { + new("MODULE.bazel") { + object : BzlModule, Text by this {}.apply(contents) + } + } + override fun close() { if (!exists(resolve("WORKSPACE"))) { workspace { @@ -408,7 +426,7 @@ object WriteWorkspace { override fun write( path: String, contents: ByteArray, - vararg options: StandardOpenOption + vararg options: StandardOpenOption, ): Path { Files.deleteIfExists(resolve(path)) return super.write(path, contents, *options) @@ -439,7 +457,7 @@ object WriteWorkspace { private class CreateWorkspace( root: Path, - write: WriteStructure = WriteStructure(root, root) + write: WriteStructure = WriteStructure(root, root), ) : Workspace, WritePackage, Structure by write { override fun close() { super.close() @@ -449,7 +467,7 @@ object WriteWorkspace { private class ModifyWorkspace( root: Path, - val replace: ReplaceStructure = ReplaceStructure(root, root) + val replace: ReplaceStructure = ReplaceStructure(root, root), ) : ChangePackage, Structure by replace { override fun resolve(path: String): Path { check(!path.endsWith("WORKSPACE")) {