-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Fs2 based PlatformIO supporting scalajs (#1327)
- Loading branch information
Showing
10 changed files
with
290 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
cliJS/src/main/scala/org/bykn/bosatsu/Fs2PlatformIO.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package org.bykn.bosatsu | ||
|
||
import _root_.bosatsu.{TypedAst => proto} | ||
import cats.MonadError | ||
import cats.data.Validated | ||
import cats.effect.IO | ||
import fs2.io.file.{Files, Path} | ||
import com.monovore.decline.Argument | ||
import org.typelevel.paiges.Doc | ||
import scala.util.{Failure, Success, Try} | ||
|
||
import cats.syntax.all._ | ||
|
||
object Fs2PlatformIO extends PlatformIO[IO, Path] { | ||
def moduleIOMonad: MonadError[IO, Throwable] = | ||
IO.asyncForIO | ||
|
||
val pathArg: Argument[Path] = | ||
new Argument[Path] { | ||
def read(string: String) = | ||
Try(Path(string)) match { | ||
case Success(value) => | ||
Validated.valid(value) | ||
case Failure(exception) => | ||
Validated.invalidNel(s"could not parse $string as path: ${exception.getMessage()}") | ||
} | ||
|
||
def defaultMetavar: String = "path" | ||
} | ||
|
||
val pathOrdering: Ordering[Path] = Path.instances.toOrdering | ||
|
||
private val FilesIO = Files.forIO | ||
|
||
def readPath(p: Path): IO[String] = | ||
FilesIO.readUtf8(p).compile.string | ||
|
||
def readPackages(paths: List[Path]): IO[List[Package.Typed[Unit]]] = | ||
paths.parTraverse { path => | ||
for { | ||
bytes <- FilesIO.readAll(path).compile.to(Array) | ||
ppack <- IO(proto.Packages.parseFrom(bytes)) | ||
packs <- IO.fromTry(ProtoConverter.packagesFromProto(Nil, ppack.packages)) | ||
} yield packs._2 | ||
} | ||
.map(_.flatten) | ||
|
||
def readInterfaces(paths: List[Path]): IO[List[Package.Interface]] = | ||
paths.parTraverse { path => | ||
for { | ||
bytes <- FilesIO.readAll(path).compile.to(Array) | ||
pifaces <- IO(proto.Interfaces.parseFrom(bytes)) | ||
ifaces <- IO.fromTry(ProtoConverter.packagesFromProto(pifaces.interfaces, Nil)) | ||
} yield ifaces._1 | ||
} | ||
.map(_.flatten) | ||
|
||
/** given an ordered list of prefered roots, if a packFile starts with one of | ||
* these roots, return a PackageName based on the rest | ||
*/ | ||
def pathPackage(roots: List[Path], packFile: Path): Option[PackageName] = | ||
PlatformIO.pathPackage(roots, packFile) { (root, pf) => | ||
if (pf.startsWith(root)) Some { | ||
root.relativize(pf).names.map(_.toString) | ||
} | ||
else None | ||
} | ||
|
||
/** Modules optionally have the capability to combine paths into a tree | ||
*/ | ||
val resolvePath: Option[(Path, PackageName) => IO[Option[Path]]] = Some { | ||
(root: Path, pack: PackageName) => { | ||
val dir = pack.parts.init.foldLeft(root)(_.resolve(_)) | ||
val filePath = dir.resolve(pack.parts.last + ".bosatsu") | ||
FilesIO.exists(filePath, true) | ||
.map { | ||
case true => Some(filePath) | ||
case false => None | ||
} | ||
} | ||
} | ||
|
||
/** some modules have paths that form directory trees | ||
* | ||
* if the given path is a directory, return Some and all the first children. | ||
*/ | ||
def unfoldDir: Option[Path => IO[Option[IO[List[Path]]]]] = Some { | ||
(path: Path) => { | ||
FilesIO.isDirectory(path, followLinks = true) | ||
.map { | ||
case true => Some { | ||
// create a list of children | ||
FilesIO.list(path).compile.toList | ||
} | ||
case false => None | ||
} | ||
} | ||
} | ||
|
||
def hasExtension(str: String): Path => Boolean = | ||
{ (path: Path) => path.extName == str } | ||
|
||
private def docStream(doc: Doc): fs2.Stream[IO, String] = | ||
fs2.Stream.fromIterator[IO](doc.renderStream(100).iterator, chunkSize = 128) | ||
|
||
def writeDoc(p: Path, d: Doc): IO[Unit] = { | ||
val pipe = Files.forIO.writeUtf8(p) | ||
pipe(docStream(d)).compile.drain | ||
} | ||
|
||
def writeStdout(doc: Doc): IO[Unit] = | ||
docStream(doc) | ||
.evalMapChunk(part => IO.print(part)) | ||
.compile | ||
.drain | ||
|
||
def resolve(base: Path, p: List[String]): Path = | ||
p.foldLeft(base)(_.resolve(_)) | ||
|
||
// this is println actually | ||
def print(str: String): IO[Unit] = | ||
IO.println(str) | ||
|
||
def writeInterfaces( | ||
interfaces: List[Package.Interface], | ||
path: Path | ||
): IO[Unit] = | ||
for { | ||
protoIfaces <- IO.fromTry(ProtoConverter.interfacesToProto(interfaces)) | ||
bytes = protoIfaces.toByteArray | ||
pipe = Files.forIO.writeAll(path) | ||
_ <- pipe(fs2.Stream.chunk(fs2.Chunk.array(bytes))).compile.drain | ||
} yield () | ||
|
||
def writePackages[A](packages: List[Package.Typed[A]], path: Path): IO[Unit] = | ||
for { | ||
protoPacks <- IO.fromTry(ProtoConverter.packagesToProto(packages)) | ||
bytes = protoPacks.toByteArray | ||
pipe = Files.forIO.writeAll(path) | ||
_ <- pipe(fs2.Stream.chunk(fs2.Chunk.array(bytes))).compile.drain | ||
} yield () | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
cliJS/src/main/scala/org/bykn/bosatsu/tool/Fs2Module.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters