From 2f4cf35abfce82e852799dd985cc843eda8ea37f Mon Sep 17 00:00:00 2001 From: Logic <38597904+LogicFan@users.noreply.github.com> Date: Sat, 31 Jul 2021 18:43:46 -0400 Subject: [PATCH] Remove the Cache class in the LoomRepositoryPlugin, use LoomFiles instead. (#464) * refactor LoomFiles to support Settings * remove Cache class, use LoomFiles class * fix checkstyle --- .../net/fabricmc/loom/LoomGradlePlugin.java | 4 +- .../fabricmc/loom/LoomRepositoryPlugin.java | 140 ++---------------- .../fabricmc/loom/extension/LoomFiles.java | 11 ++ ...mFilesImpl.java => LoomFilesBaseImpl.java} | 63 ++------ .../loom/extension/LoomFilesProjectImpl.java | 80 ++++++++++ .../loom/extension/LoomFilesSettingsImpl.java | 70 +++++++++ 6 files changed, 189 insertions(+), 179 deletions(-) rename src/main/java/net/fabricmc/loom/extension/{LoomFilesImpl.java => LoomFilesBaseImpl.java} (54%) create mode 100644 src/main/java/net/fabricmc/loom/extension/LoomFilesProjectImpl.java create mode 100644 src/main/java/net/fabricmc/loom/extension/LoomFilesSettingsImpl.java diff --git a/src/main/java/net/fabricmc/loom/LoomGradlePlugin.java b/src/main/java/net/fabricmc/loom/LoomGradlePlugin.java index 500fb12a..75344733 100644 --- a/src/main/java/net/fabricmc/loom/LoomGradlePlugin.java +++ b/src/main/java/net/fabricmc/loom/LoomGradlePlugin.java @@ -42,8 +42,8 @@ import net.fabricmc.loom.configuration.MavenPublication; import net.fabricmc.loom.configuration.ide.IdeConfiguration; import net.fabricmc.loom.configuration.providers.mappings.MappingsCache; import net.fabricmc.loom.decompilers.DecompilerConfiguration; +import net.fabricmc.loom.extension.LoomFiles; import net.fabricmc.loom.extension.LoomGradleExtensionImpl; -import net.fabricmc.loom.extension.LoomFilesImpl; import net.fabricmc.loom.extension.MinecraftGradleExtension; import net.fabricmc.loom.task.LoomTasks; @@ -78,7 +78,7 @@ public class LoomGradlePlugin implements BootstrappedPlugin { project.apply(ImmutableMap.of("plugin", "idea")); // Setup extensions, minecraft wraps loom - var extension = project.getExtensions().create(LoomGradleExtensionAPI.class, "loom", LoomGradleExtensionImpl.class, project, new LoomFilesImpl(project)); + var extension = project.getExtensions().create(LoomGradleExtensionAPI.class, "loom", LoomGradleExtensionImpl.class, project, LoomFiles.create(project)); project.getExtensions().create(LoomGradleExtensionAPI.class, "minecraft", MinecraftGradleExtension.class, extension); project.getExtensions().create("fabricApi", FabricApiExtension.class, project); diff --git a/src/main/java/net/fabricmc/loom/LoomRepositoryPlugin.java b/src/main/java/net/fabricmc/loom/LoomRepositoryPlugin.java index d0665693..c359485c 100644 --- a/src/main/java/net/fabricmc/loom/LoomRepositoryPlugin.java +++ b/src/main/java/net/fabricmc/loom/LoomRepositoryPlugin.java @@ -24,8 +24,6 @@ package net.fabricmc.loom; -import java.io.File; - import org.gradle.api.Plugin; import org.gradle.api.Project; import org.gradle.api.artifacts.dsl.RepositoryHandler; @@ -33,14 +31,15 @@ import org.gradle.api.artifacts.repositories.IvyArtifactRepository; import org.gradle.api.initialization.Settings; import org.gradle.api.invocation.Gradle; import org.gradle.api.plugins.PluginAware; +import org.jetbrains.annotations.NotNull; + +import net.fabricmc.loom.extension.LoomFiles; public class LoomRepositoryPlugin implements Plugin { @Override - public void apply(PluginAware target) { - RepositoryHandler repositories = null; - + public void apply(@NotNull PluginAware target) { if (target instanceof Settings settings) { - repositories = settings.getDependencyResolutionManagement().getRepositories(); + declareRepositories(settings.getDependencyResolutionManagement().getRepositories(), LoomFiles.create(settings)); // leave a marker so projects don't try to override these settings.getGradle().getPluginManager().apply(LoomRepositoryPlugin.class); @@ -49,23 +48,18 @@ public class LoomRepositoryPlugin implements Plugin { return; } - repositories = project.getRepositories(); + declareRepositories(project.getRepositories(), LoomFiles.create(project)); } else if (target instanceof Gradle) { return; } else { throw new IllegalArgumentException("Expected target to be a Project or Settings, but was a " + target.getClass()); } + } - Cache cache = new Cache(target); - - // MavenConfiguration.java - repositories.flatDir(repo -> { - repo.setName("UserLocalCacheFiles"); - repo.dir(cache.getRootBuildCache()); - }); + private void declareRepositories(RepositoryHandler repositories, LoomFiles files) { repositories.maven(repo -> { repo.setName("UserLocalRemappedMods"); - repo.setUrl(cache.getRemappedModCache()); + repo.setUrl(files.getRemappedModCache()); }); repositories.maven(repo -> { repo.setName("Fabric"); @@ -77,124 +71,16 @@ public class LoomRepositoryPlugin implements Plugin { }); repositories.mavenCentral(); - // MinecraftMappedProvider.java repositories.ivy(repo -> { - repo.setUrl(cache.getUserCache()); - repo.patternLayout(layout -> { - layout.artifact("[revision]/[artifact]-[revision](-[classifier])(.[ext])"); - }); + repo.setUrl(files.getUserCache()); + repo.patternLayout(layout -> layout.artifact("[revision]/[artifact]-[revision](-[classifier])(.[ext])")); repo.metadataSources(IvyArtifactRepository.MetadataSources::artifact); }); - // MinecraftProcessedProvider.java repositories.ivy(repo -> { - repo.setUrl(cache.getRootPersistentCache()); - repo.patternLayout(layout -> { - layout.artifact("[revision]/[artifact]-[revision](-[classifier])(.[ext])"); - }); + repo.setUrl(files.getRootProjectPersistentCache()); + repo.patternLayout(layout -> layout.artifact("[revision]/[artifact]-[revision](-[classifier])(.[ext])")); repo.metadataSources(IvyArtifactRepository.MetadataSources::artifact); }); } } - -final class Cache { - private PluginAware target; - - Cache(PluginAware target) { - if (target instanceof Project || target instanceof Settings) { - this.target = target; - } else { - throw new IllegalArgumentException("Expected target to be a Project or Settings, but was a " + target.getClass()); - } - } - - File getUserCache() { - File gradleUserHomeDir = null; - - if (target instanceof Settings settings) { - gradleUserHomeDir = settings.getGradle().getGradleUserHomeDir(); - } else if (target instanceof Project project) { - gradleUserHomeDir = project.getGradle().getGradleUserHomeDir(); - } else { - throw new IllegalArgumentException("Expected target to be a Project or Settings, but was a " + target.getClass()); - } - - File userCache = new File(gradleUserHomeDir, "caches" + File.separator + "fabric-loom"); - - if (!userCache.exists()) { - userCache.mkdirs(); - } - - return userCache; - } - - public File getRootPersistentCache() { - File rootDir = null; - - if (target instanceof Settings settings) { - rootDir = settings.getRootDir(); - } else if (target instanceof Project project) { - rootDir = project.getRootDir(); - } else { - throw new IllegalArgumentException("Expected target to be a Project or Settings, but was a " + target.getClass()); - } - - File persistentCache = new File(rootDir, ".gradle" + File.separator + "loom-cache"); - - if (!persistentCache.exists()) { - persistentCache.mkdirs(); - } - - return persistentCache; - } - - public File getRootBuildCache() { - File rootDir = null; - - if (target instanceof Settings settings) { - rootDir = settings.getRootDir(); - } else if (target instanceof Project project) { - rootDir = project.getRootDir(); - } else { - throw new IllegalArgumentException("Expected target to be a Project or Settings, but was a " + target.getClass()); - } - - File buildCache = new File(rootDir, "build" + File.separator + "loom-cache"); - - if (!buildCache.exists()) { - buildCache.mkdirs(); - } - - return buildCache; - } - - public File getRemappedModCache() { - File remappedModCache = new File(getRootPersistentCache(), "remapped_mods"); - - if (!remappedModCache.exists()) { - remappedModCache.mkdir(); - } - - return remappedModCache; - } - - public File getNestedModCache() { - File nestedModCache = new File(getRootPersistentCache(), "nested_mods"); - - if (!nestedModCache.exists()) { - nestedModCache.mkdir(); - } - - return nestedModCache; - } - - public File getNativesJarStore() { - File natives = new File(getUserCache(), "natives/jars"); - - if (!natives.exists()) { - natives.mkdirs(); - } - - return natives; - } -} diff --git a/src/main/java/net/fabricmc/loom/extension/LoomFiles.java b/src/main/java/net/fabricmc/loom/extension/LoomFiles.java index b24352a8..c93e0272 100644 --- a/src/main/java/net/fabricmc/loom/extension/LoomFiles.java +++ b/src/main/java/net/fabricmc/loom/extension/LoomFiles.java @@ -26,9 +26,20 @@ package net.fabricmc.loom.extension; import java.io.File; +import org.gradle.api.Project; +import org.gradle.api.initialization.Settings; + import net.fabricmc.loom.configuration.providers.MinecraftProvider; public interface LoomFiles { + static LoomFiles create(Project project) { + return new LoomFilesProjectImpl(project); + } + + static LoomFiles create(Settings settings) { + return new LoomFilesSettingsImpl(settings); + } + File getUserCache(); File getRootProjectPersistentCache(); File getProjectPersistentCache(); diff --git a/src/main/java/net/fabricmc/loom/extension/LoomFilesImpl.java b/src/main/java/net/fabricmc/loom/extension/LoomFilesBaseImpl.java similarity index 54% rename from src/main/java/net/fabricmc/loom/extension/LoomFilesImpl.java rename to src/main/java/net/fabricmc/loom/extension/LoomFilesBaseImpl.java index 1dac177d..f99395eb 100644 --- a/src/main/java/net/fabricmc/loom/extension/LoomFilesImpl.java +++ b/src/main/java/net/fabricmc/loom/extension/LoomFilesBaseImpl.java @@ -26,32 +26,15 @@ package net.fabricmc.loom.extension; import java.io.File; -import org.gradle.api.Project; +public abstract class LoomFilesBaseImpl implements LoomFiles { + protected abstract File getGradleUserHomeDir(); + protected abstract File getRootDir(); + protected abstract File getProjectDir(); + protected abstract File getBuildDir(); -import net.fabricmc.loom.configuration.providers.MinecraftProvider; + public LoomFilesBaseImpl() { } -public final class LoomFilesImpl implements LoomFiles { - private final Project project; - - private final File userCache; - private final File rootProjectPersistentCache; - private final File projectPersistentCache; - private final File projectBuildCache; - private final File remappedModCache; - private final File nativesJarStore; - - public LoomFilesImpl(Project project) { - this.project = project; - - this.userCache = createFile(project.getGradle().getGradleUserHomeDir(), "caches" + File.separator + "fabric-loom"); - this.rootProjectPersistentCache = createFile(project.getRootProject().file(".gradle"), "loom-cache"); - this.projectPersistentCache = createFile(project.file(".gradle"), "loom-cache"); - this.projectBuildCache = createFile(project.getBuildDir(), "loom-cache"); - this.remappedModCache = createFile(getRootProjectPersistentCache(), "remapped_mods"); - this.nativesJarStore = createFile(getUserCache(), "natives/jars"); - } - - private File createFile(File parent, String child) { + private static File createFile(File parent, String child) { File file = new File(parent, child); if (!file.exists()) { @@ -63,52 +46,32 @@ public final class LoomFilesImpl implements LoomFiles { @Override public File getUserCache() { - return userCache; + return createFile(getGradleUserHomeDir(), "caches" + File.separator + "fabric-loom"); } @Override public File getRootProjectPersistentCache() { - return rootProjectPersistentCache; + return createFile(getRootDir(), ".gradle" + File.separator + "loom-cache"); } @Override public File getProjectPersistentCache() { - return projectPersistentCache; + return createFile(getProjectDir(), ".gradle" + File.separator + "loom-cache"); } @Override public File getProjectBuildCache() { - return projectBuildCache; + return createFile(getBuildDir(), "loom-cache"); } @Override public File getRemappedModCache() { - return remappedModCache; + return createFile(getRootProjectPersistentCache(), "remapped_mods"); } @Override public File getNativesJarStore() { - return nativesJarStore; - } - - @Override - public boolean hasCustomNatives() { - return project.getProperties().get("fabric.loom.natives.dir") != null; - } - - @Override - public File getNativesDirectory(MinecraftProvider minecraftProvider) { - if (hasCustomNatives()) { - return new File((String) project.property("fabric.loom.natives.dir")); - } - - File natives = new File(getUserCache(), "natives/" + minecraftProvider.minecraftVersion()); - - if (!natives.exists()) { - natives.mkdirs(); - } - - return natives; + return createFile(getUserCache(), "natives/jars"); } @Override diff --git a/src/main/java/net/fabricmc/loom/extension/LoomFilesProjectImpl.java b/src/main/java/net/fabricmc/loom/extension/LoomFilesProjectImpl.java new file mode 100644 index 00000000..b047dc1f --- /dev/null +++ b/src/main/java/net/fabricmc/loom/extension/LoomFilesProjectImpl.java @@ -0,0 +1,80 @@ +/* + * This file is part of fabric-loom, licensed under the MIT License (MIT). + * + * Copyright (c) 2021 FabricMC + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.fabricmc.loom.extension; + +import java.io.File; +import java.util.Objects; + +import org.gradle.api.Project; + +import net.fabricmc.loom.configuration.providers.MinecraftProvider; + +public final class LoomFilesProjectImpl extends LoomFilesBaseImpl { + private final Project project; + + public LoomFilesProjectImpl(Project project) { + this.project = Objects.requireNonNull(project); + } + + @Override + protected File getGradleUserHomeDir() { + return project.getGradle().getGradleUserHomeDir(); + } + + @Override + protected File getRootDir() { + return project.getRootDir(); + } + + @Override + protected File getProjectDir() { + return project.getProjectDir(); + } + + @Override + protected File getBuildDir() { + return project.getBuildDir(); + } + + @Override + public boolean hasCustomNatives() { + return project.getProperties().get("fabric.loom.natives.dir") != null; + } + + @Override + public File getNativesDirectory(MinecraftProvider minecraftProvider) { + if (hasCustomNatives()) { + return new File((String) project.property("fabric.loom.natives.dir")); + } + + File natives = new File(getUserCache(), "natives/" + minecraftProvider.minecraftVersion()); + + if (!natives.exists()) { + natives.mkdirs(); + } + + return natives; + } +} diff --git a/src/main/java/net/fabricmc/loom/extension/LoomFilesSettingsImpl.java b/src/main/java/net/fabricmc/loom/extension/LoomFilesSettingsImpl.java new file mode 100644 index 00000000..7e12b0cb --- /dev/null +++ b/src/main/java/net/fabricmc/loom/extension/LoomFilesSettingsImpl.java @@ -0,0 +1,70 @@ +/* + * This file is part of fabric-loom, licensed under the MIT License (MIT). + * + * Copyright (c) 2021 FabricMC + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package net.fabricmc.loom.extension; + +import java.io.File; +import java.util.Objects; + +import org.gradle.api.initialization.Settings; + +import net.fabricmc.loom.configuration.providers.MinecraftProvider; + +public class LoomFilesSettingsImpl extends LoomFilesBaseImpl { + private final Settings settings; + + public LoomFilesSettingsImpl(Settings settings) { + this.settings = Objects.requireNonNull(settings); + } + + @Override + public boolean hasCustomNatives() { + return false; + } + + @Override + public File getNativesDirectory(MinecraftProvider minecraftProvider) { + throw new IllegalStateException("You can not access natives directory from setting stage"); + } + + @Override + protected File getGradleUserHomeDir() { + return settings.getGradle().getGradleUserHomeDir(); + } + + @Override + protected File getRootDir() { + return settings.getRootDir(); + } + + @Override + protected File getProjectDir() { + throw new IllegalStateException("You can not access project directory from setting stage"); + } + + @Override + protected File getBuildDir() { + throw new IllegalStateException("You can not access project build directory from setting stage"); + } +}