Skip to content

Commit d37ed8c

Browse files
authored
Add Mapping Conversion Tasks (#35)
1 parent 1d7d37c commit d37ed8c

File tree

10 files changed

+370
-0
lines changed

10 files changed

+370
-0
lines changed

renamer-gradle-demo/build.gradle

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,29 @@ publishing {
3333
}
3434
}
3535

36+
// Examples on how to work with mapping files
37+
// Convert from one format to another, this converts from gzip compressed, to normal tsrg
38+
def tsrg = renamer.convert('toTsrg') {
39+
map = files('libs/mappings_test-1.0.tsrg.gz')
40+
}
41+
// The input map and format can be provided in the convert function. Making it a one line
42+
def srg = renamer.convert('toSrg', tsrg, 'srg')
43+
// You can also reverse the output after converting.
44+
renamer.convert('reverse', srg) {
45+
reverse = true
46+
format = 'srg'
47+
}
48+
// Chaining Mappings A->B and B->C will output a map for A->C
49+
renamer.chain('chain') {
50+
left tsrg
51+
right files('libs/chain.tsrg')
52+
}
53+
// Merging is useful if you have multiple map files, and need to call something that only supports one map file
54+
renamer.merge('merge') {
55+
map tsrg
56+
map files('libs/chain.tsrg')
57+
}
58+
3659
//renamer {
3760
// mappings = files('libs/mappings_test-1.0.tsrg.gz')
3861
// //mappings('test', '1.0')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MovedClass BetterName

