-
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.
Merge more logic into MainModule (#1331)
- Loading branch information
Showing
10 changed files
with
141 additions
and
165 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,6 @@ | ||
package org.bykn.bosatsu | ||
|
||
import cats.effect.ExitCode | ||
import cats.effect.{IO, Resource} | ||
import java.nio.file.{Path => JPath} | ||
import org.bykn.bosatsu.tool.Output | ||
import cats.effect.IO | ||
import java.nio.file.Path | ||
|
||
object PathModule extends MainModule[IO, JPath](IOPlatformIO) { self => | ||
type Path = JPath | ||
|
||
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: tool.ExitCode): ExitCode = | ||
ec match { | ||
case tool.ExitCode.Success => ExitCode.Success | ||
case tool.ExitCode.Error => ExitCode.Error | ||
} | ||
|
||
def report(io: IO[Output[JPath]]): IO[ExitCode] = | ||
io.attempt.flatMap { | ||
case Right(out) => reportOutput(out).map(fromToolExit) | ||
case Left(err) => reportException(err).as(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)) | ||
} | ||
|
||
} | ||
object PathModule extends MainModule[IO, Path](IOPlatformIO) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,20 @@ | ||
package org.bykn.bosatsu.tool | ||
|
||
import cats.effect.{ExitCode, IO, IOApp} | ||
import cats.effect.{ExitCode => ceExitCode, IO, IOApp} | ||
|
||
object Fs2Main extends IOApp { | ||
def run(args: List[String]): IO[ExitCode] = | ||
Fs2Module.run(args) match { | ||
case Right(getOutput) => | ||
Fs2Module.report(getOutput) | ||
def fromToolExit(ec: ExitCode): ceExitCode = | ||
ec match { | ||
case ExitCode.Success => ceExitCode.Success | ||
case ExitCode.Error => ceExitCode.Error | ||
} | ||
def run(args: List[String]): IO[ceExitCode] = | ||
Fs2Module.runAndReport(args) match { | ||
case Right(io) => io.map(fromToolExit) | ||
case Left(help) => | ||
IO.blocking { | ||
System.err.println(help.toString) | ||
ExitCode.Error | ||
ceExitCode.Error | ||
} | ||
} | ||
} |
36 changes: 3 additions & 33 deletions
36
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 |
---|---|---|
@@ -1,37 +1,7 @@ | ||
package org.bykn.bosatsu.tool | ||
|
||
import cats.{effect => ce} | ||
import cats.effect.{IO, Resource} | ||
import cats.effect.IO | ||
import fs2.io.file.Path | ||
import org.bykn.bosatsu.{Par, MainModule, Fs2PlatformIO} | ||
import org.bykn.bosatsu.{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)) | ||
} | ||
|
||
} | ||
object Fs2Module extends MainModule[IO, Path](Fs2PlatformIO) |
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
Oops, something went wrong.