Skip to content

Commit

Permalink
Use Gradle ExecAction to support reading from System.in
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi authored and stuartwdouglas committed Mar 31, 2020
1 parent 7194c0c commit cd8c2c8
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,19 @@

/**
* A build item that represents the raw command line arguments as they were passed to the application.
*
* This can be passed directly to bytecode recorders that take a {@code Supplier<String[]>}.
*
* No filtering is done on these parameters.
*/
public final class RawCommandLineArgumentsBuildItem extends SimpleBuildItem
implements BytecodeRecorderImpl.ReturnedProxy, Supplier<String[]> {

/**
* As this object directly implements ReturnedProxy it can be
* passed into bytecode recorders. The runtime value of the command line parameters
* in places directly into the startup context under this key.
*/
@Override
public String __returned$proxy$key() {
return StartupContext.RAW_COMMAND_LINE_ARGS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void accept(Integer integer) {
runtimeUpdatesProcessor.checkForChangedClasses();
restartApp(runtimeUpdatesProcessor.checkForFileChange());
} catch (Exception e) {
e.printStackTrace();
log.error("Failed to restart", e);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static void main(Path appClasses, Path wiring, URL[] classPath, String...

context.getModules().add(new DevModeContext.ModuleInfo("main", new File("").getAbsolutePath(),
Collections.singleton(src.getAbsolutePath()), appClassesFile.getAbsolutePath(), res.getAbsolutePath()));
//the loading of this is super wierd, and does its own class loader delegation for some reason
//the loading of this is super weird, and does its own class loader delegation for some reason
ConfigProviderResolver.setInstance(new SmallRyeConfigProviderResolver());
DevModeMain main = new DevModeMain(context);
main.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ public MainClassBuildItem mainClassBuildStep(BuildProducer<GeneratedClassBuildIt
PackageConfig packageConfig) {
String mainClassName = MAIN_CLASS;
Map<String, String> quarkusMainAnnotations = new HashMap<>();
Collection<AnnotationInstance> defaultMains = combinedIndexBuildItem.getIndex()
Collection<AnnotationInstance> quarkusMains = combinedIndexBuildItem.getIndex()
.getAnnotations(DotName.createSimple(QuarkusMain.class.getName()));
for (AnnotationInstance i : defaultMains) {
for (AnnotationInstance i : quarkusMains) {
AnnotationValue nameValue = i.value("name");
String name = "";
if (nameValue != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import java.lang.annotation.Target;

/**
* The default main class of a Quarkus application. This annotation can appear
* at most once in a Quarkus application.
* The default main class of a Quarkus application.
*
* There are two different ways this annotation can be used. The first is to place it
* on a class with a Java main method. This main method will be the default entry point of
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package io.quarkus.gradle.tasks;

import static java.util.stream.Collectors.joining;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -25,8 +19,6 @@
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
Expand Down Expand Up @@ -381,30 +373,15 @@ public void startDev() {

args.add("-jar");
args.add(tempFile.getAbsolutePath());
ProcessBuilder pb = new ProcessBuilder(args)
.redirectErrorStream(true)
.redirectInput(ProcessBuilder.Redirect.INHERIT)
.directory(getWorkingDir());
if (getLogger().isDebugEnabled()) {
getLogger().debug("Launching JVM with command line: {}", pb.command().stream().collect(joining(" ")));
getLogger().debug("Launching JVM with command line: {}", String.join(" ", args));
}
Process p = pb.start();
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
p.destroy();
}
}, "Development Mode Shutdown Hook"));
try {
ExecutorService es = Executors.newSingleThreadExecutor();
es.submit(() -> copyOutputToConsole(p.getInputStream()));
es.shutdown();
p.waitFor();
} catch (Exception e) {
p.destroy();
throw e;
}

project.exec(action -> {
action.commandLine(args).workingDir(getWorkingDir());
action.setStandardInput(System.in)
.setErrorOutput(System.out)
.setStandardOutput(System.out);
});
} catch (Exception e) {
throw new GradleException("Failed to run", e);
}
Expand All @@ -418,18 +395,6 @@ private String getSourceEncoding() {
return null;
}

private void copyOutputToConsole(InputStream is) {
try (InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr)) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
throw new GradleException("Failed to copy output to console", e);
}
}

private void addGradlePluginDeps(StringBuilder classPathManifest, DevModeContext context) {
Configuration conf = getProject().getBuildscript().getConfigurations().getByName("classpath");
ResolvedDependency quarkusDep = conf.getResolvedConfiguration().getFirstLevelModuleDependencies().stream()
Expand Down
10 changes: 5 additions & 5 deletions docs/src/main/asciidoc/lifecycle.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ shutdown to be initiated. Let's provide our own main method:
----
package om.acme;
import io.quarkus.runtime.annotations.DefaultMain;
import io.quarkus.runtime.annotations.QuarkusMain;
import io.quarkus.runtime.Quarkus;
@DefaultMain <1>
@QuarkusMain <1>
public class Main {
public static void main(String ... args) {
Expand All @@ -83,7 +83,7 @@ This main class will bootstrap Quarkus and run it until it stops. This is no dif
generated main class, but has the advantage that you can just launch it directly from the IDE without needing
to run a Maven or Gradle command.

WARNING: It is not recommenced to do any business logic in this main method, as Quarkus has not been setup yet,
WARNING: It is not recommenced to do any business logic in this main method, as Quarkus has not been set up yet,
and Quarkus may run in a different ClassLoader. If you want to perform logic on startup use an `io.quarkus.runtime.QuarkusApplication`
as described below.

Expand All @@ -102,9 +102,9 @@ package com.acme;
import io.quarkus.runtime.Quarkus;
import io.quarkus.runtime.QuarkusApplication;
import io.quarkus.runtime.annotations.DefaultMain;
import io.quarkus.runtime.annotations.QuarkusMain;
@DefaultMain
@QuarkusMain
public class Main {
public static void main(String... args) {
Quarkus.run(MyApp.class, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public QuarkusProdModeTest setForcedDependencies(List<AppArtifact> forcedDepende
}

/**
* If this is true then the quarkus application is expected to exit immediately (i.e. is a command mode app)
* If this is true then the Quarkus application is expected to exit immediately (i.e. is a command mode app)
*/
public QuarkusProdModeTest setExpectExit(boolean expectExit) {
this.expectExit = expectExit;
Expand Down

0 comments on commit cd8c2c8

Please sign in to comment.