Skip to content

Commit

Permalink
Update to latest rules_scala_annex
Browse files Browse the repository at this point in the history
  • Loading branch information
jjudd committed Sep 9, 2024
1 parent c723830 commit 3baab2e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 33 deletions.
3 changes: 3 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Expand Down
16 changes: 8 additions & 8 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
3 changes: 2 additions & 1 deletion twirl-compiler/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,61 +1,67 @@
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]("<output>").required().action { (value, config) =>
config.copy(output = value)
arg[Path]("<output>").required().action { (value, config) =>
config.output = SandboxUtil.getSandboxPath(workDir, value)
config
}.text("output file"),

arg[File]("<sourceDirectory>").required().action { (value, config) =>
config.copy(sourceDirectory = value)
arg[Path]("<sourceDirectory>").required().action { (value, config) =>
config.sourceDirectory = SandboxUtil.getSandboxPath(workDir, value)
config
}.text("root source directory"),

arg[File]("<source>").unbounded().required().action { (value, config) =>
config.copy(source = value)
arg[Path]("<source>").unbounded().required().action { (value, config) =>
config.source = SandboxUtil.getSandboxPath(workDir, value)
config
}.text("source file"),

opt[String]('i', "additionalImport").valueName("<import>").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"),
)
}

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,
Expand All @@ -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)
}
}
Expand Down

0 comments on commit 3baab2e

Please sign in to comment.