|
| 1 | +package io.github.quafadas.RefreshPlugin |
| 2 | + |
| 3 | +import io.github.quafadas.sjsls.LiveServerConfig |
| 4 | +import mill.* |
| 5 | +import mill.scalalib.* |
| 6 | +import mill.scalajslib.* |
| 7 | +import os.Path |
| 8 | +import mill.api.Task.Simple |
| 9 | +import fs2.concurrent.Topic |
| 10 | +import cats.effect.IO |
| 11 | +// import mill.scalajslib.* |
| 12 | +// import coursier.maven.MavenRepository |
| 13 | +// import mill.api.Result |
| 14 | +// import mill.util.Jvm.createJar |
| 15 | +// import mill.define.PathRef |
| 16 | +// import mill.scalalib.api.CompilationResult |
| 17 | +// // import de.tobiasroeser.mill.vcs.version.VcsVersion |
| 18 | +// import scala.util.Try |
| 19 | +// import mill.scalalib.publish.PomSettings |
| 20 | +// import mill.scalalib.publish.License |
| 21 | +// import mill.scalalib.publish.VersionControl |
| 22 | +// import os.SubPath |
| 23 | +// import ClasspathHelp.* |
| 24 | +import cats.effect.unsafe.implicits.global |
| 25 | +import io.github.quafadas.sjsls.LiveServerConfig |
| 26 | +import cats.effect.ExitCode |
| 27 | +import scala.util.{Try, Success, Failure} |
| 28 | +import scala.concurrent.Future |
| 29 | +import mill.api.BuildCtx |
| 30 | +import mill.scalajslib.api.Report |
| 31 | +implicit val ec: scala.concurrent.ExecutionContext = scala.concurrent.ExecutionContext.global |
| 32 | + |
| 33 | +trait ScalaJsRefreshModule extends ScalaJSModule: |
| 34 | + |
| 35 | + lazy val updateServer = Topic[IO, Unit].unsafeRunSync() |
| 36 | + |
| 37 | + |
| 38 | + def indexHtml = Task{ |
| 39 | + os.write.over(Task.dest / "index.html", io.github.quafadas.sjsls.vanillaTemplate) |
| 40 | + PathRef(Task.dest / "index.html") |
| 41 | + } |
| 42 | + |
| 43 | + def assetsDir = Task{ |
| 44 | + "assets" |
| 45 | + } |
| 46 | + |
| 47 | + def assets = Task{ |
| 48 | + os.write.over(Task.dest / "style.css", io.github.quafadas.sjsls.lessStyle(true).render) |
| 49 | + PathRef(Task.dest / assetsDir()) |
| 50 | + } |
| 51 | + |
| 52 | + def port = Task { |
| 53 | + 8080 |
| 54 | + } |
| 55 | + |
| 56 | + def openBrowser= Task { |
| 57 | + true |
| 58 | + } |
| 59 | + |
| 60 | + def logLevel = Task { |
| 61 | + "warn" |
| 62 | + } |
| 63 | + |
| 64 | + def dezombify = Task { |
| 65 | + true |
| 66 | + } |
| 67 | + |
| 68 | + def siteGen = Task{ |
| 69 | + val assets_ = assets() |
| 70 | + val path = fastLinkJS().dest |
| 71 | + os.copy.over(Task.dest / "index.html", indexHtml().path) |
| 72 | + os.copy.over(Task.dest / assetsDir(), assets_.path) |
| 73 | + updateServer.publish1(println("publishing update")) |
| 74 | + (assets_.path.toString(), path.path.toString()) |
| 75 | + } |
| 76 | + |
| 77 | + def lcs = Task.Worker{ |
| 78 | + val (assets, js) = siteGen() |
| 79 | + LiveServerConfig( |
| 80 | + baseDir = None, |
| 81 | + outDir = Some(js), |
| 82 | + port = com.comcast.ip4s.Port.fromInt(port()).getOrElse(throw new IllegalArgumentException(s"invalid port: ${port()}")), |
| 83 | + indexHtmlTemplate = Some(assets), |
| 84 | + buildTool = io.github.quafadas.sjsls.NoBuildTool(), // Here we are a slave to the build tool |
| 85 | + openBrowserAt = "/index.html", |
| 86 | + preventBrowserOpen = !openBrowser(), |
| 87 | + dezombify = dezombify(), |
| 88 | + logLevel = logLevel() |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | + def serve = Task.Worker{ |
| 93 | + // Let's kill off anything that is a zombie on the port we want to use |
| 94 | + val p = port() |
| 95 | + |
| 96 | + BuildCtx.withFilesystemCheckerDisabled { |
| 97 | + new RefreshServer(lcs()) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + class RefreshServer(lcs: LiveServerConfig) extends AutoCloseable { |
| 102 | + val server = io.github.quafadas.sjsls.LiveServer.main(lcs).allocated |
| 103 | + |
| 104 | + server.map(_._1).unsafeRunSync() |
| 105 | + |
| 106 | + override def close(): Unit = { |
| 107 | + // This is the shutdown hook for http4s |
| 108 | + println("Shutting down server...") |
| 109 | + server.map(_._2).flatten.unsafeRunSync() |
| 110 | + } |
| 111 | + } |
| 112 | + |
0 commit comments