Skip to content

Commit

Permalink
Relativize jar paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ity committed Jul 18, 2018
1 parent 2834948 commit 5cedd46
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
16 changes: 11 additions & 5 deletions src/scala/org/pantsbuild/zinc/compiler/OutputUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,24 @@ object OutputUtils {
sorted
}

def relativize(base: String, path: Path): String = {
new File(base).toURI().relativize(new File(path.toString).toURI()).getPath()
}

/**
* Create a JAR of of filePaths provided.
*
* @param filePaths set of all paths to be added to the JAR
* @param outputJarPath Absolute Path to the output JAR being created
* @param jarEntryTime time to be set for each JAR entry
*/
def createJar(filePaths: mutable.TreeSet[Path], outputJarPath: Path, jarEntryTime: Long) {
def createJar(
base: String, filePaths: mutable.TreeSet[Path], outputJarPath: Path, jarEntryTime: Long) {

val target = new JarOutputStream(Files.newOutputStream(outputJarPath))

def addToJar(source: Path): FileVisitResult = {
val jarEntry = new JarEntry(source.toString)
def addToJar(source: Path, entryName: String): FileVisitResult = {
val jarEntry = new JarEntry(entryName)
// setting jarEntry time to a fixed value for all entries within the jar so that jars are
// byte-for-byte reproducible.
jarEntry.setTime(jarEntryTime)
Expand All @@ -62,7 +67,8 @@ object OutputUtils {
FileVisitResult.CONTINUE
}

filePaths.map(addToJar(_))
val pathToName = filePaths.zipWithIndex.map{case(k, v) => (k, relativize(base, k))}.toMap
pathToName.map(e => addToJar(e._1, e._2))
target.close()
}

Expand All @@ -75,7 +81,7 @@ object OutputUtils {
// Sort the contents of the classesDirectory for deterministic jar creation
val sortedClasses = sort(classesDirectory)

createJar(sortedClasses, outputJarPath, jarCreationTime)
createJar(classesDirectory.toString, sortedClasses, outputJarPath, jarCreationTime)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class JarCreationSpec extends WordSpec with MustMatchers {
IO.withTemporaryDirectory { tempOutputDir =>
val jarOutputPath = Paths.get(tempOutputDir.toString, "spec-empty-output.jar")

OutputUtils.createJar(filePaths, jarOutputPath, System.currentTimeMillis())
OutputUtils.createJar(tempInputDir.toString, filePaths, jarOutputPath, System.currentTimeMillis())
OutputUtils.existsClass(jarOutputPath, "NonExistent.class") must be(false)
}
}
Expand All @@ -30,14 +30,15 @@ class JarCreationSpec extends WordSpec with MustMatchers {
"JarCreationWithClasses" should {
"succeed when input classes are provided" in {
IO.withTemporaryDirectory { tempInputDir =>
val tempFile = File.createTempFile(tempInputDir.toString, "Clazz.class")
val tempFile = File.createTempFile("Temp", ".class", tempInputDir)
val filePaths = mutable.TreeSet(tempFile.toPath)

IO.withTemporaryDirectory { tempOutputDir =>
val jarOutputPath = Paths.get(tempOutputDir.toString, "spec-valid-output.jar")

OutputUtils.createJar(filePaths, jarOutputPath, System.currentTimeMillis())
OutputUtils.existsClass(jarOutputPath, tempFile.toString) must be(true)
OutputUtils.createJar(tempInputDir.toString, filePaths, jarOutputPath, System.currentTimeMillis())
OutputUtils.existsClass(jarOutputPath, tempFile.toString) must be(false)
OutputUtils.existsClass(jarOutputPath, tempFile.getName) must be(true)
}
}
}
Expand Down

0 comments on commit 5cedd46

Please sign in to comment.