|
5 | 5 | package net.minecraftforge.accesstransformers.gradle; |
6 | 6 |
|
7 | 7 | 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; |
8 | 11 | import org.gradle.api.artifacts.transform.InputArtifact; |
9 | 12 | import org.gradle.api.artifacts.transform.TransformAction; |
10 | 13 | import org.gradle.api.artifacts.transform.TransformOutputs; |
|
21 | 24 | import org.gradle.api.provider.ListProperty; |
22 | 25 | import org.gradle.api.provider.Property; |
23 | 26 | import org.gradle.api.provider.Provider; |
| 27 | +import org.gradle.api.provider.ProviderFactory; |
24 | 28 | import org.gradle.api.tasks.Classpath; |
25 | 29 | import org.gradle.api.tasks.Console; |
26 | 30 | import org.gradle.api.tasks.Input; |
|
33 | 37 |
|
34 | 38 | import javax.inject.Inject; |
35 | 39 | import java.io.File; |
| 40 | +import java.io.FileNotFoundException; |
36 | 41 | import java.io.IOException; |
37 | 42 | import java.nio.file.Files; |
38 | 43 | import java.nio.file.StandardCopyOption; |
@@ -79,7 +84,7 @@ public interface Parameters extends TransformParameters { |
79 | 84 | /// |
80 | 85 | /// @return A property for the main class to use |
81 | 86 | /// @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(); |
83 | 88 |
|
84 | 89 | /// The **executable path** of the Java launcher to use to run AccessTransformers. |
85 | 90 | /// |
@@ -109,6 +114,37 @@ public interface Parameters extends TransformParameters { |
109 | 114 | /// |
110 | 115 | /// @return A property for the caches directory |
111 | 116 | @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 | + } |
112 | 148 | } |
113 | 149 |
|
114 | 150 | private final AccessTransformersProblems problems; |
@@ -240,6 +276,57 @@ public void transform(TransformOutputs outputs) { |
240 | 276 | } |
241 | 277 | } |
242 | 278 |
|
| 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 | + |
243 | 330 | /// Uses [String#replace(CharSequence, CharSequence)] but with pre-compiled patterns provided in the given map. |
244 | 331 | /// |
245 | 332 | /// @param s The string to replace |
|
0 commit comments