diff --git a/build.sbt b/build.sbt index 7f35c957..2e553af6 100644 --- a/build.sbt +++ b/build.sbt @@ -10,7 +10,8 @@ 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", diff --git a/project/plugins.sbt b/project/plugins.sbt index 83a3da10..a0e2b9d3 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1 +1,2 @@ addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % "0.4.6") +addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.15.0") \ No newline at end of file diff --git a/src/main/scala/aqua/AquaGen.scala b/src/main/scala/aqua/AquaGen.scala new file mode 100644 index 00000000..3d428235 --- /dev/null +++ b/src/main/scala/aqua/AquaGen.scala @@ -0,0 +1,56 @@ +package aqua + +import cats.data.Validated +import cats.effect.{ExitCode, IO, IOApp} + +import java.io.{File, PrintWriter} +import scala.io.Source + +object AquaGen extends IOApp { + + override def run(args: List[String]): IO[ExitCode] = + IO { + val error = "There should be two arguments: path/to/input/dir and path/to/output/dir" + val process = for { + input <- args.headOption.toRight(error) + output <- args.lift(1).toRight(error) + inputDir <- { + val inputDir = new File(input) + if (!inputDir.isDirectory && !inputDir.exists()) Left("Input path should be a dir and exists") + else Right(inputDir) + } + outputDir <- { + val outputDir = new File(output) + if (!outputDir.isDirectory && !outputDir.exists()) Left("Output path should be a dir") + else Right(outputDir) + } + _ = convertAqua(inputDir.listFiles().toList, outputDir) + + } yield { + + } + + process.fold(err => { + println(err); + ExitCode.Error + }, _ => ExitCode.Success) + + } + + def convertAqua(files: List[File], outputDir: File): Unit = { + for { + file <- files + } yield { + val src = Source.fromFile(file) + val lines = try src.mkString finally src.close() + val result = Aqua.generate(lines) match { + case Validated.Valid(v) ⇒ + v.mkString("\n") + case Validated.Invalid(errs) ⇒ + errs.map(_.showForConsole(lines)).toList.mkString("\n") + } + new PrintWriter(outputDir.toPath.resolve(file.getName + ".result").toFile.getAbsolutePath) { write(result); close() } + + } + } +} diff --git a/src/main/scala/aqua/Main.scala b/src/main/scala/aqua/Test.scala similarity index 95% rename from src/main/scala/aqua/Main.scala rename to src/main/scala/aqua/Test.scala index 89d07e98..db7a8277 100644 --- a/src/main/scala/aqua/Main.scala +++ b/src/main/scala/aqua/Test.scala @@ -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 {