From 231393be215075d8815997442d61095eaad995d2 Mon Sep 17 00:00:00 2001 From: Kit Langton Date: Fri, 15 Jul 2022 20:47:56 -0400 Subject: [PATCH] comment out backend --- build.sbt | 4 +- cli/src/main/scala/zio/app/Backend.scala | 212 ++++++++++++----------- cli/src/main/scala/zio/app/Main.scala | 198 ++++++++++----------- 3 files changed, 208 insertions(+), 206 deletions(-) diff --git a/build.sbt b/build.sbt index b3ab658..c07a24c 100644 --- a/build.sbt +++ b/build.sbt @@ -143,8 +143,8 @@ lazy val cliFrontend = project libraryDependencies ++= Seq( "io.github.kitlangton" %%% "animus" % animusVersion, "com.raquo" %%% "laminar" % laminarVersion, - "io.github.cquiroz" %%% "scala-java-time" % "2.4.0", - "io.github.cquiroz" %%% "scala-java-time-tzdb" % "2.4.0", + "io.github.cquiroz" %%% "scala-java-time" % scalaJavaTimeVersion, + "io.github.cquiroz" %%% "scala-java-time-tzdb" % scalaJavaTimeVersion, "io.laminext" %%% "websocket" % laminextVersion, "com.softwaremill.sttp.client3" %%% "core" % sttpVersion, "com.softwaremill.sttp.client3" %%% "monix" % sttpVersion, diff --git a/cli/src/main/scala/zio/app/Backend.scala b/cli/src/main/scala/zio/app/Backend.scala index 08091cd..393ce4d 100644 --- a/cli/src/main/scala/zio/app/Backend.scala +++ b/cli/src/main/scala/zio/app/Backend.scala @@ -1,105 +1,107 @@ -package zio.app - -import boopickle.Default._ -import boopickle.Pickler -import io.netty.buffer.Unpooled -import io.netty.handler.codec.http.{HttpHeaderNames, HttpHeaderValues} -import zhttp.http._ -import zhttp.service.Server -import zhttp.socket.{Socket, WebSocketFrame} -import zio._ -import zio.app.cli.protocol.{ClientCommand, ServerCommand} -import zio.process.{Command, CommandError} -import zio.stream.ZStream - -import java.nio.ByteBuffer -import scala.io.Source -import scala.util.{Failure, Success, Try} - -object Backend extends ZIOAppDefault { - def appSocket: Socket[FileSystemService with SbtManager, Throwable, WebSocketFrame, WebSocketFrame] = - pickleSocket { (command: ClientCommand) => - command match { - case ClientCommand.ChangeDirectory(path) => - ZStream.fromZIO(FileSystemService.cd(path)).drain - - case ClientCommand.Subscribe => - SbtManager.launchVite merge - SbtManager.backendSbtStream - .zipWithLatest(SbtManager.frontendSbtStream)(_ -> _) - .zipWithLatest(FileSystemService.stateStream)(_ -> _) - .map { case ((b, f), fs) => - val command: ServerCommand = ServerCommand.State(b, f, fs) - val byteBuf = Pickle.intoBytes(command) - WebSocketFrame.binary(Chunk.fromArray(byteBuf.array())) - } - } - } - - implicit def chunkPickler[A](implicit aPickler: Pickler[A]): Pickler[Chunk[A]] = - transformPickler[Chunk[A], List[A]](as => Chunk.from(as))(_.toList) - - private def app: HttpApp[FileSystemService with SbtManager, Throwable] = - Http.collectZIO { - case Method.GET -> !! / "ws" => - Response.fromSocket(appSocket) - - case Method.GET -> !! / "assets" / file => - val source = Source.fromResource(s"dist/assets/$file").getLines().mkString("\n") - - val contentTypeHtml: Header = - if (file.endsWith(".js")) (HttpHeaderNames.CONTENT_TYPE, "text/javascript") - else (HttpHeaderNames.CONTENT_TYPE, "text/css") - - ZIO.succeed { - Response( - headers = Headers(contentTypeHtml), - data = HttpData.fromChunk(Chunk.fromArray(source.getBytes(HTTP_CHARSET))) - ) - } - - case Method.GET -> !! => - val html = Source.fromResource(s"dist/index.html").getLines().mkString("\n") - - val contentTypeHtml: Header = (HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_HTML) - ZIO.succeed { - Response( - data = HttpData.fromChunk(Chunk.fromArray(html.getBytes(HTTP_CHARSET))), - headers = Headers(contentTypeHtml) - ) - } - - case other => - println(s"RECEIVED NOT FOUND: $other") - ZIO.succeed { Response.status(Status.NotFound) } - } - - lazy val program = for { - port <- System.envOrElse("PORT", "9630").map(_.toInt).orElseSucceed(9630) - _ <- Console.printLine(s"STARTING SERVER ON PORT $port") - _ <- openBrowser.delay(1.second).fork - _ <- Server.start(port, app) - } yield () - - private def openBrowser: ZIO[Any, CommandError, ExitCode] = - Command("open", "http://localhost:9630").exitCode - - override def run = - program - .provide(SbtManager.live, FileSystemService.live) - - private def pickleSocket[R, E, A: Pickler]( - f: A => ZStream[R, E, WebSocketFrame] - ): Socket[R, E, WebSocketFrame, WebSocketFrame] = - Socket.collect { - case WebSocketFrame.Binary(bytes) => - Try(Unpickle[A].fromBytes(ByteBuffer.wrap(bytes.toArray))) match { - case Failure(error) => - ZStream.fromZIO(Console.printLineError(s"Decoding Error: $error").orDie).drain - case Success(command) => - f(command) - } - case other => - ZStream.fromZIO(ZIO.succeed(println(s"RECEIVED $other"))).drain - } -} +//package zio.app +// +//import boopickle.Default._ +//import boopickle.Pickler +//import io.netty.buffer.Unpooled +//import io.netty.handler.codec.http.{HttpHeaderNames, HttpHeaderValues} +//import zhttp.http._ +//import zhttp.service.{ChannelEvent, Server} +//import zhttp.socket._ +//import zio._ +//import zio.app.cli.protocol.{ClientCommand, ServerCommand} +//import zio.process.{Command, CommandError} +//import zio.stream.ZStream +// +//import java.nio.ByteBuffer +//import scala.io.Source +//import scala.util.{Failure, Success, Try} +// +//object Backend extends ZIOAppDefault { +// def appSocket: SocketApp[FileSystemService with SbtManager] = +// pickleSocket { (command: ClientCommand) => +// command match { +// case ClientCommand.ChangeDirectory(path) => +// FileSystemService.cd(path) +// +// case ClientCommand.Subscribe => +// SbtManager.launchVite merge +// SbtManager.backendSbtStream +// .zipWithLatest(SbtManager.frontendSbtStream)(_ -> _) +// .zipWithLatest(FileSystemService.stateStream)(_ -> _) +// .map { case ((b, f), fs) => +// val command: ServerCommand = ServerCommand.State(b, f, fs) +// val byteBuf = Pickle.intoBytes(command) +// WebSocketFrame.binary(Chunk.fromArray(byteBuf.array())) +// } +// } +// } +// +// implicit def chunkPickler[A](implicit aPickler: Pickler[A]): Pickler[Chunk[A]] = +// transformPickler[Chunk[A], List[A]](as => Chunk.from(as))(_.toList) +// +// private def app: HttpApp[FileSystemService with SbtManager, Throwable] = +// Http.collectZIO { +// case Method.GET -> !! / "ws" => +// Response.fromSocket(appSocket) +// +// case Method.GET -> !! / "assets" / file => +// val source = Source.fromResource(s"dist/assets/$file").getLines().mkString("\n") +// +// val contentTypeHtml: Header = +// if (file.endsWith(".js")) (HttpHeaderNames.CONTENT_TYPE, "text/javascript") +// else (HttpHeaderNames.CONTENT_TYPE, "text/css") +// +// ZIO.succeed { +// Response( +// headers = Headers(contentTypeHtml), +// data = HttpData.fromChunk(Chunk.fromArray(source.getBytes(HTTP_CHARSET))), +// ) +// } +// +// case Method.GET -> !! => +// val html = Source.fromResource(s"dist/index.html").getLines().mkString("\n") +// +// val contentTypeHtml: Header = (HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.TEXT_HTML) +// ZIO.succeed { +// Response( +// data = HttpData.fromChunk(Chunk.fromArray(html.getBytes(HTTP_CHARSET))), +// headers = Headers(contentTypeHtml), +// ) +// } +// +// case other => +// println(s"RECEIVED NOT FOUND: $other") +// ZIO.succeed(Response.status(Status.NotFound)) +// } +// +// lazy val program = for { +// port <- System.envOrElse("PORT", "9630").map(_.toInt).orElseSucceed(9630) +// _ <- Console.printLine(s"STARTING SERVER ON PORT $port") +// _ <- openBrowser.delay(1.second).fork +// _ <- Server.start(port, app) +// } yield () +// +// private def openBrowser: ZIO[Any, CommandError, ExitCode] = +// Command("open", "http://localhost:9630").exitCode +// +// override def run = +// program +// .provide(SbtManager.live, FileSystemService.live) +// +// private def pickleSocket[R, E, A: Pickler]( +// f: A => ZIO[R, E, Any], +// ) = +// Http.collectZIO[WebSocketChannelEvent] { +// +// case ChannelEvent(ch, ChannelEvent.ChannelRead(WebSocketFrame.Binary(bytes))) => +// Try(Unpickle[A].fromBytes(ByteBuffer.wrap(bytes.toArray))) match { +// case Failure(error) => +// Console.printLineError(s"Decoding Error: $error").orDie +// case Success(command) => +// f(command) +// } +// } +// case other => +// Console.printLineError(s"UNEXPECTED SOCKET EVENT $other") +// } +//} diff --git a/cli/src/main/scala/zio/app/Main.scala b/cli/src/main/scala/zio/app/Main.scala index 5b22076..fcfb7c2 100644 --- a/cli/src/main/scala/zio/app/Main.scala +++ b/cli/src/main/scala/zio/app/Main.scala @@ -1,99 +1,99 @@ -package zio.app - -import view.View._ -import view._ -import zio._ -import zio.process.{Command, CommandError} - -import java.io.File - -object Main extends ZIOAppDefault { - def print(string: String): UIO[Unit] = ZIO.succeed(println(string)) - - def run = { - getArgs.flatMap { args => - if (args.headOption.contains("new")) { - createTemplateProject - } else if (args.headOption.contains("dev")) { - val view = vertical( - "Running Dev Mode", - "http://localhost:9630".blue - ) - println(view.renderNow) - for { - fiber <- Console.readLine.fork - result <- Backend.run raceFirst fiber.await.exitCode - } yield result - } else { - renderHelp - } - } - } - - private val createTemplateProject: ZIO[Any, Throwable, Unit] = for { - _ <- print("Configure your new ZIO app.".cyan.renderNow) - name <- TemplateGenerator.execute - pwd <- System.property("user.dir").someOrFail(new Error("Can't get PWD")) - dir = new File(new File(pwd), name) - _ <- runYarnInstall(dir) - view = vertical( - horizontal("Created ", name.yellow).bordered, - "Run the following commands to get started:", - s"cd $name".yellow, - "zio-app dev".yellow - ) - _ <- print(view.renderNow) - } yield () - - private def renderInvalidCommandError(command: String) = { - val view = - vertical( - vertical( - horizontal( - "Invalid Command:".red, - s" $command" - ) - ).bordered - .overlay( - "ERROR".red.reversed.padding(2, 0), - Alignment.topLeft - ), - horizontal( - "Are you're sure you're running ", - "zio-app dev".cyan, - " in a directory created using ", - "zio-app new".cyan, - "?" - ).bordered - ) - print("") *> - print(view.renderNow) - } - - private val renderHelp: UIO[Unit] = { - val view = - vertical( - horizontal("new".cyan, " Create a new zio-app"), - horizontal("dev".cyan, " Activate live-reloading dev mode") - ).bordered - .overlay( - text("commands", Color.Yellow).paddingH(2), - Alignment.topRight - ) - - print(view.renderNow) - } - - private def runYarnInstall(dir: File): ZIO[Any, CommandError, Unit] = - Command("yarn", "install") - .workingDirectory(dir) - .linesStream - .foreach(print) - .tapError { - case err if err.getMessage.contains("""Cannot run program "yarn"""") => - Command("npm", "i", "-g", "yarn").successfulExitCode - case _ => - ZIO.unit - } - .retryN(1) -} +//package zio.app +// +//import view.View._ +//import view._ +//import zio._ +//import zio.process.{Command, CommandError} +// +//import java.io.File +// +//object Main extends ZIOAppDefault { +// def print(string: String): UIO[Unit] = ZIO.succeed(println(string)) +// +// def run = { +// getArgs.flatMap { args => +// if (args.headOption.contains("new")) { +// createTemplateProject +// } else if (args.headOption.contains("dev")) { +// val view = vertical( +// "Running Dev Mode", +// "http://localhost:9630".blue +// ) +// println(view.renderNow) +// for { +// fiber <- Console.readLine.fork +// result <- Backend.run raceFirst fiber.await.exitCode +// } yield result +// } else { +// renderHelp +// } +// } +// } +// +// private val createTemplateProject: ZIO[Any, Throwable, Unit] = for { +// _ <- print("Configure your new ZIO app.".cyan.renderNow) +// name <- TemplateGenerator.execute +// pwd <- System.property("user.dir").someOrFail(new Error("Can't get PWD")) +// dir = new File(new File(pwd), name) +// _ <- runYarnInstall(dir) +// view = vertical( +// horizontal("Created ", name.yellow).bordered, +// "Run the following commands to get started:", +// s"cd $name".yellow, +// "zio-app dev".yellow +// ) +// _ <- print(view.renderNow) +// } yield () +// +// private def renderInvalidCommandError(command: String) = { +// val view = +// vertical( +// vertical( +// horizontal( +// "Invalid Command:".red, +// s" $command" +// ) +// ).bordered +// .overlay( +// "ERROR".red.reversed.padding(2, 0), +// Alignment.topLeft +// ), +// horizontal( +// "Are you're sure you're running ", +// "zio-app dev".cyan, +// " in a directory created using ", +// "zio-app new".cyan, +// "?" +// ).bordered +// ) +// print("") *> +// print(view.renderNow) +// } +// +// private val renderHelp: UIO[Unit] = { +// val view = +// vertical( +// horizontal("new".cyan, " Create a new zio-app"), +// horizontal("dev".cyan, " Activate live-reloading dev mode") +// ).bordered +// .overlay( +// text("commands", Color.Yellow).paddingH(2), +// Alignment.topRight +// ) +// +// print(view.renderNow) +// } +// +// private def runYarnInstall(dir: File): ZIO[Any, CommandError, Unit] = +// Command("yarn", "install") +// .workingDirectory(dir) +// .linesStream +// .foreach(print) +// .tapError { +// case err if err.getMessage.contains("""Cannot run program "yarn"""") => +// Command("npm", "i", "-g", "yarn").successfulExitCode +// case _ => +// ZIO.unit +// } +// .retryN(1) +//}