Skip to content

Commit

Permalink
All kinds of bugfixes, small features and documentation, plus error r…
Browse files Browse the repository at this point in the history
…eporting.
  • Loading branch information
marchermans committed Aug 7, 2024
1 parent 9ebf09a commit 2551855
Show file tree
Hide file tree
Showing 66 changed files with 1,961 additions and 791 deletions.
289 changes: 216 additions & 73 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import net.neoforged.gradle.common.extensions.*;
import net.neoforged.gradle.common.extensions.dependency.replacement.ReplacementLogic;
import net.neoforged.gradle.common.extensions.repository.IvyRepository;
import net.neoforged.gradle.common.extensions.sourcesets.SourceSetDependencyExtensionImpl;
import net.neoforged.gradle.common.extensions.sourcesets.SourceSetInheritanceExtensionImpl;
import net.neoforged.gradle.common.extensions.subsystems.SubsystemsExtension;
import net.neoforged.gradle.common.rules.LaterAddedReplacedDependencyRule;
import net.neoforged.gradle.common.runs.ide.IdeRunIntegrationManager;
Expand All @@ -23,6 +25,9 @@
import net.neoforged.gradle.dsl.common.extensions.*;
import net.neoforged.gradle.dsl.common.extensions.dependency.replacement.DependencyReplacement;
import net.neoforged.gradle.dsl.common.extensions.repository.Repository;
import net.neoforged.gradle.dsl.common.extensions.sourceset.RunnableSourceSet;
import net.neoforged.gradle.dsl.common.extensions.sourceset.SourceSetDependencyExtension;
import net.neoforged.gradle.dsl.common.extensions.sourceset.SourceSetInheritanceExtension;
import net.neoforged.gradle.dsl.common.extensions.subsystems.Subsystems;
import net.neoforged.gradle.dsl.common.runs.run.RunManager;
import net.neoforged.gradle.dsl.common.runs.type.RunTypeManager;
Expand Down Expand Up @@ -76,7 +81,7 @@ public void apply(Project project) {
project.getExtensions().create(Repository.class, "ivyDummyRepository", IvyRepository.class, project);
project.getExtensions().create(MinecraftArtifactCache.class, "minecraftArtifactCache", MinecraftArtifactCacheExtension.class, project);
project.getExtensions().create(DependencyReplacement.class, "dependencyReplacements", ReplacementLogic.class, project);
project.getExtensions().create(NeoGradleProblemReporter.class, PROBLEM_REPORTER_EXTENSION_NAME, NeoGradleProblemReporter.class, project, problems.forNamespace(PROBLEM_NAMESPACE));
project.getExtensions().create(NeoGradleProblemReporter.class, PROBLEM_REPORTER_EXTENSION_NAME, NeoGradleProblemReporter.class, problems.forNamespace(PROBLEM_NAMESPACE));
project.getExtensions().create(AccessTransformers.class, "accessTransformers", AccessTransformersExtension.class, project);

project.getExtensions().create(Minecraft.class, "minecraft", MinecraftExtension.class, project);
Expand Down Expand Up @@ -107,6 +112,10 @@ public void apply(Project project) {
project.getExtensions().getByType(SourceSetContainer.class).configureEach(sourceSet -> {
sourceSet.getExtensions().create(ProjectHolder.class, ProjectHolderExtension.NAME, ProjectHolderExtension.class, project);
sourceSet.getExtensions().create(RunnableSourceSet.NAME, RunnableSourceSet.class, project);

sourceSet.getExtensions().create(SourceSetDependencyExtension.class, "depends", SourceSetDependencyExtensionImpl.class, sourceSet);
sourceSet.getExtensions().create(SourceSetInheritanceExtension.class, "inherits", SourceSetInheritanceExtensionImpl.class, sourceSet);

sourceSet.getExtensions().add("runtimeDefinition", project.getObjects().property(CommonRuntimeDefinition.class));
});

Expand Down Expand Up @@ -142,7 +151,11 @@ public void apply(Project project) {
private void applyAfterEvaluate(final Project project) {
//We now eagerly get all runs and configure them.
final RunManager runs = project.getExtensions().getByType(RunManager.class);
runs.realizeAll(run -> RunsUtil.configure(project, run));
runs.realizeAll(run -> RunsUtil.configure(
project,
run,
!runs.getNames().contains(run.getName()) //Internal runs are not directly registered, so they don't show up in the name list.
));
IdeRunIntegrationManager.getInstance().apply(project);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ private static void configureRunConventions(Project project, Conventions convent
project.getExtensions().configure(RunManager.class, runContainer -> runContainer.configureAll(run -> {
final Configuration runSpecificRuntimeConfiguration = project.getConfigurations().maybeCreate(ConfigurationUtils.getRunName(run, configurations.getPerRunRuntimeConfigurationPostFix().get()));

run.getDependencies().get().getRuntime().add(runRuntimeConfiguration);
run.getDependencies().get().getRuntime().add(runSpecificRuntimeConfiguration);
run.getDependencies().getRuntime().add(runRuntimeConfiguration);
run.getDependencies().getRuntime().add(runSpecificRuntimeConfiguration);
}));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public abstract class JarJarArtifacts {
private transient final SetProperty<ResolvedComponentResult> includedRootComponents;
private transient final SetProperty<ResolvedArtifactResult> includedArtifacts;

private final Project project;
private final NeoGradleProblemReporter reporter;
private final DependencyFilter dependencyFilter;
private final DependencyVersionInformationHandler dependencyVersionInformationHandler;

Expand Down Expand Up @@ -74,8 +74,8 @@ public DependencyVersionInformationHandler getDependencyVersionInformationHandle
}

@Inject
public JarJarArtifacts(Project project) {
this.project = project;
public JarJarArtifacts(NeoGradleProblemReporter reporter) {
this.reporter = reporter;
dependencyFilter = getObjectFactory().newInstance(DefaultDependencyFilter.class);
dependencyVersionInformationHandler = getObjectFactory().newInstance(DefaultDependencyVersionInformationHandler.class);
includedRootComponents = getObjectFactory().setProperty(ResolvedComponentResult.class);
Expand Down Expand Up @@ -148,8 +148,6 @@ private List<ResolvedJarJarArtifact> getIncludedJars(DependencyFilter filter, De
if (version != null && versionRange != null) {
data.add(new ResolvedJarJarArtifact(result.getFile(), version, versionRange, jarIdentifier.group(), jarIdentifier.artifact()));
} else {
final NeoGradleProblemReporter reporter = project.getExtensions().getByType(NeoGradleProblemReporter.class);

throw reporter.throwing(spec ->
spec.id("jarjar", "no-version-range")
.contextualLabel("Missing version range for " + jarIdentifier.group() + ":" + jarIdentifier.artifact())
Expand Down Expand Up @@ -207,7 +205,6 @@ private void collectFromComponent(ResolvedComponentResult rootComponent, Set<Con
String originalVersion = getVersionFrom(originalVariant);

if (!Objects.equals(version, originalVersion)) {
final NeoGradleProblemReporter reporter = project.getExtensions().getByType(NeoGradleProblemReporter.class);
throw reporter.throwing(spec ->
spec.id("jarjar", "version-mismatch")
.contextualLabel("Version mismatch for " + originalVariant.getOwner())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,39 +151,6 @@ public TaskProvider<? extends IdePostSyncExecutionTask> getOrCreateIdeImportTask
//None found -> Create one.
idePostSyncTask = project.getTasks().register(IDE_POST_SYNC_TASK_NAME, IdePostSyncExecutionTask.class);

/*
final SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class);
sourceSets.configureEach(sourceSet -> {
final Configuration compileClasspath = project.getConfigurations().getByName(sourceSet.getCompileClasspathConfigurationName());
final Configuration runtimeClasspath = project.getConfigurations().getByName(sourceSet.getRuntimeClasspathConfigurationName());
final Configuration noneProjectDependentCompileClasspath = project.getConfigurations().maybeCreate(ConfigurationUtils.configurationNameOf(
sourceSet, "noneProjectDependentCompileClasspath"));
final Configuration noneProjectDependentRuntimeClasspath = project.getConfigurations().maybeCreate(ConfigurationUtils.configurationNameOf(
sourceSet, "noneProjectDependentRuntimeClasspath"));
noneProjectDependentCompileClasspath.getDependencies().addAllLater(
project.provider(compileClasspath::getAllDependencies)
);
noneProjectDependentRuntimeClasspath.getDependencies().addAllLater(
project.provider(runtimeClasspath::getAllDependencies)
);
noneProjectDependentCompileClasspath.withDependencies(dependencies -> {
dependencies.removeIf(dependency -> dependency instanceof ProjectDependency);
});
noneProjectDependentRuntimeClasspath.withDependencies(dependencies -> {
dependencies.removeIf(dependency -> dependency instanceof ProjectDependency);
});
idePostSyncTask.configure(task -> {
task.getIdePostSyncFiles().from(noneProjectDependentCompileClasspath);
task.getIdePostSyncFiles().from(noneProjectDependentRuntimeClasspath);
});
});
*/

//Register the task to run after the IDE import is complete
apply(new IdeImportAction() {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
package net.neoforged.gradle.common.extensions;


import net.neoforged.gradle.common.util.NeoGradleUtils;
import org.gradle.api.Action;
import org.gradle.api.InvalidUserDataException;
import org.gradle.api.Project;
import org.gradle.api.artifacts.ResolvedDependency;
import org.gradle.api.logging.Logger;
import org.gradle.api.problems.ProblemReporter;
import org.gradle.api.problems.ProblemSpec;
import org.gradle.api.problems.Severity;

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;

public class NeoGradleProblemReporter {

private final Project project;
private final ProblemReporter delegate;

public NeoGradleProblemReporter(Project project, ProblemReporter delegate) {
this.project = project;
public NeoGradleProblemReporter(ProblemReporter delegate) {
this.delegate = delegate;
}

public void reporting(Action<ProblemSpec> spec) {
delegate.reporting(spec);
public void reporting(Action<NeoGradleProblemSpec> spec, Logger logger) {
delegate.reporting(problemSpec -> {
final NeoGradleProblemSpec neoGradleProblemSpec = new NeoGradleProblemSpec();
spec.execute(neoGradleProblemSpec);

final String url = readMeUrl(neoGradleProblemSpec.section);

problemSpec.id(neoGradleProblemSpec.category, neoGradleProblemSpec.id)
.contextualLabel(neoGradleProblemSpec.contextualLabel)
.solution(neoGradleProblemSpec.solution)
.details(neoGradleProblemSpec.details)
.severity(Severity.WARNING)
.documentedAt(url);

neoGradleProblemSpec.log(logger);
});


}

public RuntimeException throwing(Action<NeoGradleProblemSpec> spec) {
Expand All @@ -49,14 +59,8 @@ public RuntimeException throwing(Action<NeoGradleProblemSpec> spec) {
});
}

private String readMeUrl(String section) {
final String neogradleVersion;
try(final InputStream stream = Objects.requireNonNull(getClass().getClassLoader().getResource("version.neogradle")).openStream()) {
neogradleVersion = new String(stream.readAllBytes());
} catch (IOException e) {
throw new RuntimeException("Failed to read NeoGradle version", e);
}

private static String readMeUrl(String section) {
final String neogradleVersion = NeoGradleUtils.getNeogradleVersion();
final String branchMajorVersion = neogradleVersion.split("\\.")[0];
final String branchMinorVersion = neogradleVersion.split("\\.")[1];
final String branch = "NG_%s.%s".formatted(branchMajorVersion, branchMinorVersion);
Expand Down Expand Up @@ -97,5 +101,14 @@ public NeoGradleProblemSpec section(String section) {
this.section = section;
return this;
}

private void log(Logger logger) {
logger.warn("-------------------------------------");
logger.warn("NeoGradle detected a problem with your project: %s".formatted(contextualLabel));
logger.warn("Details: %s".formatted(details));
logger.warn("Potential Solution: %s".formatted(solution));
logger.warn("More information: %s".formatted(readMeUrl(section)));
logger.warn("-------------------------------------");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.neoforged.gradle.common.extensions.base;

import org.gradle.api.Project;
import org.gradle.api.file.Directory;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.Provider;

Expand All @@ -14,54 +15,66 @@ public WithEnabledProperty(Project project, String prefix) {
super(project);
this.prefix = prefix;

getIsEnabled().convention(
getBooleanLocalProperty("enabled").orElse(true)
getIsEnabled().set(
getBooleanLocalProperty("enabled", true)
);
};

public WithEnabledProperty(WithEnabledProperty parent, String prefix) {
super(parent.project);

this.prefix = String.format("%s.%s", parent.prefix, prefix);
getIsEnabled().convention(
parent.getIsEnabled().zip(getBooleanLocalProperty("enabled"), (parentEnabled, enabled) -> parentEnabled && enabled).orElse(true)
getIsEnabled().set(
parent.getIsEnabled().zip(getBooleanLocalProperty("enabled", true), (parentEnabled, enabled) -> parentEnabled && enabled)
);
}

public abstract Property<Boolean> getIsEnabled();

private Provider<String> getStringLocalProperty(String propertyName) {
return super.getStringProperty(String.format("%s.%s", prefix, propertyName));
protected Provider<String> getStringLocalProperty(String propertyName, String defaultValue) {
return super.getStringProperty(String.format("%s.%s", prefix, propertyName), defaultValue);
}

private Provider<Boolean> getBooleanLocalProperty(String propertyName) {
return super.getBooleanProperty(String.format("%s.%s", prefix, propertyName));
protected Provider<Directory> getDirectoryLocalProperty(String propertyName, Provider<Directory> defaultValue) {
return super.getDirectoryProperty(String.format("%s.%s", prefix, propertyName), defaultValue);
}

private Provider<List<String>> getSpaceSeparatedListLocalProperty(String propertyName) {
return super.getSpaceSeparatedListProperty(String.format("%s.%s", prefix, propertyName));
protected Provider<Boolean> getBooleanLocalProperty(String propertyName, boolean defaultValue) {
return super.getBooleanProperty(String.format("%s.%s", prefix, propertyName), defaultValue, false);
}

protected Provider<List<String>> getSpaceSeparatedListLocalProperty(String propertyName, List<String> defaultValue) {
return super.getSpaceSeparatedListProperty(String.format("%s.%s", prefix, propertyName), defaultValue);
}

@Override
protected Provider<String> getStringProperty(String propertyName) {
protected Provider<String> getStringProperty(String propertyName, String defaultValue) {
return getIsEnabled().zip(
getStringLocalProperty(propertyName),
getStringLocalProperty(propertyName, defaultValue),
(enabled, value) -> enabled ? value : ""
);
}

@Override
protected Provider<Boolean> getBooleanProperty(String propertyName) {
protected Provider<Directory> getDirectoryProperty(String propertyName, Provider<Directory> defaultValue) {
return getIsEnabled().zip(
getDirectoryLocalProperty(propertyName, defaultValue),
(enabled, value) -> enabled ? value : null
);
}

@Override
protected Provider<Boolean> getBooleanProperty(String propertyName, boolean defaultValue, boolean disabledValue) {
return getIsEnabled().zip(
getBooleanLocalProperty(propertyName),
(enabled, value) -> enabled ? value : false
getBooleanLocalProperty(propertyName, defaultValue),
(enabled, value) -> enabled ? value : disabledValue
);
}

@Override
protected Provider<List<String>> getSpaceSeparatedListProperty(String propertyName) {
protected Provider<List<String>> getSpaceSeparatedListProperty(String propertyName, List<String> defaultValue) {
return getIsEnabled().zip(
getSpaceSeparatedListLocalProperty(propertyName),
getSpaceSeparatedListLocalProperty(propertyName, defaultValue),
(enabled, value) -> enabled ? value : List.of()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.file.Directory;
import org.gradle.api.provider.Provider;

import java.io.File;
import java.util.Arrays;
import java.util.List;

Expand All @@ -16,11 +18,18 @@ public WithPropertyLookup(Project project) {
this.project = project;
}

protected Provider<String> getStringProperty(String propertyName) {
return this.project.getProviders().gradleProperty(SUBSYSTEM_PROPERTY_PREFIX + propertyName);
protected Provider<String> getStringProperty(String propertyName, String defaultValue) {
return this.project.getProviders().gradleProperty(SUBSYSTEM_PROPERTY_PREFIX + propertyName)
.orElse(defaultValue);
}

protected Provider<Directory> getDirectoryProperty(String propertyName, Provider<Directory> defaultValue) {
return this.project.getProviders().gradleProperty(SUBSYSTEM_PROPERTY_PREFIX + propertyName)
.flatMap(path -> project.getLayout().dir(project.provider(() -> new File(path))))
.orElse(defaultValue);
}

protected Provider<Boolean> getBooleanProperty(String propertyName) {
protected Provider<Boolean> getBooleanProperty(String propertyName, boolean defaultValue, boolean disabledValue) {
String fullPropertyName = SUBSYSTEM_PROPERTY_PREFIX + propertyName;
return this.project.getProviders().gradleProperty(fullPropertyName)
.map(value -> {
Expand All @@ -29,12 +38,14 @@ protected Provider<Boolean> getBooleanProperty(String propertyName) {
} catch (Exception e) {
throw new GradleException("Gradle Property " + fullPropertyName + " is not set to a boolean value: '" + value + "'");
}
});
})
.orElse(defaultValue);
}

protected Provider<List<String>> getSpaceSeparatedListProperty(String propertyName) {
protected Provider<List<String>> getSpaceSeparatedListProperty(String propertyName, List<String> defaultValue) {
return this.project.getProviders().gradleProperty(SUBSYSTEM_PROPERTY_PREFIX + propertyName)
.map(s -> Arrays.asList(s.split("\\s+")));
.map(s -> Arrays.asList(s.split("\\s+")))
.orElse(defaultValue);
}

public Project getProject() {
Expand Down
Loading

0 comments on commit 2551855

Please sign in to comment.