Skip to content

Commit 4ebfe35

Browse files
committed
Add ArtifactAccessTransformer.Parameters#defaults
This helper method can be used when registering the transformer to set some defaults used by the default AccessTransformers tool. WARNING: Some of the parameters will not have their defaults set if the transform is registered after project evaluation! See the relevant JavaDoc comment for more details.
1 parent 0eae2ac commit 4ebfe35

File tree

2 files changed

+89
-34
lines changed

2 files changed

+89
-34
lines changed

at-gradle/src/main/java/net/minecraftforge/accesstransformers/gradle/AccessTransformersExtensionImpl.java

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,13 @@
2020
import org.gradle.api.attributes.AttributeContainer;
2121
import org.gradle.api.attributes.Category;
2222
import org.gradle.api.file.ProjectLayout;
23-
import org.gradle.api.file.RegularFile;
24-
import org.gradle.api.file.RegularFileProperty;
2523
import org.gradle.api.logging.Logger;
2624
import org.gradle.api.logging.Logging;
2725
import org.gradle.api.model.ObjectFactory;
2826
import org.gradle.api.provider.ProviderFactory;
2927
import org.jetbrains.annotations.Nullable;
3028

3129
import javax.inject.Inject;
32-
import java.io.FileNotFoundException;
33-
import java.io.IOException;
3430
import java.util.ArrayList;
3531
import java.util.Collection;
3632
import java.util.Comparator;
@@ -233,7 +229,7 @@ private void applyToConstraint(Configuration configuration, ModuleIdentifier mod
233229
}
234230

