Skip to content

Commit

Permalink
Fix target resolving (#336)
Browse files Browse the repository at this point in the history
* fix and add test

* Update cli/src/main/scala/aqua/files/AquaFileSources.scala

Co-authored-by: folex <[email protected]>

* fix PR

Co-authored-by: folex <[email protected]>
Co-authored-by: Dmitry Kurinskiy <[email protected]>
  • Loading branch information
3 people authored Oct 25, 2021
1 parent 836dccc commit dacf4d6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
21 changes: 21 additions & 0 deletions cli/.jvm/src/test/scala/SourcesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,27 @@ class SourcesSpec extends AsyncFlatSpec with Matchers {
.unsafeToFuture()
}

"AquaFileSources" should "resolve correct path for target when file is in current directory" in {
val path = Path("cli/.jvm/src/test/test-dir")
val filePath = path.resolve("file.aqua")

val targetPath = Path("/target/dir/")

val sourceGen = new AquaFileSources[IO](path, Nil)

val suffix = "_custom.super"

sourceGen
.resolveTargetPath(filePath, targetPath, suffix)
.map { resolved =>
resolved.isValid shouldBe true

val targetFilePath = resolved.toOption.get
targetFilePath.toString shouldBe "/target/dir/file_custom.super"
}
.unsafeToFuture()
}

"AquaFileSources" should "write correct file with correct path" in {
val path = Path("cli/.jvm/src/test/test-dir")
val filePath = path.resolve("imports/import.aqua")
Expand Down
36 changes: 23 additions & 13 deletions cli/src/main/scala/aqua/files/AquaFileSources.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import cats.implicits.catsSyntaxApplicativeId
import cats.syntax.either.*
import cats.syntax.flatMap.*
import cats.syntax.functor.*
import cats.syntax.applicative.*
import cats.syntax.monad.*
import cats.syntax.traverse.*
import cats.{Functor, Monad}
Expand Down Expand Up @@ -90,21 +91,30 @@ class AquaFileSources[F[_]: AquaIO: Monad: Files: Functor](
targetPath: Path,
suffix: String
): F[Validated[Throwable, Path]] =
getDir(sourcesPath).map { srcDir =>
Validated.catchNonFatal {
val srcFilePath = srcDir.absolute.normalize
.relativize(srcFile.absolute.normalize)

val targetDir =
Files[F].isDirectory(sourcesPath).flatMap {
case false =>
Validated.catchNonFatal {
targetPath.absolute.normalize
.resolve(
srcFilePath
)
.resolve(srcFile.fileName.toString.stripSuffix(".aqua") + suffix)
}.pure[F]
case true =>
getDir(sourcesPath).map { srcDir =>
Validated.catchNonFatal {
val srcFilePath = srcDir.absolute.normalize
.relativize(srcFile.absolute.normalize)

targetDir.parent
.getOrElse(targetDir)
.resolve(srcFile.fileName.toString.stripSuffix(".aqua") + suffix)
}
// use `srcFilePath` as a suffix for target file path, so the directory structure is replicated
val targetDir =
targetPath.absolute.normalize
.resolve(
srcFilePath
)

targetDir.parent
.getOrElse(targetDir)
.resolve(srcFile.fileName.toString.stripSuffix(".aqua") + suffix)
}
}
}

// Write content to a file and return a success message
Expand Down

0 comments on commit dacf4d6

Please sign in to comment.