Skip to content

Commit

Permalink
Add golemScalaOutputDirectory
Browse files Browse the repository at this point in the history
  • Loading branch information
danieletorelli committed May 8, 2024
1 parent 8463f5b commit 9307d94
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 24 deletions.
21 changes: 18 additions & 3 deletions src/main/scala/cloud/golem/GolemScalaPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
25 changes: 20 additions & 5 deletions src/main/scala/cloud/golem/GolemScalaPluginInternal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,45 @@ private[golem] object GolemScalaPluginInternal {
val scalaMacrosParadise = "2.1.1"
}

lazy val baseSettings: Seq[Setting[?]] =
lazy val baseSettings: Seq[Setting[?]] = {
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"
val golemScalaWitBindgenOutput = (Compile / sourceManaged).value / "scala" / golemScalaPackageName.value / "Api.scala"
val golemScalaWitFullPath = golemScalaWitPath.value / s"${golemScalaPackageName.value}.wit"
import scala.sys.process.*
Seq(
if (!golemScalaWitFullPath.exists()) {
sys.error(s"""
|'${golemScalaWitFullPath.getAbsolutePath}' does not exist.
|Make sure 'golemScalaPackageName' is set in your build.sbt
""".stripMargin)
}
val output = Seq(
"bash",
"-xc",
s"golem-scalajs-wit-bindgen -w $golemScalaWitFullPath -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 += witBindgen.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
)

Expand Down
16 changes: 8 additions & 8 deletions src/sbt-test/golem-scala/example1/test
Original file line number Diff line number Diff line change
@@ -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
16 changes: 8 additions & 8 deletions src/sbt-test/golem-scala/example2/test
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9307d94

Please sign in to comment.