Skip to content

Commit d4eed45

Browse files
JonathingLexManos
andcommitted
Rework the tools system (#26)
This is mostly a GradleUtils Shared change, but was large enough to warrant a minor version bump. This also includes some deprecations and (binary compatible) structural changes to GradleUtils itself. Co-authored-by: LexManos <LexManos@gmail.com>
1 parent 3c3948b commit d4eed45

34 files changed

+566
-206
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ gradlePlugin {
9494

9595
plugins.register('gradleutils') {
9696
id = 'net.minecraftforge.gradleutils'
97-
implementationClass = 'net.minecraftforge.gradleutils.GradleUtilsPlugin'
97+
implementationClass = 'net.minecraftforge.gradleutils.internal.GradleUtilsPlugin'
9898
displayName = gradleutils.displayName
9999
description = project.description
100100
tags = ['minecraftforge']

gradle/wrapper/gradle-wrapper.jar

718 Bytes
Binary file not shown.

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-9.3.0-rc-1-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-9.3.0-rc-2-bin.zip
44
networkTimeout=10000
55
validateDistributionUrl=true
66
zipStoreBase=GRADLE_USER_HOME

gradleutils-shared/src/main/java/net/minecraftforge/gradleutils/shared/EnhancedPlugin.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public non-sealed abstract class EnhancedPlugin<T> implements Plugin<T>, Enhance
3535
private final @Nullable String toolsExtName;
3636

3737
private @UnknownNullability T target;
38-
private ToolsExtensionImpl tools = this.getObjects().newInstance(ToolsExtensionImpl.class, (Callable<? extends JavaToolchainService>) this::toolchainsForTools);
38+
private @Nullable ToolsExtensionImpl tools;
3939
private final EnhancedProblems problemsInternal;
4040

4141
/// The object factory provided by Gradle services.
@@ -66,11 +66,6 @@ public non-sealed abstract class EnhancedPlugin<T> implements Plugin<T>, Enhance
6666
/// Service Injection</a>
6767
protected abstract @Inject ProviderFactory getProviders();
6868

69-
/// The Java toolchain service provided by Gradle services.
70-
///
71-
/// @return The Java toolchain service
72-
protected abstract @Inject JavaToolchainService getJavaToolchains();
73-
7469
/// This constructor must be called by all subclasses using a public constructor annotated with [Inject]. The name
7570
/// and display name passed in are used in a minimal instance of [EnhancedProblems], which is used to set up the
7671
/// plugin's [global][#globalCaches()] and [local][#localCaches()] caches. Additionally, the name is used to
@@ -103,12 +98,20 @@ protected EnhancedPlugin(String name, String displayName, @Nullable String tools
10398
/// @param target The target for this plugin
10499
@Override
105100
public final void apply(T target) {
106-
this.setup(this.target = target);
101+
if (this.toolsExtName != null && target instanceof ExtensionAware extensionAware) {
102+
this.tools = (ToolsExtensionImpl) extensionAware.getExtensions().create(ToolsExtension.class, this.toolsExtName, ToolsExtensionImpl.class);
103+
104+
try {
105+
var gradle = (Gradle) InvokerHelper.getProperty(this.target, "gradle");
106+
var tools = (ToolsExtensionImpl) gradle.getExtensions().findByName(this.toolsExtName);
107+
if (tools != null)
108+
this.tools.definitions.addAll(tools.definitions);
109+
} catch (Exception ignored) { }
110+
} else {
111+
this.tools = this.getObjects().newInstance(ToolsExtensionImpl.class);
112+
}
107113

108-
if (this.toolsExtName != null && target instanceof ExtensionAware extensionAware)
109-
this.tools = extensionAware.getExtensions().create(this.toolsExtName, ToolsExtensionImpl.class, (Callable<? extends JavaToolchainService>) this::toolchainsForTools);
110-
// else
111-
// this.tools = this.getObjects().newInstance(ToolsExtensionImpl.class, (Callable<? extends JavaToolchainService>) this::toolchainsForTools);
114+
this.setup(this.target = target);
112115
}
113116

114117
/// Called when this plugin is applied to do setup work.
@@ -138,6 +141,9 @@ final EnhancedProblems getProblemsInternal() {
138141

139142
@Override
140143
public Tool.Resolved getTool(Tool tool) {
144+
if (this.tools == null)
145+
throw new IllegalStateException("Plugin has not yet been applied");
146+
141147
ProviderFactory providers;
142148
try {
143149
providers = this.target instanceof Project ? this.getProviders() : ((Gradle) InvokerHelper.getProperty(this.target, "gradle")).getRootProject().getProviders();
@@ -148,11 +154,6 @@ public Tool.Resolved getTool(Tool tool) {
148154
return ((ToolInternal) tool).get(this.globalCaches(), providers, this.tools);
149155
}
150156

151-
// NOTE: Use this in Tool implementations. Enhanced plugins do not enforce application on projects.
152-
JavaToolchainService toolchainsForTools() {
153-
return this.target instanceof Project ? this.getJavaToolchains() : ((Gradle) InvokerHelper.getProperty(this.target, "gradle")).getRootProject().getExtensions().getByType(JavaToolchainService.class);
154-
}
155-
156157

157158
/* CACHES */
158159

gradleutils-shared/src/main/java/net/minecraftforge/gradleutils/shared/SharedUtil.java

Lines changed: 163 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.gradle.api.artifacts.Configuration;
2121
import org.gradle.api.artifacts.Dependency;
2222
import org.gradle.api.artifacts.FileCollectionDependency;
23+
import org.gradle.api.artifacts.ModuleIdentifier;
24+
import org.gradle.api.artifacts.ModuleVersionIdentifier;
2325
import org.gradle.api.artifacts.ModuleVersionSelector;
2426
import org.gradle.api.logging.LogLevel;
2527
import org.gradle.api.logging.Logger;
@@ -250,6 +252,166 @@ public List<String> getArgs() {
250252

251253
//region Dependency Information
252254

255+
public record SimpleModuleIdentifier(String getGroup, String getName) implements ModuleIdentifier {
256+
static SimpleModuleIdentifier of(String group, String name) {
257+
return new SimpleModuleIdentifier(group, name);
258+
}
259+
260+
static SimpleModuleIdentifier of(String module) {
261+
var substring = module.split(":");
262+
if (substring.length != 2)
263+
throw new IllegalArgumentException("Invalid non-versioned module identifier: " + module);
264+
265+
return of(substring[0], substring[1]);
266+
}
267+
}
268+
269+
public record SimpleModuleVersionIdentifier(ModuleIdentifier getModule, String getVersion,
270+
@Nullable String classifier,
271+
String extension) implements ModuleVersionIdentifier {
272+
@Override
273+
public String getGroup() {
274+
return getModule.getGroup();
275+
}
276+
277+
@Override
278+
public String getName() {
279+
return getModule.getName();
280+
}
281+
282+
public String getDownloadUrl(String prefix) {
283+
var builder = new StringBuilder();
284+
285+
// Use HTTPS by default if protocol not defined
286+
if (!prefix.contains("://"))
287+
builder.append("https://");
288+
289+
builder.append(prefix);
290+
291+
// Account for trailing slash
292+
if (!prefix.endsWith("/"))
293+
builder.append('/');
294+
295+
builder.append(getGroup().replace('.', '/'))
296+
.append('/').append(getName())
297+
.append('/').append(getVersion())
298+
.append('/').append(getFileName());
299+
300+
return builder.toString();
301+
}
302+
303+
public String getFileName() {
304+
var builder = new StringBuilder()
305+
.append(getName())
306+
.append('-')
307+
.append(getVersion());
308+
309+
if (classifier != null)
310+
builder.append('-').append(classifier);
311+
312+
return builder.append('.').append(extension).toString();
313+
}
314+
315+
static SimpleModuleVersionIdentifier of(ModuleIdentifier module, String version) {
316+
return of(module, version, null, "jar");
317+
}
318+
319+
static SimpleModuleVersionIdentifier of(ModuleIdentifier module, String version, @Nullable String classifier, String extension) {
320+
return new SimpleModuleVersionIdentifier(module, version, classifier, extension);
321+
}
322+
323+
static SimpleModuleVersionIdentifier of(String module, String version) {
324+
return of(SimpleModuleIdentifier.of(module), version);
325+
}
326+
327+
static SimpleModuleVersionIdentifier of(String group, String name, String version) {
328+
return of(SimpleModuleIdentifier.of(group, name), version);
329+
}
330+
331+
static SimpleModuleVersionIdentifier of(String group, String name, String version, @Nullable String classifier, String extension) {
332+
return of(SimpleModuleIdentifier.of(group, name), version, classifier, extension);
333+
}
334+
335+
static SimpleModuleVersionIdentifier of(String artifact) {
336+
var split = artifact.split(":", 4);
337+
var group = split[0];
338+
var name = split[1];
339+
340+
String version;
341+
@Nullable String classifier = null;
342+
String extension = "jar";
343+
344+
// Check if version has @ before :
345+
if (split[2].indexOf('@') > 0) {
346+
if (split.length > 3)
347+
throw new IllegalArgumentException("Invalid module version identifier (found @ character before another : character): " + artifact);
348+
349+
var s = split[2].split("@");
350+
version = s[0];
351+
extension = s[1];
352+
} else {
353+
version = split[2];
354+
}
355+
356+
// Check if classifier has an @
357+
if (split.length > 3) {
358+
if (split[3].indexOf('@') > 0) {
359+
var s = split[2].split("@");
360+
classifier = s[0];
361+
extension = s[1];
362+
} else {
363+
classifier = split[3];
364+
}
365+
}
366+
367+
return of(group, name, version, classifier, extension);
368+
}
369+
370+
public SimpleModuleVersionIdentifier withVersion(String version) {
371+
return of(getModule(), version, classifier(), extension());
372+
}
373+
374+
public SimpleModuleVersionIdentifier withClassifier(String classifier) {
375+
return of(getModule(), getVersion(), classifier, extension());
376+
}
377+
378+
public SimpleModuleVersionIdentifier withExtension(String extension) {
379+
return of(getModule(), getVersion(), classifier(), extension);
380+
}
381+
382+
@Override
383+
public String toString() {
384+
var builder = new StringBuilder()
385+
.append(getGroup())
386+
.append(':').append(getName())
387+
.append(':').append(getVersion());
388+
389+
if (classifier != null)
390+
builder.append(':').append(classifier);
391+
392+
if ("jar".equals(extension))
393+
builder.append('@').append(extension);
394+
395+
return builder.toString();
396+
}
397+
}
398+
399+
public static SimpleModuleVersionIdentifier moduleOf(String artifact) {
400+
return SimpleModuleVersionIdentifier.of(artifact);
401+
}
402+
403+
public static SimpleModuleIdentifier moduleOf(String group, String name) {
404+
return SimpleModuleIdentifier.of(group, name);
405+
}
406+
407+
public static SimpleModuleVersionIdentifier moduleOf(String group, String name, String version) {
408+
return SimpleModuleVersionIdentifier.of(group, name, version);
409+
}
410+
411+
public static SimpleModuleVersionIdentifier moduleOf(ModuleIdentifier module, String version) {
412+
return SimpleModuleVersionIdentifier.of(module, version);
413+
}
414+
253415
public static String dependencyToArtifactString(Dependency dependency) {
254416
var builder = new StringBuilder();
255417

@@ -437,7 +599,7 @@ public static Comparator<String> versionComparator() {
437599
public static Class<? extends Comparator<String>> versionComparatorClass() {
438600
return StaticVersionComparator.class;
439601
}
440-
//endergion
602+
//endregion
441603

442604
//region Domain Object Handling
443605

gradleutils-shared/src/main/java/net/minecraftforge/gradleutils/shared/Tool.java

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import org.gradle.api.Action;
88
import org.gradle.api.Named;
9+
import org.gradle.api.artifacts.ModuleVersionIdentifier;
910
import org.gradle.api.file.ConfigurableFileCollection;
1011
import org.gradle.api.file.FileCollection;
1112
import org.gradle.api.provider.Property;
@@ -23,38 +24,61 @@
2324
public sealed interface Tool extends Named, Serializable permits ToolInternal, Tool.Resolved {
2425
/// Creates a new tool with the given information.
2526
///
26-
/// @param name The name for this tool (will be used in the file name)
27-
/// @param version The version for this tool (will be used in the file name)
28-
/// @param downloadUrl The download URL for this tool
27+
/// @param name The name for this tool, used to reference it in configuration and for the file name
28+
/// @param artifact The artifact for this tool, used to get the download URL
29+
/// @param mavenUrl The maven URL this tool is hosted on (if protocol is omitted, prepends `https://`, and
30+
/// appends adds trailing slash if missing)
2931
/// @param javaVersion The Java version this tool was built with, or should run on
30-
/// @param mainClass The main class to use when executing this tool
32+
/// @param mainClass The main class to use when executing this tool (optional)
3133
/// @return The tool
32-
static Tool of(String name, String version, String downloadUrl, int javaVersion, String mainClass) {
33-
return new ToolImpl(name, version, downloadUrl, javaVersion, mainClass);
34+
static Tool of(String name, String artifact, String mavenUrl, int javaVersion, @Nullable String mainClass) {
35+
return new ToolImpl(name, artifact, mavenUrl, javaVersion, mainClass);
3436
}
3537

3638
/// Creates a new tool with the given information.
3739
///
38-
/// @param name The name for this tool (will be used in the file name)
39-
/// @param version The version for this tool (will be used in the file name)
40-
/// @param downloadUrl The download URL for this tool
40+
/// @param name The name for this tool, used to reference it in configuration and for the file name
41+
/// @param artifact The artifact for this tool, used to get the download URL
42+
/// @param mavenUrl The maven URL this tool is hosted on (if protocol is omitted, prepends `https://`, and
43+
/// appends adds trailing slash if missing)
4144
/// @param javaVersion The Java version this tool was built with, or should run on
4245
/// @return The tool
43-
static Tool of(String name, String version, String downloadUrl, int javaVersion) {
44-
return new ToolImpl(name, version, downloadUrl, javaVersion, null);
46+
static Tool of(String name, String artifact, String mavenUrl, int javaVersion) {
47+
return new ToolImpl(name, artifact, mavenUrl, javaVersion, null);
4548
}
4649

50+
/// Creates a new tool with the given information.
51+
///
52+
/// @param name The name for this tool, used to reference it in configuration and for the file name
53+
/// @param artifact The artifact for this tool, used to get the download URL
54+
/// @param javaVersion The Java version this tool was built with, or should run on
55+
/// @param mainClass The main class to use when executing this tool (optional)
56+
/// @return The tool
57+
static Tool ofForge(String name, String artifact, int javaVersion, String mainClass) {
58+
return new ToolImpl(name, artifact, "https://maven.minecraftforge.net/", javaVersion, mainClass);
59+
}
60+
61+
/// Creates a new tool with the given information.
62+
///
63+
/// @param name The name for this tool, used to reference it in configuration and for the file name
64+
/// @param artifact The artifact for this tool, used to get the download URL
65+
/// @param javaVersion The Java version this tool was built with, or should run on
66+
/// @return The tool
67+
static Tool ofForge(String name, String artifact, int javaVersion) {
68+
return new ToolImpl(name, artifact, "https://maven.minecraftforge.net/", javaVersion, null);
69+
}
70+
71+
/// The module for this tool.
72+
///
73+
/// @return The module for this tool
74+
ModuleVersionIdentifier getModule();
75+
4776
/// The name for this tool. Primarily used by [ToolExecBase] to create a default tool directory.
4877
///
4978
/// @return The name of this tool
5079
@Override
5180
String getName();
5281

53-
/// The version of this tool.
54-
///
55-
/// @return The version of this tool
56-
String getVersion();
57-
5882
/// The Java version this tool was built with. Primarily used by [ToolExecBase] to determine the
5983
/// [org.gradle.jvm.toolchain.JavaLauncher].
6084
///
@@ -80,20 +104,33 @@ default boolean hasMainClass() {
80104
/// @see #getClasspath()
81105
@ApiStatus.Experimental
82106
sealed interface Definition extends Named permits ToolInternal.Definition {
107+
/// Gets the version to use for the tool. If empty, the static default set by the plugin will be used.
108+
///
109+
/// @return The version to use instead of the one configured by the plugin.
110+
/// @apiNote This will *not* be used if [#getClasspath()] or [#getArtifact()] has a value set.
111+
Property<String> getVersion();
112+
113+
/// Gets the artifact, in "group:name:version[:classifier][@extension]" format to use for this tool.
114+
/// If empty, the static default set by the plugin will be used.
115+
///
116+
/// @return The full artifact to use for this tool.
117+
/// @apiNote This will *not* be used if [#getClasspath()] has a value set.
118+
Property<String> getArtifact();
119+
83120
/// Gets the classpath to use for the tool. If empty, the static default set by the plugin will be used.
84121
///
85122
/// @return The classpath
86123
/// @apiNote This is *not* the dependency's classpath. This is the classpath used in
87-
/// [org.gradle.process.JavaExecSpec#setClasspath(FileCollection)] to invoke AccessTransformers.
124+
/// [org.gradle.process.JavaExecSpec#setClasspath(FileCollection)] to invoke this tool.
88125
ConfigurableFileCollection getClasspath();
89126

90-
/// Gets the main class to invoke when running AccessTransformers.
127+
/// Gets the main class to invoke when running this tool.
91128
///
92129
/// @return The property for the main class.
93130
/// @apiNote This is *not required* if the [classpath][#getClasspath()] is a single executable jar.
94131
Property<String> getMainClass();
95132

96-
/// Gets the Java launcher used to run AccessTransformers.
133+
/// Gets the Java launcher used to run this tool.
97134
///
98135
/// This can be easily acquired using [Java toolchains][org.gradle.jvm.toolchain.JavaToolchainService].
99136
///

0 commit comments

Comments
 (0)