From 39e292fe40a29a58f3fe23402f1d5675d6861c42 Mon Sep 17 00:00:00 2001 From: modmuss Date: Tue, 26 Nov 2024 11:24:43 +0000 Subject: [PATCH] Cache source mappings (#1220) (#1221) --- .../MinecraftJarProcessorManager.java | 4 + .../task/service/SourceMappingsService.java | 82 +++++++++---------- 2 files changed, 45 insertions(+), 41 deletions(-) diff --git a/src/main/java/net/fabricmc/loom/configuration/processors/MinecraftJarProcessorManager.java b/src/main/java/net/fabricmc/loom/configuration/processors/MinecraftJarProcessorManager.java index 39b63559f..5f8809dfa 100644 --- a/src/main/java/net/fabricmc/loom/configuration/processors/MinecraftJarProcessorManager.java +++ b/src/main/java/net/fabricmc/loom/configuration/processors/MinecraftJarProcessorManager.java @@ -116,6 +116,10 @@ public String getJarHash() { return Checksum.sha1Hex(getCacheValue().getBytes(StandardCharsets.UTF_8)).substring(0, 10); } + public String getSourceMappingsHash() { + return Checksum.sha1Hex(getCacheValue().getBytes(StandardCharsets.UTF_8)); + } + public boolean requiresProcessingJar(Path jar) { Objects.requireNonNull(jar); diff --git a/src/main/java/net/fabricmc/loom/task/service/SourceMappingsService.java b/src/main/java/net/fabricmc/loom/task/service/SourceMappingsService.java index 9c178dd37..5173169ea 100644 --- a/src/main/java/net/fabricmc/loom/task/service/SourceMappingsService.java +++ b/src/main/java/net/fabricmc/loom/task/service/SourceMappingsService.java @@ -31,13 +31,13 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; import org.gradle.api.Project; import org.gradle.api.file.ConfigurableFileCollection; import org.gradle.api.provider.Provider; import org.gradle.api.tasks.InputFiles; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import net.fabricmc.loom.LoomGradleExtension; import net.fabricmc.loom.api.mappings.layered.MappingsNamespace; @@ -56,6 +56,7 @@ public class SourceMappingsService extends Service { public static final ServiceType TYPE = new ServiceType<>(Options.class, SourceMappingsService.class); + private static final Logger LOGGER = LoggerFactory.getLogger(SourceMappingsService.class); public interface Options extends Service.Options { @InputFiles @@ -71,64 +72,63 @@ public static Provider create(Project project) { } private static Path getMappings(Project project) { - LoomGradleExtension extension = LoomGradleExtension.get(project); - Path inputMappings = extension.getMappingConfiguration().tinyMappings; - - MemoryMappingTree mappingTree = new MemoryMappingTree(); + final LoomGradleExtension extension = LoomGradleExtension.get(project); + final MinecraftJarProcessorManager jarProcessor = MinecraftJarProcessorManager.create(project); - try (Reader reader = Files.newBufferedReader(inputMappings, StandardCharsets.UTF_8)) { - MappingReader.read(reader, new MappingSourceNsSwitch(mappingTree, MappingsNamespace.INTERMEDIARY.toString())); - } catch (IOException e) { - throw new RuntimeException("Failed to read mappings", e); + if (jarProcessor == null) { + LOGGER.info("No jar processor found, not creating source mappings, using project mappings"); + return extension.getMappingConfiguration().tinyMappings; } - final List mappingsProcessors = new ArrayList<>(); + final Path dir = extension.getFiles().getProjectPersistentCache().toPath().resolve("source_mappings"); + final Path path = dir.resolve(jarProcessor.getSourceMappingsHash() + ".tiny"); - MinecraftJarProcessorManager minecraftJarProcessorManager = MinecraftJarProcessorManager.create(project); - - if (minecraftJarProcessorManager != null) { - mappingsProcessors.add(mappings -> { - try (var serviceFactory = new ScopedServiceFactory()) { - final var configContext = new ConfigContextImpl(project, serviceFactory, extension); - return minecraftJarProcessorManager.processMappings(mappings, new MappingProcessorContextImpl(configContext)); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - }); + if (Files.exists(path) && !extension.refreshDeps()) { + LOGGER.debug("Using cached source mappings"); + return path; } - if (mappingsProcessors.isEmpty()) { - return inputMappings; + LOGGER.info("Creating source mappings for hash {}", jarProcessor.getSourceMappingsHash()); + + try { + Files.createDirectories(dir); + Files.deleteIfExists(path); + createMappings(project, jarProcessor, path); + } catch (IOException e) { + throw new UncheckedIOException("Failed to create source mappings", e); } - boolean transformed = false; + return path; + } - for (GenerateSourcesTask.MappingsProcessor mappingsProcessor : mappingsProcessors) { - if (mappingsProcessor.transform(mappingTree)) { - transformed = true; - } - } + private static void createMappings(Project project, MinecraftJarProcessorManager jarProcessor, Path outputMappings) throws IOException { + LoomGradleExtension extension = LoomGradleExtension.get(project); + Path inputMappings = extension.getMappingConfiguration().tinyMappings; + MemoryMappingTree mappingTree = new MemoryMappingTree(); - if (!transformed) { - return inputMappings; + try (Reader reader = Files.newBufferedReader(inputMappings, StandardCharsets.UTF_8)) { + MappingReader.read(reader, new MappingSourceNsSwitch(mappingTree, MappingsNamespace.INTERMEDIARY.toString())); } - final Path outputMappings; + GenerateSourcesTask.MappingsProcessor mappingsProcessor = mappings -> { + try (var serviceFactory = new ScopedServiceFactory()) { + final var configContext = new ConfigContextImpl(project, serviceFactory, extension); + return jarProcessor.processMappings(mappings, new MappingProcessorContextImpl(configContext)); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }; - try { - outputMappings = Files.createTempFile("loom-transitive-mappings", ".tiny"); - } catch (IOException e) { - throw new RuntimeException("Failed to create temp file", e); + boolean transformed = mappingsProcessor.transform(mappingTree); + + if (!transformed) { + LOGGER.info("No mappings processors transformed the mappings"); } try (Writer writer = Files.newBufferedWriter(outputMappings, StandardCharsets.UTF_8)) { var tiny2Writer = new Tiny2FileWriter(writer, false); mappingTree.accept(new MappingSourceNsSwitch(tiny2Writer, MappingsNamespace.NAMED.toString())); - } catch (IOException e) { - throw new RuntimeException("Failed to write mappings", e); } - - return outputMappings; } public SourceMappingsService(Options options, ServiceFactory serviceFactory) {