Add decompile cache rules properties. (#1217)

Closes #1206
This commit is contained in:
modmuss
2024-11-22 10:25:56 +00:00
committed by GitHub
parent 13ed992ad1
commit 6492178eaf
3 changed files with 41 additions and 2 deletions

View File

@@ -93,6 +93,7 @@ import net.fabricmc.loom.util.ExceptionUtil;
import net.fabricmc.loom.util.FileSystemUtil;
import net.fabricmc.loom.util.IOStringConsumer;
import net.fabricmc.loom.util.Platform;
import net.fabricmc.loom.util.gradle.GradleUtils;
import net.fabricmc.loom.util.gradle.SyncTaskBuildService;
import net.fabricmc.loom.util.gradle.ThreadedProgressLoggerConsumer;
import net.fabricmc.loom.util.gradle.ThreadedSimpleProgressLogger;
@@ -174,6 +175,14 @@ public abstract class GenerateSourcesTask extends AbstractLoomTask {
@Internal
protected abstract RegularFileProperty getDecompileCacheFile();
@ApiStatus.Internal
@Input
protected abstract Property<Integer> getMaxCachedFiles();
@ApiStatus.Internal
@Input
protected abstract Property<Integer> getMaxCacheFileAge();
// Injects
@Inject
protected abstract WorkerExecutor getWorkerExecutor();
@@ -238,6 +247,9 @@ public abstract class GenerateSourcesTask extends AbstractLoomTask {
getMappings().set(SourceMappingsService.create(getProject()));
getMaxCachedFiles().set(GradleUtils.getIntegerPropertyProvider(getProject(), Constants.Properties.DECOMPILE_CACHE_MAX_FILES).orElse(50_000));
getMaxCacheFileAge().set(GradleUtils.getIntegerPropertyProvider(getProject(), Constants.Properties.DECOMPILE_CACHE_MAX_AGE).orElse(90));
mustRunAfter(getProject().getTasks().withType(AbstractRemapJarTask.class));
}
@@ -273,9 +285,17 @@ public abstract class GenerateSourcesTask extends AbstractLoomTask {
}
// TODO ensure we have a lock on this file to prevent multiple tasks from running at the same time
// TODO handle being unable to read the cache file
Files.createDirectories(cacheFile.getParent());
if (Files.exists(cacheFile)) {
try (FileSystemUtil.Delegate fs = FileSystemUtil.getJarFileSystem(cacheFile, true)) {
// Success, cache exists and can be read
} catch (IOException e) {
getLogger().warn("Discarding invalid decompile cache file: {}", cacheFile, e);
Files.delete(cacheFile);
}
}
try (FileSystemUtil.Delegate fs = FileSystemUtil.getJarFileSystem(cacheFile, true)) {
runWithCache(fs.getRoot());
}
@@ -289,13 +309,14 @@ public abstract class GenerateSourcesTask extends AbstractLoomTask {
final Path classesInputJar = getClassesInputJar().getSingleFile().toPath();
final Path sourcesOutputJar = getSourcesOutputJar().get().getAsFile().toPath();
final Path classesOutputJar = getClassesOutputJar().getSingleFile().toPath();
final var cacheRules = new CachedFileStoreImpl.CacheRules(50_000, Duration.ofDays(90));
final var cacheRules = new CachedFileStoreImpl.CacheRules(getMaxCachedFiles().get(), Duration.ofDays(getMaxCacheFileAge().get()));
final var decompileCache = new CachedFileStoreImpl<>(cacheRoot, CachedData.SERIALIZER, cacheRules);
final String cacheKey = getCacheKey();
final CachedJarProcessor cachedJarProcessor = new CachedJarProcessor(decompileCache, cacheKey);
final CachedJarProcessor.WorkRequest workRequest;
getLogger().info("Decompile cache key: {}", cacheKey);
getLogger().debug("Decompile cache rules: {}", cacheRules);
try (var timer = new Timer("Prepare job")) {
workRequest = cachedJarProcessor.prepareJob(classesInputJar);

View File

@@ -139,6 +139,8 @@ public class Constants {
* Only set this when you have a good reason to do so, the default should be fine for almost all cases.
*/
public static final String RUNTIME_JAVA_COMPATIBILITY_VERSION = "fabric.loom.runtimeJavaCompatibilityVersion";
public static final String DECOMPILE_CACHE_MAX_FILES = "fabric.loom.decompileCacheMaxFiles";
public static final String DECOMPILE_CACHE_MAX_AGE = "fabric.loom.decompileCacheMaxAge";
}
public static final class Manifest {

View File

@@ -86,6 +86,22 @@ public final class GradleUtils {
});
}
public static Provider<Integer> getIntegerPropertyProvider(Project project, String key) {
return project.provider(() -> {
final Object value = project.findProperty(key);
if (value == null) {
return null;
}
try {
return Integer.parseInt(value.toString());
} catch (final NumberFormatException ex) {
throw new IllegalArgumentException("Property " + key + " must be an integer", ex);
}
});
}
public static boolean getBooleanProperty(Project project, String key) {
return getBooleanPropertyProvider(project, key).getOrElse(false);
}