summaryrefslogtreecommitdiff
path: root/core/src/test
diff options
context:
space:
mode:
authorLi Haoyi <haoyi.sg@gmail.com>2017-11-05 16:53:18 -0800
committerLi Haoyi <haoyi.sg@gmail.com>2017-11-05 16:53:18 -0800
commit011e8ef1059196a373a0eee32c58dfdaee3493f2 (patch)
tree3b61cac5eb6070e7cc0757e22be684a683d64985 /core/src/test
parent427134c820f4c9a9e281ba3607ab4879c6185613 (diff)
downloadmill-011e8ef1059196a373a0eee32c58dfdaee3493f2.tar.gz
mill-011e8ef1059196a373a0eee32c58dfdaee3493f2.tar.bz2
mill-011e8ef1059196a373a0eee32c58dfdaee3493f2.zip
Crib `createJar` code from CBT to properly set manifest
Diffstat (limited to 'core/src/test')
-rw-r--r--core/src/test/scala/forge/JavaCompileJarTests.scala62
1 files changed, 46 insertions, 16 deletions
diff --git a/core/src/test/scala/forge/JavaCompileJarTests.scala b/core/src/test/scala/forge/JavaCompileJarTests.scala
index f4a5fc88..33383a95 100644
--- a/core/src/test/scala/forge/JavaCompileJarTests.scala
+++ b/core/src/test/scala/forge/JavaCompileJarTests.scala
@@ -1,7 +1,7 @@
package forge
import java.io.FileOutputStream
-import java.util.jar.JarEntry
+import java.util.jar.{JarEntry, JarOutputStream}
import ammonite.ops._
import forge.define.Target
@@ -22,24 +22,53 @@ object JavaCompileJarTests extends TestSuite{
}
+ private def createManifest(mainClass: Option[String]) = {
+ val m = new java.util.jar.Manifest()
+ m.getMainAttributes.put(java.util.jar.Attributes.Name.MANIFEST_VERSION, "1.0")
+ m.getMainAttributes.putValue( "Created-By", "Scala Forge" )
+ mainClass.foreach(
+ m.getMainAttributes.put(java.util.jar.Attributes.Name.MAIN_CLASS, _)
+ )
+ m
+ }
+
+ def createJar(outputPath: Path, inputPaths: Seq[Path], mainClass: Option[String] = None ): Option[Path] = {
+ rm(outputPath)
+ if(inputPaths.isEmpty) None
+ else {
+ mkdir(outputPath/up)
+
+ val jar = new JarOutputStream(
+ new FileOutputStream(outputPath.toIO),
+ createManifest(mainClass)
+ )
+
+ try{
+ assert(inputPaths.forall(exists(_)))
+ for{
+ p <- inputPaths
+ (file, mapping) <-
+ if (p.isFile) Iterator(p -> empty/p.last)
+ else ls.rec(p).filter(_.isFile).map(sub => sub -> sub.relativeTo(p))
+ } {
+ val entry = new JarEntry(mapping.toString)
+ entry.setTime(file.mtime.toMillis)
+ jar.putNextEntry(entry)
+ jar.write(read.bytes(file))
+ jar.closeEntry
+ }
+ } finally {
+ jar.close
+ }
+
+ Some(outputPath)
+ }
+ }
case class jarUp(roots: Target[PathRef]*) extends Target[PathRef]{
val inputs = roots
def evaluate(args: Args): PathRef = {
-
- val output = new java.util.jar.JarOutputStream(new FileOutputStream(args.dest.toIO))
- for{
- root0 <- args.args
- root = root0.asInstanceOf[PathRef]
-
- path <- ls.rec(root.path)
- if path.isFile
- }{
- val relative = path.relativeTo(root.path)
- output.putNextEntry(new JarEntry(relative.toString))
- output.write(read.bytes(path))
- }
- output.close()
+ createJar(args.dest, args.args.map(_.asInstanceOf[PathRef].path))
PathRef(args.dest)
}
}
@@ -123,7 +152,8 @@ object JavaCompileJarTests extends TestSuite{
val jarContents = %%('jar, "-tf", workspacePath/'jar)(workspacePath).out.string
val expectedJarContents =
- """hello.txt
+ """META-INF/MANIFEST.MF
+ |hello.txt
|test/Bar.class
|test/BarThree.class
|test/BarTwo.class