Skip to content

Commit

Permalink
Extract property prefix, use an enum for loglevel.
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed Nov 29, 2023
1 parent 004b16c commit e5ec2a1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,35 @@
package net.neoforged.gradle.common.extensions.subsystems;

import net.minecraftforge.gdi.ConfigurableDSLElement;
import net.neoforged.gradle.dsl.common.extensions.subsystems.DecompilerLogLevel;
import net.neoforged.gradle.dsl.common.extensions.subsystems.Subsystems;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.provider.ProviderFactory;

import javax.inject.Inject;
import java.util.Arrays;
import java.util.Locale;

public abstract class SubsystemsExtension implements ConfigurableDSLElement<Subsystems>, Subsystems {
private static final String PROPERTY_PREFIX = "neogradle.subsystems.";
private final Project project;

@Inject
public SubsystemsExtension(Project project) {
this.project = project;

ProviderFactory providers = project.getProviders();
getDecompiler().getMaxMemory().convention(providers.gradleProperty("neogradle.subsystems.decompiler.maxMemory"));
getDecompiler().getMaxThreads().convention(providers.gradleProperty("neogradle.subsystems.decompiler.maxThreads").map(Integer::parseUnsignedInt));
getDecompiler().getLogLevel().convention(providers.gradleProperty("neogradle.subsystems.decompiler.logLevel"));
getDecompiler().getJvmArgs().convention(providers.gradleProperty("neogradle.subsystems.decompiler.jvmArgs").map(s -> Arrays.asList(s.split("\\s+"))));
getDecompiler().getMaxMemory().convention(providers.gradleProperty(PROPERTY_PREFIX + "decompiler.maxMemory"));
getDecompiler().getMaxThreads().convention(providers.gradleProperty(PROPERTY_PREFIX + "decompiler.maxThreads").map(Integer::parseUnsignedInt));
getDecompiler().getLogLevel().convention(providers.gradleProperty(PROPERTY_PREFIX + "decompiler.logLevel").map(s -> {
try {
return DecompilerLogLevel.valueOf(s.toUpperCase(Locale.ROOT));
} catch (Exception e) {
throw new GradleException("Unknown DecompilerLogLevel: " + s + ". Available options: " + Arrays.toString(DecompilerLogLevel.values()));
}
}));
getDecompiler().getJvmArgs().convention(providers.gradleProperty(PROPERTY_PREFIX + "decompiler.jvmArgs").map(s -> Arrays.asList(s.split("\\s+"))));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ interface Decompiler extends ConfigurableDSLElement<Decompiler> {

/**
* The log-level to use for the decompiler. Supported values: info, debug, warn, error.
* Defaults to info.
* Defaults to {@link DecompilerLogLevel#INFO}.
*/
@Input
@Optional
@DSLProperty
Property<String> getLogLevel();
Property<DecompilerLogLevel> getLogLevel();

/**
* Allows additional JVM arguments to be added to the decompiler invocation.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.neoforged.gradle.dsl.common.extensions.subsystems

enum DecompilerLogLevel {
TRACE,
INFO,
WARN,
ERROR
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.neoforged.gradle.dsl.common.extensions.Minecraft;
import net.neoforged.gradle.dsl.common.extensions.MinecraftArtifactCache;
import net.neoforged.gradle.dsl.common.extensions.subsystems.Decompiler;
import net.neoforged.gradle.dsl.common.extensions.subsystems.DecompilerLogLevel;
import net.neoforged.gradle.dsl.common.extensions.subsystems.Subsystems;
import net.neoforged.gradle.dsl.common.runtime.naming.TaskBuildingContext;
import net.neoforged.gradle.dsl.common.runtime.tasks.Runtime;
Expand All @@ -38,6 +39,7 @@
import net.neoforged.gradle.neoform.util.NeoFormRuntimeUtils;
import net.neoforged.gradle.util.CopyingFileTreeVisitor;
import org.apache.commons.lang3.StringUtils;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
Expand Down Expand Up @@ -157,7 +159,7 @@ private static TaskProvider<? extends Runtime> createDecompile(NeoFormRuntimeSpe
Decompiler settings = spec.getProject().getExtensions().getByType(Subsystems.class).getDecompiler();
String maxMemory = settings.getMaxMemory().getOrElse(defaultMaxMemory);
int maxThreads = settings.getMaxThreads().getOrElse(0);
String logLevel = settings.getLogLevel().getOrElse("INFO");
String logLevel = getDecompilerLogLevelArg(settings.getLogLevel().getOrElse(DecompilerLogLevel.INFO), function.getVersion());

if (settings.getJvmArgs().isPresent()) {
jvmArgs.addAll(settings.getJvmArgs().get());
Expand All @@ -175,6 +177,21 @@ private static TaskProvider<? extends Runtime> createDecompile(NeoFormRuntimeSpe
});
}

private static String getDecompilerLogLevelArg(DecompilerLogLevel logLevel, String version) {
switch (logLevel) {
case TRACE:
return "trace";
case INFO:
return "info";
case WARN:
return "warn";
case ERROR:
return "error";
default:
throw new GradleException("LogLevel " + logLevel + " not supported by " + version);
}
}

private TaskProvider<? extends Runtime> createExecute(final NeoFormRuntimeSpecification spec, final NeoFormConfigConfigurationSpecV1.Step step, final NeoFormConfigConfigurationSpecV1.Function function) {
return spec.getProject().getTasks().register(CommonRuntimeUtils.buildTaskName(spec, step.getName()), Execute.class, task -> {
task.getExecutingJar().set(ToolUtilities.resolveTool(task.getProject(), function.getVersion()));
Expand Down

0 comments on commit e5ec2a1

Please sign in to comment.