Skip to content

Commit

Permalink
remove PathMapper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jtjeferreira committed Dec 10, 2024
1 parent e27c3f6 commit d396704
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 167 deletions.
72 changes: 0 additions & 72 deletions io/src/main/scala/sbt/io/PathMapper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -102,78 +102,6 @@ abstract class Mapper {
*/
def flat(newDirectory: File): FileMap = file => Some(new File(newDirectory, file.getName))

/**
* Selects all descendants of `base` directory and maps them to a path relative to `base`.
* `base` itself is not included.
*/
def allSubpaths(base: File): Traversable[(File, String)] =
selectSubpaths(base, AllPassFilter)

/**
* Selects descendants of `base` directory matching `filter` and maps them to a path relative to `base`.
* `base` itself is not included.
*/
def selectSubpaths(base: File, filter: FileFilter): Traversable[(File, String)] =
PathFinder(base).globRecursive(filter).get().collect {
case f if f != base => f -> base.toPath.relativize(f.toPath).toString
}

/**
* return a Seq of mappings which effect is to add a whole directory in the generated package
*
* @example In order to create mappings for a static directory "extra" add
* {{{
* mappings ++= directory(baseDirectory.value / "extra")
* }}}
*
* The resulting mappings sequence will look something like this
*
* {{{
* File(baseDirectory/extras) -> "extras"
* File(baseDirectory/extras/file1) -> "extras/file1"
* File(baseDirectory/extras/file2) -> "extras/file2"
* ...
* }}}
*
* @param baseDirectory The directory that should be turned into a mappings sequence.
* @return mappings The `baseDirectory` and all of its contents
*/
def directory(baseDirectory: File): Seq[(File, String)] =
Option(baseDirectory.getParentFile)
.map(parent => PathFinder(baseDirectory).allPaths pair relativeTo(parent))
.getOrElse(PathFinder(baseDirectory).allPaths pair basic)

/**
* return a Seq of mappings excluding the directory itself.
*
* @example In order to create mappings for a static directory "extra" add
* {{{
* mappings ++= contentOf(baseDirectory.value / "extra")
* }}}
*
* The resulting mappings sequence will look something like this
*
* {{{
* File(baseDirectory/extras/file1) -> "file1"
* File(baseDirectory/extras/file2) -> "file2"
* ...
* }}}
*
* @example Add a static directory "extra" and re-map the destination to a different path
* {{{
* mappings ++= contentOf(baseDirectory.value / "extra").map {
* case (src, destination) => src -> s"new/path/destination"
* }
* }}}
*
* @param baseDirectory The directory that should be turned into a mappings sequence.
* @return mappings - The `basicDirectory`'s contents exlcuding `basicDirectory` itself
*/
def contentOf(baseDirectory: File): Seq[(File, String)] = (
(PathFinder(baseDirectory).allPaths --- PathFinder(baseDirectory))
pair relativeTo(baseDirectory)
)

private[this] def fold[A, B, T](zero: A => Option[B], in: Iterable[T])(
f: T => A => Option[B]
): A => Option[B] =
Expand Down
95 changes: 0 additions & 95 deletions io/src/test/scala/sbt/io/PathMapperSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,101 +43,6 @@ class PathMapperSpec extends flatspec.FixtureAnyFlatSpec with Matchers {
)
}

"directory" should "create mappings including the baseDirectory" in { tempDirectory =>
val nestedFile1 = Files.createFile(tempDirectory resolve "file1").toFile
val nestedFile2 = Files.createFile(tempDirectory resolve "file2").toFile
val nestedDir = Files.createDirectory(tempDirectory resolve "dir1")
val nestedDirFile = Files.createDirectory(nestedDir resolve "dir1-file1").toFile

IO.touch(nestedFile1)
IO.touch(nestedFile2)
IO.createDirectory(nestedDir.toFile)
IO.touch(nestedDirFile)

val mappings = Path.directory(tempDirectory.toFile).map { case (f, s) => (f, file(s)) }

mappings should contain theSameElementsAs List(
tempDirectory.toFile -> file(s"${tempDirectory.getFileName}"),
nestedFile1 -> file(s"${tempDirectory.getFileName}/file1"),
nestedFile2 -> file(s"${tempDirectory.getFileName}/file2"),
nestedDir.toFile -> file(s"${tempDirectory.getFileName}/dir1"),
nestedDirFile -> file(s"${tempDirectory.getFileName}/dir1/dir1-file1")
)
}

it should "create one mapping entry for an empty directory" in { tempDirectory =>
val mappings = Path.directory(tempDirectory.toFile)

mappings should contain theSameElementsAs List[(File, String)](
tempDirectory.toFile -> s"${tempDirectory.getFileName}"
)
}

it should "create an empty mappings sequence for a non-existing directory" in { tempDirectory =>
val nonExistingDirectory = tempDirectory.resolve("imaginary")
val mappings = Path.directory(nonExistingDirectory.toFile)

mappings should be(empty)
}

it should "create one mapping entry if the directory is a file" in { tempDirectory =>
val file = tempDirectory.resolve("file").toFile
IO.touch(file)
val mappings = Path.directory(file)

mappings should contain theSameElementsAs List[(File, String)](
file -> s"${file.getName}"
)
}

"contentOf" should "create mappings excluding the baseDirectory" in { tempDirectory =>
val nestedFile1 = Files.createFile(tempDirectory resolve "file1").toFile
val nestedFile2 = Files.createFile(tempDirectory resolve "file2").toFile
val nestedDir = Files.createDirectory(tempDirectory resolve "dir1")
val nestedDirFile = Files.createDirectory(nestedDir resolve "dir1-file1").toFile

IO.touch(nestedFile1)
IO.touch(nestedFile2)
IO.createDirectory(nestedDir.toFile)
IO.touch(nestedDirFile)

val mappings = Path.contentOf(tempDirectory.toFile).map { case (f, s) => (f, file(s)) }

mappings should contain theSameElementsAs List(
nestedFile1 -> file(s"file1"),
nestedFile2 -> file(s"file2"),
nestedDir.toFile -> file(s"dir1"),
nestedDirFile -> file(s"dir1/dir1-file1")
)
}

it should "create an empty mappings sequence for an empty directory" in { tempDirectory =>
val mappings = Path.contentOf(tempDirectory.toFile)

mappings should be(empty)
}

it should "create an empty mappings sequence for a non-existing directory" in { tempDirectory =>
val nonExistingDirectory = tempDirectory.resolve("imaginary")
val mappings = Path.contentOf(nonExistingDirectory.toFile)

mappings should be(empty)
}

it should "create an empty mappings sequence if the directory is a file" in { tempDirectory =>
val file = tempDirectory.resolve("file").toFile
val mappings = Path.contentOf(file)

mappings should be(empty)
}

it should "not include the base directory" in { tempDirectory =>
val file = Files.createFile(tempDirectory.resolve("file"))
val paths = Path.allSubpaths(tempDirectory.toFile).toVector.map(_._1.toPath).toSet
assert(paths.contains(file))
assert(!paths.contains(tempDirectory))
}

override protected def withFixture(test: OneArgTest): Outcome = {
val tmpDir = Files.createTempDirectory("path-mappings")
try {
Expand Down

0 comments on commit d396704

Please sign in to comment.