From 3baab2e31b269caaa65ed060f953c5c2ea46ac76 Mon Sep 17 00:00:00 2001 From: James Judd Date: Mon, 9 Sep 2024 12:30:34 -0600 Subject: [PATCH] Update to latest rules_scala_annex --- .bazelrc | 3 ++ BUILD.bazel | 1 + WORKSPACE | 16 +++--- test/BUILD.bazel | 2 +- twirl-compiler/BUILD.bazel | 3 +- .../CommandLineTwirlTemplateCompiler.scala | 52 +++++++++++-------- 6 files changed, 44 insertions(+), 33 deletions(-) diff --git a/.bazelrc b/.bazelrc index dde1a8d..8e2ee85 100644 --- a/.bazelrc +++ b/.bazelrc @@ -4,6 +4,9 @@ common --noenable_bzlmod common:ci --color=yes +build --experimental_worker_multiplex_sandboxing +build --experimental_worker_cancellation + build --java_language_version="21" build --java_runtime_version="remotejdk_21" build --tool_java_language_version="21" diff --git a/BUILD.bazel b/BUILD.bazel index 132a92f..b6a5268 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -9,6 +9,7 @@ default_java_toolchain( name = "repository_default_toolchain_21", configuration = DEFAULT_TOOLCHAIN_CONFIGURATION, java_runtime = "@rules_java//toolchains:remotejdk_21", + javac_supports_worker_multiplex_sandboxing = True, source_version = "21", target_version = "21", ) diff --git a/WORKSPACE b/WORKSPACE index 04a43d8..d3d1231 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -5,9 +5,9 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") # rules_java http_archive( name = "rules_java", - sha256 = "647bb31c0d51882549def6f67ee9078df697043406ed4a5144bbdf3b17f91e33", + sha256 = "41131de4417de70b9597e6ebd515168ed0ba843a325dc54a81b92d7af9a7b3ea", urls = [ - "https://github.com/bazelbuild/rules_java/releases/download/7.8.0/rules_java-7.8.0.tar.gz", + "https://github.com/bazelbuild/rules_java/releases/download/7.9.0/rules_java-7.9.0.tar.gz", ], ) @@ -59,11 +59,11 @@ load( twirl_compiler_cli_test_pinned_maven_install() # higherkindness/rules_scala -rules_scala_annex_version = "c8c4345e3f354753ed4ae7830618467ab59262c6" +rules_scala_annex_version = "f23c16037db66efb541dbbf5e17e6604886c85ff" http_archive( name = "rules_scala_annex", - integrity = "sha256-pmES8mOOeirB5woNYT2w97+5+C0Bt8ghrj9lHBKkMy8=", + integrity = "sha256-b/cPeh6J1Mq63u6fSWdEHAKL/kWfPhZcNL7m9If7PWM=", strip_prefix = "rules_scala-{}".format(rules_scala_annex_version), type = "zip", url = "https://github.com/lucidsoftware/rules_scala/archive/{}.zip".format(rules_scala_annex_version), @@ -200,17 +200,17 @@ load_env_vars( ) # rules_twirl (for tests) -rules_twirl_version = "299de4ffed6950dac2696098e364e94f67ca0eb5" +rules_twirl_version = "bc5652a7fd003cf6c45951c6e375c96b76c6e3ef" http_archive( - name = "io_bazel_rules_twirl", - sha256 = "84020420828292888d815c887572e76a62b3f82cd2d16ab3f748642b08251002", + name = "rules_twirl", + sha256 = "035c0599a0a5390452811ee4845e2907ac146e47d7dc47188cbe13446e8d5ba1", strip_prefix = "rules_twirl-{}".format(rules_twirl_version), type = "zip", url = "https://github.com/lucidsoftware/rules_twirl/archive/{}.zip".format(rules_twirl_version), ) -load("@io_bazel_rules_twirl//:workspace.bzl", "twirl_repositories") +load("@rules_twirl//:workspace.bzl", "twirl_repositories") twirl_repositories() diff --git a/test/BUILD.bazel b/test/BUILD.bazel index a154077..89214f2 100644 --- a/test/BUILD.bazel +++ b/test/BUILD.bazel @@ -1,7 +1,7 @@ # Borrowed from lucidsoftware/rules_twirl but using //twirl-compiler for the compiler cli load("@rules_scala_annex//rules:scala.bzl", "scala_test") -load("@io_bazel_rules_twirl//twirl:twirl.bzl", "twirl_templates") +load("@rules_twirl//twirl:twirl.bzl", "twirl_templates") twirl_templates( name = "twirl-test-templates-basic", diff --git a/twirl-compiler/BUILD.bazel b/twirl-compiler/BUILD.bazel index ec9aec2..a6a8b57 100644 --- a/twirl-compiler/BUILD.bazel +++ b/twirl-compiler/BUILD.bazel @@ -10,8 +10,9 @@ scala_library( tags = ["maven_coordinates=com.lucidchart:" + artifact_id + ":{pom_version}"], visibility = ["//visibility:public"], deps = [ - "@rules_scala_annex//src/main/scala/higherkindness/rules_scala/common/worker", "@rules_scala_annex//src/main/scala/higherkindness/rules_scala/common/error", + "@rules_scala_annex//src/main/scala/higherkindness/rules_scala/common/sandbox", + "@rules_scala_annex//src/main/scala/higherkindness/rules_scala/common/worker", "@twirl_compiler_cli_maven//:com_github_scopt_scopt_3", "@twirl_compiler_cli_maven//:org_playframework_twirl_twirl_compiler_3", ], diff --git a/twirl-compiler/src/main/scala/CommandLineTwirlTemplateCompiler.scala b/twirl-compiler/src/main/scala/CommandLineTwirlTemplateCompiler.scala index ee522f7..37f62e9 100644 --- a/twirl-compiler/src/main/scala/CommandLineTwirlTemplateCompiler.scala +++ b/twirl-compiler/src/main/scala/CommandLineTwirlTemplateCompiler.scala @@ -1,48 +1,54 @@ package rulestwirl.twirl import higherkindness.rules_scala.common.error.AnnexWorkerError +import higherkindness.rules_scala.common.sandbox.SandboxUtil import higherkindness.rules_scala.common.worker.WorkerMain import play.twirl.compiler.TwirlCompiler import java.io.{File, PrintStream} -import java.nio.file.{Files, Paths} -import scala.collection.JavaConverters._ +import java.nio.file.{Files, Path, Paths} +import scala.jdk.CollectionConverters._ import scala.util.boundary, boundary.break import scopt.OParser object CommandLineTwirlTemplateCompiler extends WorkerMain[Unit] { case class Config( - additionalImports: Seq[String] = Seq.empty[String], - source: File = new File("."), - sourceDirectory: File = new File("."), - templateFormats: Map[String, String] = Map.empty[String, String], - output: File = new File("."), + var additionalImports: List[String] = List.empty[String], + var source: Path = Paths.get("."), + var sourceDirectory: Path = Paths.get("."), + var templateFormats: Map[String, String] = Map.empty[String, String], + var output: Path = Paths.get("."), ) val builder = OParser.builder[Config] - val parser = { + def parser(workDir: Path) = { import builder._ OParser.sequence( programName("twirl-compiler"), head("Twirl Template Compiler", "0.2"), - arg[File]("").required().action { (value, config) => - config.copy(output = value) + arg[Path]("").required().action { (value, config) => + config.output = SandboxUtil.getSandboxPath(workDir, value) + config }.text("output file"), - arg[File]("").required().action { (value, config) => - config.copy(sourceDirectory = value) + arg[Path]("").required().action { (value, config) => + config.sourceDirectory = SandboxUtil.getSandboxPath(workDir, value) + config }.text("root source directory"), - arg[File]("").unbounded().required().action { (value, config) => - config.copy(source = value) + arg[Path]("").unbounded().required().action { (value, config) => + config.source = SandboxUtil.getSandboxPath(workDir, value) + config }.text("source file"), opt[String]('i', "additionalImport").valueName("").unbounded().action { (value, config) => - config.copy(additionalImports = config.additionalImports ++ Seq(value)) + config.additionalImports = config.additionalImports ++ List(value) + config }.text("additional imports to add to the compiled templates"), opt[(String, String)]('t', "templateFormat").unbounded().action({ case ((key, value), config) => - config.copy(templateFormats = config.templateFormats + (key -> value)) + config.templateFormats = config.templateFormats + (key -> value) + config }).keyValueName("format", "formatterType").text("additional template formats to use when compiling templates"), ) } @@ -50,12 +56,12 @@ object CommandLineTwirlTemplateCompiler extends WorkerMain[Unit] { def compileTwirl(config: Config): Unit = { val templateFormats = defaultFormats ++ config.templateFormats - val extension = config.source.getName.split('.').last + val extension = config.source.getFileName().toString().split('.').last val formatterType = templateFormats(extension) val result = TwirlCompiler.compileVirtual( - content = new String(Files.readAllBytes(config.source.toPath)), - source = config.source, - sourceDirectory = config.sourceDirectory, + content = new String(Files.readAllBytes(config.source)), + source = config.source.toFile(), + sourceDirectory = config.sourceDirectory.toFile(), formatterType = formatterType, additionalImports = config.additionalImports.map(_.replace("%format%", extension)), inclusiveDot = false, @@ -64,18 +70,18 @@ object CommandLineTwirlTemplateCompiler extends WorkerMain[Unit] { // TwirlCompiler.compileVirtual generates a footer comment that contains non-reproducible metatdata; remove it val sansMetadata = result.content.split("\n").span(s => !s.contains("-- GENERATED --"))._1.dropRight(1).mkString("\n") - Files.write(config.output.toPath, sansMetadata.getBytes) + Files.write(config.output, sansMetadata.getBytes) } override def init(args: Option[Array[String]]): Unit = () - protected def work(ctx: Unit, args: Array[String], out: PrintStream): Unit = { + protected def work(ctx: Unit, args: Array[String], out: PrintStream, workDir: Path, verbosity: Int): Unit = { val finalArgs = args.toList.flatMap { case arg if arg.startsWith("@") => Files.readAllLines(Paths.get(arg.tail)).asScala case arg => Array(arg) } - OParser.parse(parser, finalArgs, Config()).map(compileTwirl).getOrElse { + OParser.parse(parser(workDir), finalArgs, Config()).map(compileTwirl).getOrElse { throw new AnnexWorkerError(3) } }