Remove the Cache class in the LoomRepositoryPlugin, use LoomFiles instead. (#464)

* refactor LoomFiles to support Settings

* remove Cache class, use LoomFiles class

* fix checkstyle
dev/0.11
Logic 2021-07-31 18:43:46 -04:00 committed by GitHub
parent e5b972cf98
commit 2f4cf35abf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 189 additions and 179 deletions

View File

@ -42,8 +42,8 @@ import net.fabricmc.loom.configuration.MavenPublication;
import net.fabricmc.loom.configuration.ide.IdeConfiguration; import net.fabricmc.loom.configuration.ide.IdeConfiguration;
import net.fabricmc.loom.configuration.providers.mappings.MappingsCache; import net.fabricmc.loom.configuration.providers.mappings.MappingsCache;
import net.fabricmc.loom.decompilers.DecompilerConfiguration; import net.fabricmc.loom.decompilers.DecompilerConfiguration;
import net.fabricmc.loom.extension.LoomFiles;
import net.fabricmc.loom.extension.LoomGradleExtensionImpl; import net.fabricmc.loom.extension.LoomGradleExtensionImpl;
import net.fabricmc.loom.extension.LoomFilesImpl;
import net.fabricmc.loom.extension.MinecraftGradleExtension; import net.fabricmc.loom.extension.MinecraftGradleExtension;
import net.fabricmc.loom.task.LoomTasks; import net.fabricmc.loom.task.LoomTasks;
@ -78,7 +78,7 @@ public class LoomGradlePlugin implements BootstrappedPlugin {
project.apply(ImmutableMap.of("plugin", "idea")); project.apply(ImmutableMap.of("plugin", "idea"));
// Setup extensions, minecraft wraps loom // 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(LoomGradleExtensionAPI.class, "minecraft", MinecraftGradleExtension.class, extension);
project.getExtensions().create("fabricApi", FabricApiExtension.class, project); project.getExtensions().create("fabricApi", FabricApiExtension.class, project);

View File

@ -24,8 +24,6 @@
package net.fabricmc.loom; package net.fabricmc.loom;
import java.io.File;
import org.gradle.api.Plugin; import org.gradle.api.Plugin;
import org.gradle.api.Project; import org.gradle.api.Project;
import org.gradle.api.artifacts.dsl.RepositoryHandler; 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.initialization.Settings;
import org.gradle.api.invocation.Gradle; import org.gradle.api.invocation.Gradle;
import org.gradle.api.plugins.PluginAware; import org.gradle.api.plugins.PluginAware;
import org.jetbrains.annotations.NotNull;
import net.fabricmc.loom.extension.LoomFiles;
public class LoomRepositoryPlugin implements Plugin<PluginAware> { public class LoomRepositoryPlugin implements Plugin<PluginAware> {
@Override @Override
public void apply(PluginAware target) { public void apply(@NotNull PluginAware target) {
RepositoryHandler repositories = null;
if (target instanceof Settings settings) { 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 // leave a marker so projects don't try to override these
settings.getGradle().getPluginManager().apply(LoomRepositoryPlugin.class); settings.getGradle().getPluginManager().apply(LoomRepositoryPlugin.class);
@ -49,23 +48,18 @@ public class LoomRepositoryPlugin implements Plugin<PluginAware> {
return; return;
} }
repositories = project.getRepositories(); declareRepositories(project.getRepositories(), LoomFiles.create(project));
} else if (target instanceof Gradle) { } else if (target instanceof Gradle) {
return; return;
} else { } else {
throw new IllegalArgumentException("Expected target to be a Project or Settings, but was a " + target.getClass()); throw new IllegalArgumentException("Expected target to be a Project or Settings, but was a " + target.getClass());
} }
}
Cache cache = new Cache(target); private void declareRepositories(RepositoryHandler repositories, LoomFiles files) {
// MavenConfiguration.java
repositories.flatDir(repo -> {
repo.setName("UserLocalCacheFiles");
repo.dir(cache.getRootBuildCache());
});
repositories.maven(repo -> { repositories.maven(repo -> {
repo.setName("UserLocalRemappedMods"); repo.setName("UserLocalRemappedMods");
repo.setUrl(cache.getRemappedModCache()); repo.setUrl(files.getRemappedModCache());
}); });
repositories.maven(repo -> { repositories.maven(repo -> {
repo.setName("Fabric"); repo.setName("Fabric");
@ -77,124 +71,16 @@ public class LoomRepositoryPlugin implements Plugin<PluginAware> {
}); });
repositories.mavenCentral(); repositories.mavenCentral();
// MinecraftMappedProvider.java
repositories.ivy(repo -> { repositories.ivy(repo -> {
repo.setUrl(cache.getUserCache()); repo.setUrl(files.getUserCache());
repo.patternLayout(layout -> { repo.patternLayout(layout -> layout.artifact("[revision]/[artifact]-[revision](-[classifier])(.[ext])"));
layout.artifact("[revision]/[artifact]-[revision](-[classifier])(.[ext])");
});
repo.metadataSources(IvyArtifactRepository.MetadataSources::artifact); repo.metadataSources(IvyArtifactRepository.MetadataSources::artifact);
}); });
// MinecraftProcessedProvider.java
repositories.ivy(repo -> { repositories.ivy(repo -> {
repo.setUrl(cache.getRootPersistentCache()); repo.setUrl(files.getRootProjectPersistentCache());
repo.patternLayout(layout -> { repo.patternLayout(layout -> layout.artifact("[revision]/[artifact]-[revision](-[classifier])(.[ext])"));
layout.artifact("[revision]/[artifact]-[revision](-[classifier])(.[ext])");
});
repo.metadataSources(IvyArtifactRepository.MetadataSources::artifact); 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;
}
}

View File

@ -26,9 +26,20 @@ package net.fabricmc.loom.extension;
import java.io.File; import java.io.File;
import org.gradle.api.Project;
import org.gradle.api.initialization.Settings;
import net.fabricmc.loom.configuration.providers.MinecraftProvider; import net.fabricmc.loom.configuration.providers.MinecraftProvider;
public interface LoomFiles { public interface LoomFiles {
static LoomFiles create(Project project) {
return new LoomFilesProjectImpl(project);
}
static LoomFiles create(Settings settings) {
return new LoomFilesSettingsImpl(settings);
}
File getUserCache(); File getUserCache();
File getRootProjectPersistentCache(); File getRootProjectPersistentCache();
File getProjectPersistentCache(); File getProjectPersistentCache();

View File

@ -26,32 +26,15 @@ package net.fabricmc.loom.extension;
import java.io.File; 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 static File createFile(File parent, String child) {
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) {
File file = new File(parent, child); File file = new File(parent, child);
if (!file.exists()) { if (!file.exists()) {
@ -63,52 +46,32 @@ public final class LoomFilesImpl implements LoomFiles {
@Override @Override
public File getUserCache() { public File getUserCache() {
return userCache; return createFile(getGradleUserHomeDir(), "caches" + File.separator + "fabric-loom");
} }
@Override @Override
public File getRootProjectPersistentCache() { public File getRootProjectPersistentCache() {
return rootProjectPersistentCache; return createFile(getRootDir(), ".gradle" + File.separator + "loom-cache");
} }
@Override @Override
public File getProjectPersistentCache() { public File getProjectPersistentCache() {
return projectPersistentCache; return createFile(getProjectDir(), ".gradle" + File.separator + "loom-cache");
} }
@Override @Override
public File getProjectBuildCache() { public File getProjectBuildCache() {
return projectBuildCache; return createFile(getBuildDir(), "loom-cache");
} }
@Override @Override
public File getRemappedModCache() { public File getRemappedModCache() {
return remappedModCache; return createFile(getRootProjectPersistentCache(), "remapped_mods");
} }
@Override @Override
public File getNativesJarStore() { public File getNativesJarStore() {
return nativesJarStore; return createFile(getUserCache(), "natives/jars");
}
@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;
} }
@Override @Override

View File

@ -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;
}
}

View File

@ -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");
}
}