From 212a2b8dc675a62c8a53e332d2f22b1ed933a929 Mon Sep 17 00:00:00 2001 From: Daniele Torelli Date: Mon, 19 Aug 2024 16:02:34 +0200 Subject: [PATCH 1/4] Remove duplicated checks --- src/sbt-test/scala-js/example1/test | 4 ---- src/sbt-test/scala-js/example2/test | 4 ---- 2 files changed, 8 deletions(-) diff --git a/src/sbt-test/scala-js/example1/test b/src/sbt-test/scala-js/example1/test index a1e038a..045d4fe 100644 --- a/src/sbt-test/scala-js/example1/test +++ b/src/sbt-test/scala-js/example1/test @@ -2,11 +2,7 @@ > +fullLinkJS $ 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/dist/main.js $ exists target/dist/main.js.map -$ exists target/dist/main.js -$ exists target/dist/main.js.map diff --git a/src/sbt-test/scala-js/example2/test b/src/sbt-test/scala-js/example2/test index a1e038a..045d4fe 100644 --- a/src/sbt-test/scala-js/example2/test +++ b/src/sbt-test/scala-js/example2/test @@ -2,11 +2,7 @@ > +fullLinkJS $ 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/dist/main.js $ exists target/dist/main.js.map -$ exists target/dist/main.js -$ exists target/dist/main.js.map From efcec3d1d974b6c8af1df47cfb2c8e4a44aa491e Mon Sep 17 00:00:00 2001 From: Daniele Torelli Date: Mon, 19 Aug 2024 16:03:59 +0200 Subject: [PATCH 2/4] Add WASM ShoppingCart test --- src/sbt-test/wasm/shopping-cart/build.sbt | 7 ++ src/sbt-test/wasm/shopping-cart/package.json | 10 +++ .../shopping-cart/project/build.properties | 1 + .../wasm/shopping-cart/project/plugins.sbt | 7 ++ .../src/main/scala/example/ShoppingCart.scala | 87 +++++++++++++++++++ src/sbt-test/wasm/shopping-cart/test | 6 ++ .../wasm/shopping-cart/wit/example.wit | 37 ++++++++ 7 files changed, 155 insertions(+) create mode 100644 src/sbt-test/wasm/shopping-cart/build.sbt create mode 100644 src/sbt-test/wasm/shopping-cart/package.json create mode 100644 src/sbt-test/wasm/shopping-cart/project/build.properties create mode 100644 src/sbt-test/wasm/shopping-cart/project/plugins.sbt create mode 100644 src/sbt-test/wasm/shopping-cart/src/main/scala/example/ShoppingCart.scala create mode 100644 src/sbt-test/wasm/shopping-cart/test create mode 100644 src/sbt-test/wasm/shopping-cart/wit/example.wit diff --git a/src/sbt-test/wasm/shopping-cart/build.sbt b/src/sbt-test/wasm/shopping-cart/build.sbt new file mode 100644 index 0000000..3548a6e --- /dev/null +++ b/src/sbt-test/wasm/shopping-cart/build.sbt @@ -0,0 +1,7 @@ +ThisBuild / version := "0.1" +ThisBuild / scalaVersion := "2.13.13" +ThisBuild / crossScalaVersions += "2.12.19" + +lazy val root = (project in file(".")) + .enablePlugins(WasmComponentPlugin) + .settings(wasmComponentPackageName := "example") diff --git a/src/sbt-test/wasm/shopping-cart/package.json b/src/sbt-test/wasm/shopping-cart/package.json new file mode 100644 index 0000000..cbd07d5 --- /dev/null +++ b/src/sbt-test/wasm/shopping-cart/package.json @@ -0,0 +1,10 @@ +{ + "scripts": { + "build": "jco componentize -w wit -o target/dist/main.wasm target/dist/main.js", + "componentize": "npm run build" + }, + "devDependencies": { + "@bytecodealliance/componentize-js": "0.10.2", + "@bytecodealliance/jco": "1.4.0" + } +} \ No newline at end of file diff --git a/src/sbt-test/wasm/shopping-cart/project/build.properties b/src/sbt-test/wasm/shopping-cart/project/build.properties new file mode 100644 index 0000000..4d5f78c --- /dev/null +++ b/src/sbt-test/wasm/shopping-cart/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.9.9 \ No newline at end of file diff --git a/src/sbt-test/wasm/shopping-cart/project/plugins.sbt b/src/sbt-test/wasm/shopping-cart/project/plugins.sbt new file mode 100644 index 0000000..e9b8f58 --- /dev/null +++ b/src/sbt-test/wasm/shopping-cart/project/plugins.sbt @@ -0,0 +1,7 @@ +sys.props.get("plugin.version") match { + case Some(version) => + addSbtPlugin("cloud.golem" % "sbt-wasm-component" % version) + case _ => + sys.error("""|The system property 'plugin.version' is not defined. + |Specify this property using the scriptedLaunchOpts -D.""".stripMargin) +} diff --git a/src/sbt-test/wasm/shopping-cart/src/main/scala/example/ShoppingCart.scala b/src/sbt-test/wasm/shopping-cart/src/main/scala/example/ShoppingCart.scala new file mode 100644 index 0000000..02cd1bd --- /dev/null +++ b/src/sbt-test/wasm/shopping-cart/src/main/scala/example/ShoppingCart.scala @@ -0,0 +1,87 @@ +package example + +final case class State(userId: String, items: List[ProductItem]) { self => + def withUserId(userId: String): State = self.copy(userId = userId) + + def addItem(item: ProductItem): State = self.copy(items = self.items :+ item) + + def removeItem(productId: String): State = self.copy(items = self.items.filterNot(_.productId == productId)) + + def updateItemQuantity(productId: String, quantity: Integer): State = + self.copy(items = self.items.map { item => + if (item.productId == productId) ProductItem(item.productId, item.name, item.price, quantity) + else item + }) + + def clear: State = self.copy(items = List.empty) +} +object State { + val empty = State(userId = "", items = List.empty) +} + +@cloud.golem.WitExport +object ShoppingCart extends Api { self => + private var state = State.empty + + def initializeCart(userId: String): WitResult[String, String] = { + println(s"Initializing cart for user $userId") + if (math.random() > 0.1) { + state = state.withUserId(userId) + WitResult.ok(userId) + } else WitResult.err("Error while initializing cart") + } + + def addItem(item: ProductItem): Unit = { + println(s"Adding item to the cart of user ${state.userId}") + state = state.addItem(item) + } + + def removeItem(productId: String): Unit = { + println(s"Removing item with product ID $productId from the cart of user ${state.userId}") + state = state.removeItem(productId) + } + + def updateItemQuantity(productId: String, quantity: Integer): Unit = { + println(s"Updating quantity of item with product ID $productId to $quantity in the cart of user ${state.userId}") + + state = state.updateItemQuantity(productId, quantity) + } + + def checkout(): CheckoutResult = { + def reserveInventory(): Either[String, Unit] = + if (math.random() < 0.1) Left("Inventory not available") else Right(()) + + def chargeCreditCard(): Either[String, Unit] = Right(()) + + def generateOrder(): String = "238738674" + + def dispatchOrder(): Either[String, Unit] = Right(()) + + def clearState(): Unit = state = state.clear + + val result = + for { + _ <- reserveInventory() + _ <- chargeCreditCard() + orderId = generateOrder() + _ <- dispatchOrder() + _ = clearState() + _ = println(s"Checkout for order $orderId") + } yield OrderConfirmation(orderId) + + result match { + case Right(orderConfirmation) => CheckoutResult.success(orderConfirmation) + case Left(error) => CheckoutResult.error(error) + } + } + + def getCartContents(): WitList[ProductItem] = { + println(s"Getting cart contents for user ${state.userId}") + WitList.fromList(state.items) + } + + def getFirstItem(): WitOption[ProductItem] = { + println(s"Getting first item for user ${state.userId}") + WitOption.fromOption(state.items.headOption) + } +} \ No newline at end of file diff --git a/src/sbt-test/wasm/shopping-cart/test b/src/sbt-test/wasm/shopping-cart/test new file mode 100644 index 0000000..4143f91 --- /dev/null +++ b/src/sbt-test/wasm/shopping-cart/test @@ -0,0 +1,6 @@ +> +clean +$ exec cargo install golem-scalajs-wit-bindgen +> +wasmComponent +$ exists target/dist/main.js +$ exists target/dist/main.js.map +$ exists target/dist/main.wasm \ No newline at end of file diff --git a/src/sbt-test/wasm/shopping-cart/wit/example.wit b/src/sbt-test/wasm/shopping-cart/wit/example.wit new file mode 100644 index 0000000..d28d93b --- /dev/null +++ b/src/sbt-test/wasm/shopping-cart/wit/example.wit @@ -0,0 +1,37 @@ +package pack:name; + +interface api { + record product-item { + product-id: string, + name: string, + price: float32, + quantity: u32, + } + + record order-confirmation { + order-id: string, + } + + variant checkout-result { + error(string), + success(order-confirmation), + } + + initialize-cart: func(user-id: string) -> result; + + add-item: func(item: product-item) -> (); + + remove-item: func(product-id: string) -> (); + + update-item-quantity: func(product-id: string, quantity: u32) -> (); + + checkout: func() -> checkout-result; + + get-cart-contents: func() -> list; + + get-first-item: func() -> option; +} + +world main { + export api; +} \ No newline at end of file From b6069de840d5869ca24f49a6869e73f559e6de60 Mon Sep 17 00:00:00 2001 From: Daniele Torelli Date: Mon, 19 Aug 2024 16:04:14 +0200 Subject: [PATCH 3/4] Minor changes --- project/Settings.scala | 1 - .../scala/cloud/golem/WasmComponentPluginInternal.scala | 8 ++++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/project/Settings.scala b/project/Settings.scala index 7967fd8..e025681 100644 --- a/project/Settings.scala +++ b/project/Settings.scala @@ -10,7 +10,6 @@ object Settings { import xerial.sbt.Sonatype import xerial.sbt.Sonatype.SonatypeKeys.* - project.settings( inThisBuild( List( diff --git a/src/main/scala/cloud/golem/WasmComponentPluginInternal.scala b/src/main/scala/cloud/golem/WasmComponentPluginInternal.scala index 6d4c53a..7377441 100644 --- a/src/main/scala/cloud/golem/WasmComponentPluginInternal.scala +++ b/src/main/scala/cloud/golem/WasmComponentPluginInternal.scala @@ -18,7 +18,7 @@ private[golem] object WasmComponentPluginInternal { ) def checkCommandOrFail(command: String)(error: => String): Unit = { import scala.sys.process.* - val commandExists = Seq("bash", "-xc", s"which $command").! == 0 + val commandExists = Seq("sh", "-xc", s"which $command").! == 0 if (!commandExists) sys.error(error) } Def.settings( @@ -46,7 +46,7 @@ private[golem] object WasmComponentPluginInternal { """.stripMargin } val output = Seq( - "bash", + "sh", "-xc", s"$bindGenCommand -w ${wasmComponentWitFullPath.value} -p ${wasmComponentPackageName.value}" ).!! @@ -66,8 +66,8 @@ private[golem] object WasmComponentPluginInternal { """.stripMargin } - Seq("bash", "-xc", s"$npmCommand install").!! - Seq("bash", "-xc", s"$npmCommand run build").!! + Seq("sh", "-xc", s"$npmCommand install").!! + Seq("sh", "-xc", s"$npmCommand run build").!! }, wasmComponent := (wasmComponent dependsOn (Compile / fullLinkJS)).value, Compile / sourceGenerators += Def.taskIf { From 5432ead3ed8bcaba17e29dec2c460a0501241844 Mon Sep 17 00:00:00 2001 From: Daniele Torelli Date: Mon, 19 Aug 2024 16:16:35 +0200 Subject: [PATCH 4/4] Build every branch --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 824e95b..4d10897 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,7 +3,7 @@ name: CI on: pull_request: push: - branches: [main] + branches: ["**"] release: types: - published @@ -38,7 +38,7 @@ jobs: runs-on: ubuntu-latest needs: [test] - if: github.event_name != 'pull_request' + if: github.ref == 'refs/heads/main' || github.event_name == 'release' steps: - uses: actions/checkout@v4 with: