Skip to content

Commit

Permalink
Implement native support for version catalogues (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
marchermans authored May 13, 2024
1 parent 101ae05 commit df362d4
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 131 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.neoforged.gradle.common.runs.run;

import net.neoforged.gradle.dsl.common.runs.run.DependencyHandler;
import net.neoforged.gradle.dsl.common.util.ConfigurationUtils;
import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
Expand All @@ -14,80 +15,18 @@ public abstract class DependencyHandlerImpl implements DependencyHandler {

private final Project project;

private final Configuration configuration;

@Inject
public DependencyHandlerImpl(Project project) {
this.project = project;
this.configuration = project.getConfigurations().detachedConfiguration();
this.configuration.setCanBeResolved(true);
this.configuration.setCanBeConsumed(false);
this.configuration.setTransitive(false);
}

public Project getProject() {
return project;
}

public Configuration getConfiguration() {
final Configuration configuration = ConfigurationUtils.temporaryConfiguration(project);
configuration.fromDependencyCollector(this.getRuntime());
return configuration;
}

@Override
public Dependency runtime(Object dependencyNotation) {
if (dependencyNotation instanceof Configuration) {
this.configuration.extendsFrom((Configuration) dependencyNotation);
return null;
}
final Dependency dependency = project.getDependencies().create(dependencyNotation);
configuration.getDependencies().add(dependency);
return dependency;
}

@Override
public Dependency runtime(Object dependencyNotation, Action<Dependency> configureClosure) {
if (dependencyNotation instanceof Configuration) {
if (configureClosure != null) {
throw new GradleException("Cannot add a Configuration with a configuration closure.");
}
this.configuration.extendsFrom((Configuration) dependencyNotation);
return null;
}
final Dependency dependency = project.getDependencies().create(dependencyNotation);
configureClosure.execute(dependency);
configuration.getDependencies().add(dependency);
return dependency;
}

@Override
public Dependency create(Object dependencyNotation) {
final Dependency dependency = project.getDependencies().create(dependencyNotation);
configuration.getDependencies().add(dependency);
return dependency;
}

@Override
public Dependency create(Object dependencyNotation, Action<Dependency> configureClosure) {
final Dependency dependency = project.getDependencies().create(dependencyNotation);
configureClosure.execute(dependency);
return dependency;
}

@Override
public Dependency module(Object notation) {
return project.getDependencies().module(notation);
}

@Override
public Dependency module(Object notation, Action<Dependency> configureClosure) {
final Dependency dependency = project.getDependencies().module(notation);
configureClosure.execute(dependency);
return dependency;
}

@Override
public Dependency project(Map<String, ?> notation) {
final Dependency dependency = project.getDependencies().project(notation);
return dependency;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.Optional;
import org.gradle.internal.jvm.Jvm;
import org.gradle.jvm.toolchain.JavaLanguageVersion;
import org.gradle.jvm.toolchain.JavaLauncher;
import org.gradle.jvm.toolchain.JavaToolchainService;
import org.gradle.jvm.toolchain.internal.CurrentJvmToolchainSpec;

import java.io.File;
import java.util.Objects;

public abstract class JavaRuntimeTask extends DownloadingTask implements WithJavaVersion {

public JavaRuntimeTask() {
getJavaVersion().convention(getProject().getExtensions().getByType(JavaPluginExtension.class).getToolchain().getLanguageVersion());
getJavaLauncher().convention(getJavaToolChain().flatMap(toolChain -> {
if (!getJavaVersion().isPresent()) {
return toolChain.launcherFor(new CurrentJvmToolchainSpec(getObjectFactory()));
return toolChain.launcherFor(javaToolchainSpec -> javaToolchainSpec.getLanguageVersion().set(JavaLanguageVersion.of(Objects.requireNonNull(Jvm.current().getJavaVersion()).getMajorVersion())));
}

return toolChain.launcherFor(spec -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,78 +6,25 @@ import net.minecraftforge.gdi.annotations.ClosureEquivalent
import org.gradle.api.Action
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.dsl.Dependencies
import org.gradle.api.artifacts.dsl.DependencyCollector
import org.gradle.api.tasks.Internal

/**
* A custom dependency handler which manages runtime dependencies for a run configuration.
*/
@CompileStatic
interface DependencyHandler extends BaseDSLElement<DependencyHandler> {
interface DependencyHandler extends BaseDSLElement<DependencyHandler>, Dependencies {
/**
* The dependency configuration that contains all the declared dependencies.
*/
@Internal
Configuration getConfiguration();

/**
* Adds a runtime dependency to the run configuration.
* Adds a dependency to the runtime configuration.
*
* @param dependencyNotation The dependency notation.
* @return The runtime dependency.
* @return The dependency.
*/
Dependency runtime(Object dependencyNotation);

/**
* Adds a runtime dependency to the run configuration.
*
* @param dependencyNotation The dependency notation.
* @param configureClosure The closure to configure the runtime dependency.
* @return The runtime dependency.
*/
@ClosureEquivalent
Dependency runtime(Object dependencyNotation, Action<Dependency> configureClosure);

/**
* Creates a new run dependency from the given notation.
*
* @param dependencyNotation The run dependency notation.
* @return The run dependency.
*/
Dependency create(Object dependencyNotation);

/**
* Creates a new run dependency from the given notation and configures it.
*
* @param dependencyNotation The run dependency notation.
* @param configureClosure The closure to configure the run dependency.
* @return The run dependency.
*/
@ClosureEquivalent
Dependency create(Object dependencyNotation, Action<Dependency> configureClosure);

/**
* Creates a new run dependency from the given module notation.
*
* @param notation the module notation.
* @return The run dependency.
*/
Dependency module(Object notation);

/**
* Creates a new run dependency from the given module notation and configures it.
*
* @param notation the module notation.
* @param configureClosure The closure to configure the module dependency.
* @return The run dependency.
*/
@ClosureEquivalent
Dependency module(Object notation, Action<Dependency> configureClosure);

/**
* Creates a new run dependency from the given project notation.
*
* @param notation the project notation.
* @return The run dependency.
*/
Dependency project(Map<String, ?> notation);
DependencyCollector getRuntime();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.neoforged.gradle.dsl.common.util

import groovy.transform.CompileStatic
import net.neoforged.gradle.dsl.common.extensions.dependency.replacement.DependencyReplacement
import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ConfigurationContainer
Expand All @@ -11,9 +12,13 @@ import org.gradle.api.provider.Provider
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.SourceSetContainer

import java.util.concurrent.atomic.AtomicInteger

@CompileStatic
class ConfigurationUtils {

private static AtomicInteger temporaryConfigurationCounter = new AtomicInteger(0)

private ConfigurationUtils() {
throw new IllegalStateException("Can not instantiate an instance of: ConfigurationUtils. This is a utility class")
}
Expand All @@ -26,7 +31,8 @@ class ConfigurationUtils {
* @return The detached configuration
*/
static Configuration temporaryConfiguration(final Project project, final Dependency... dependencies) {
final Configuration configuration = project.getConfigurations().detachedConfiguration(dependencies)
final Configuration configuration = project.getConfigurations().detachedConfiguration(dependencies);

configuration.setCanBeConsumed(false)
configuration.setCanBeResolved(true)

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ spock_version=2.1
spock_groovy_version=3.0
mockito_version=4.11.0
jimfs_version=1.2
trainingwheels_version=1.0.42
trainingwheels_version=1.0.43
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.compile.JavaCompile;
import org.gradle.internal.jvm.Jvm;
import org.gradle.jvm.toolchain.JavaLanguageVersion;
import org.gradle.jvm.toolchain.JavaToolchainService;
import org.gradle.jvm.toolchain.internal.CurrentJvmToolchainSpec;
Expand All @@ -32,6 +33,7 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Objects;
import java.util.zip.ZipOutputStream;

@CacheableTask
Expand Down Expand Up @@ -63,7 +65,7 @@ public RecompileSourceJar() {
getJavaVersion().convention(getProject().getExtensions().getByType(JavaPluginExtension.class).getToolchain().getLanguageVersion());
getJavaLauncher().convention(getJavaToolChain().flatMap(toolChain -> {
if (!getJavaVersion().isPresent()) {
return toolChain.launcherFor(new CurrentJvmToolchainSpec(getObjectFactory()));
return toolChain.launcherFor(javaToolchainSpec -> javaToolchainSpec.getLanguageVersion().set(JavaLanguageVersion.of(Objects.requireNonNull(Jvm.current().getJavaVersion()).getMajorVersion())));
}

return toolChain.launcherFor(spec -> spec.getLanguageVersion().set(getJavaVersion()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class FunctionalTests extends BuilderBasedTestSpecification {
when:
def run = project.run {
it.tasks(':neoFormRecompile')
it.stacktrace()
}

then:
Expand Down
Loading

0 comments on commit df362d4

Please sign in to comment.