Merge pull request #3 from fluencelabs/aqua-gen

Aqua gen
This commit is contained in:
Dmitry Kurinskiy 2021-03-19 11:51:22 +03:00 committed by GitHub
commit 6c4bea84ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 80 additions and 4 deletions

View File

@ -1,4 +1,4 @@
version = 2.5.0
version = 2.7.5
docstrings = JavaDoc

View File

@ -10,13 +10,16 @@ lazy val root = project
name := "aqua-hll",
version := "0.1.0",
scalaVersion := dottyVersion,
mainClass in (Compile, run) := Some("aqua.Main"),
mainClass in (Compile, run) := Some("aqua.AquaGen"),
mainClass in assembly := Some("aqua.AquaGen"),
libraryDependencies ++= Seq(
"org.typelevel" %% "cats-effect" % "3.0.0-RC2",
"org.typelevel" %% "cats-parse" % "0.3.1",
"org.typelevel" %% "cats-free" % catsV,
"com.github.julien-truffaut" %% "monocle-core" % monocleV,
"com.github.julien-truffaut" %% "monocle-macro" % monocleV
"com.github.julien-truffaut" %% "monocle-macro" % monocleV,
"co.fs2" %% "fs2-core" % "3.0.0-M7",
"co.fs2" %% "fs2-io" % "3.0.0-M7"
),
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.5" % Test
)

View File

@ -1 +1,2 @@
addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % "0.4.6")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.15.0")

View File

@ -0,0 +1,72 @@
package aqua
import cats.data.Validated
import cats.effect.{ExitCode, IO, IOApp}
import fs2.io.file.Files
import fs2.{text, Stream}
import cats.implicits._
import java.io.{File, PrintWriter}
import scala.io.Source
import java.nio.file.{Path, Paths}
final case class ParseArgsException(private val message: String, private val cause: Throwable = None.orNull)
extends Exception(message, cause)
object AquaGen extends IOApp {
override def run(args: List[String]): IO[ExitCode] = {
val io = for {
args <- IO.fromEither(parseArgs(args))
(input, output) = args
_ <- convertAqua(input, output)
} yield {}
io.map(_ => ExitCode.Success)
.handleErrorWith(err => {
println(err)
IO(ExitCode.Error)
})
}
def parseArgs(args: List[String]): Either[ParseArgsException, (List[File], Path)] = {
val error = ParseArgsException("There should be two arguments: path/to/input/dir and path/to/output/dir")
for {
input <- args.headOption.toRight(error)
output <- args.lift(1).toRight(error)
inputDir <- {
val inputDir = new File(input)
if (!inputDir.isDirectory && !inputDir.exists())
Left(ParseArgsException("Input path should be a dir and exists"))
else Right(inputDir)
}
outputDir <- {
val outputDir = new File(output)
if (!outputDir.isDirectory && !outputDir.exists()) Left(ParseArgsException("Output path should be a dir"))
else Right(outputDir)
}
} yield (inputDir.listFiles().toList, outputDir.toPath)
}
def convertAqua(files: List[File], outputDir: Path): IO[List[Unit]] = {
(for {
file <- files
} yield {
Files[IO]
.readAll(file.toPath, 4096)
.through(text.utf8Decode)
.map(text =>
Aqua.generate(text) match {
case Validated.Valid(v)
v.mkString("\n")
case Validated.Invalid(errs)
errs.map(_.showForConsole(text)).toList.mkString("\n")
}
)
.through(text.utf8Encode)
.through(Files[IO].writeAll(outputDir.resolve(file.getName + ".result")))
.compile
.drain
}).sequence
}
}

View File

@ -5,7 +5,7 @@ import cats.data.Validated
import scala.io.Source
object Main extends IOApp.Simple {
object Test extends IOApp.Simple {
override def run: IO[Unit] =
IO {