Skip to content

Commit

Permalink
Merge pull request #175 from eatkins/jdk9-relativize
Browse files Browse the repository at this point in the history
Update relativize method for java 9
  • Loading branch information
eed3si9n committed Aug 3, 2018
2 parents 265cba2 + 212b407 commit fb0acb1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions io/src/main/scala/sbt/io/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -608,12 +608,14 @@ object IO {
* If `file` or `base` are not absolute, they are first resolved against the current working directory.
*/
def relativize(base: File, file: File): Option[String] = {
val basePath = (if (base.isAbsolute) base else base.getCanonicalFile).toPath
val filePath = (if (file.isAbsolute) file else file.getCanonicalFile).toPath
if ((filePath startsWith basePath) || (filePath.normalize() startsWith basePath.normalize())) {
val basePath = (if (base.isAbsolute) base else base.getCanonicalFile).toPath.normalize()
val filePath = (if (file.isAbsolute) file else file.getCanonicalFile).toPath.normalize()
if (filePath startsWith basePath) {
val relativePath = catching(classOf[IllegalArgumentException]) opt (basePath relativize filePath)
relativePath map (_.toString)
} else None
} else {
None
}
}

def copy(sources: Traversable[(File, File)]): Set[File] = copy(sources, CopyOptions())
Expand Down
2 changes: 1 addition & 1 deletion io/src/test/scala/sbt/io/IOSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class IOSpec extends FlatSpec with Matchers {
val relativeRootDir = new File(nestedDir, "..")

IO.relativize(rootDir.toFile, nestedFile).map(file) shouldBe Some(file("meh.file"))
IO.relativize(relativeRootDir, nestedFile).map(file) shouldBe Some(file("../../meh.file"))
IO.relativize(relativeRootDir, nestedFile).map(file) shouldBe Some(file("meh.file"))
}

it should "relativize . dirs" in {
Expand Down

0 comments on commit fb0acb1

Please sign in to comment.