Skip to content

Commit

Permalink
Fix and test no cache gen sources (#1183)
Browse files Browse the repository at this point in the history
* Fix and test no cache gen sources

* Spotless
  • Loading branch information
modmuss50 authored Sep 30, 2024
1 parent e5b785a commit e70ca3d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
39 changes: 17 additions & 22 deletions src/main/java/net/fabricmc/loom/task/GenerateSourcesTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ public void run() throws IOException {
}

if (!getUseCache().get()) {
getLogger().info("Not using decompile cache.");

try (var timer = new Timer("Decompiled sources")) {
runWithoutCache();
} catch (Exception e) {
Expand Down Expand Up @@ -340,49 +342,42 @@ private void runWithCache(Path cacheRoot) throws IOException {
final ClassLineNumbers existingLinenumbers = workRequest.lineNumbers();
final ClassLineNumbers lineNumbers = ClassLineNumbers.merge(existingLinenumbers, outputLineNumbers);

if (lineNumbers == null) {
getLogger().info("No line numbers to remap, skipping remapping");
return;
}

Path tempJar = Files.createTempFile("loom", "linenumber-remap.jar");
Files.delete(tempJar);

try (var timer = new Timer("Remap line numbers")) {
remapLineNumbers(lineNumbers, classesInputJar, tempJar);
}

Files.move(tempJar, classesOutputJar, StandardCopyOption.REPLACE_EXISTING);
applyLineNumbers(lineNumbers, classesInputJar, classesOutputJar);

try (var timer = new Timer("Prune cache")) {
decompileCache.prune();
}
}

private void runWithoutCache() throws IOException {
Path inputJar = getClassesInputJar().getSingleFile().toPath();
final Path outputJar = getSourcesOutputJar().get().getAsFile().toPath();
final Path classesInputJar = getClassesInputJar().getSingleFile().toPath();
final Path sourcesOutputJar = getSourcesOutputJar().get().getAsFile().toPath();
final Path classesOutputJar = getClassesOutputJar().getSingleFile().toPath();

// The final output sources jar
Path workClassesJar = classesInputJar;

if (getUnpickDefinitions().isPresent()) {
try (var timer = new Timer("Unpick")) {
inputJar = unpickJar(inputJar, null);
workClassesJar = unpickJar(workClassesJar, null);
}
}

ClassLineNumbers lineNumbers;

try (var timer = new Timer("Decompile")) {
lineNumbers = runDecompileJob(inputJar, outputJar, null);
lineNumbers = runDecompileJob(workClassesJar, sourcesOutputJar, null);
}

if (Files.notExists(outputJar)) {
if (Files.notExists(sourcesOutputJar)) {
throw new RuntimeException("Failed to decompile sources");
}

getLogger().info("Decompiled sources written to {}", outputJar);
getLogger().info("Decompiled sources written to {}", sourcesOutputJar);

applyLineNumbers(lineNumbers, classesInputJar, classesOutputJar);
}

private void applyLineNumbers(@Nullable ClassLineNumbers lineNumbers, Path classesInputJar, Path classesOutputJar) throws IOException {
if (lineNumbers == null) {
getLogger().info("No line numbers to remap, skipping remapping");
return;
Expand All @@ -392,10 +387,10 @@ private void runWithoutCache() throws IOException {
Files.delete(tempJar);

try (var timer = new Timer("Remap line numbers")) {
remapLineNumbers(lineNumbers, inputJar, tempJar);
remapLineNumbers(lineNumbers, classesInputJar, tempJar);
}

Files.move(tempJar, outputJar, StandardCopyOption.REPLACE_EXISTING);
Files.move(tempJar, classesOutputJar, StandardCopyOption.REPLACE_EXISTING);
}

private String getCacheKey() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,27 @@ import static org.gradle.testkit.runner.TaskOutcome.SUCCESS
class UnpickTest extends Specification implements GradleProjectTestTrait {
static final String MAPPINGS = "21w13a-net.fabricmc.yarn.21w13a.21w13a+build.30-v2"

def "unpick decompile"() {
def "unpick decompile #version #useCache"() {
setup:
def gradle = gradleProject(project: "unpick", version: version)

when:
def result = gradle.run(task: "genSources")
def result = gradle.run(tasks: useCache ? [
"genSourcesWithVineflower",
"--info"
] : [
"genSourcesWithVineflower",
"--no-use-cache",
"--info"
])
then:
result.task(":genSources").outcome == SUCCESS
result.task(":genSourcesWithVineflower").outcome == SUCCESS
getClassSource(gradle, "net/minecraft/block/CakeBlock.java").contains("Block.DEFAULT_SET_BLOCK_STATE_FLAG")
result.output.contains(useCache ? "Using decompile cache." : "Not using decompile cache.")

where:
version << STANDARD_TEST_VERSIONS
useCache << [true, false]
}

def "unpick build"() {
Expand Down

0 comments on commit e70ca3d

Please sign in to comment.