-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Apify the attribute schema and disambiguation rules for the Minecraft…
… Libraries (#168)
- Loading branch information
Showing
9 changed files
with
163 additions
and
52 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/main/java/net/neoforged/minecraftdependencies/DefaultValueDisambiguationRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package net.neoforged.minecraftdependencies; | ||
|
||
import org.gradle.api.attributes.AttributeDisambiguationRule; | ||
import org.gradle.api.attributes.MultipleCandidatesDetails; | ||
|
||
import javax.inject.Inject; | ||
|
||
/** | ||
* Sets a default value for an attribute if no value is requested. | ||
*/ | ||
abstract class DefaultValueDisambiguationRule<T> implements AttributeDisambiguationRule<T> { | ||
private final T defaultValue; | ||
|
||
@Inject | ||
public DefaultValueDisambiguationRule(T defaultValue) { | ||
this.defaultValue = defaultValue; | ||
} | ||
|
||
@Override | ||
public void execute(MultipleCandidatesDetails<T> details) { | ||
var consumerValue = details.getConsumerValue(); | ||
if (consumerValue != null && details.getCandidateValues().contains(consumerValue)) { | ||
details.closestMatch(consumerValue); | ||
} else { | ||
for (var candidateValue : details.getCandidateValues()) { | ||
if (candidateValue.equals(defaultValue)) { | ||
details.closestMatch(candidateValue); | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/net/neoforged/minecraftdependencies/DistributionDisambiguationRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package net.neoforged.minecraftdependencies; | ||
|
||
import javax.inject.Inject; | ||
|
||
abstract class DistributionDisambiguationRule extends DefaultValueDisambiguationRule<MinecraftDistribution> { | ||
@Inject | ||
public DistributionDisambiguationRule(MinecraftDistribution defaultValue) { | ||
super(defaultValue); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/net/neoforged/minecraftdependencies/MinecraftDependenciesPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package net.neoforged.minecraftdependencies; | ||
|
||
import org.gradle.api.Plugin; | ||
import org.gradle.api.Project; | ||
|
||
/** | ||
* Applies defaults for the Gradle attributes introduced by the <a href="https://github.com/neoforged/GradleMinecraftDependencies">Minecraft Dependencies modules</a>. | ||
* <p> | ||
* The defaults are: | ||
* <ul> | ||
* <li>{@code net.neoforged.distribution} defaults to {@code client}</li> | ||
* <li>{@code net.neoforged.operatingsystem} defaults to the current operating system</li> | ||
* </ul> | ||
*/ | ||
public class MinecraftDependenciesPlugin implements Plugin<Project> { | ||
@Override | ||
public void apply(Project project) { | ||
project.getDependencies().attributesSchema(attributesSchema -> { | ||
// Set up a disambiguation that by default selects the client distribution libraries | ||
// This happens under the assumption that client is usually a superset of server. | ||
var defaultDistribution = project.getObjects().named(MinecraftDistribution.class, MinecraftDistribution.CLIENT); | ||
attributesSchema.attribute(MinecraftDistribution.ATTRIBUTE).getDisambiguationRules().add(DistributionDisambiguationRule.class, spec -> spec.params( | ||
defaultDistribution | ||
)); | ||
|
||
var defaultOperatingSystem = project.getObjects().named(OperatingSystem.class, getDefaultOperatingSystem()); | ||
attributesSchema.attribute(OperatingSystem.ATTRIBUTE).getDisambiguationRules().add(OperatingSystemDisambiguationRule.class, spec -> spec.params( | ||
defaultOperatingSystem | ||
)); | ||
}); | ||
} | ||
|
||
private static String getDefaultOperatingSystem() { | ||
return switch (net.neoforged.moddevgradle.internal.utils.OperatingSystem.current()) { | ||
case LINUX -> OperatingSystem.LINUX; | ||
case MACOS -> OperatingSystem.MACOSX; | ||
case WINDOWS -> OperatingSystem.WINDOWS; | ||
}; | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/net/neoforged/minecraftdependencies/MinecraftDistribution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package net.neoforged.minecraftdependencies; | ||
|
||
import org.gradle.api.Named; | ||
import org.gradle.api.attributes.Attribute; | ||
|
||
/** | ||
* The source of this attribute is the list of dependencies declared by the server and client Minecraft distributions. | ||
* <p> | ||
* | ||
* @see <a href="https://github.com/neoforged/GradleMinecraftDependencies/blob/999449cc54c5c01fff1a66406be6e72872b75979/buildSrc/src/main/groovy/net/neoforged/minecraftdependencies/GenerateModuleMetadata.groovy#L83">GradleMinecraftDependencies project</a> | ||
*/ | ||
public interface MinecraftDistribution extends Named { | ||
Attribute<MinecraftDistribution> ATTRIBUTE = Attribute.of("net.neoforged.distribution", MinecraftDistribution.class); | ||
|
||
String CLIENT = "client"; | ||
String SERVER = "server"; | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/net/neoforged/minecraftdependencies/OperatingSystem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package net.neoforged.minecraftdependencies; | ||
|
||
import org.gradle.api.Named; | ||
import org.gradle.api.attributes.Attribute; | ||
|
||
/** | ||
* This attribute is used to differentiate between the different native libraries used by Minecraft. | ||
* <p> | ||
* The client in particular uses a rule-based system to declare dependencies that only apply to certain | ||
* operating systems. We model libraries that are declared using such rules by using this attribute. | ||
* | ||
* @see <a href="https://github.com/neoforged/GradleMinecraftDependencies/blob/999449cc54c5c01fff1a66406be6e72872b75979/buildSrc/src/main/groovy/net/neoforged/minecraftdependencies/GenerateModuleMetadata.groovy#L140">GradleMinecraftDependencies</a> | ||
*/ | ||
public interface OperatingSystem extends Named { | ||
Attribute<OperatingSystem> ATTRIBUTE = Attribute.of("net.neoforged.operatingsystem", OperatingSystem.class); | ||
|
||
String LINUX = "linux"; | ||
String MACOSX = "osx"; | ||
String WINDOWS = "windows"; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/net/neoforged/minecraftdependencies/OperatingSystemDisambiguationRule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package net.neoforged.minecraftdependencies; | ||
|
||
import javax.inject.Inject; | ||
|
||
abstract class OperatingSystemDisambiguationRule extends DefaultValueDisambiguationRule<OperatingSystem> { | ||
@Inject | ||
public OperatingSystemDisambiguationRule(OperatingSystem defaultValue) { | ||
super(defaultValue); | ||
} | ||
} |
15 changes: 0 additions & 15 deletions
15
src/main/java/net/neoforged/moddevgradle/internal/DistributionDisambiguation.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 0 additions & 19 deletions
19
src/main/java/net/neoforged/moddevgradle/internal/OperatingSystemDisambiguation.java
This file was deleted.
Oops, something went wrong.