Skip to content

Commit

Permalink
upgrade to Sjsonnet 0.4.9 (was 0.4.0)
Browse files Browse the repository at this point in the history
There were some breaking changes:
1. replace `SjsonnetMain.createParseCache` with `DefaultParseCache`
2. external Maps are now passed as JSON-encoded strings (not `ujson.Value`s). We use `ujson.write`, mimicking what SjsonnetMain does.
3. the resolver is now a `Importer` trait

Also upgrades
 - scala library from 2.13.10 to 2.13.12
 - scalatest from 3.2.9 to 3.2.17
 - junit from 5.7.2 to 5.10.1
 - log4j from 2.17.1 to 2.22.1
  • Loading branch information
chklauser committed Jan 26, 2024
1 parent ee9689c commit 8895c55
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 24 deletions.
6 changes: 3 additions & 3 deletions examples/default-task/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ repositories {
}

dependencies {
implementation("org.scala-lang:scala-library:2.13.10")
testImplementation("org.scalatest:scalatest_2.13:3.2.9")
testImplementation(platform("org.junit:junit-bom:5.7.2"))
implementation("org.scala-lang:scala-library:2.13.12")
testImplementation("org.scalatest:scalatest_2.13:3.2.17")
testImplementation(platform("org.junit:junit-bom:5.10.1"))
testImplementation("org.junit.jupiter:junit-jupiter")
}

Expand Down
12 changes: 6 additions & 6 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
scala
`java-gradle-plugin`
`maven-publish`
id("com.gradle.plugin-publish") version "1.2.0"
id("com.gradle.plugin-publish") version "1.2.1"
}

group = "io.github.chklauser.sjsonnet"
Expand All @@ -13,16 +13,16 @@ repositories {
}

dependencies {
implementation("org.scala-lang:scala-library:2.13.10")
implementation("com.databricks:sjsonnet_2.13:0.4.0")
testImplementation("org.scalatest:scalatest_2.13:3.2.9")
testImplementation(platform("org.junit:junit-bom:5.7.2"))
implementation("org.scala-lang:scala-library:2.13.12")
implementation("com.databricks:sjsonnet_2.13:0.4.9")
testImplementation("org.scalatest:scalatest_2.13:3.2.17")
testImplementation(platform("org.junit:junit-bom:5.10.1"))
testImplementation("org.junit.jupiter:junit-jupiter")
constraints {
implementation("org.apache.logging.log4j:log4j-core") {
version {
strictly("[2.17, 3[")
prefer("2.17.1")
prefer("2.22.1")
}
because("CVE-2021-44228, CVE-2021-45046, CVE-2021-45105: Log4j vulnerable to remote code execution and other critical security vulnerabilities")
}
Expand Down
2 changes: 1 addition & 1 deletion plugin/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pluginManagement {
}

plugins {
id("org.ajoberstar.reckon.settings") version "0.18.0"
id("org.ajoberstar.reckon.settings") version "0.18.2"
}

extensions.configure<org.ajoberstar.reckon.gradle.ReckonExtension> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import sjsonnet.{Expr, FileScope, Path, SjsonnetMain}
import java.util.concurrent.atomic.AtomicBoolean
import scala.collection.mutable
import internal.implicits._
import sjsonnet.DefaultParseCache
import sjsonnet.ParseCache

class SjsonnetPlugin extends Plugin[Project] {
private[this] var sourceSetRegistered = new AtomicBoolean(false)
Expand Down Expand Up @@ -84,6 +86,6 @@ class SjsonnetPlugin extends Plugin[Project] {
}

object SjsonnetPlugin {
val parseCache: ThreadLocal[mutable.HashMap[(Path, String), Parsed[(Expr, FileScope)]]] =
ThreadLocal.withInitial(() => SjsonnetMain.createParseCache())
val parseCache: ThreadLocal[ParseCache] =
ThreadLocal.withInitial(() => new DefaultParseCache())
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import org.gradle.api.provider.{MapProperty, Property}
import org.gradle.api.tasks.{CacheableTask, Input, InputFiles, OutputDirectory, PathSensitive, PathSensitivity, SkipWhenEmpty, TaskAction}
import org.gradle.api.{DefaultTask, GradleException}
import os.Path
import sjsonnet.Importer
import sjsonnet.ResolvedFile
import sjsonnet.{OsPath, SjsonnetMain}

import java.io.{BufferedOutputStream, File, FileOutputStream, OutputStreamWriter, StringWriter}
Expand Down Expand Up @@ -92,9 +94,9 @@ class SjsonnetTask extends DefaultTask() {
case value => throw new GradleException(s"Cannot translate $path=$value into a JSON value to pass to Jsonnet.")
}}

private def externalMap(source: java.util.Map[String, Any]): Map[String, ujson.Value] = {
private def externalMap(source: java.util.Map[String, Any]): Map[String, String] = {
import scala.jdk.CollectionConverters._
source.asScala.toMap.map(entry => (entry._1, javaToJson("", entry._2)))
source.asScala.toMap.map(entry => (entry._1, ujson.write(javaToJson("", entry._2))))
}

@TaskAction
Expand Down Expand Up @@ -155,23 +157,27 @@ class SjsonnetTask extends DefaultTask() {
val workingDirectory = sjsonnet.OsPath(os.Path(sourceFile, os.Path(sourceFile)))

val allowedImports = if(knownImports.nonEmpty) { Some(knownImports) } else { None }
val resolver = SjsonnetMain.resolveImport(searchRoot, allowedImports) _
val resolverWithLogging = (p:sjsonnet.Path,s: String) => {
val result = resolver(p,s)
if(getLogger.isDebugEnabled) {
getLogger.debug("jsonnet import working_directory={}, import={} ==> {}", Array(
p, s, result.map(_._1)
): _*)
val importer = SjsonnetMain.resolveImport(searchRoot, allowedImports)
val importerWithLogging = new Importer {
override def resolve(docBase: sjsonnet.Path, importName: String): Option[sjsonnet.Path] = {
val result = importer.resolve(docBase, importName)
if(getLogger.isDebugEnabled) {
getLogger.debug("jsonnet import working_directory={}, import={} ==> {}", Array(
docBase, importName, result
): _*)
}
result
}
result

override def read(path: sjsonnet.Path): Option[ResolvedFile] = importer.read(path)
}

new sjsonnet.Interpreter(
SjsonnetPlugin.parseCache.get(),
externalMap(externalVariables.get()),
externalMap(topLevelArguments.get()),
workingDirectory,
resolverWithLogging)
importerWithLogging,
SjsonnetPlugin.parseCache.get())
}

private def bufferedUtf8OutputStreamWriter(destinationFile: File) = {
Expand Down

0 comments on commit 8895c55

Please sign in to comment.