renamer-gradle/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ java {
2828

2929
gradleutils.pluginDevDefaults(configurations, libs.versions.gradle)
3030

31+
repositories {
32+
maven gradleutils.forgeMaven
33+
}
34+
3135
dependencies {
3236
// Static Analysis
3337
api libs.annotations.jspecify
@@ -38,6 +42,7 @@ dependencies {
3842

3943
// GradleUtils Shared Base
4044
implementation libs.gradleutils.shared
45+
implementation libs.srgutils
4146
}
4247

4348
tasks.named('jar', Jar) {

renamer-gradle/settings.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,8 @@ dependencyResolutionManagement.versionCatalogs.register('libs') {
3838

3939
// GradleUtils Shared Base
4040
library 'gradleutils-shared', 'net.minecraftforge', 'gradleutils-shared' versionRef 'gradleutils'
41+
42+
// For converting different mapping formats
43+
library 'srgutils', 'net.minecraftforge:srgutils:0.6.1'
4144
}
4245
//@formatter:on
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) Forge Development LLC
3+
* SPDX-License-Identifier: LGPL-2.1-only
4+
*/
5+
package net.minecraftforge.renamer.gradle;
6+
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.util.Locale;
10+
11+
import javax.inject.Inject;
12+
13+
import org.gradle.api.DefaultTask;
14+
import org.gradle.api.file.ConfigurableFileCollection;
15+
import org.gradle.api.file.RegularFileProperty;
16+
import org.gradle.api.provider.Property;
17+
import org.gradle.api.provider.Provider;
18+
import org.gradle.api.tasks.Classpath;
19+
import org.gradle.api.tasks.Input;
20+
import org.gradle.api.tasks.InputFiles;
21+
import org.gradle.api.tasks.OutputFile;
22+
import org.gradle.api.tasks.TaskAction;
23+
import org.gradle.api.tasks.TaskProvider;
24+
25+
import net.minecraftforge.srgutils.IMappingFile;
26+
import net.minecraftforge.srgutils.IMappingFile.Format;
27+
28+
public abstract class ChainMappings extends DefaultTask implements RenamerTask {
29+
public abstract @InputFiles @Classpath ConfigurableFileCollection getLeft();
30+
public abstract @InputFiles @Classpath ConfigurableFileCollection getRight();
31+
public abstract @OutputFile RegularFileProperty getOutput();
32+
public abstract @Input Property<String> getFormat();
33+
public abstract @Input Property<Boolean> getReverse();
34+
35+
@Inject
36+
public ChainMappings() {
37+
var output = getProject().getLayout().getBuildDirectory().dir("mappings");
38+
this.getOutput().convention(output.map(d -> d.file(getName() + '.' + this.getFormat().get())));
39+
this.getFormat().convention("tsrg");
40+
this.getReverse().convention(false);
41+
}
42+
43+
@TaskAction
44+
protected void exec() throws IOException {
45+
var left = IMappingFile.load(this.getLeft().getSingleFile());
46+
var right = IMappingFile.load(this.getRight().getSingleFile());
47+
48+
var output = getOutput().getAsFile().get();
49+
50+
var format = Format.get(getFormat().get().toLowerCase(Locale.ENGLISH));
51+
if (format == null)
52+
throw new IllegalArgumentException("Unknown format: " + getFormat().get());
53+
54+
Files.createDirectories(output.getParentFile().toPath());
55+
var map = left.chain(right);
56+
map.write(output.toPath(), format, getReverse().get());
57+
}
58+
59+
public void left(Provider<?> provider) {
60+
this.getLeft().setFrom(Util.toConfiguration(getProject(), provider));
61+
}
62+
63+
public void left(TaskProvider<?> task) {
64+
this.getLeft().setFrom(Util.toFile(task));
65+
}
66+
67+
public void left(ConfigurableFileCollection value) {
68+
this.getLeft().setFrom(value);
69+
}
70+
71+
public void right(Provider<?> provider) {
72+
this.getRight().setFrom(Util.toConfiguration(getProject(), provider));
73+
}
74+
75+
public void right(TaskProvider<?> task) {
76+
this.getRight().setFrom(Util.toFile(task));
77+
}
78+
79+
public void right(ConfigurableFileCollection value) {
80+
this.getRight().setFrom(value);
81+
}
82+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (c) Forge Development LLC
3+
* SPDX-License-Identifier: LGPL-2.1-only
4+
*/
5+
package net.minecraftforge.renamer.gradle;
6+
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.util.Locale;
10+
11+
import javax.inject.Inject;
12+
13+
import org.gradle.api.DefaultTask;
14+
import org.gradle.api.file.ConfigurableFileCollection;
15+
import org.gradle.api.file.RegularFileProperty;
16+
import org.gradle.api.provider.Property;
17+
import org.gradle.api.provider.Provider;
18+
import org.gradle.api.tasks.Classpath;
19+
import org.gradle.api.tasks.Input;
20+
import org.gradle.api.tasks.InputFiles;
21+
import org.gradle.api.tasks.OutputFile;
22+
import org.gradle.api.tasks.TaskAction;
23+
import org.gradle.api.tasks.TaskProvider;
24+
25+
import net.minecraftforge.srgutils.IMappingFile;
26+
import net.minecraftforge.srgutils.IMappingFile.Format;
27+
28+
public abstract class ConvertMappings extends DefaultTask implements RenamerTask {
29+
public abstract @InputFiles @Classpath ConfigurableFileCollection getMap();
30+
public abstract @OutputFile RegularFileProperty getOutput();
31+
public abstract @Input Property<String> getFormat();
32+
public abstract @Input Property<Boolean> getReverse();
33+
34+
@Inject
35+
public ConvertMappings() {
36+
var output = getProject().getLayout().getBuildDirectory().dir("mappings");
37+
this.getOutput().convention(output.map(d -> d.file(getName() + '.' + this.getFormat().get())));
38+
this.getFormat().convention("tsrg");
39+
this.getReverse().convention(false);
40+
}
41+
42+
@TaskAction
43+
protected void exec() throws IOException {
44+
var map = IMappingFile.load(this.getMap().getSingleFile());
45+
var output = getOutput().getAsFile().get();
46+
var format = Format.get(getFormat().get().toLowerCase(Locale.ENGLISH));
47+
if (format == null)
48+
throw new IllegalArgumentException("Unknown format: " + getFormat().get());
49+
50+
Files.createDirectories(output.getParentFile().toPath());
51+
map.write(output.toPath(), format, getReverse().get());
52+
}
53+
54+
public void map(Provider<?> provider) {
55+
this.getMap().setFrom(Util.toConfiguration(getProject(), provider));
56+
}
57+
58+
public void map(TaskProvider<?> task) {
59+
this.getMap().setFrom(Util.toFile(task));
60+
}
61+
62+
public void map(ConfigurableFileCollection value) {
63+
this.getMap().setFrom(value);
64+
}
65+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) Forge Development LLC
3+
* SPDX-License-Identifier: LGPL-2.1-only
4+
*/
5+
package net.minecraftforge.renamer.gradle;
6+
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.util.Locale;
10+
11+
import javax.inject.Inject;
12+
13+
import org.gradle.api.DefaultTask;
14+
import org.gradle.api.file.ConfigurableFileCollection;
15+
import org.gradle.api.file.RegularFileProperty;
16+
import org.gradle.api.provider.ListProperty;
17+
import org.gradle.api.provider.Property;
18+
import org.gradle.api.provider.Provider;
19+
import org.gradle.api.tasks.Classpath;
20+
import org.gradle.api.tasks.Input;
21+
import org.gradle.api.tasks.InputFiles;
22+
import org.gradle.api.tasks.OutputFile;
23+
import org.gradle.api.tasks.TaskAction;
24+
import org.gradle.api.tasks.TaskProvider;
25+
26+
import net.minecraftforge.srgutils.IMappingFile;
27+
import net.minecraftforge.srgutils.IMappingFile.Format;
28+
29+
public abstract class MergeMappings extends DefaultTask implements RenamerTask {
30+
public abstract @InputFiles @Classpath ListProperty<ConfigurableFileCollection> getMaps();
31+
public abstract @OutputFile RegularFileProperty getOutput();
32+
public abstract @Input Property<String> getFormat();
33+
public abstract @Input Property<Boolean> getReverse();
34+
35+
@Inject
36+
public MergeMappings() {
37+
var output = getProject().getLayout().getBuildDirectory().dir("mappings");
38+
this.getOutput().convention(output.map(d -> d.file(getName() + '.' + this.getFormat().get())));
39+
this.getFormat().convention("tsrg");
40+
this.getReverse().convention(false);
41+
}
42+
43+
@TaskAction
44+
protected void exec() throws IOException {
45+
var output = getOutput().getAsFile().get();
46+
47+
var format = Format.get(getFormat().get().toLowerCase(Locale.ENGLISH));
48+
if (format == null)
49+
throw new IllegalArgumentException("Unknown format: " + getFormat().get());
50+
51+
IMappingFile map = null;
52+
for (var cfg : getMaps().get()) {
53+
// I am specifically forcing this to be a single file to go along with other tasks
54+
// And because ConfigurableFileCollections use a set which is unordered and order matters
55+
var file = cfg.getSingleFile();
56+
var current = IMappingFile.load(file);
57+
if (map == null)
58+
map = current;
59+
else
60+
map = map.merge(current);
61+
}
62+
63+
Files.createDirectories(output.getParentFile().toPath());
64+
map.write(output.toPath(), format, getReverse().get());
65+
}
66+
67+
public ConfigurableFileCollection map(Provider<?> provider) {
68+
var ret = this.getProject().files(Util.toConfiguration(getProject(), provider));
69+
this.getMaps().add(ret);
70+
return ret;
71+
}
72+
73+
public ConfigurableFileCollection map(TaskProvider<?> task) {
74+
var ret = this.getProject().files(Util.toFile(task));
75+
this.getMaps().add(ret);
76+
return ret;
77+
}
78+
79+
public ConfigurableFileCollection map(ConfigurableFileCollection value) {
80+
this.getMaps().add(value);
81+
return value;
82+
}
83+
}

renamer-gradle/src/main/java/net/minecraftforge/renamer/gradle/RenamerExtension.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,48 @@ default TaskProvider<RenameJar> classes(String name, TaskProvider<? extends Abst
6161
}
6262

6363
TaskProvider<RenameJar> classes(String name, TaskProvider<? extends AbstractArchiveTask> input, Action<? super RenameJar> action);
64+
65+
66+
// Convert mapping files from one format to another, Provider version accepts anything that can be resolved to a dependency
67+
default TaskProvider<ConvertMappings> convert(String name) {
68+
return convert(name, (Provider<?>)null, "tsrg", task -> {});
69+
}
70+
default TaskProvider<ConvertMappings> convert(String name, Action<? super ConvertMappings> action) {
71+
return convert(name, (Provider<?>)null, "tsrg", action);
72+
}
73+
default TaskProvider<ConvertMappings> convert(String name, Provider<?> input) {
74+
return convert(name, input, "tsrg", task -> {});
75+
}
76+
default TaskProvider<ConvertMappings> convert(String name, Provider<?> input, Action<? super ConvertMappings> action) {
77+
return convert(name, input, "tsrg", action);
78+
}
79+
default TaskProvider<ConvertMappings> convert(String name, Provider<?> input, String format) {
80+
return convert(name, input, format, task -> {});
81+
}
82+
TaskProvider<ConvertMappings> convert(String name, Provider<?> input, String format, Action<? super ConvertMappings> action);
83+
84+
85+
// Convert mappings files from one format to another, TaskProvider accepts anything that has a 'RegularFileProperty getOutput()'
86+
default TaskProvider<ConvertMappings> convert(String name, TaskProvider<?> input) {
87+
return convert(name, input, "tsrg", task -> {});
88+
}
89+
default TaskProvider<ConvertMappings> convert(String name, TaskProvider<?> input, Action<? super ConvertMappings> action) {
90+
return convert(name, input, "tsrg", action);
91+
}
92+
default TaskProvider<ConvertMappings> convert(String name, TaskProvider<?> input, String format) {
93+
return convert(name, input, format, task -> {});
94+
}
95+
TaskProvider<ConvertMappings> convert(String name, TaskProvider<?> input, String format, Action<? super ConvertMappings> action);
96+
97+
// Chains two mapping files together, Provider accepts anything that can be resolved to a dependency, TaskProvider accepts anything that has a 'RegularFileProperty getOutput()'
98+
default TaskProvider<ChainMappings> chain(String name) {
99+
return chain(name, task -> {});
100+
}
101+
TaskProvider<ChainMappings> chain(String name, Action<? super ChainMappings> action);
102+
103+
// Merges multiple mapping files together, Provider accepts anything that can be resolved to a dependency, TaskProvider accepts anything that has a 'RegularFileProperty getOutput()'
104+
default TaskProvider<MergeMappings> merge(String name) {
105+
return merge(name, task -> {});
106+
}
107+
TaskProvider<MergeMappings> merge(String name, Action<? super MergeMappings> action);
64108
}

renamer-gradle/src/main/java/net/minecraftforge/renamer/gradle/RenamerExtensionImpl.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import org.gradle.api.model.ObjectFactory;
1414
import org.gradle.api.provider.Provider;
1515
import org.gradle.api.tasks.TaskProvider;
16+
import org.jspecify.annotations.Nullable;
17+
1618
import javax.inject.Inject;
1719

1820
abstract class RenamerExtensionImpl implements RenamerExtensionInternal {
@@ -67,4 +69,38 @@ public TaskProvider<RenameJar> classes(String name, Action<? super RenameJar> ac
6769

6870
return ret;
6971
}
72+
73+
@Override
74+
public TaskProvider<ConvertMappings> convert(String name, @Nullable Provider<?> input, String format, Action<? super ConvertMappings> action) {
75+
var output = getProject().getLayout().getBuildDirectory().file("mappings/" + name + '.' + format);
76+
return getProject().getTasks().register(name, ConvertMappings.class, task -> {
77+
if (input != null)
78+
task.map(input);
79+
task.getFormat().set(format);
80+
task.getOutput().set(output);
81+
action.execute(task);
82+
});
83+
}
84+
85+
@Override
86+
public TaskProvider<ConvertMappings> convert(String name, @Nullable TaskProvider<?> input, String format, Action<? super ConvertMappings> action) {
87+
var output = getProject().getLayout().getBuildDirectory().file("mappings/" + name + '.' + format);
88+
return getProject().getTasks().register(name, ConvertMappings.class, task -> {
89+
if (input != null)
90+
task.map(input);
91+
task.getFormat().set(format);
92+
task.getOutput().set(output);
93+
action.execute(task);
94+
});
95+
}
96+
97+
@Override
98+
public TaskProvider<ChainMappings> chain(String name, Action<? super ChainMappings> action) {
99+
return getProject().getTasks().register(name, ChainMappings.class, action);
100+
}
101+
102+
@Override
103+
public TaskProvider<MergeMappings> merge(String name, Action<? super MergeMappings> action) {
104+
return getProject().getTasks().register(name, MergeMappings.class, action);
105+
}
70106
}

0 commit comments

Comments
 (0)