Skip to content

Commit

Permalink
Improve command check (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
danieletorelli authored May 8, 2024
1 parent 1ccbdd8 commit 6212b0b
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/main/scala/cloud/golem/WasmComponentPluginInternal.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ private[golem] object WasmComponentPluginInternal {
lazy val wasmComponentWitFullPath = Def.task(
wasmComponentWitPath.value / s"${wasmComponentWitName.value}.wit"
)
def checkCommandOrFail(command: String)(error: => String): Unit = {
import scala.sys.process.*
val commandExists = Seq("bash", "-xc", s"which $command").! == 0
if (!commandExists) sys.error(error)
}
Def.settings(
wasmComponentOutputDirectory := target.value / "dist",
wasmComponentWitPath := (ThisBuild / baseDirectory).value / "wit",
Expand All @@ -32,32 +37,37 @@ private[golem] object WasmComponentPluginInternal {
import scala.sys.process.*

val bindGenCommand = "golem-scalajs-wit-bindgen"
val bindgenCommandExists = Seq(
"bash",
"-xc",
s"which $bindGenCommand"
).! == 0
if (!bindgenCommandExists) {
sys.error(s"""
|$bindGenCommand not found. Run `cargo install $bindGenCommand`.
|https://learn.golem.cloud/docs/building-components/tier-1/scala
""".stripMargin)
checkCommandOrFail(bindGenCommand) {
s"""
|$bindGenCommand not found.
|
|Run `cargo install $bindGenCommand` or
|refer to https://learn.golem.cloud/docs/building-components/tier-1/scala for installation instructions.
""".stripMargin
}

val output = Seq(
"bash",
"-xc",
s"$bindGenCommand -w ${wasmComponentWitFullPath.value} -p ${wasmComponentPackageName.value}"
).!!

IO.write(wasmComponentWitBindgenOutput, output)
Seq(wasmComponentWitBindgenOutput)
}
},
wasmComponent := {
import scala.sys.process.*
Seq("bash", "-xc", "npm install").!!
Seq("bash", "-xc", "npm run build").!!

val npmCommand = "npm"
checkCommandOrFail(npmCommand) {
s"""
|$npmCommand command not found.
|
|Refer to https://nodejs.org/en/download for installation instructions.
""".stripMargin
}

Seq("bash", "-xc", s"$npmCommand install").!!
Seq("bash", "-xc", s"$npmCommand run build").!!
},
wasmComponent := (wasmComponent dependsOn (Compile / fullLinkJS)).value,
Compile / sourceGenerators += Def.taskIf {
Expand Down

0 comments on commit 6212b0b

Please sign in to comment.