From 81353191bd2239273ef319c6f0714ab633409a0e Mon Sep 17 00:00:00 2001 From: Daniele Torelli Date: Wed, 8 May 2024 12:49:49 -0600 Subject: [PATCH] Add golemScalaOutputDirectory --- .../scala/cloud/golem/GolemScalaPlugin.scala | 21 ++++++-- .../golem/GolemScalaPluginInternal.scala | 49 +++++++++++++++---- src/sbt-test/golem-scala/example1/test | 16 +++--- src/sbt-test/golem-scala/example2/test | 16 +++--- 4 files changed, 73 insertions(+), 29 deletions(-) diff --git a/src/main/scala/cloud/golem/GolemScalaPlugin.scala b/src/main/scala/cloud/golem/GolemScalaPlugin.scala index a4697c0..dd09068 100644 --- a/src/main/scala/cloud/golem/GolemScalaPlugin.scala +++ b/src/main/scala/cloud/golem/GolemScalaPlugin.scala @@ -6,10 +6,25 @@ import sbt.plugins.JvmPlugin object GolemScalaPlugin extends AutoPlugin { object autoImport { - lazy val golemScalaWitPath = settingKey[File]("Path to the wit file") - lazy val golemScalaPackageName = settingKey[String]("Package name") + lazy val golemScalaOutputDirectory = SettingKey[File]( + "golemScalaOutputDirectory", + "Output directory", + KeyRanks.Invisible + ) + lazy val golemScalaWitPath = SettingKey[File]( + "golemScalaWitPath", + "Path to the wit file", + KeyRanks.Invisible + ) + lazy val golemScalaPackageName = SettingKey[String]( + "golemScalaPackageName", + "Package name", + KeyRanks.Invisible + ) lazy val witBindgen = - taskKey[Unit]("Runs golem-scalajs-wit-bindgen to generate WIT bindings") + taskKey[Seq[File]]( + "Runs golem-scalajs-wit-bindgen to generate WIT bindings" + ) lazy val component = taskKey[Unit]("Runs componentize-js on the generated main.js file") } diff --git a/src/main/scala/cloud/golem/GolemScalaPluginInternal.scala b/src/main/scala/cloud/golem/GolemScalaPluginInternal.scala index 108c60e..875698a 100644 --- a/src/main/scala/cloud/golem/GolemScalaPluginInternal.scala +++ b/src/main/scala/cloud/golem/GolemScalaPluginInternal.scala @@ -12,30 +12,59 @@ private[golem] object GolemScalaPluginInternal { val scalaMacrosParadise = "2.1.1" } - lazy val baseSettings: Seq[Setting[?]] = + lazy val baseSettings: Seq[Setting[?]] = { + lazy val golemScalaWitFullPath = + Def + .task(golemScalaWitPath.value / s"${golemScalaPackageName.value}.wit") Def.settings( + golemScalaOutputDirectory := target.value / "dist", golemScalaWitPath := (ThisBuild / baseDirectory).value / "wit", - golemScalaPackageName := "main", + golemScalaPackageName := moduleName.value, witBindgen := { - val golemScalaWitFullPath = (ThisBuild / baseDirectory).value / golemScalaWitPath.value.getPath / s"${golemScalaPackageName.value}.wit" - import scala.sys.process.* - Seq( - "bash", - "-xc", - s"golem-scalajs-wit-bindgen -w $golemScalaWitFullPath -p ${golemScalaPackageName.value}" - ).!! + if (!golemScalaWitFullPath.value.exists()) { + sys.error(s""" + |'${golemScalaWitFullPath.value.getAbsolutePath}' does not exist. + |Make sure 'golemScalaPackageName' is set correctly in your build.sbt + """.stripMargin) + } else { + val golemScalaWitBindgenOutput = (Compile / sourceManaged).value / "scala" / golemScalaPackageName.value / "Api.scala" + import scala.sys.process.* + + val output = Seq( + "bash", + "-xc", + s"golem-scalajs-wit-bindgen -w ${golemScalaWitFullPath.value} -p ${golemScalaPackageName.value}" + ).!! + + IO.write(golemScalaWitBindgenOutput, output) + Seq(golemScalaWitBindgenOutput) + } }, component := { import scala.sys.process.* Seq("bash", "-xc", "npm install").!! Seq("bash", "-xc", "npm run build").!! }, - component := (component dependsOn (Compile / fullLinkJS)).value + component := (component dependsOn (Compile / fullLinkJS)).value, + Compile / sourceGenerators += Def.taskIf { + if (golemScalaWitFullPath.value.exists()) witBindgen.value + else { + println( + s""" + |'${golemScalaWitFullPath.value.getAbsolutePath}' does not exist. + |Make sure 'golemScalaPackageName' is set correctly in your build.sbt""".stripMargin + ) + Nil + } + }.taskValue ) + } lazy val scalaJsSettings: Seq[Setting[?]] = Def.settings( scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.ESModule) }, + Compile / fullLinkJS / scalaJSLinkerOutputDirectory := golemScalaOutputDirectory.value, + Compile / fastLinkJS / scalaJSLinkerOutputDirectory := golemScalaOutputDirectory.value, libraryDependencies += "cloud.golem" %% "golem-scala-macros" % Versions.macros ) diff --git a/src/sbt-test/golem-scala/example1/test b/src/sbt-test/golem-scala/example1/test index fb5bbf1..a1e038a 100644 --- a/src/sbt-test/golem-scala/example1/test +++ b/src/sbt-test/golem-scala/example1/test @@ -1,12 +1,12 @@ > +clean > +fullLinkJS -$ exists target/scala-2.12/root-opt/main.js -$ exists target/scala-2.12/root-opt/main.js.map -$ exists target/scala-2.13/root-opt/main.js -$ exists target/scala-2.13/root-opt/main.js.map +$ exists target/dist/main.js +$ exists target/dist/main.js.map +$ exists target/dist/main.js +$ exists target/dist/main.js.map > +clean > +fastLinkJS -$ exists target/scala-2.12/root-fastopt/main.js -$ exists target/scala-2.12/root-fastopt/main.js.map -$ exists target/scala-2.13/root-fastopt/main.js -$ exists target/scala-2.13/root-fastopt/main.js.map +$ exists target/dist/main.js +$ exists target/dist/main.js.map +$ exists target/dist/main.js +$ exists target/dist/main.js.map diff --git a/src/sbt-test/golem-scala/example2/test b/src/sbt-test/golem-scala/example2/test index fb5bbf1..a1e038a 100644 --- a/src/sbt-test/golem-scala/example2/test +++ b/src/sbt-test/golem-scala/example2/test @@ -1,12 +1,12 @@ > +clean > +fullLinkJS -$ exists target/scala-2.12/root-opt/main.js -$ exists target/scala-2.12/root-opt/main.js.map -$ exists target/scala-2.13/root-opt/main.js -$ exists target/scala-2.13/root-opt/main.js.map +$ exists target/dist/main.js +$ exists target/dist/main.js.map +$ exists target/dist/main.js +$ exists target/dist/main.js.map > +clean > +fastLinkJS -$ exists target/scala-2.12/root-fastopt/main.js -$ exists target/scala-2.12/root-fastopt/main.js.map -$ exists target/scala-2.13/root-fastopt/main.js -$ exists target/scala-2.13/root-fastopt/main.js.map +$ exists target/dist/main.js +$ exists target/dist/main.js.map +$ exists target/dist/main.js +$ exists target/dist/main.js.map