diff options
author | Li Haoyi <haoyi.sg@gmail.com> | 2017-11-05 17:34:36 -0800 |
---|---|---|
committer | Li Haoyi <haoyi.sg@gmail.com> | 2017-11-05 17:34:36 -0800 |
commit | 7a6c80301fa5f405f1d9ffca2776b19cf9a29b9a (patch) | |
tree | 2f3f4375097756bded9dfbe8bd546b448b2cd394 /core/src/main/scala/forge/modules/Jvm.scala | |
parent | 011e8ef1059196a373a0eee32c58dfdaee3493f2 (diff) | |
download | mill-7a6c80301fa5f405f1d9ffca2776b19cf9a29b9a.tar.gz mill-7a6c80301fa5f405f1d9ffca2776b19cf9a29b9a.tar.bz2 mill-7a6c80301fa5f405f1d9ffca2776b19cf9a29b9a.zip |
Break out reusable JVM specific stuff into its own file
Diffstat (limited to 'core/src/main/scala/forge/modules/Jvm.scala')
-rw-r--r-- | core/src/main/scala/forge/modules/Jvm.scala | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/core/src/main/scala/forge/modules/Jvm.scala b/core/src/main/scala/forge/modules/Jvm.scala new file mode 100644 index 00000000..79a2c989 --- /dev/null +++ b/core/src/main/scala/forge/modules/Jvm.scala @@ -0,0 +1,70 @@ +package forge.modules + +import java.io.FileOutputStream +import java.util.jar.{JarEntry, JarOutputStream} + +import ammonite.ops._ +import forge.define.Target +import forge.eval.PathRef +import forge.util.Args + + +object Jvm { + + def subprocess(mainClass: String, + classPath: Seq[Path]) = { + import ammonite.ops.ImplicitWd._ + %("java", "-cp", classPath.mkString(":"), mainClass) + } + + 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 = { + createJar(args.dest, args.args.map(_.asInstanceOf[PathRef].path)) + PathRef(args.dest) + } + } +} |