Skip to content

Commit cc8ce77

Browse files
LexManosJonathing
authored andcommitted
Log ToolExec tasks
1 parent 3719336 commit cc8ce77

File tree

2 files changed

+69
-30
lines changed

2 files changed

+69
-30
lines changed

gradleutils-shared/src/main/java/net/minecraftforge/gradleutils/shared/EnhancedTask.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,22 @@ default DirectoryProperty workingProjectDirectory() {
7373
///
7474
/// @param ext The extension to use for the file
7575
/// @return A provider for the file
76-
default Provider<RegularFile> getDefaultOutputFile(String ext) {
77-
return this.localCaches().file(String.format("%s/output.%s", this.getName(), ext)).map(this.getPlugin().getProblemsInternal().ensureFileLocation());
76+
default @Internal Provider<RegularFile> getDefaultOutputFile(String ext) {
77+
return this.getOutputFile(String.format("output.%s", ext));
78+
}
79+
80+
/// The default output log file to use for this task.
81+
///
82+
/// @return A provider for the file
83+
default @Internal Provider<RegularFile> getDefaultLogFile() {
84+
return this.getOutputFile("log.txt");
85+
}
86+
87+
/// A file with the specified name in the default output directory.
88+
///
89+
/// @param fileName The name of the output file
90+
/// @return A provider for the file
91+
default @Internal Provider<RegularFile> getOutputFile(String fileName) {
92+
return this.localCaches().file(String.format("%s/%s", this.getName(), fileName)).map(this.getPlugin().getProblemsInternal().ensureFileLocation());
7893
}
7994
}

gradleutils-shared/src/main/java/net/minecraftforge/gradleutils/shared/ToolExecBase.java

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
88
import org.gradle.api.DefaultTask;
99
import org.gradle.api.Transformer;
10-
import org.gradle.api.file.ConfigurableFileCollection;
11-
import org.gradle.api.file.DirectoryProperty;
12-
import org.gradle.api.file.FileSystemLocation;
13-
import org.gradle.api.file.FileSystemLocationProperty;
10+
import org.gradle.api.file.*;
1411
import org.gradle.api.logging.LogLevel;
1512
import org.gradle.api.logging.LoggingManager;
1613
import org.gradle.api.model.ObjectFactory;
@@ -19,15 +16,8 @@
1916
import org.gradle.api.provider.Property;
2017
import org.gradle.api.provider.Provider;
2118
import org.gradle.api.provider.ProviderFactory;
22-
import org.gradle.api.tasks.Classpath;
19+
import org.gradle.api.tasks.*;
2320
import org.gradle.api.tasks.Console;
24-
import org.gradle.api.tasks.Input;
25-
import org.gradle.api.tasks.InputFiles;
26-
import org.gradle.api.tasks.Internal;
27-
import org.gradle.api.tasks.JavaExec;
28-
import org.gradle.api.tasks.Nested;
29-
import org.gradle.api.tasks.Optional;
30-
import org.gradle.api.tasks.TaskAction;
3121
import org.gradle.jvm.toolchain.JavaLanguageVersion;
3222
import org.gradle.jvm.toolchain.JavaLauncher;
3323
import org.gradle.jvm.toolchain.JavaToolchainService;
@@ -39,13 +29,14 @@
3929
import org.jetbrains.annotations.UnknownNullability;
4030

4131
import javax.inject.Inject;
42-
import java.io.File;
32+
import java.io.*;
4333
import java.util.ArrayList;
4434
import java.util.HashMap;
4535
import java.util.List;
4636
import java.util.Locale;
4737
import java.util.Map;
4838
import java.util.concurrent.Callable;
39+
import java.util.stream.Collectors;
4940

5041
/// This tool execution task is a template on top of [JavaExec] to make executing [tools][Tool] much easier and more
5142
/// consistent between plugins.
@@ -79,6 +70,8 @@ public abstract class ToolExecBase<P extends EnhancedProblems> extends DefaultTa
7970
protected abstract @Nested Property<JavaLauncher> getToolchainLauncher();
8071

8172
public abstract @Input @Optional Property<Boolean> getPreferToolchainJvm();
73+
74+
public abstract @Internal DirectoryProperty getWorkingDir();
8275
//endregion
8376

8477
//region Logging
@@ -90,6 +83,8 @@ public abstract class ToolExecBase<P extends EnhancedProblems> extends DefaultTa
9083
protected abstract @Console Property<LogLevel> getStandardOutputLogLevel();
9184

9285
protected abstract @Console Property<LogLevel> getStandardErrorLogLevel();
86+
87+
protected abstract @OutputFile RegularFileProperty getLogFile();
9388
//endregion
9489

9590
protected abstract @Inject ObjectFactory getObjects();
@@ -127,6 +122,9 @@ protected ToolExecBase(Tool tool) {
127122

128123
this.getStandardOutputLogLevel().convention(LogLevel.LIFECYCLE);
129124
this.getStandardErrorLogLevel().convention(LogLevel.ERROR);
125+
126+
this.getWorkingDir().convention(this.getDefaultOutputDirectory());
127+
this.getLogFile().convention(this.getDefaultLogFile());
130128
}
131129

132130
/// The enhanced problems instance to use for this task.
@@ -153,7 +151,7 @@ protected void addArguments() { }
153151
/// @implNote Not invoking this method from an overriding method *will* result in the tool never being executed and
154152
/// [#addArguments()] never being run.
155153
@TaskAction
156-
protected ExecResult exec() {
154+
protected ExecResult exec() throws IOException {
157155
this.args = new ArrayList<>();
158156
this.jvmArgs = new ArrayList<>();
159157
this.environment = new HashMap<>();
@@ -180,20 +178,46 @@ protected ExecResult exec() {
180178
javaLauncher = getJavaLauncher().get();
181179
}
182180

183-
return this.getExecOperations().javaexec(spec -> {
184-
spec.setIgnoreExitValue(true);
185-
186-
spec.setClasspath(this.getClasspath());
187-
spec.getMainClass().set(this.getMainClass());
188-
spec.setExecutable(javaLauncher.getExecutablePath().getAsFile().getAbsolutePath());
189-
spec.setArgs(args);
190-
spec.setJvmArgs(jvmArgs);
191-
spec.setEnvironment(this.environment);
192-
spec.setSystemProperties(this.systemProperties);
193-
194-
spec.setStandardOutput(SharedUtil.toLog(this.getLogger(), stdOutLevel));
195-
spec.setErrorOutput(SharedUtil.toLog(this.getLogger(), stdErrLevel));
196-
});
181+
final File workingDirectory = this.getWorkingDir().get().getAsFile();
182+
if (!workingDirectory.exists())
183+
workingDirectory.mkdirs();
184+
185+
try (PrintWriter log = new PrintWriter(new FileWriter(this.getLogFile().get().getAsFile()), true)) {
186+
return this.getExecOperations().javaexec(spec -> {
187+
spec.setIgnoreExitValue(true);
188+
189+
spec.setWorkingDir(this.getWorkingDir());
190+
spec.setClasspath(this.getClasspath());
191+
spec.getMainClass().set(this.getMainClass());
192+
spec.setExecutable(javaLauncher.getExecutablePath().getAsFile().getAbsolutePath());
193+
spec.setArgs(args);
194+
spec.setJvmArgs(jvmArgs);
195+
spec.setEnvironment(this.environment);
196+
spec.setSystemProperties(this.systemProperties);
197+
198+
spec.setStandardOutput(SharedUtil.toLog(
199+
line -> {
200+
this.getLogger().log(stdOutLevel, line);
201+
log.println(line);
202+
}
203+
));
204+
spec.setErrorOutput(SharedUtil.toLog(
205+
line -> {
206+
this.getLogger().log(stdErrLevel, line);
207+
log.println(line);
208+
}
209+
));
210+
211+
log.println("Java Launcher: " + spec.getExecutable());
212+
log.println("Working directory: " + workingDirectory.getAbsolutePath());
213+
log.println("Main class: " + this.getMainClass().get());
214+
log.println("Arguments: " + args.stream().collect(Collectors.joining(", ", "'", "'")));
215+
log.println("JVM Arguments: " + jvmArgs.stream().collect(Collectors.joining(", ", "'", "'")));
216+
log.println("Classpath:");
217+
getClasspath().forEach(f -> log.println(" - " + f.getAbsolutePath()));
218+
log.println("====================================");
219+
});
220+
}
197221
}
198222

199223
@SuppressWarnings("DataFlowIssue")

0 commit comments

Comments
 (0)