Skip to content

Commit

Permalink
Eagerly initialize rt cache in Main.main and Client.main (i.e., for b…
Browse files Browse the repository at this point in the history
…oth interactive/non-interactive modes), mainly for a more predictable client/server on Windows and Java 9.
  • Loading branch information
robby-phd committed Mar 27, 2018
1 parent 20f7a10 commit a53b0c3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions clientserver/src/mill/clientserver/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public static void main(String[] args) throws Exception{
if (setJnaNoSys) {
System.setProperty("jna.nosys", "true");
}
if (ClientServer.isJava9OrAbove) ClientServer.initRt(args);
int index = 0;
while (index < 5) {
index += 1;
Expand Down
41 changes: 40 additions & 1 deletion clientserver/src/mill/clientserver/ClientServer.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,58 @@
package mill.clientserver;


import io.github.retronym.java9rtexport.Export;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

class ClientServer {
public class ClientServer {
public static boolean isWindows = System.getProperty("os.name").toLowerCase().startsWith("windows");
public static boolean isJava9OrAbove = !System.getProperty("java.specification.version").startsWith("1.");

// Windows named pipe prefix (see https://github.com/sbt/ipcsocket/blob/v1.0.0/README.md)
// Win32NamedPipeServerSocket automatically adds this as a prefix (if it is not already is prefixed),
// but Win32NamedPipeSocket does not
// https://github.com/sbt/ipcsocket/blob/v1.0.0/src/main/java/org/scalasbt/ipcsocket/Win32NamedPipeServerSocket.java#L36
public static String WIN32_PIPE_PREFIX = "\\\\.\\pipe\\";

public static void initRt(String[] args) throws IOException {
// Ideally, rtJarName should be using ammonite.runtime.Classpath.rtJarName
// however, that one is written in Scala which pulls in lots of deps
// This can be easily changed on the Ammonite side
String javaVersion = System.getProperty("java.version");
String rtJarName = "rt-" + javaVersion + ".jar";

String home = String.join(File.separator,
new String[] { System.getProperty("user.home"), ".mill", "ammonite" });;

// Ideally, this should use ammonite.main.CLI, but that one is also in Scala
for (int i = 0; i < args.length; i++) {
switch (args[i]) {
case "-h":
case "--home":
i++;
if (i < args.length) {
home = args[i];
}
break;
default:
break;
}
}

File rtFile = new File(home + File.separator + rtJarName);
if (!rtFile.exists()) {
// No logger just yet, so info is printed using System.out directly
System.out.println("Copying Java " + javaVersion + " runtime jar to " + home + " ...");
System.out.flush();
rtFile.getParentFile().mkdirs();
java.nio.file.Files.copy(Export.export().toPath(), rtFile.toPath());
}
}

public static String[] parseArgs(InputStream argStream) throws IOException {

int argsLength = argStream.read();
Expand Down
3 changes: 3 additions & 0 deletions main/src/mill/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ object Main {
case Array(s, _*) if s == "-i" || s == "--interactive" => args.tail
case _ => args
}
// This is unnecessary but done to be consistent with client/server mode
if (mill.clientserver.ClientServer.isJava9OrAbove)
mill.clientserver.ClientServer.initRt(as)
val (result, _) = main0(
as,
None,
Expand Down

0 comments on commit a53b0c3

Please sign in to comment.