Skip to content

Commit

Permalink
Allow enabling extensions from the classpath via CLI args (#127)
Browse files Browse the repository at this point in the history
* Allow enabling extensions from the classpath via CLI args

* Change from arbitrary args to --ext(ension)=name arg

* Add MixinExtension$CLIProvider

* System.err
  • Loading branch information
jpenilla authored Oct 15, 2024
1 parent d65d8bc commit b19f62e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/main/java/net/fabricmc/tinyremapper/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.regex.Pattern;

import net.fabricmc.tinyremapper.TinyRemapper.LinkedMethodPropagation;
import net.fabricmc.tinyremapper.extension.mixin.MixinExtension;

public class Main {
public static void main(String[] rawArgs) {
Expand All @@ -60,7 +62,12 @@ public static void main(String[] rawArgs) {
boolean inferNameFromSameLvIndex = false;
NonClassCopyMode ncCopyMode = NonClassCopyMode.FIX_META_INF;
int threads = -1;
boolean enableMixin = false;

Map<String, TinyRemapper.CLIExtensionProvider> providerMap = new HashMap<>();
List<TinyRemapper.Extension> providedExtensions = new ArrayList<>();
ServiceLoader<TinyRemapper.CLIExtensionProvider> cliProviderLoader =
ServiceLoader.load(TinyRemapper.CLIExtensionProvider.class);
cliProviderLoader.iterator().forEachRemaining(provider -> providerMap.put(provider.name(), provider));

for (String arg : rawArgs) {
if (arg.startsWith("--")) {
Expand Down Expand Up @@ -143,7 +150,11 @@ public static void main(String[] rawArgs) {

break;
case "mixin":
enableMixin = true;
handleExtension(providerMap, "mixin", providedExtensions);
break;
case "ext":
case "extension":
handleExtension(providerMap, arg.substring(valueSepPos + 1), providedExtensions);
break;
default:
System.out.println("invalid argument: "+arg+".");
Expand Down Expand Up @@ -255,8 +266,8 @@ public static void main(String[] rawArgs) {
.inferNameFromSameLvIndex(inferNameFromSameLvIndex)
.threads(threads);

if (enableMixin) {
builder = builder.extension(new MixinExtension());
for (TinyRemapper.Extension ext : providedExtensions) {
ext.attach(builder);
}

TinyRemapper remapper = builder.build();
Expand All @@ -276,4 +287,19 @@ public static void main(String[] rawArgs) {

System.out.printf("Finished after %.2f ms.\n", (System.nanoTime() - startTime) / 1e6);
}

private static void handleExtension(Map<String, TinyRemapper.CLIExtensionProvider> providerMap, String extName, List<TinyRemapper.Extension> providedExtensions) {
TinyRemapper.CLIExtensionProvider provider = providerMap.get(extName);

if (provider == null) {
System.err.println("No such extension: " + extName);
System.exit(1);
}

TinyRemapper.Extension extension = provider.provideExtension();

if (extension != null) {
providedExtensions.add(extension);
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/net/fabricmc/tinyremapper/TinyRemapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,13 @@ public interface Extension {
void attach(TinyRemapper.Builder builder);
}

public interface CLIExtensionProvider {
String name();

// nullable return
Extension provideExtension();
}

public interface AnalyzeVisitorProvider {
/**
* @deprecated use {@link #insertAnalyzeVisitor(boolean, int, String, ClassVisitor, InputTag[])} instead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ public class MixinExtension implements TinyRemapper.Extension {
private final Set<AnnotationTarget> targets;
private final /* @Nullable */ Predicate<InputTag> inputTagFilter;

public static final class CLIProvider implements TinyRemapper.CLIExtensionProvider {
@Override
public String name() {
return "mixin";
}

@Override
public TinyRemapper.Extension provideExtension() {
return new MixinExtension();
}
}

public enum AnnotationTarget {
/**
* The string literal in mixin annotation. E.g. Mixin, Invoker, Accessor, Inject,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
net.fabricmc.tinyremapper.extension.mixin.MixinExtension$CLIProvider

0 comments on commit b19f62e

Please sign in to comment.