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 1 commit
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
54 changes: 54 additions & 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,60 @@ object Path extends Mapper {
val sep: Char = java.io.File.separatorChar

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

/**
* 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 in Universal ++= directory(baseDirectory.value / "extra")
* }}}
*
* The resulting mappings sequence will look something like this
*
* {{{
* File(extras/file1) -> "extras/file1"
* File(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.

This example seems to be wrong. I'm getting:

scala> for ((file, path) <- Path directory (file("base") / "extra")) println(s"""$file -> "$path"""")
base/extra -> "extra"
base/extra/file1 -> "extra/file1"
base/extra/file2 -> "extra/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 in Universal ++= contentOf(baseDirectory.value / "extra")
* }}}
*
* The resulting mappings sequence will look something like this
*
* {{{
* File(extras/file1) -> "file1"
* File(extras/file2) -> "file2"
* ...
* }}}
Copy link
Member

Choose a reason for hiding this comment

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

This example seems to be wrong. I'm getting:

scala> for ((file, path) <- Path contentOf (file("base") / "extra")) println(s"""$file -> "$path"""")
base/extra/file1 -> "file1"
base/extra/file2 -> "file2"

*
* @example Add a static directory "extra" and re-map the destination to a different path
* {{{
* mappings in Universal ++= 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.

this example doesn't compile (missing )) and is missing an explanation at the end.

* }
* }}}
*
* @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)
}

object PathFinder {
Expand Down
56 changes: 56 additions & 0 deletions io/src/test/scala/sbt/io/PathSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package sbt.io

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

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

class PathSpec extends fixture.FlatSpec with Matchers {

type FixtureParam = NioPath

"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)
}
}
}