Skip to content

Commit

Permalink
Use a Configuration to model the additional dependencies of each run,…
Browse files Browse the repository at this point in the history
… since configurations seem to be better at tracking task dependencies.
  • Loading branch information
shartte committed Dec 17, 2023
1 parent 4ca3fa1 commit 34b2c01
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package net.neoforged.gradle.common.runs.run;

import net.neoforged.gradle.dsl.common.runs.run.DependencyHandler;
import net.neoforged.gradle.dsl.common.runs.run.RunDependency;
import org.gradle.api.Action;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.Dependency;
Expand All @@ -14,66 +14,80 @@ 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() {
return configuration;
}

@Override
public RunDependency runtime(Object dependencyNotation) {
public Dependency runtime(Object dependencyNotation) {
if (dependencyNotation instanceof Configuration) {
this.configuration.extendsFrom((Configuration) dependencyNotation);
return null;
}
final Dependency dependency = project.getDependencies().create(dependencyNotation);
final RunDependency runDependency = project.getObjects().newInstance(RunDependencyImpl.class, project, dependency);
getRuntime().add(runDependency);
return runDependency;
configuration.getDependencies().add(dependency);
return dependency;
}

@Override
public RunDependency runtime(Object dependencyNotation, Action<Dependency> configureClosure) {
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);
final RunDependency runDependency = project.getObjects().newInstance(RunDependencyImpl.class, project, dependency);
getRuntime().add(runDependency);
return runDependency;
configuration.getDependencies().add(dependency);
return dependency;
}

@Override
public RunDependency create(Object dependencyNotation) {
public Dependency create(Object dependencyNotation) {
final Dependency dependency = project.getDependencies().create(dependencyNotation);
return project.getObjects().newInstance(RunDependencyImpl.class, project, dependency);
configuration.getDependencies().add(dependency);
return dependency;
}

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

@Override
public RunDependency module(Object notation) {
final Dependency dependency = project.getDependencies().module(notation);
return project.getObjects().newInstance(RunDependencyImpl.class, project, dependency);
public Dependency module(Object notation) {
return project.getDependencies().module(notation);
}

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

@Override
public RunDependency project(Map<String, ?> notation) {
public Dependency project(Map<String, ?> notation) {
final Dependency dependency = project.getDependencies().project(notation);
return project.getObjects().newInstance(RunDependencyImpl.class, project, dependency);
}

@Override
public RunDependency configuration(Configuration notation) {
return project.getObjects().newInstance(ConfigurationRunDependencyImpl.class, project, notation);
return dependency;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void exec() {

classpath(run.getClasspath());

run.getDependencies().get().getRuntime().get().forEach(runDependency -> classpath(runDependency.getDependency()));
classpath(run.getDependencies().get().getConfiguration());

super.exec();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,29 @@ package net.neoforged.gradle.dsl.common.runs.run
import groovy.transform.CompileStatic
import net.minecraftforge.gdi.BaseDSLElement
import net.minecraftforge.gdi.annotations.ClosureEquivalent
import net.minecraftforge.gdi.annotations.DSLProperty
import org.gradle.api.Action
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.Dependency
import org.gradle.api.provider.ListProperty
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal

/**
* A custom dependency handler which manages runtime dependencies for a run configuration.
*/
@CompileStatic
interface DependencyHandler extends BaseDSLElement<DependencyHandler> {

/**
* Gets the runtime dependencies for the run configuration.
* @return The runtime dependencies for the run configuration.
* The dependency configuration that contains all the declared dependencies.
*/
@Input
@DSLProperty
ListProperty<RunDependency> getRuntime();
@Internal
Configuration getConfiguration();

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

/**
* Adds a runtime dependency to the run configuration.
Expand All @@ -40,15 +35,15 @@ interface DependencyHandler extends BaseDSLElement<DependencyHandler> {
* @return The runtime dependency.
*/
@ClosureEquivalent
RunDependency runtime(Object dependencyNotation, Action<Dependency> configureClosure);
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.
*/
RunDependency create(Object dependencyNotation);
Dependency create(Object dependencyNotation);

/**
* Creates a new run dependency from the given notation and configures it.
Expand All @@ -58,15 +53,15 @@ interface DependencyHandler extends BaseDSLElement<DependencyHandler> {
* @return The run dependency.
*/
@ClosureEquivalent
RunDependency create(Object dependencyNotation, Action<Dependency> configureClosure);
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.
*/
RunDependency module(Object notation);
Dependency module(Object notation);

/**
* Creates a new run dependency from the given module notation and configures it.
Expand All @@ -76,21 +71,13 @@ interface DependencyHandler extends BaseDSLElement<DependencyHandler> {
* @return The run dependency.
*/
@ClosureEquivalent
RunDependency module(Object notation, Action<Dependency> configureClosure);
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.
*/
RunDependency project(Map<String, ?> notation);

/**
* Creates a new run dependency from the given configuration notation.
*
* @param notation The configuration to use.
* @return The run dependency.
*/
RunDependency configuration(Configuration notation);
Dependency project(Map<String, ?> notation);
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ protected Map<String, String> buildRunInterpolationData(RunImpl run) {
task.getInputFiles().from(this.additionalUserDevDependencies);
task.getInputFiles().from(neoformRuntimeDefinition.getMinecraftDependenciesConfiguration());

run.getDependencies().get().getRuntime().get().forEach(runDependency -> task.getInputFiles().from(runDependency.getDependency()));
task.getInputFiles().from(run.getDependencies().get().getConfiguration());
}
);
configureAssociatedTask(minecraftClasspathSerializer);
Expand Down

0 comments on commit 34b2c01

Please sign in to comment.