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

Add Scala 2 example #14

Merged
merged 5 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
templates/**/*.wasm
/target
.idea
.DS_Store
6 changes: 6 additions & 0 deletions examples/scala2/INSTRUCTIONS
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
See the documentation about installing tooling: https://golem.cloud/learn/scala2

Build the project with sbt:
sbt component

The `target/dist/component-name.wasm` file is ready to be uploaded to Golem Cloud!
4 changes: 4 additions & 0 deletions examples/scala2/scala2-shopping-cart/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.bsp
.idea
target
node_modules
15 changes: 15 additions & 0 deletions examples/scala2/scala2-shopping-cart/.scalafmt.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version = 2.7.5
maxColumn = 120
align = most
continuationIndent.defnSite = 2
assumeStandardLibraryStripMargin = true
docstrings = JavaDoc
lineEndings = preserve
includeCurlyBraceInSelectChains = false
danglingParentheses = true
spaces {
inImportCurlyBraces = true
}
optIn.annotationNewlines = true

rewrite.rules = [SortImports, RedundantBraces]
45 changes: 45 additions & 0 deletions examples/scala2/scala2-shopping-cart/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
ThisBuild / version := "0.1.0"

ThisBuild / scalaVersion := "2.13.12"

lazy val root = (project in file("."))
.settings(
name := "component-name"
)
.enablePlugins(ScalaJSPlugin)
.settings(
scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.ESModule) },
Compile / fullLinkJS / scalaJSLinkerOutputDirectory := target.value / "dist"
)

lazy val witBindgen = taskKey[Unit]("Runs golem-scalajs-wit-bindgen to generate WIT bindings")

witBindgen := {
import scala.sys.process._

Seq("bash", "-xc", "golem-scalajs-wit-bindgen -w wit/main.wit -p example").!!
}

lazy val component = taskKey[Unit]("Runs componentize-js on the generated main.js file")

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.task {
import scala.sys.process._

val file = (Compile / sourceManaged).value / "scala" / "example" / "Api.scala"

IO.write(
file,
Seq("bash", "-xc", "golem-scalajs-wit-bindgen -w wit/main.wit -p example").!!
)

Seq(file)
}.taskValue
13 changes: 13 additions & 0 deletions examples/scala2/scala2-shopping-cart/componentize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { componentize } from '@bytecodealliance/componentize-js';
import { readFile, writeFile } from 'node:fs/promises';
import { resolve } from 'node:path';

const jsSource = await readFile('target/dist/main.js', 'utf8');

const { component } = await componentize(jsSource, {
witPath: resolve('wit'),
enableStdout: true,
preview2Adapter: 'adapters/tier2/wasi_snapshot_preview1.wasm'
});

await writeFile('target/dist/component-name.wasm', component);
8 changes: 8 additions & 0 deletions examples/scala2/scala2-shopping-cart/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"description": "A stateful Golem actor representing a shopping cart, implemented in Scala 2 using Scala.js",
"requiresAdapter": true,
"exclude": [
"target",
"node_modules"
]
}
Loading
Loading