Skip to content

Commit

Permalink
Add settings and tasks (#3)
Browse files Browse the repository at this point in the history
* Add settings and tasks
* Fix tests
* Cleanup settings
* Add golemScalaOutputDirectory
  • Loading branch information
danieletorelli authored May 8, 2024
1 parent 6a0d0d2 commit 29a798d
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 41 deletions.
51 changes: 26 additions & 25 deletions src/main/scala/cloud/golem/GolemScalaPlugin.scala
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
package cloud.golem

import sbt.*
import sbt.Keys.*
import org.scalajs.sbtplugin.ScalaJSPlugin
import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport.*
import sbt.plugins.JvmPlugin

object GolemScalaPlugin extends AutoPlugin {
private object Versions {
val macros = "0.1.0"
val scalaMacrosParadise = "2.1.1"
object autoImport {
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[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")
}

override def trigger: PluginTrigger = allRequirements

override def requires: Plugins = JvmPlugin && ScalaJSPlugin

override lazy val projectSettings: Seq[Setting[?]] = Seq(
scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.ESModule) },
libraryDependencies += "cloud.golem" %% "golem-scala-macros" % Versions.macros
) ++ macroParadiseSettings

private lazy val macroParadiseSettings = Seq(
scalacOptions ++= {
if (scalaVersion.value.startsWith("2.13")) Seq("-Ymacro-annotations")
else Nil
},
libraryDependencies ++= {
if (scalaVersion.value.startsWith("2.12")) {
Seq(
compilerPlugin(
"org.scalamacros" % "paradise" % Versions.scalaMacrosParadise cross CrossVersion.full
)
)
} else Nil
}
)
override lazy val projectSettings: Seq[Setting[?]] =
GolemScalaPluginInternal.baseSettings ++
GolemScalaPluginInternal.scalaJsSettings ++
GolemScalaPluginInternal.macroParadiseSettings
}
86 changes: 86 additions & 0 deletions src/main/scala/cloud/golem/GolemScalaPluginInternal.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package cloud.golem

import sbt.*
import sbt.Keys.*
import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport.*

private[golem] object GolemScalaPluginInternal {
import GolemScalaPlugin.autoImport.*

private object Versions {
val macros = "0.1.0"
val scalaMacrosParadise = "2.1.1"
}

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 := moduleName.value,
witBindgen := {
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,
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
)

lazy val macroParadiseSettings: Seq[Setting[?]] = Def.settings(
scalacOptions ++= {
if (scalaVersion.value.startsWith("2.13")) Seq("-Ymacro-annotations")
else Nil
},
libraryDependencies ++= {
if (scalaVersion.value.startsWith("2.12")) {
Seq(
compilerPlugin(
"org.scalamacros" % "paradise" % Versions.scalaMacrosParadise cross CrossVersion.full
)
)
} else Nil
}
)
}
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 29a798d

Please sign in to comment.