Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve command check #13

Merged
merged 1 commit into from
May 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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