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 aa7e9c8
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 29 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
48 changes: 38 additions & 10 deletions src/main/scala/cloud/golem/GolemScalaPluginInternal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,58 @@ 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.task {
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
)

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 aa7e9c8

Please sign in to comment.