Skip to content

Commit

Permalink
Merge pull request #18122 from stuartwdouglas/gradle-dev
Browse files Browse the repository at this point in the history
Improve the Gradle dev experience from the CLI
  • Loading branch information
gsmet authored Jun 25, 2021
2 parents afa3acd + 370076a commit a58d520
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 21 deletions.
16 changes: 12 additions & 4 deletions devtools/cli/src/main/java/io/quarkus/cli/Dev.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.Callable;
import java.util.function.Supplier;

import io.quarkus.cli.build.BaseBuildCommand;
import io.quarkus.cli.build.BuildSystemRunner;
Expand Down Expand Up @@ -37,15 +38,22 @@ public Integer call() {
output.throwIfUnmatchedArguments(spec.commandLine());

BuildSystemRunner runner = getRunner();
BuildSystemRunner.BuildCommandArgs commandArgs = runner.prepareDevMode(devOptions, propertiesOptions, debugOptions,
List<Supplier<BuildSystemRunner.BuildCommandArgs>> commandArgs = runner.prepareDevMode(devOptions,
propertiesOptions, debugOptions,
params);

if (devOptions.isDryRun()) {
dryRunDev(spec.commandLine().getHelp(), runner.getBuildTool(), commandArgs);
dryRunDev(spec.commandLine().getHelp(), runner.getBuildTool(), commandArgs.iterator().next().get());
return CommandLine.ExitCode.OK;
}

return runner.run(commandArgs);
Integer ret = 1;
for (Supplier<BuildSystemRunner.BuildCommandArgs> i : commandArgs) {
ret = runner.run(i.get());
if (ret != 0) {
return ret;
}
}
return ret;
} catch (Exception e) {
return output.handleCommandException(e,
"Unable to launch project in dev mode: " + e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

import io.quarkus.cli.common.BuildOptions;
import io.quarkus.cli.common.CategoryListFormatOptions;
Expand Down Expand Up @@ -97,7 +98,8 @@ Integer listExtensions(RunModeOption runMode, ListFormatOptions format, boolean
BuildCommandArgs prepareBuild(BuildOptions buildOptions, PropertiesOptions propertiesOptions, RunModeOption runMode,
List<String> params);

BuildCommandArgs prepareDevMode(DevOptions devOptions, PropertiesOptions propertiesOptions, DebugOptions debugOptions,
List<Supplier<BuildCommandArgs>> prepareDevMode(DevOptions devOptions, PropertiesOptions propertiesOptions,
DebugOptions debugOptions,
List<String> params);

Path getProjectRoot();
Expand Down
35 changes: 33 additions & 2 deletions devtools/cli/src/main/java/io/quarkus/cli/build/GradleRunner.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package io.quarkus.cli.build;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import io.quarkus.cli.common.BuildOptions;
import io.quarkus.cli.common.CategoryListFormatOptions;
Expand Down Expand Up @@ -142,7 +147,7 @@ public BuildCommandArgs prepareBuild(BuildOptions buildOptions, PropertiesOption
}

@Override
public BuildCommandArgs prepareDevMode(DevOptions devOptions, PropertiesOptions propertiesOptions,
public List<Supplier<BuildCommandArgs>> prepareDevMode(DevOptions devOptions, PropertiesOptions propertiesOptions,
DebugOptions debugOptions, List<String> params) {
ArrayDeque<String> args = new ArrayDeque<>();
setGradleProperties(args, false);
Expand All @@ -162,7 +167,33 @@ public BuildCommandArgs prepareDevMode(DevOptions devOptions, PropertiesOptions
args.addAll(flattenMappedProperties(propertiesOptions.properties));
// Add any other unmatched arguments
args.addAll(params);
return prependExecutable(args);
try {
Path outputFile = Files.createTempFile("quarkus-dev", ".txt");
args.add("-Dio.quarkus.devmode-args=" + outputFile.toAbsolutePath().toString());
BuildCommandArgs buildCommandArgs = prependExecutable(args);
return Arrays.asList(new Supplier<BuildCommandArgs>() {
@Override
public BuildCommandArgs get() {
return buildCommandArgs;
}
}, new Supplier<BuildCommandArgs>() {
@Override
public BuildCommandArgs get() {
try {
List<String> lines = Files.readAllLines(outputFile).stream().filter(s -> !s.isBlank())
.collect(Collectors.toList());
BuildCommandArgs cmd = new BuildCommandArgs();
cmd.arguments = lines.toArray(new String[0]);
cmd.targetDirectory = buildCommandArgs.targetDirectory;
return cmd;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}

void setSkipTests(ArrayDeque<String> args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.ArrayDeque;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;

import io.quarkus.cli.common.BuildOptions;
import io.quarkus.cli.common.CategoryListFormatOptions;
Expand Down Expand Up @@ -86,7 +87,7 @@ public BuildCommandArgs prepareBuild(BuildOptions buildOptions, PropertiesOption
}

@Override
public BuildCommandArgs prepareDevMode(DevOptions devOptions, PropertiesOptions propertiesOptions,
public List<Supplier<BuildCommandArgs>> prepareDevMode(DevOptions devOptions, PropertiesOptions propertiesOptions,
DebugOptions debugOptions, List<String> params) {
throw new UnsupportedOperationException("Not there yet. ;)");
}
Expand Down
12 changes: 10 additions & 2 deletions devtools/cli/src/main/java/io/quarkus/cli/build/MavenRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;

import io.quarkus.cli.Version;
import io.quarkus.cli.common.BuildOptions;
Expand Down Expand Up @@ -149,7 +151,7 @@ public BuildCommandArgs prepareBuild(BuildOptions buildOptions, PropertiesOption
}

@Override
public BuildCommandArgs prepareDevMode(DevOptions devOptions, PropertiesOptions propertiesOptions,
public List<Supplier<BuildCommandArgs>> prepareDevMode(DevOptions devOptions, PropertiesOptions propertiesOptions,
DebugOptions debugOptions, List<String> params) {
ArrayDeque<String> args = new ArrayDeque<>();
setMavenProperties(args, false);
Expand All @@ -168,7 +170,13 @@ public BuildCommandArgs prepareDevMode(DevOptions devOptions, PropertiesOptions
args.addAll(flattenMappedProperties(propertiesOptions.properties));
// Add any other unmatched arguments
args.addAll(params);
return prependExecutable(args);
BuildCommandArgs buildCommandArgs = prependExecutable(args);
return Collections.singletonList(new Supplier<BuildCommandArgs>() {
@Override
public BuildCommandArgs get() {
return buildCommandArgs;
}
});
}

void setSkipTests(ArrayDeque<String> args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.gradle.tasks;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -55,6 +56,7 @@

public class QuarkusDev extends QuarkusTask {

public static final String IO_QUARKUS_DEVMODE_ARGS = "io.quarkus.devmode-args";
private Set<File> filesIncludedInClasspath = new HashSet<>();

private File buildDir;
Expand Down Expand Up @@ -186,12 +188,22 @@ public void startDev() {

try {
QuarkusDevModeLauncher runner = newLauncher();
getProject().exec(action -> {
action.commandLine(runner.args()).workingDir(getWorkingDir());
action.setStandardInput(System.in)
.setErrorOutput(System.out)
.setStandardOutput(System.out);
});
String outputFile = System.getProperty(IO_QUARKUS_DEVMODE_ARGS);
if (outputFile == null) {
getProject().exec(action -> {
action.commandLine(runner.args()).workingDir(getWorkingDir());
action.setStandardInput(System.in)
.setErrorOutput(System.out)
.setStandardOutput(System.out);
});
} else {
try (BufferedWriter is = Files.newBufferedWriter(Paths.get(outputFile))) {
for (String i : runner.args()) {
is.write(i);
is.newLine();
}
}
}

} catch (Exception e) {
throw new GradleException("Failed to run", e);
Expand All @@ -218,9 +230,11 @@ private QuarkusDevModeLauncher newLauncher() throws Exception {
.outputDir(getBuildDir())
.debug(System.getProperty("debug"))
.debugHost(System.getProperty("debugHost", "localhost"))
.suspend(System.getProperty("suspend"))
.jvmArgs("-Dquarkus.test.basic-console=true")
.jvmArgs("-Dio.quarkus.launched-from-ide=true");
.suspend(System.getProperty("suspend"));
if (System.getProperty(IO_QUARKUS_DEVMODE_ARGS) == null) {
builder.jvmArgs("-Dquarkus.test.basic-console=true")
.jvmArgs("-Dio.quarkus.launched-from-ide=true");
}

if (getJvmArgs() != null) {
builder.jvmArgs(getJvmArgs());
Expand Down
14 changes: 12 additions & 2 deletions docs/src/main/asciidoc/gradle-tooling.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,24 @@ You can install all extensions which match a globbing pattern:
[[dev-mode]]
== Development mode

Quarkus comes with a built-in development mode.
Quarkus comes with a built-in development mode. The best way to run this is via the link:cli-tooling.adoc[Quarkus CLI]
Run your application with:

[source,bash]
----
./gradlew quarkusDev
quarkus dev
----

If you don't want to install the Quarkus CLI you can run development mode directly from gradle:

[source,bash]
----
./gradlew --console=plain quarkusDev
----

Note that if you run it this way the continuous testing experience will not be as nice, as gradle runs as a daemon
Quarkus can't draw the 'pretty' test output so falls back to just logging the output.

You can then update the application sources, resources and configurations.
The changes are automatically reflected in your running application.
This is great to do development spanning UI and database as you see changes reflected immediately.
Expand Down

0 comments on commit a58d520

Please sign in to comment.