diff options
Diffstat (limited to 'main/src/mill/modules/Jvm.scala')
-rw-r--r-- | main/src/mill/modules/Jvm.scala | 131 |
1 files changed, 64 insertions, 67 deletions
diff --git a/main/src/mill/modules/Jvm.scala b/main/src/mill/modules/Jvm.scala index 4c26b297..b4a55de3 100644 --- a/main/src/mill/modules/Jvm.scala +++ b/main/src/mill/modules/Jvm.scala @@ -8,7 +8,6 @@ import java.nio.file.attribute.PosixFilePermission import java.util.Collections import java.util.jar.{JarEntry, JarFile, JarOutputStream} -import ammonite.ops._ import coursier.{Cache, Dependency, Fetch, Repository, Resolution} import coursier.util.{Gather, Task} import geny.Generator @@ -23,11 +22,11 @@ import scala.collection.JavaConverters._ object Jvm { def interactiveSubprocess(mainClass: String, - classPath: Agg[Path], + classPath: Agg[os.Path], jvmArgs: Seq[String] = Seq.empty, envArgs: Map[String, String] = Map.empty, mainArgs: Seq[String] = Seq.empty, - workingDir: Path = null, + workingDir: os.Path = null, background: Boolean = false): Unit = { val args = Vector("java") ++ @@ -41,16 +40,16 @@ object Jvm { def baseInteractiveSubprocess(commandArgs: Seq[String], envArgs: Map[String, String], - workingDir: Path) = { + workingDir: os.Path) = { val process = baseInteractiveSubprocess0(commandArgs, envArgs, workingDir) val exitCode = process.waitFor() if (exitCode == 0) () - else throw InteractiveShelloutException() + else throw new Exception("Interactive Subprocess Failed") } def baseInteractiveSubprocess0(commandArgs: Seq[String], envArgs: Map[String, String], - workingDir: Path) = { + workingDir: os.Path) = { val builder = new java.lang.ProcessBuilder() for ((k, v) <- envArgs){ @@ -86,7 +85,7 @@ object Jvm { def runLocal(mainClass: String, - classPath: Agg[Path], + classPath: Agg[os.Path], mainArgs: Seq[String] = Seq.empty) (implicit ctx: Ctx): Unit = { inprocess(classPath, classLoaderOverrideSbtTesting = false, isolated = true, closeContextClassLoaderWhenDone = true, cl => { @@ -110,7 +109,7 @@ object Jvm { - def inprocess[T](classPath: Agg[Path], + def inprocess[T](classPath: Agg[os.Path], classLoaderOverrideSbtTesting: Boolean, isolated: Boolean, closeContextClassLoaderWhenDone: Boolean, @@ -143,11 +142,11 @@ object Jvm { } def subprocess(mainClass: String, - classPath: Agg[Path], + classPath: Agg[os.Path], jvmArgs: Seq[String] = Seq.empty, envArgs: Map[String, String] = Map.empty, mainArgs: Seq[String] = Seq.empty, - workingDir: Path = null) + workingDir: os.Path = null) (implicit ctx: Ctx) = { val commandArgs = @@ -157,7 +156,7 @@ object Jvm { mainArgs val workingDir1 = Option(workingDir).getOrElse(ctx.dest) - mkdir(workingDir1) + os.makeDir.all(workingDir1) val builder = new java.lang.ProcessBuilder() .directory(workingDir1.toIO) @@ -170,10 +169,10 @@ object Jvm { val stdout = proc.getInputStream val stderr = proc.getErrorStream val sources = Seq( - (stdout, Left(_: Bytes), ctx.log.outputStream), - (stderr, Right(_: Bytes),ctx.log.errorStream ) + (stdout, Left(_: os.Bytes), ctx.log.outputStream), + (stderr, Right(_: os.Bytes),ctx.log.errorStream ) ) - val chunks = mutable.Buffer.empty[Either[Bytes, Bytes]] + val chunks = mutable.Buffer.empty[Either[os.Bytes, os.Bytes]] while( // Process.isAlive doesn't exist on JDK 7 =/ util.Try(proc.exitValue).isFailure || @@ -186,7 +185,7 @@ object Jvm { readSomething = true val array = new Array[Byte](subStream.available()) val actuallyRead = subStream.read(array) - chunks.append(wrapper(new ammonite.ops.Bytes(array))) + chunks.append(wrapper(new os.Bytes(array))) parentStream.write(array, 0, actuallyRead) } } @@ -195,8 +194,8 @@ object Jvm { Thread.sleep(2) } - if (proc.exitValue() != 0) throw new InteractiveShelloutException() - else ammonite.ops.CommandResult(proc.exitValue(), chunks) + if (proc.exitValue() != 0) throw new Exception("Subprocess failed") + else os.CommandResult(proc.exitValue(), chunks) } private def createManifest(mainClass: Option[String]) = { @@ -214,42 +213,42 @@ object Jvm { * called out.jar in the implicit ctx.dest folder. An optional main class may * be provided for the jar. An optional filter function may also be provided to * selectively include/exclude specific files. - * @param inputPaths - `Agg` of `Path`s containing files to be included in the jar + * @param inputPaths - `Agg` of `os.Path`s containing files to be included in the jar * @param mainClass - optional main class for the jar * @param fileFilter - optional file filter to select files to be included. - * Given a `Path` (from inputPaths) and a `RelPath` for the individual file, + * Given a `os.Path` (from inputPaths) and a `os.RelPath` for the individual file, * return true if the file is to be included in the jar. * @param ctx - implicit `Ctx.Dest` used to determine the output directory for the jar. * @return - a `PathRef` for the created jar. */ - def createJar(inputPaths: Agg[Path], + def createJar(inputPaths: Agg[os.Path], mainClass: Option[String] = None, - fileFilter: (Path, RelPath) => Boolean = (p: Path, r: RelPath) => true) + fileFilter: (os.Path, os.RelPath) => Boolean = (p: os.Path, r: os.RelPath) => true) (implicit ctx: Ctx.Dest): PathRef = { val outputPath = ctx.dest / "out.jar" - rm(outputPath) + os.remove.all(outputPath) - val seen = mutable.Set.empty[RelPath] - seen.add("META-INF" / "MANIFEST.MF") + val seen = mutable.Set.empty[os.RelPath] + seen.add(os.rel / "META-INF" / "MANIFEST.MF") val jar = new JarOutputStream( new FileOutputStream(outputPath.toIO), createManifest(mainClass) ) try{ - assert(inputPaths.forall(exists(_))) + assert(inputPaths.forall(os.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)) + if (os.isFile(p)) Iterator(p -> os.rel / p.last) + else os.walk(p).filter(os.isFile).map(sub => sub -> sub.relativeTo(p)).sorted if !seen(mapping) && fileFilter(p, mapping) } { seen.add(mapping) val entry = new JarEntry(mapping.toString) - entry.setTime(file.mtime.toMillis) + entry.setTime(os.mtime(file)) jar.putNextEntry(entry) - jar.write(read.bytes(file)) + jar.write(os.read.bytes(file)) jar.closeEntry() } } finally { @@ -259,17 +258,10 @@ object Jvm { PathRef(outputPath) } - def newOutputStream(p: java.nio.file.Path, append: Boolean = false) = { - val options = - if(append) Seq(StandardOpenOption.APPEND) - else Seq(StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE) - Files.newOutputStream(p, options:_*) - } - - def createAssembly(inputPaths: Agg[Path], + def createAssembly(inputPaths: Agg[os.Path], mainClass: Option[String] = None, prependShellScript: String = "", - base: Option[Path] = None, + base: Option[os.Path] = None, assemblyRules: Seq[Assembly.Rule] = Assembly.defaultRules) (implicit ctx: Ctx.Dest with Ctx.Log): PathRef = { @@ -279,7 +271,7 @@ object Jvm { val hm = new java.util.HashMap[String, String]() base match{ - case Some(b) => cp(b, tmp) + case Some(b) => os.copy(b, tmp) case None => hm.put("create", "true") } @@ -288,23 +280,25 @@ object Jvm { val manifest = createManifest(mainClass) val manifestPath = zipFs.getPath(JarFile.MANIFEST_NAME) Files.createDirectories(manifestPath.getParent) - val manifestOut = newOutputStream(manifestPath) + val manifestOut = Files.newOutputStream( + manifestPath, + StandardOpenOption.TRUNCATE_EXISTING, + StandardOpenOption.CREATE + ) manifest.write(manifestOut) manifestOut.close() Assembly.groupAssemblyEntries(inputPaths, assemblyRules).view - .map { - case (mapping, aggregate) => - zipFs.getPath(mapping) -> aggregate - } .foreach { - case (path, AppendEntry(entries)) => + case (mapping, AppendEntry(entries)) => + val path = zipFs.getPath(mapping) val concatenated = new SequenceInputStream( Collections.enumeration(entries.map(_.inputStream).asJava)) - writeEntry(path, concatenated, append = Files.exists(path)) - case (path, WriteOnceEntry(entry)) => + writeEntry(os.Path(path), concatenated, append = true) + case (mapping, WriteOnceEntry(entry)) => + val path = zipFs.getPath(mapping) if (Files.notExists(path)) { - writeEntry(path, entry.inputStream, append = false) + writeEntry(os.Path(path), entry.inputStream, append = false) } } @@ -312,33 +306,36 @@ object Jvm { val output = ctx.dest / "out.jar" // Prepend shell script and make it executable - if (prependShellScript.isEmpty) mv(tmp, output) + if (prependShellScript.isEmpty) os.move(tmp, output) else{ val lineSep = if (!prependShellScript.endsWith("\n")) "\n\r\n" else "" - val outputStream = newOutputStream(output.toNIO) - IO.stream(new ByteArrayInputStream((prependShellScript + lineSep).getBytes()), outputStream) - IO.stream(read.getInputStream(tmp), outputStream) - outputStream.close() + os.write( + output, + Seq[os.Source]( + prependShellScript + lineSep, + os.read.inputStream(tmp) + ) + ) if (!scala.util.Properties.isWin) { - val perms = Files.getPosixFilePermissions(output.toNIO) - perms.add(PosixFilePermission.GROUP_EXECUTE) - perms.add(PosixFilePermission.OWNER_EXECUTE) - perms.add(PosixFilePermission.OTHERS_EXECUTE) - Files.setPosixFilePermissions(output.toNIO, perms) + os.perms.set( + output, + os.perms(output) + + PosixFilePermission.GROUP_EXECUTE + + PosixFilePermission.OWNER_EXECUTE + + PosixFilePermission.OTHERS_EXECUTE + ) } } PathRef(output) } - private def writeEntry(p: java.nio.file.Path, is: InputStream, append: Boolean): Unit = { - if (p.getParent != null) Files.createDirectories(p.getParent) - val outputStream = newOutputStream(p, append) - - IO.stream(is, outputStream) + private def writeEntry(p: os.Path, is: InputStream, append: Boolean): Unit = { + if (p.toNIO.getParent != null) Files.createDirectories(p.toNIO.getParent) + if (append) os.write(p, is) + else os.write.append(p, is) - outputStream.close() is.close() } @@ -376,7 +373,7 @@ object Jvm { ) } def createLauncher(mainClass: String, - classPath: Agg[Path], + classPath: Agg[os.Path], jvmArgs: Seq[String]) (implicit ctx: Ctx.Dest)= { val isWin = scala.util.Properties.isWin @@ -387,7 +384,7 @@ object Jvm { val outputPath = ctx.dest / (if (isBatch) "run.bat" else "run") val classPathStrs = classPath.map(_.toString) - write(outputPath, launcherUniversalScript(mainClass, classPathStrs, classPathStrs, jvmArgs)) + os.write(outputPath, launcherUniversalScript(mainClass, classPathStrs, classPathStrs, jvmArgs)) if (!isWin) { val perms = Files.getPosixFilePermissions(outputPath.toNIO) @@ -454,7 +451,7 @@ object Jvm { val (errors, successes) = load(sourceOrJar) if(errors.isEmpty){ mill.Agg.from( - successes.map(p => PathRef(Path(p), quick = true)).filter(_.path.ext == "jar") + successes.map(p => PathRef(os.Path(p), quick = true)).filter(_.path.ext == "jar") ) }else{ val errorDetails = errors.map(e => s"${ammonite.util.Util.newLine} ${e.describe}").mkString |