235231
private Attribute<Boolean> register(Object dependency, AccessTransformersConfigurationInternal config) {
236-
this.validateConfigFile(dependency, config.getConfig());
232+
ArtifactAccessTransformer.validateConfig(this.problems, this.getProviders(), dependency, config.getConfig());
237233

238234
int index = getIndex();
239235
var attribute = Attribute.of("net.minecraftforge.accesstransformers.automatic." + index, Boolean.class);
@@ -281,34 +277,6 @@ private int getIndex() {
281277
return index;
282278
}
283279

284-
private void validateConfigFile(Object dependency, RegularFileProperty atFileProperty) {
285-
var dependencyToString = dependency instanceof Dependency d ? Util.toString(d) : dependency.toString();
286-
287-
// check that consumer has defined the config
288-
RegularFile atFileSource;
289-
try {
290-
atFileSource = atFileProperty.get();
291-
} catch (IllegalStateException e) {
292-
throw this.problems.accessTransformerConfigNotDefined(new RuntimeException("Failed to resolve config file property", e), dependencyToString);
293-
}
294-
295-
// check that the file exists
296-
var atFile = atFileSource.getAsFile();
297-
var atFilePath = this.getLayout().getProjectDirectory().getAsFile().toPath().relativize(atFile.toPath()).toString();
298-
if (!atFile.exists())
299-
throw this.problems.accessTransformerConfigMissing(new RuntimeException(new FileNotFoundException("Config file does not exist at " + atFilePath)), dependencyToString, atFilePath);
300-
301-
// check that the file can be read and isn't empty
302-
String atFileContents;
303-
try {
304-
atFileContents = this.getProviders().fileContents(atFileSource).getAsText().get();
305-
} catch (Throwable e) {
306-
throw this.problems.accessTransformerConfigUnreadable(new RuntimeException(new IOException("Failed to read config file at " + atFilePath, e)), dependencyToString, atFilePath);
307-
}
308-
if (atFileContents.isBlank())
309-
throw this.problems.accessTransformerConfigEmpty(new IllegalStateException("Config file must not be blank at " + atFilePath), dependencyToString, atFilePath);
310-
}
311-
312280
@Override
313281
public AccessTransformersContainer register(Action<? super AccessTransformersContainer.Options> options) {
314282
return this.container = AccessTransformersContainerInternal.register(this.project, options);

at-gradle/src/main/java/net/minecraftforge/accesstransformers/gradle/ArtifactAccessTransformer.java

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
package net.minecraftforge.accesstransformers.gradle;
66

77
import net.minecraftforge.util.hash.HashStore;
8+
import org.gradle.api.Action;
9+
import org.gradle.api.Project;
10+
import org.gradle.api.artifacts.Dependency;
811
import org.gradle.api.artifacts.transform.InputArtifact;
912
import org.gradle.api.artifacts.transform.TransformAction;
1013
import org.gradle.api.artifacts.transform.TransformOutputs;
@@ -21,6 +24,7 @@
2124
import org.gradle.api.provider.ListProperty;
2225
import org.gradle.api.provider.Property;
2326
import org.gradle.api.provider.Provider;
27+
import org.gradle.api.provider.ProviderFactory;
2428
import org.gradle.api.tasks.Classpath;
2529
import org.gradle.api.tasks.Console;
2630
import org.gradle.api.tasks.Input;
@@ -33,6 +37,7 @@
3337

3438
import javax.inject.Inject;
3539
import java.io.File;
40+
import java.io.FileNotFoundException;
3641
import java.io.IOException;
3742
import java.nio.file.Files;
3843
import java.nio.file.StandardCopyOption;
@@ -79,7 +84,7 @@ public interface Parameters extends TransformParameters {
7984
///
8085
/// @return A property for the main class to use
8186
/// @apiNote This is only required if you are providing a custom AccessTransformers which is not a single jar.
82-
@Optional @Input Property<String> getMainClass();
87+
@Input @Optional Property<String> getMainClass();
8388

8489
/// The **executable path** of the Java launcher to use to run AccessTransformers.
8590
///
@@ -109,6 +114,37 @@ public interface Parameters extends TransformParameters {
109114
///
110115
/// @return A property for the caches directory
111116
@InputDirectory DirectoryProperty getCachesDir();
117+
118+
/// Returns an action that sets the default values for the parameters of the artifact access transformer.
119+
///
120+
/// @param project The project using AccessTransformers
121+
/// @apiNote Using this in [org.gradle.api.artifacts.dsl.DependencyHandler#registerTransform(Class, Action)]
122+
/// after the project has evaluated **will not** populate some crucial defaults, as the project is inaccessible
123+
/// after configuration. If you use this, make sure you do before the project has finished evaluation so that
124+
/// the property values can be finalized into the cache.
125+
static Action<? super Parameters> defaults(Project project) {
126+
return defaults(project, it -> { });
127+
}
128+
129+
/// Returns an action that sets the default values for the parameters of the artifact access transformer.
130+
///
131+
/// @param project The project using AccessTransformers
132+
/// @param action An action to run after the defaults are set
133+
/// @apiNote Using this in [org.gradle.api.artifacts.dsl.DependencyHandler#registerTransform(Class, Action)]
134+
/// after the project has evaluated **will not** populate some crucial defaults, as the project is inaccessible
135+
/// after configuration. If you use this, make sure you do before the project has finished evaluation so that
136+
/// the property values can be finalized into the cache.
137+
static Action<? super Parameters> defaults(Project project, Action<? super Parameters> action) {
138+
var plugin = project.getPlugins().getPlugin(AccessTransformersPlugin.class);
139+
return parameters -> {
140+
parameters.getClasspath().from(plugin.getTool(Tools.ACCESSTRANSFORMERS));
141+
parameters.getMainClass().value(Tools.ACCESSTRANSFORMERS.getMainClass());
142+
parameters.getJavaLauncher().value(Util.launcherFor(project, Tools.ACCESSTRANSFORMERS.getJavaVersion()).map(Util.LAUNCHER_EXECUTABLE));
143+
parameters.getArgs().value(Constants.ACCESSTRANSFORMERS_DEFAULT_ARGS);
144+
parameters.getCachesDir().value(plugin.localCaches());
145+
action.execute(parameters);
146+
};
147+
}
112148
}
113149

114150
private final AccessTransformersProblems problems;
@@ -240,6 +276,57 @@ public void transform(TransformOutputs outputs) {
240276
}
241277
}
242278

279+
/// Validates the given AccessTransformer configuration file to ensure it is ready for use by the artifact
280+
/// transformer.
281+
///
282+
/// @param project The project using AccessTransformers
283+
/// @param dependency The dependency to be transformed
284+
/// @param config The AccessTransformer configuration file
285+
/// @throws RuntimeException If validation of the configuration file failed
286+
public static void validateConfig(Project project, Object dependency, RegularFileProperty config) {
287+
validateConfig(project.getObjects(), project.getProviders(), dependency, config);
288+
}
289+
290+
/// Validates the given AccessTransformer configuration file to ensure it is ready for use by the artifact
291+
/// transformer.
292+
///
293+
/// @param objects The object factory for debugging
294+
/// @param providers The provider factory for debugging
295+
/// @param dependency The dependency to be transformed
296+
/// @param config The AccessTransformer configuration file
297+
/// @throws RuntimeException If validation of the configuration file failed
298+
public static void validateConfig(ObjectFactory objects, ProviderFactory providers, Object dependency, RegularFileProperty config) {
299+
validateConfig(objects.newInstance(AccessTransformersProblems.class), providers, dependency, config);
300+
}
301+
302+
static void validateConfig(AccessTransformersProblems problems, ProviderFactory providers, Object dependency, RegularFileProperty atFileProperty) {
303+
var dependencyToString = dependency instanceof Dependency d ? Util.toString(d) : dependency.toString();
304+
305+
// check that consumer has defined the config
306+
RegularFile atFileSource;
307+
try {
308+
atFileSource = atFileProperty.get();
309+
} catch (IllegalStateException e) {
310+
throw problems.accessTransformerConfigNotDefined(new RuntimeException("Failed to resolve config file property", e), dependencyToString);
311+
}
312+
313+
// check that the file exists
314+
var atFile = atFileSource.getAsFile();
315+
var atFilePath = atFile.getPath();
316+
if (!atFile.exists())
317+
throw problems.accessTransformerConfigMissing(new RuntimeException(new FileNotFoundException("Config file does not exist at " + atFilePath)), dependencyToString, atFilePath);
318+
319+
// check that the file can be read and isn't empty
320+
String atFileContents;
321+
try {
322+
atFileContents = providers.fileContents(atFileSource).getAsText().get();
323+
} catch (Throwable e) {
324+
throw problems.accessTransformerConfigUnreadable(new RuntimeException(new IOException("Failed to read config file at " + atFilePath, e)), dependencyToString, atFilePath);
325+
}
326+
if (atFileContents.isBlank())
327+
throw problems.accessTransformerConfigEmpty(new IllegalStateException("Config file must not be blank at " + atFilePath), dependencyToString, atFilePath);
328+
}
329+
243330
/// Uses [String#replace(CharSequence, CharSequence)] but with pre-compiled patterns provided in the given map.
244331
///
245332
/// @param s The string to replace

0 commit comments

Comments
 (0)