Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper methods to create mappings #38

Merged
merged 6 commits into from
Mar 28, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions io/src/main/scala/sbt/io/Path.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ object Path extends Mapper {
val sep: Char = java.io.File.separatorChar

def toURLs(files: Seq[File]): Array[URL] = files.map(_.toURI.toURL).toArray

}

object PathFinder {
Expand Down
54 changes: 54 additions & 0 deletions io/src/main/scala/sbt/io/PathMapper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,60 @@ abstract class Mapper {
def selectSubpaths(base: File, filter: FileFilter): Traversable[(File, String)] =
(PathFinder(base) ** filter --- PathFinder(base)) pair (relativeTo(base) | flat)

/**
* 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/file1) -> "extras/file1"
* File($baseDirectory/extras/file2) -> "extras/"file2"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's an extra "

* ...
* }}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this still missing $baseDirectory/extra -> "extra"?

*
*
* @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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's still a missing ) here

* }
* }}}
*
* @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] =
(zero /: in)((mapper, base) => f(base) | mapper)
}
58 changes: 54 additions & 4 deletions io/src/test/scala/sbt/io/PathMapperSpec.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package sbt.io

import java.nio.file.{ Files, Path => NioPath }

import org.scalatest._
import Path._
import sbt.io.syntax._

class PathMapperSpec extends fixture.FlatSpec with Matchers {

import Path._, sbt.io.syntax._
type FixtureParam = NioPath

class PathMapperSpec extends FlatSpec with Matchers {
"PathMapper" should "create copy resource mappings correctly" in {
val base = file("/")
"rebase | flat" should "copy resource mappings correctly" in { tempDirectory =>
val base = tempDirectory.toFile

val files = Seq(base / "src" / "main" / "resources" / "scalac-plugin.xml")
val dirs = Seq(
Expand All @@ -22,4 +27,49 @@ class PathMapperSpec extends FlatSpec with Matchers {
base / "target" / "scala-2.11" / "classes" / "scalac-plugin.xml"
)
}

"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

val mappings = Path.directory(tempDirectory.toFile)

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

"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

val mappings = Path.contentOf(tempDirectory.toFile)

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

override protected def withFixture(test: OneArgTest): Outcome = {
val tmpDir = Files.createTempDirectory("path-mappings")
try {
withFixture(test.toNoArgTest(tmpDir))
} finally {
// cleanup an delete the temp directory
IO.delete(tmpDir.toFile)
}
}
}