Skip to content

Commit

Permalink
Switch to Gradles approach for serializing System properties (which e…
Browse files Browse the repository at this point in the history
…xpects the user to escape quotes themselves). Escape VM arguments passed to Eclipse. (#59)
  • Loading branch information
shartte authored Nov 28, 2023
1 parent 1f578ed commit dcbaf5b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* A simple manager which configures runs based on the IDE it is attached to.
Expand Down Expand Up @@ -123,10 +124,6 @@ public void idea(Project project, IdeaModel idea, ProjectSettings ideaExtension)

}

private static String quoteAndJoin(List<String> args) {
return args.stream().map(arg -> "\"" + arg + "\"").collect(Collectors.joining(" "));
}

@Override
public void eclipse(Project project, EclipseModel eclipse) {
ProjectUtils.afterEvaluate(project, () -> {
Expand All @@ -147,11 +144,8 @@ public void eclipse(Project project, EclipseModel eclipse) {
final JavaApplicationLaunchConfig debugRun =
JavaApplicationLaunchConfig.builder(eclipse.getProject().getName())
.workingDirectory(runImpl.getWorkingDirectory().get().getAsFile().getAbsolutePath())
.vmArgs(runImpl.realiseJvmArguments().toArray(new String[0]))
.args(runImpl.getProgramArguments().get()
.stream()
.map(arg -> "\"" + arg + "\"")
.toArray(String[]::new))
.vmArgs(quoteStream(runImpl.realiseJvmArguments()).toArray(String[]::new))
.args(quoteStream(runImpl.getProgramArguments().get()).toArray(String[]::new))
.envVar(adaptEnvironment(runImpl, RunsUtil::buildRunWithEclipseModClasses))
.useArgumentsFile()
.build(runImpl.getMainClass().get());
Expand Down Expand Up @@ -179,6 +173,25 @@ public void eclipse(Project project, EclipseModel eclipse) {
});
}

private static String quoteAndJoin(List<String> args) {
return quoteStream(args).collect(Collectors.joining(" "));
}

private static Stream<String> quoteStream(List<String> args) {
return args.stream().map(RunsImportAction::quote);
}

/**
* This expects users to escape quotes in their system arguments on their own, which matches
* Gradles own behavior when used in JavaExec.
*/
private static String quote(String arg) {
if (!arg.contains(" ")) {
return arg;
}
return "\"" + arg + "\"";
}

private TaskProvider<?> createIdeBeforeRunTask(Project project, String name, Run run, RunImpl runImpl) {
final TaskProvider<?> ideBeforeRunTask = project.getTasks().register(CommonRuntimeUtils.buildTaskName("ideBeforeRun", name), task -> {
for (SourceSet sourceSet : run.getModSources().get()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,14 @@ public void configureInternally(final @NotNull RunType spec) {
public List<String> realiseJvmArguments() {
final List<String> args = new ArrayList<>(getJvmArguments().get());

getSystemProperties().get().forEach((key, value) -> {
args.add(String.format("-D%s=\"%s\"", key, value));
});
// This mirrors the logic found in Gradle itself, which also does not quote key nor value
for (Map.Entry<String, String> entry : getSystemProperties().get().entrySet()) {
if (entry.getValue() != null && !entry.getValue().isEmpty()) {
args.add("-D" + entry.getKey() + "=" + entry.getValue());
} else {
args.add("-D" + entry.getKey());
}
}

return args;
}
Expand Down

0 comments on commit dcbaf5b

Please sign in to comment.