Skip to content

Commit

Permalink
simplify BazelBspApp a bit (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnynek authored Jan 3, 2023
1 parent 3713554 commit 64e15ea
Showing 1 changed file with 37 additions and 41 deletions.
78 changes: 37 additions & 41 deletions src/main/scala/afenton/bazel/bsp/BazelBspApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import afenton.bazel.bsp.protocol.TextDocumentIdentifier
import cats.data.NonEmptyList
import cats.effect.ExitCode
import cats.effect.IO
import cats.effect.kernel.Ref
import cats.effect.std.Console
import cats.effect.std.Queue
import cats.syntax.all.*
import com.monovore.decline._
Expand Down Expand Up @@ -46,6 +44,21 @@ object BazelBspApp
Opts
.flag("verbose", help = "Include trace output on stderr")
.orFalse
.map { verbose =>
server(verbose)(
fs2.io.stdinUtf8[IO](10_000),
fs2.text.utf8.encode.andThen(fs2.io.stdout),
fs2.text.utf8.encode.andThen(fs2.io.stderr)
)
.handleErrorWith { e =>
IO.blocking {
System.err.println(
s"ERROR: 💣 💣 💣 \n${e.toString} \n${e.getStackTrace.mkString("\n")}"
)
ExitCode.Error
}
}
}

val verifySetupOpt =
Opts
Expand All @@ -54,50 +67,36 @@ object BazelBspApp
help =
"Verifies that Bazel is correctly configured for use with Bazel BSP"
)
.orFalse
.as {
for
result <- Verifier.validateSetup
_ <- printVerifyResult(result)
yield ExitCode.Success
}

val setupOpt =
Opts
.flag("setup", help = "write BSP configuration files into CWD")
.orFalse
.as {
for
cwd <- FilesIO.cwd
_ <- writeBspConfig(cwd)
yield ExitCode.Success
}

def printVerifyResult(
result: List[(String, Either[String, Unit])]
): IO[Unit] =
IO.println(
result
.map {
case (n, Right(_)) => s"$n"
case (n, Left(err)) => s"$n\n $err\n"
.traverse_ {
case (n, Right(_)) => IO.println(s"$n")
case (n, Left(err)) => IO.println(s"$n\n $err\n")
}
.mkString("\n")
)

def main: Opts[IO[ExitCode]] =
(verboseOpt, verifySetupOpt, setupOpt).mapN { (verbose, verify, setup) =>
if setup then
for
cwd <- FilesIO.cwd
_ <- writeBspConfig(cwd)
yield ExitCode.Success
else if verify then
for
result <- Verifier.validateSetup
_ <- printVerifyResult(result)
yield ExitCode.Success
else
server(verbose)(
fs2.io.stdinUtf8[IO](10_000),
fs2.text.utf8.encode.andThen(fs2.io.stdout),
fs2.text.utf8.encode.andThen(fs2.io.stderr)
)
.handleError { case e =>
System.err.println(
s"ERROR: 💣 💣 💣 \n${e.toString} \n${e.getStackTrace.mkString("\n")}"
)
ExitCode.Error
}
}
verboseOpt
.orElse(verifySetupOpt)
.orElse(setupOpt)

def writeBspConfig(workspaceRoot: Path): IO[Unit] =
val toPath = workspaceRoot.resolve(".bsp")
Expand All @@ -117,7 +116,7 @@ object BazelBspApp
)
.compile
.drain
_ <- Console[IO].println(s"Wrote setup config to ${toPath}")
_ <- IO.println(s"Wrote setup config to ${toPath}")
yield ()

private lazy val bspConfig: String = s"""
Expand Down Expand Up @@ -147,8 +146,7 @@ object BazelBspApp
.through(messageDispatcher(BspServer.jsonRpcRouter(bspServer)))
.evalTap(response => logger.trace(s"response: ${response}"))
.evalMap {
case resp: Response =>
outQ.offer(resp)
case resp: Response => outQ.offer(resp)
case u: Unit => IO.unit
}

Expand All @@ -168,7 +166,7 @@ object BazelBspApp
outPipe: Pipe[IO, String, Unit],
errPipe: Pipe[IO, String, Unit]
): IO[ExitCode] =
val program = for
for
loggerStream <- Logger.queue(100, errPipe, verbose)
(logger, logStream) = loggerStream
outQ <- Queue.bounded[IO, Message](100)
Expand All @@ -180,6 +178,4 @@ object BazelBspApp
logStream
).parJoin(3)
_ <- all.compile.drain
yield ()

program.as(ExitCode.Success)
yield ExitCode.Success

0 comments on commit 64e15ea

Please sign in to comment.