possible to use file as input (#152)

This commit is contained in:
Dima 2021-06-03 15:45:09 +03:00 committed by GitHub
parent 6f6299e035
commit 6a96098227
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 16 deletions

View File

@ -28,10 +28,18 @@ object AppOps {
Validated
.fromEither(Validated.catchNonFatal {
val f = p.toFile
if (f.exists() && f.isDirectory) {
Right(p)
if (f.exists()) {
if (f.isFile) {
val filename = f.getName
val ext = Option(filename)
.filter(_.contains("."))
.map(f => f.substring(f.lastIndexOf(".") + 1))
.getOrElse("")
if (ext != "aqua") Left("File must be with 'aqua' extension")
else Right(p)
} else Right(p)
} else {
Left(s"There is no path '${p.toString}' or it is not a directory")
Left(s"There is no path '${p.toString}'")
}
}.toEither.left.map(t => s"An error occurred on imports reading: ${t.getMessage}").flatten)
.toValidatedNel
@ -41,7 +49,7 @@ object AppOps {
Opts
.option[Path](
"input",
"Path to the input directory that contains your .aqua files. It can only be a directory",
"Path to an aqua file or an input directory that contains your .aqua files",
"i"
)
.mapValidated(checkPath)

View File

@ -48,16 +48,19 @@ object AquaCli extends IOApp with LogSupport {
// if there is `--help` or `--version` flag - show help and version
// otherwise continue program execution
h.map(_ => helpAndExit) orElse v.map(_ => versionAndExit) getOrElse
h.map(_ => helpAndExit) orElse v.map(_ => versionAndExit) getOrElse {
val target = if (toAir) AquaCompiler.AirTarget else AquaCompiler.TypescriptTarget
val bc = {
val bc = BodyConfig(wrapWithXor = !noXor)
bc.copy(relayVarName = bc.relayVarName.filterNot(_ => noRelay))
}
AquaCompiler
.compileFilesTo[F](
input,
imports,
output,
if (toAir) AquaCompiler.AirTarget else AquaCompiler.TypescriptTarget, {
val bc = BodyConfig(wrapWithXor = !noXor)
bc.copy(relayVarName = bc.relayVarName.filterNot(_ => noRelay))
}
target,
bc
)
.map {
case Validated.Invalid(errs) =>
@ -67,6 +70,7 @@ object AquaCli extends IOApp with LogSupport {
results.map(println)
ExitCode.Success
}
}
}
}

View File

@ -36,13 +36,16 @@ object AquaCompiler extends LogSupport {
def targetPath(ext: String): Validated[Throwable, Path] =
Validated.catchNonFatal {
val srcDir = if (srcPath.toFile.isDirectory) srcPath else srcPath.getParent
val srcFilePath = srcDir.toAbsolutePath
.normalize()
.relativize(modFile.toAbsolutePath.normalize())
val targetAqua =
targetPath.toAbsolutePath
.normalize()
.resolve(
srcPath.toAbsolutePath
.normalize()
.relativize(modFile.toAbsolutePath.normalize())
srcFilePath
)
val fileName = targetAqua.getFileName

View File

@ -21,10 +21,14 @@ object AquaFiles {
// TODO use effect instead of Try
EitherT
.fromEither[F](
Try(sourcePath.toFile)
.filter(_.isDirectory)
.flatMap(d => Try(d.listFiles().toList))
.toEither
Try {
val f = sourcePath.toFile
if (f.isDirectory) {
f.listFiles().toList
} else {
List(f)
}
}.toEither
)
.leftMap[AquaFileError](FileSystemError)
.leftMap(NonEmptyChain.one)