Skip to content

Commit

Permalink
Some minor changes after re-reading code (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnynek authored Oct 5, 2023
1 parent fb755a8 commit 2cb8878
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 48 deletions.
45 changes: 21 additions & 24 deletions src/main/scala/bazeltools/bsp4bazel/BazelBspServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import bazeltools.bsp4bazel.protocol.*
import bazeltools.bsp4bazel.runner.BazelLabel
import bazeltools.bsp4bazel.runner.BazelRunner
import bazeltools.bsp4bazel.runner.BspBazelRunner
import cats.data.NonEmptyList
import cats.effect.IO
import cats.effect.kernel.Ref
import cats.effect.std.Queue
Expand Down Expand Up @@ -51,21 +52,16 @@ class Bsp4BazelServer(
)
)
)
resp <- IO.pure {
val compileProvider = CompileProvider(List("scala"))

InitializeBuildResult(
compileProvider = CompileProvider(List("scala"))
yield InitializeBuildResult(
"Bazel",
BuildInfo.version,
BuildInfo.bspVersion,
BuildServerCapabilities(
compileProvider = Some(compileProvider),
inverseSourcesProvider = Some(true),
canReload = Some(true)
)
)
}
yield resp
))

def buildInitialized(params: Unit): IO[Unit] =
for
Expand Down Expand Up @@ -145,16 +141,14 @@ class Bsp4BazelServer(
): IO[ScalacOptionsResult] =
for
_ <- logger.info("buildTarget/scalacOptions")
resp = ScalacOptionsResult(Nil)
yield resp
yield ScalacOptionsResult(Nil)

def buildTargetJavacOptions(
params: JavacOptionsParams
): IO[JavacOptionsResult] =
for
_ <- logger.info("buildTarget/javacOptions")
resp = JavacOptionsResult(Nil)
yield resp
yield JavacOptionsResult(Nil)

private def doCompile(
workspaceRoot: Path,
Expand Down Expand Up @@ -260,8 +254,7 @@ class Bsp4BazelServer(
yield ()

def buildShutdown(params: Unit): IO[Unit] =
for _ <- logger.info("build/shutdown")
yield ()
logger.info("build/shutdown")

def buildExit(params: Unit): IO[Unit] =
for
Expand All @@ -280,7 +273,7 @@ class Bsp4BazelServer(
case Right(bazelTarget) =>
bazelRunner
.targetSources(bazelTarget)
.map(ss => (bt, ss.map(s => TextDocumentIdentifier.file(s))))
.map(ss => (bt, ss.map(TextDocumentIdentifier.file)))
case Left(err) =>
IO.raiseError(err)
}
Expand Down Expand Up @@ -309,24 +302,21 @@ class Bsp4BazelServer(
): IO[DependencySourcesResult] =
for
_ <- logger.info("buildTarget/dependencySources")
resp = DependencySourcesResult(Nil)
yield resp
yield DependencySourcesResult(Nil)

def buildTargetScalaMainClasses(
params: ScalaMainClassesParams
): IO[ScalaMainClassesResult] =
for
_ <- logger.info("buildTarget/scalaMainClasses")
resp = ScalaMainClassesResult(Nil, None)
yield resp
yield ScalaMainClassesResult(Nil, None)

def buildTargetCleanCache(params: CleanCacheParams): IO[CleanCacheResult] =
for _ <- logger.info("buildTarget/cleanCache")
yield CleanCacheResult(Some("Cleaned"), true)

def cancelRequest(params: CancelParams): IO[Unit] =
for _ <- logger.info("$/cancelRequest")
yield ()
logger.info("$/cancelRequest")

end Bsp4BazelServer

Expand Down Expand Up @@ -355,13 +345,17 @@ object Bsp4BazelServer:
]]
):

private def invertMap[K, V](map: Map[K, List[V]]): Map[V, List[K]] =
private def invertMap[K, V](map: Map[K, List[V]]): Map[V, NonEmptyList[K]] =
map.toList
.flatMap((bt, ls) => ls.map(l => (l, bt)))
.groupMap(_._1)(_._2)
.map { case (v, ks) =>
// we know there is at least one K for each V
(v, NonEmptyList.fromListUnsafe(ks))
}

private val sourceTargets
: Map[TextDocumentIdentifier, List[BuildTargetIdentifier]] =
: Map[TextDocumentIdentifier, NonEmptyList[BuildTargetIdentifier]] =
invertMap(_targetSources)

