-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add settings and tasks * Fix tests * Cleanup settings * Add golemScalaOutputDirectory
- Loading branch information
1 parent
6a0d0d2
commit 29a798d
Showing
4 changed files
with
128 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |