Skip to content

Commit

Permalink
invoke via option
Browse files Browse the repository at this point in the history
  • Loading branch information
Quafadas committed Jun 25, 2024
1 parent a4f1916 commit bbafbf1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 19 deletions.
47 changes: 31 additions & 16 deletions project/src/build.runner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ import cats.effect.IO
import cats.effect.ResourceIO
import cats.syntax.all.*

sealed trait BuildTool
class ScalaCli extends BuildTool
class Mill extends BuildTool
sealed trait BuildTool(val invokedVia: String)
class ScalaCli
extends BuildTool(
if isWindows then "scala-cli.bat" else "scala-cli"
)
class Mill
extends BuildTool(
if isWindows then "mill.bat" else "mill"
)

private lazy val isWindows: Boolean =
System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows")
Expand All @@ -33,23 +39,31 @@ def buildRunner(
workDir: fs2.io.file.Path,
outDir: fs2.io.file.Path,
extraBuildArgs: List[String],
millModuleName: Option[String]
millModuleName: Option[String],
buildToolInvocation: Option[String]
)(
logger: Scribe[IO]
): ResourceIO[Unit] = tool match
case scli: ScalaCli => buildRunnerScli(linkingTopic, workDir, outDir, extraBuildArgs)(logger)
case m: Mill =>
buildRunnerMill(
linkingTopic,
workDir,
millModuleName.getOrElse(throw new Exception("must have a module name when running with mill")),
extraBuildArgs
)(logger)
): ResourceIO[Unit] =
val invokeVia = buildToolInvocation.getOrElse(tool.invokedVia)
tool match
case scli: ScalaCli =>
buildRunnerScli(linkingTopic, workDir, outDir, invokeVia, extraBuildArgs)(logger)
case m: Mill =>
buildRunnerMill(
linkingTopic,
workDir,
millModuleName.getOrElse(throw new Exception("must have a module name when running with mill")),
invokeVia,
extraBuildArgs
)(logger)
end match
end buildRunner

def buildRunnerScli(
linkingTopic: Topic[IO, Unit],
workDir: fs2.io.file.Path,
outDir: fs2.io.file.Path,
invokeVia: String,
extraBuildArgs: List[String]
)(
logger: Scribe[IO]
Expand All @@ -66,12 +80,12 @@ def buildRunnerScli(
) ++ extraBuildArgs

logger
.trace(scalaCliArgs.toString())
.trace(s"Invoking via : $invokeVia with args : ${scalaCliArgs.toString()}")
.toResource
.flatMap(
_ =>
ProcessBuilder(
if isWindows then "scala-cli.bat" else "scala-cli",
invokeVia,
scalaCliArgs
).withWorkingDirectory(workDir)
.spawn[IO]
Expand Down Expand Up @@ -103,6 +117,7 @@ def buildRunnerMill(
linkingTopic: Topic[IO, Unit],
workDir: fs2.io.file.Path,
moduleName: String,
invokeVia: String,
extraBuildArgs: List[String]
)(
logger: Scribe[IO]
Expand Down Expand Up @@ -140,7 +155,7 @@ def buildRunnerMill(
) ++ extraBuildArgs
// TODO pipe this to stdout so that we can see linker progress / errors.
val builder = ProcessBuilder(
if isWindows then "mill.bat" else "mill",
invokeVia,
millargs
).withWorkingDirectory(workDir)
.spawn[IO]
Expand Down
17 changes: 14 additions & 3 deletions project/src/live.server.scala
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ object LiveServer extends IOApp:
}
.orNone

val buildToolInvocation: Opts[Option[String]] = Opts
.option[String](
"build-tool-invocation",
"This string will be passed to an fs2 process which invokes the build tool. By default it's 'scala-cli', or `mill`, " +
"and is assumed is on the path"
)
.orNone

case class LiveServerConfig(
baseDir: Option[String],
outDir: Option[String] = None,
Expand All @@ -189,7 +197,8 @@ object LiveServer extends IOApp:
extraBuildArgs: List[String] = List.empty,
millModuleName: Option[String] = None,
stylesDir: Option[String] = None,
indexHtmlTemplate: Option[String] = None
indexHtmlTemplate: Option[String] = None,
buildToolInvocation: Option[String] = None
)

def parseOpts = (
Expand All @@ -206,7 +215,8 @@ object LiveServer extends IOApp:
extraBuildArgsOpt,
millModuleNameOpt,
stylesDirOpt,
indexHtmlTemplateOpt
indexHtmlTemplateOpt,
buildToolInvocation
).mapN(LiveServerConfig.apply)

def main(lsc: LiveServerConfig): Resource[IO, Server] =
Expand Down Expand Up @@ -270,7 +280,8 @@ object LiveServer extends IOApp:
baseDirPath,
outDirPath,
lsc.extraBuildArgs,
lsc.millModuleName
lsc.millModuleName,
lsc.buildToolInvocation
)(logger)

app <- routes(outDirString, refreshTopic, indexOpts, proxyRoutes, fileToHashRef, lsc.clientRoutingPrefix)(logger)
Expand Down

0 comments on commit bbafbf1

Please sign in to comment.