Skip to content

Commit

Permalink
Add cliJS module and bosatsu_node
Browse files Browse the repository at this point in the history
  • Loading branch information
johnynek committed Dec 19, 2024
1 parent 927a678 commit bf70813
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,27 @@ jobs:
- '2.13.15'
java:
- '8'
testWithNode:
runs-on: ubuntu-latest
steps:
- uses: "actions/[email protected]"
- uses: "coursier/cache-action@v2"
- name: "graalvm setup"
uses: "olafurpg/setup-scala@v13"
with:
java-version: "${{matrix.java}}"
- name: "build node"
run: |
sbt "++${{matrix.scala}}; cliJSJS/fullOptJS"
- name: "run bosatsu tests"
run: |
./bosatsu_node test --input_dir test_workspace/ --package_root test_workspace/
strategy:
matrix:
scala:
- '2.13.15'
java:
- '8'
testC:
runs-on: ubuntu-latest
strategy:
Expand Down
9 changes: 9 additions & 0 deletions bosatsu_node
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

set -euo pipefail

SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
# hide the punycode deprecation warning
export NODE_OPTIONS="--no-deprecation"
# make sure to run sbt cliJSJS/fullOptJS
node $SCRIPT_DIR/cliJS/.js/target/scala-2.13/bosatsu-clijs-opt "$@"
17 changes: 17 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,23 @@ lazy val core =
lazy val coreJVM = core.jvm
lazy val coreJS = core.js

lazy val cliJS =
(crossProject(JSPlatform).crossType(CrossType.Pure) in file("cliJS"))
.settings(
commonSettings,
commonJsSettings,
name := "bosatsu-clijs",
assembly / test := {},
mainClass := Some("org.bykn.bosatsu.tool.Fs2Main"),
)
.jsSettings(
scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.CommonJSModule) },
scalaJSLinkerConfig ~= { _.withSourceMap(false).withOptimizer(true) },
mainClass := Some("org.bykn.bosatsu.tool.Fs2Main"),
scalaJSUseMainModuleInitializer := true,
)
.dependsOn(base, core)

lazy val jsapi =
(crossProject(JSPlatform).crossType(CrossType.Pure) in file("jsapi"))
.settings(
Expand Down
16 changes: 16 additions & 0 deletions cliJS/src/main/scala/org/bykn/bosatsu/tool/Fs2Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.bykn.bosatsu.tool

import cats.effect.{ExitCode, IO, IOApp}

object Fs2Main extends IOApp {
def run(args: List[String]): IO[ExitCode] =
Fs2Module.run(args) match {
case Right(getOutput) =>
Fs2Module.report(getOutput)
case Left(help) =>
IO.blocking {
System.err.println(help.toString)
ExitCode.Error
}
}
}
37 changes: 37 additions & 0 deletions core/src/main/scala/org/bykn/bosatsu/tool/Fs2Module.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.bykn.bosatsu.tool

import cats.{effect => ce}
import cats.effect.{IO, Resource}
import fs2.io.file.Path
import org.bykn.bosatsu.{Par, MainModule, Fs2PlatformIO}

object Fs2Module extends MainModule[IO, Path](Fs2PlatformIO) { self =>
val parResource: Resource[IO, Par.EC] =
Resource.make(IO(Par.newService()))(es => IO(Par.shutdownService(es)))
.map(Par.ecFromService(_))

def withEC[A](fn: Par.EC => IO[A]): IO[A] =
parResource.use(fn)

def fromToolExit(ec: ExitCode): ce.ExitCode =
ec match {
case ExitCode.Success => ce.ExitCode.Success
case ExitCode.Error => ce.ExitCode.Error
}

def report(io: IO[Output[Path]]): IO[ce.ExitCode] =
io.attempt.flatMap {
case Right(out) => reportOutput(out).map(fromToolExit)
case Left(err) => reportException(err).as(ce.ExitCode.Error)
}

def reportException(ex: Throwable): IO[Unit] =
mainExceptionToString(ex) match {
case Some(msg) =>
IO.consoleForIO.errorln(msg)
case None =>
IO.consoleForIO.errorln("unknown error:\n") *>
IO.blocking(ex.printStackTrace(System.err))
}

}

0 comments on commit bf70813

Please sign in to comment.