def sourcesForTarget(
Expand All @@ -372,7 +366,10 @@ object Bsp4BazelServer:
def targetsForSource(
td: TextDocumentIdentifier
): List[BuildTargetIdentifier] =
sourceTargets.get(td).getOrElse(Nil)
sourceTargets.get(td) match {
case Some(nel) => nel.toList
case None => Nil
}

object TargetSourceMap:
def empty: TargetSourceMap = TargetSourceMap(Map.empty)
13 changes: 7 additions & 6 deletions src/main/scala/bazeltools/bsp4bazel/FilesIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ object FilesIO:
glob: Option[String] = None,
maxDepth: Int = 100
): Stream[IO, Path] =
val paths = Stream
.fromIterator[IO](
Files
// Files.walk could start doing IO
val itIO = IO.blocking(Files
.walk(root, maxDepth, FileVisitOption.FOLLOW_LINKS)
.iterator
.asScala,
100
)
.asScala)

val paths =
Stream.eval(itIO)
.flatMap(Stream.fromIterator[IO](_, 100))

glob match
case Some(glob) =>
Expand Down
35 changes: 17 additions & 18 deletions src/main/scala/bazeltools/bsp4bazel/runner/BazelRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,18 @@ object BazelRunner:
expected: BazelResult.ExitCode*
): IO[Unit] =
if !expected.contains(BazelResult.ExitCode.fromCode(er.exitCode)) then
for
err <- er.debugString
_ <- IO.raiseError(BazelRunError(err, er.exitCode))
yield ()
er.debugString
.flatMap { err =>
IO.raiseError(BazelRunError(err, er.exitCode))
}
else IO.unit

case class BazelRunError(detailsMessage: String, exitCode: Int)
extends Error(s"Bazel Run Failed: $exitCode\n$detailsMessage")

sealed trait BazelWrapper(val command: String)
object BazelWrapper:
case class At(path: Path) extends BazelWrapper(path.toAbsolutePath.toString)
case object Default extends BazelWrapper("bazel")
enum BazelWrapper(val command: String):
case At(path: Path) extends BazelWrapper(path.toAbsolutePath.toString)
case Default extends BazelWrapper("bazel")

def default(
workspaceRoot: Path,
Expand All @@ -163,27 +162,27 @@ object BazelRunner:

private def runBazel(
command: Command,
expr: Option[String]
args: List[String]
): Resource[IO, ExecutionResult] =
for
_ <- Resource.eval(
logger.info(
s"Running ${bazelWrapper.command} ${command.asString} ${expr.getOrElse("_no_args_")}"
s"Running ${bazelWrapper.command} ${command.asString} ${args.mkString(" ")}"
)
)
er <- SubProcess
.from(
workspaceRoot,
bazelWrapper.command
)
.withArgs(command.asString :: expr.toList)
.withArgs(command.asString :: args)
.runUntilExit(FiniteDuration(30, TimeUnit.MINUTES))
_ <- Resource.eval(logger.info(s"Exited with ${er.exitCode}"))
yield er

private def runBazelExpectOk(
command: Command,
expr: Option[String]
expr: List[String]
): Resource[IO, ExecutionResult] =
for
er <- runBazel(command, expr)
Expand All @@ -193,24 +192,24 @@ object BazelRunner:
yield er

def query(expr: String): IO[BazelResult] =
runBazel(Command.Query, Some(expr)).use(
runBazel(Command.Query, expr :: Nil).use(
BazelResult.fromExecutionResult(_)
)

def build(label: BazelLabel): IO[BazelResult] =
runBazel(Command.Build, Some(label.asString))
runBazel(Command.Build, label.asString :: Nil)
.use(BazelResult.fromExecutionResult(_))

def run(label: BazelLabel): IO[BazelResult] =
runBazel(Command.Run, Some(label.asString))
runBazel(Command.Run, label.asString :: Nil)
.use(BazelResult.fromExecutionResult(_))

def test(label: BazelLabel): IO[BazelResult] =
runBazel(Command.Test, Some(label.asString))
runBazel(Command.Test, label.asString :: Nil)
.use(BazelResult.fromExecutionResult(_))

def shutdown: IO[Unit] =
runBazelExpectOk(Command.Shutdown, None).use_
runBazelExpectOk(Command.Shutdown, Nil).use_

def clean: IO[Unit] =
runBazelExpectOk(Command.Clean, None).use_
runBazelExpectOk(Command.Clean, Nil).use_

0 comments on commit 2cb8878

Please sign in to comment.