Skip to content

Commit

Permalink
[FIx]: Fix AT issues and mute execute output further (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
marchermans committed Jun 24, 2024
1 parent 0b8939f commit 41bf68d
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.neoforged.gradle.dsl.common.util.GameArtifact;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.tasks.TaskProvider;
Expand Down Expand Up @@ -55,6 +56,9 @@ public abstract class CommonRuntimeDefinition<S extends CommonRuntimeSpecificati

@NotNull
private final Consumer<TaskProvider<? extends Runtime>> associatedTaskConsumer;

@NotNull
private final ConfigurableFileCollection allDependencies;

@NotNull
private final VersionJson versionJson;
Expand All @@ -76,6 +80,9 @@ protected CommonRuntimeDefinition(
this.minecraftDependenciesConfiguration = minecraftDependenciesConfiguration;
this.associatedTaskConsumer = associatedTaskConsumer;
this.versionJson = versionJson;

this.allDependencies = specification.getProject().files();
this.allDependencies.from(getMinecraftDependenciesConfiguration());
}

@Override
Expand Down Expand Up @@ -152,6 +159,12 @@ public VersionJson getVersionJson() {
return versionJson;
}

@NotNull
@Override
public final ConfigurableFileCollection getAllDependencies() {
return allDependencies;
}

public void configureRun(RunImpl run) {
final Map<String, String> runtimeInterpolationData = buildRunInterpolationData(run);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public DefaultExecute() {

getRuntimeProgramArguments().convention(getProgramArguments());
getMultiRuntimeArguments().convention(getMultiArguments().AsMap());

getLogLevel().convention(LogLevel.ERROR);
}

@ServiceReference(CommonProjectPlugin.EXECUTE_SERVICE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.tasks.CacheableTask;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.PathSensitive;
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.SkipWhenEmpty;
import org.gradle.api.tasks.*;

import java.io.File;
import java.util.List;
Expand All @@ -38,6 +33,15 @@ public SourceAccessTransformer() {

args.add("--libraries-list=" + getLibraries().get().getAsFile().getAbsolutePath());

final StringBuilder builder = new StringBuilder();
getClasspath().forEach(f -> {
if (!builder.isEmpty()) {
builder.append(File.pathSeparator);
}
builder.append(f.getAbsolutePath());
});
args.add("--classpath=" + builder.toString());

args.add(inputFile.getAsFile().getAbsolutePath());
args.add(outputFile.getAbsolutePath());

Expand All @@ -48,6 +52,7 @@ public SourceAccessTransformer() {

getJavaVersion().convention(getProject().getExtensions().getByType(JavaPluginExtension.class).getToolchain().getLanguageVersion());
getTransformers().finalizeValueOnRead();
getLogLevel().set(LogLevel.DISABLED);
}

@InputFile
Expand All @@ -58,6 +63,11 @@ public SourceAccessTransformer() {
@PathSensitive(PathSensitivity.NONE)
public abstract RegularFileProperty getLibraries();

@InputFiles
@Optional
@PathSensitive(PathSensitivity.NONE)
public abstract ConfigurableFileCollection getClasspath();

@InputFiles
@SkipWhenEmpty
@PathSensitive(PathSensitivity.NONE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.neoforged.gradle.dsl.common.tasks.WithOutput;
import net.neoforged.gradle.dsl.common.util.CommonRuntimeUtils;
import net.neoforged.gradle.util.StringCapitalizationUtils;
import org.gradle.api.file.FileCollection;
import org.gradle.api.file.FileTree;
import org.gradle.api.tasks.TaskProvider;

Expand All @@ -21,7 +22,7 @@ private CommonRuntimeTaskUtils() {
throw new IllegalStateException("Can not instantiate an instance of: CommonRuntimeTaskUtils. This is a utility class");
}

public static TaskProvider<? extends SourceAccessTransformer> createSourceAccessTransformer(Definition<?> definition, String namePreFix, File workspaceDirectory, Consumer<TaskProvider<? extends Runtime>> dependentTaskConfigurationHandler, FileTree files, Collection<String> data, TaskProvider<? extends WithOutput> listLibs) {
public static TaskProvider<? extends SourceAccessTransformer> createSourceAccessTransformer(Definition<?> definition, String namePreFix, File workspaceDirectory, Consumer<TaskProvider<? extends Runtime>> dependentTaskConfigurationHandler, FileTree files, Collection<String> data, TaskProvider<? extends WithOutput> listLibs, FileCollection additionalClasspathElements) {
final TaskProvider<AccessTransformerFileGenerator> generator;
if (!data.isEmpty()) {
generator = definition.getSpecification().getProject().getTasks().register(CommonRuntimeUtils.buildTaskName(definition.getSpecification(), namePreFix + "AccessTransformerGenerator"), AccessTransformerFileGenerator.class, task -> {
Expand All @@ -41,6 +42,7 @@ public static TaskProvider<? extends SourceAccessTransformer> createSourceAccess
}
task.dependsOn(listLibs);
task.getLibraries().set(listLibs.flatMap(WithOutput::getOutput));
task.getClasspath().from(additionalClasspathElements);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import net.neoforged.gradle.dsl.common.tasks.WithOutput
import net.neoforged.gradle.dsl.common.util.GameArtifact
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.Dependency
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.TaskProvider
import org.jetbrains.annotations.NotNull

Expand Down Expand Up @@ -87,4 +89,13 @@ interface Definition<S extends Specification> {
*/
@NotNull
abstract TaskProvider<? extends WithOutput> getListLibrariesTaskProvider();

/**
* Returns all the files which should be considered dependencies of the runtime.
* This includes the runtime's own dependencies, as well as the dependencies of the minecraft dependency.
*
* @return The dependencies of the runtime.
*/
@NotNull
ConfigurableFileCollection getAllDependencies()
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import net.minecraftforge.gdi.annotations.DefaultMethods
import net.neoforged.gradle.dsl.common.tasks.specifications.ExecuteSpecification
import net.neoforged.gradle.dsl.common.util.RegexUtils
import org.gradle.api.file.FileTree
import org.gradle.api.logging.Logger
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.TaskAction
import org.gradle.process.JavaExecSpec
Expand All @@ -22,7 +23,6 @@ import java.util.stream.Collectors
@DefaultMethods
interface Execute extends WithWorkspace, WithOutput, WithJavaVersion, ExecuteSpecification {


default List<String> interpolateVariableSubstitution(String value, String previous) {
final Map<String, Provider<String>> runtimeArguments = getRuntimeArguments().get()
final Map<String, Provider<List<String>>> multiRuntimeArguments = getMultiRuntimeArguments().get()
Expand Down Expand Up @@ -87,7 +87,7 @@ interface Execute extends WithWorkspace, WithOutput, WithJavaVersion, ExecuteSpe
final List<String> substituted = ((Execute) this).interpolateVariableSubstitution(value, previous)

if (substituted.size() != 1) {
interpolated.removeAt(interpolated.size() - 1);
interpolated.removeAt(interpolated.size() - 1)
}

interpolated.addAll(substituted)
Expand All @@ -110,10 +110,12 @@ interface Execute extends WithWorkspace, WithOutput, WithJavaVersion, ExecuteSpe

final Execute me = this

try (BufferedOutputStream log_out = new BufferedOutputStream(new FileOutputStream(consoleLogFile))) {
try (LoggerOutputStream error_out = new LoggerOutputStream(me.getLogger(), me.getLogLevel().get())
BufferedOutputStream log_out = new BufferedOutputStream(new FileOutputStream(consoleLogFile))
LogLevelAwareOutputStream standard_out = new LogLevelAwareOutputStream(log_out, ExecuteSpecification.LogLevel.WARN, getLogLevel().get()) ){
getExecuteOperation().javaexec({ JavaExecSpec java ->
PrintWriter writer = new PrintWriter(log_out)
Function<String, CharSequence> quote = s -> (CharSequence)('"' + s + '"')
Function<String, CharSequence> quote = s -> (CharSequence) ('"' + s + '"')
writer.println("JVM Args: " + jvmArgs.get().stream().map(quote).collect(Collectors.joining(", ")))
writer.println("Run Args: " + programArgs.get().stream().map(quote).collect(Collectors.joining(", ")))
writer.println("JVM: " + executable.get())
Expand All @@ -130,11 +132,71 @@ interface Execute extends WithWorkspace, WithOutput, WithJavaVersion, ExecuteSpe
java.setClasspath(me.getObjectFactory().fileCollection().from(me.getExecutingJar().get()))
java.setWorkingDir(me.getOutputDirectory().get())
java.getMainClass().set(mainClass)
java.setStandardOutput(log_out)
java.setStandardOutput(standard_out)
java.setErrorOutput(error_out)
}).rethrowFailure().assertNormalExitValue()

return outputFile;
return outputFile
}
}

private static final class LogLevelAwareOutputStream extends OutputStream {

private final OutputStream target;
private final boolean shouldLog;

public LogLevelAwareOutputStream(OutputStream target, ExecuteSpecification.LogLevel minLevel, ExecuteSpecification.LogLevel currentLevel) {
this.target = target;
this.shouldLog = minLevel.ordinal() > currentLevel.ordinal(); //Inverse selection logic, if current is error and min is warn then it should not log.
}

@Override
void write(int b) throws IOException {
if (shouldLog) {
target.write(b);
}
}
}

private static final class LoggerOutputStream
extends OutputStream {
private final ByteArrayOutputStream baos = new ByteArrayOutputStream(1000)
private final Logger logger
private final ExecuteSpecification.LogLevel level

LoggerOutputStream(Logger logger, ExecuteSpecification.LogLevel level) {
this.logger = logger
this.level = level
}

@Override
void write(int b) {
if (level == ExecuteSpecification.LogLevel.DISABLED) return

if (((char) b) == '\n') {
String line = baos.toString()
baos.reset()

switch (level) {
case ExecuteSpecification.LogLevel.TRACE:
logger.trace(line)
break
case ExecuteSpecification.LogLevel.DEBUG:
logger.debug(line)
break
case ExecuteSpecification.LogLevel.ERROR:
logger.error(line)
break
case ExecuteSpecification.LogLevel.INFO:
logger.info(line)
break
case ExecuteSpecification.LogLevel.WARN:
logger.warn(line)
break
}
} else {
baos.write(b)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import org.gradle.api.tasks.PathSensitivity

interface ExecuteSpecification extends ProjectSpecification, OutputSpecification, JavaVersionSpecification {

enum LogLevel {
TRACE, DEBUG, INFO, WARN, ERROR, DISABLED
}


/**
* Defines the jvm arguments in a list which are passed to the java executable.
*
Expand Down Expand Up @@ -114,4 +119,8 @@ interface ExecuteSpecification extends ProjectSpecification, OutputSpecification
*/
@Internal
MapProperty<String, Provider<List<String>>> getMultiRuntimeArguments();

@DSLProperty
@Input
Property<LogLevel> getLogLevel();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Constants {
public static final String DEFAULT_PARCHMENT_GROUP = "org.parchmentmc.data"
public static final String DEFAULT_PARCHMENT_ARTIFACT_PREFIX = "parchment-"
public static final String DEFAULT_PARCHMENT_MAVEN_URL = "https://maven.parchmentmc.org/"
public static final String JST_TOOL_ARTIFACT = "net.neoforged.jst:jst-cli-bundle:1.0.38"
public static final String JST_TOOL_ARTIFACT = "net.neoforged.jst:jst-cli-bundle:1.0.39"
public static final String DEVLOGIN_TOOL_ARTIFACT = "net.covers1624:DevLogin:0.1.0.4"
public static final String DEVLOGIN_MAIN_CLASS = "net.covers1624.devlogin.DevLogin"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package net.neoforged.gradle.dsl.userdev.runtime.specification;

import groovy.transform.CompileStatic
import net.neoforged.gradle.dsl.common.runtime.spec.Specification;
import org.gradle.api.provider.Provider;
import net.neoforged.gradle.dsl.common.runtime.spec.Specification
import net.neoforged.gradle.dsl.neoform.runtime.specification.NeoFormSpecification;
import org.gradle.api.provider.Provider
import org.gradle.api.specs.Spec;
import org.jetbrains.annotations.NotNull;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public class NeoFormRuntimeDefinition extends CommonRuntimeDefinition<NeoFormRun

private final TaskProvider<DownloadAssets> assetsTaskProvider;
private final TaskProvider<ExtractNatives> nativesTaskProvider;
private TaskProvider<? extends WithOutput> debuggingMappingsTaskProvider;

public NeoFormRuntimeDefinition(@NotNull NeoFormRuntimeSpecification specification,
@NotNull LinkedHashMap<String, TaskProvider<? extends WithOutput>> taskOutputs,
Expand All @@ -47,6 +46,8 @@ public NeoFormRuntimeDefinition(@NotNull NeoFormRuntimeSpecification specificati
this.neoform = neoform;
this.assetsTaskProvider = assetsTaskProvider;
this.nativesTaskProvider = nativesTaskProvider;

this.getAllDependencies().from(getSpecification().getAdditionalRecompileDependencies());
}

@Override
Expand All @@ -58,11 +59,9 @@ public NeoFormConfigConfigurationSpecV2 getNeoFormConfig() {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof NeoFormRuntimeDefinition)) return false;
if (!(o instanceof NeoFormRuntimeDefinition that)) return false;
if (!super.equals(o)) return false;

NeoFormRuntimeDefinition that = (NeoFormRuntimeDefinition) o;

return neoform.equals(that.neoform);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public Provider<File> getNeoFormArchive() {
}

@Override
public FileCollection getAdditionalRecompileDependencies() {
public @NotNull FileCollection getAdditionalRecompileDependencies() {
return additionalRecompileDependencies;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.neoforged.gradle.dsl.common.runtime.tasks.tree.TaskTreeAdapter;
import net.neoforged.gradle.dsl.common.tasks.WithOutput;
import org.gradle.api.Project;
import org.gradle.api.file.FileCollection;
import org.gradle.api.tasks.TaskProvider;

public class NeoFormAccessTransformerUtils {
Expand All @@ -24,7 +25,7 @@ public static TaskTreeAdapter createAccessTransformerAdapter(final Project proje
return null;
}

final TaskProvider<? extends SourceAccessTransformer> accessTransformerTask = CommonRuntimeTaskUtils.createSourceAccessTransformer(definition, "User", runtimeWorkspace, dependentTaskConfigurationHandler, accessTransformerFiles.getFiles().getAsFileTree(), accessTransformerFiles.getEntries().get(), definition.getListLibrariesTaskProvider());
final TaskProvider<? extends SourceAccessTransformer> accessTransformerTask = CommonRuntimeTaskUtils.createSourceAccessTransformer(definition, "User", runtimeWorkspace, dependentTaskConfigurationHandler, accessTransformerFiles.getFiles().getAsFileTree(), accessTransformerFiles.getEntries().get(), definition.getListLibrariesTaskProvider(), definition.getAllDependencies());
accessTransformerTask.configure(task -> task.getInputFile().set(previousTasksOutput.flatMap(WithOutput::getOutput)));
accessTransformerTask.configure(task -> task.dependsOn(previousTasksOutput));
return accessTransformerTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public UserDevRuntimeDefinition(@NotNull UserDevRuntimeSpecification specificati
this.additionalUserDevDependencies.getDependencies().add(
clientExtraJar
);

this.getAllDependencies().from(neoformRuntimeDefinition.getAllDependencies());
this.getAllDependencies().from(getAdditionalUserDevDependencies());
this.getAllDependencies().from(getUserdevConfiguration());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private TaskTreeAdapter createAccessTransformerAdapter(final String accessTransf
userDev.matching(filter -> filter.include(accessTransformerDirectory + "/**"));

return (definition, previousTasksOutput, runtimeWorkspace, gameArtifacts, mappingVersionData, dependentTaskConfigurationHandler) -> {
final TaskProvider<? extends SourceAccessTransformer> accessTransformerTask = CommonRuntimeTaskUtils.createSourceAccessTransformer(definition, "Forges", runtimeWorkspace, dependentTaskConfigurationHandler, accessTransformerFiles, Collections.emptyList(), definition.getListLibrariesTaskProvider());
final TaskProvider<? extends SourceAccessTransformer> accessTransformerTask = CommonRuntimeTaskUtils.createSourceAccessTransformer(definition, "Forges", runtimeWorkspace, dependentTaskConfigurationHandler, accessTransformerFiles, Collections.emptyList(), definition.getListLibrariesTaskProvider(), definition.getAllDependencies());
accessTransformerTask.configure(task -> task.getInputFile().set(previousTasksOutput.flatMap(WithOutput::getOutput)));
accessTransformerTask.configure(task -> task.dependsOn(previousTasksOutput));
return accessTransformerTask;
Expand Down

0 comments on commit 41bf68d

Please sign in to comment.