Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addressed final comments
Browse files Browse the repository at this point in the history
ity committed Jul 19, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent f7a8e6f commit df624cb
Showing 3 changed files with 25 additions and 15 deletions.
31 changes: 20 additions & 11 deletions src/scala/org/pantsbuild/zinc/compiler/OutputUtils.scala
Original file line number Diff line number Diff line change
@@ -19,17 +19,21 @@ object OutputUtils {
* @param dir File handle containing the contents to sort
* @return sorted set of all paths within the `dir`
*/
def sort(dir:File): mutable.TreeSet[(Path, Boolean)] = {
val sorted = new mutable.TreeSet[(Path, Boolean)]()
def sort(dir:File): mutable.TreeSet[Path] = {
val sorted = new mutable.TreeSet[Path]()

val fileSortVisitor = new SimpleFileVisitor[Path]() {
override def preVisitDirectory(path: Path, attrs: BasicFileAttributes): FileVisitResult = {
sorted.add(path, false)
if (!path.endsWith("/")) {
sorted.add(Paths.get(path.toString, "/"))
} else {
sorted.add(path)
}
FileVisitResult.CONTINUE
}

override def visitFile(path: Path, attrs: BasicFileAttributes): FileVisitResult = {
sorted.add(path, true)
sorted.add(path)
FileVisitResult.CONTINUE
}
}
@@ -50,25 +54,30 @@ object OutputUtils {
* @param jarEntryTime time to be set for each JAR entry
*/
def createJar(
base: String, filePaths: mutable.TreeSet[(Path, Boolean)], outputJarPath: Path, jarEntryTime: Long) {
base: String, filePaths: mutable.TreeSet[Path], outputJarPath: Path, jarEntryTime: Long) {

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

def addToJar(source: (Path, Boolean), entryName: String): FileVisitResult = {
val jarEntry = new JarEntry(entryName)
def jarEntry(name: String): JarEntry = {
val jarEntry = new JarEntry(name)
// setting jarEntry time to a fixed value for all entries within the jar so that jars are
// byte-for-byte reproducible.
jarEntry.setTime(jarEntryTime)
jarEntry
}

target.putNextEntry(jarEntry)
if (source._2) {
Files.copy(source._1, target)
def addToJar(source: Path, entryName: String): FileVisitResult = {
if (source.toFile.isDirectory) {
target.putNextEntry(jarEntry(entryName))
} else {
target.putNextEntry(jarEntry(entryName))
Files.copy(source, target)
}
target.closeEntry()
FileVisitResult.CONTINUE
}

val pathToName = filePaths.zipWithIndex.map{case(k, v) => (k, relativize(base, k._1))}.toMap
val pathToName = filePaths.zipWithIndex.map{case(k, v) => (k, relativize(base, k))}.toMap
pathToName.map(e => addToJar(e._1, e._2))
target.close()
}
2 changes: 1 addition & 1 deletion src/scala/org/pantsbuild/zinc/compiler/Settings.scala
Original file line number Diff line number Diff line change
@@ -50,7 +50,7 @@ case class Settings(
sbt: SbtJars = SbtJars(),
_incOptions: IncOptions = IncOptions(),
analysis: AnalysisOptions = AnalysisOptions(),
creationTime: Long = System.currentTimeMillis()
creationTime: Long = 0
) {
import Settings._

Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ class JarCreationSpec extends WordSpec with MustMatchers {
"JarCreationWithoutClasses" should {
"succeed when input classes are not provided" in {
IO.withTemporaryDirectory { tempInputDir =>
val filePaths = new mutable.TreeSet[(Path, Boolean)]()
val filePaths = new mutable.TreeSet[Path]()

IO.withTemporaryDirectory { tempOutputDir =>
val jarOutputPath = Paths.get(tempOutputDir.toString, "spec-empty-output.jar")
@@ -26,11 +26,12 @@ class JarCreationSpec extends WordSpec with MustMatchers {
}
}
}

"JarCreationWithClasses" should {
"succeed when input classes are provided" in {
IO.withTemporaryDirectory { tempInputDir =>
val tempFile = File.createTempFile("Temp", ".class", tempInputDir)
val filePaths = mutable.TreeSet((tempFile.toPath, true))
val filePaths = mutable.TreeSet(tempFile.toPath)

IO.withTemporaryDirectory { tempOutputDir =>
val jarOutputPath = Paths.get(tempOutputDir.toString, "spec-valid-output.jar")
@@ -48,7 +49,7 @@ class JarCreationSpec extends WordSpec with MustMatchers {
IO.withTemporaryDirectory { tempInputDir =>
val nestedTempDir = Files.createTempDirectory(tempInputDir.toPath, "tmp")
val nestedTempClass = File.createTempFile("NestedTemp", ".class", nestedTempDir.toFile)
val filePaths = mutable.TreeSet((nestedTempDir, false), (nestedTempClass.toPath, true))
val filePaths = mutable.TreeSet(nestedTempDir, nestedTempClass.toPath)
IO.withTemporaryDirectory { tempOutputDir =>
val jarOutputPath = Paths.get(tempOutputDir.toString, "spec-valid-output.jar")

0 comments on commit df624cb

Please sign in to comment.