-
-
Notifications
You must be signed in to change notification settings - Fork 437
Universal Main #1729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Universal Main #1729
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
main/client/src/mill/main/client/IsolatedMillMainLoader.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package mill.main.client; | ||
|
|
||
| import java.lang.reflect.Method; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| class IsolatedMillMainLoader { | ||
|
|
||
| public static class LoadResult { | ||
|
|
||
| public final Optional<Method> millMainMethod; | ||
| public final boolean canLoad; | ||
| public final long loadTime; | ||
|
|
||
| public LoadResult(Optional<Method> millMainMethod, final long loadTime) { | ||
| this.millMainMethod = millMainMethod; | ||
| this.canLoad = millMainMethod.isPresent(); | ||
| this.loadTime = loadTime; | ||
| } | ||
| } | ||
|
|
||
| private static Optional<LoadResult> canLoad = Optional.empty(); | ||
|
|
||
| public static LoadResult load() { | ||
| if (canLoad.isPresent()) { | ||
| return canLoad.get(); | ||
| } else { | ||
| long startTime = System.currentTimeMillis(); | ||
| Optional<Method> millMainMethod = Optional.empty(); | ||
| try { | ||
| Class<?> millMainClass = IsolatedMillMainLoader.class.getClassLoader().loadClass("mill.MillMain"); | ||
| Method mainMethod = millMainClass.getMethod("main", String[].class); | ||
| millMainMethod = Optional.of(mainMethod); | ||
| } catch (ClassNotFoundException | NoSuchMethodException e) { | ||
| millMainMethod = Optional.empty(); | ||
| } | ||
|
|
||
| long loadTime = System.currentTimeMillis() - startTime; | ||
| LoadResult result = new LoadResult(millMainMethod, loadTime); | ||
| canLoad = Optional.of(result); | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| public static void runMain(String[] args) throws Exception { | ||
| LoadResult loadResult = load(); | ||
| if (loadResult.millMainMethod.isPresent()) { | ||
| if (!MillEnv.millJvmOptsAlreadyApplied() && MillEnv.millJvmOptsFile().exists()) { | ||
| // System.err.println("Warning: Settings from file `" + propOptsFile + "` are currently ignored."); | ||
| System.err.println("Launching Mill as sub-process ..."); | ||
| int exitVal = launchMillAsSubProcess(args); | ||
| System.exit(exitVal); | ||
| } else { | ||
| // launch mill in-process | ||
| // it will call System.exit for us | ||
| Method mainMethod = loadResult.millMainMethod.get(); | ||
| mainMethod.invoke(null, new Object[]{args}); | ||
| } | ||
| } else { | ||
| throw new RuntimeException("Cannot load mill.MillMain class"); | ||
| } | ||
| } | ||
|
|
||
| private static int launchMillAsSubProcess(String[] args) throws Exception { | ||
| boolean setJnaNoSys = System.getProperty("jna.nosys") == null; | ||
|
|
||
| List<String> l = new ArrayList<>(); | ||
| l.addAll(MillEnv.millLaunchJvmCommand(setJnaNoSys)); | ||
| l.add("mill.MillMain"); | ||
| l.addAll(Arrays.asList(args)); | ||
|
|
||
| Process running = new ProcessBuilder() | ||
| .command(l) | ||
| .inheritIO() | ||
| .start(); | ||
| return running.waitFor(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.