diff --git a/cli/src/main/scala/org/bykn/bosatsu/PathModule.scala b/cli/src/main/scala/org/bykn/bosatsu/PathModule.scala index 90d4171b5..eb0a5eafc 100644 --- a/cli/src/main/scala/org/bykn/bosatsu/PathModule.scala +++ b/cli/src/main/scala/org/bykn/bosatsu/PathModule.scala @@ -1,6 +1,6 @@ package org.bykn.bosatsu -import cats.effect.IO +import cats.effect.{IO, Resource} import org.typelevel.paiges.{Doc, Document} import java.nio.file.{Path => JPath} @@ -14,21 +14,15 @@ import cats.syntax.all._ object PathModule extends MainModule[IO, JPath](IOPlatformIO) { self => type Path = JPath - def print(str: => String): IO[Unit] = + def print(str: String): IO[Unit] = IO.println(str) + 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] = - IO.delay(Par.newService()) - .flatMap { es => - fn(Par.ecFromService(es)) - .flatMap { a => - IO(Par.shutdownService(es)).as(a) - } - .recoverWith { case e => - IO(Par.shutdownService(es)) *> - IO.raiseError(e) - } - } + parResource.use(fn) def report(io: IO[Output]): IO[ExitCode] = io.attempt.flatMap { diff --git a/cli/src/test/scala/org/bykn/bosatsu/PathModuleTest.scala b/cli/src/test/scala/org/bykn/bosatsu/PathModuleTest.scala index 8cbd4a1b8..42ea770cb 100644 --- a/cli/src/test/scala/org/bykn/bosatsu/PathModuleTest.scala +++ b/cli/src/test/scala/org/bykn/bosatsu/PathModuleTest.scala @@ -14,6 +14,7 @@ import org.scalatest.funsuite.AnyFunSuite import cats.effect.unsafe.implicits.global class PathModuleTest extends AnyFunSuite { + import PathModule.platformIO.pathPackage implicit val arbPath: Arbitrary[Path] = Arbitrary { @@ -24,7 +25,7 @@ class PathModuleTest extends AnyFunSuite { test("test some hand written examples") { def pn(roots: List[String], file: String): Option[PackageName] = - PathModule.pathPackage(roots.map(Paths.get(_)), Paths.get(file)) + pathPackage(roots.map(Paths.get(_)), Paths.get(file)) assert( pn(List("/root0", "/root1"), "/root0/Bar.bosatsu") == Some( @@ -56,13 +57,13 @@ class PathModuleTest extends AnyFunSuite { test("no roots means no Package") { forAll { (p: Path) => - assert(PathModule.pathPackage(Nil, p) == None) + assert(pathPackage(Nil, p) == None) } } test("empty path is not okay for a package") { forAll { (roots: List[Path]) => - assert(PathModule.pathPackage(roots, Paths.get("")) == None) + assert(pathPackage(roots, Paths.get("")) == None) } } @@ -74,7 +75,7 @@ class PathModuleTest extends AnyFunSuite { PackageName.parse( rest.asScala.map(_.toString.toLowerCase.capitalize).mkString("/") ) - assert(PathModule.pathPackage(root :: otherRoots, path) == pack) + assert(pathPackage(root :: otherRoots, path) == pack) } forAll(law(_, _, _)) @@ -91,7 +92,7 @@ class PathModuleTest extends AnyFunSuite { test("if none of the roots are prefixes we have none") { forAll { (r0: Path, roots0: List[Path], file: Path) => val roots = (r0 :: roots0).filterNot(_.toString == "") - val pack = PathModule.pathPackage(roots, file) + val pack = pathPackage(roots, file) val noPrefix = !roots.exists { r => file.asScala.toList.startsWith(r.asScala.toList)