Skip to content

Commit

Permalink
Fixes, thanks Gradle.
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed Dec 4, 2023
1 parent 7ab3541 commit ffac2a3
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,21 @@

import javax.inject.Inject;

import org.jetbrains.annotations.ApiStatus;
import org.gradle.api.Action;
import org.objectweb.asm.ClassVisitor;

/**
* A remapper extension can be used to add extra processing to the remapping process.
*
* <p>Implementations of RemapperExtension's are subject to the following constraints:
* <ul>
* <li>Do not implement {@link #getParameters()} in your class, the method will be implemented by Gradle.</li>
* <li>Constructors must be annotated with {@link Inject}.</li>
* </ul>
* <p>Implementations of RemapperExtension's must have the following:
* A single constructor annotated with {@link Inject}, and taking a single argument of the parameters.
* Or a single constructor annotated with {@link Inject} taking no arguments, when the extension does not have any parameters.
*
* <p>Use {@link net.fabricmc.loom.api.LoomGradleExtensionAPI#addRemapperExtension(Class, Class, Action)} to register a remapper extension.
*
* @param <T> Parameter type for the extension. Should be {@link RemapperParameters.None} if the action does not have parameters.
*/
public interface RemapperExtension<T extends RemapperParameters> {
/**
* @return The parameters for this extension.
*/
@ApiStatus.NonExtendable
@Inject
T getParameters();

/**
* Return a {@link ClassVisitor} that will be used when remapping the given class.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@

package net.fabricmc.loom.api.remapping;

import org.jetbrains.annotations.ApiStatus;

/**
* Marker interface for parameter objects to {@link RemapperExtension}s.
*
* <p>Design based off of Gradle's {@link org.gradle.workers.WorkParameters}.
*/
public interface RemapperParameters {
final class None implements RemapperParameters {
@ApiStatus.Internal
public static None INSTANCE = new None();

private None() {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.SetProperty;
Expand Down Expand Up @@ -390,10 +391,17 @@ public void createRemapConfigurations(SourceSet sourceSet) {

@Override
public <T extends RemapperParameters> void addRemapperExtension(Class<RemapperExtension<T>> remapperExtensionClass, Class<T> parametersClass, Action<T> parameterAction) {
T parameters = getProject().getObjects().newInstance(parametersClass);
parameterAction.execute(parameters);
final ObjectFactory objectFactory = getProject().getObjects();
final RemapperExtensionHolder holder;

if (parametersClass != RemapperParameters.None.class) {
T parameters = objectFactory.newInstance(parametersClass);
parameterAction.execute(parameters);
holder = objectFactory.newInstance(RemapperExtensionHolder.class, parameters);
} else {
holder = objectFactory.newInstance(RemapperExtensionHolder.class, RemapperParameters.None.INSTANCE);
}

RemapperExtensionHolder holder = getProject().getObjects().newInstance(RemapperExtensionHolder.class, parameters);
holder.getRemapperExtensionClassName().set(remapperExtensionClass.getName());
remapperExtensions.add(holder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.Optional;
import org.jetbrains.annotations.Nullable;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.commons.Remapper;
Expand All @@ -42,6 +43,7 @@
import net.fabricmc.tinyremapper.api.TrClass;

public abstract class RemapperExtensionHolder {
// Null when RemapperParameters.None.class
private final RemapperParameters remapperParameters;

@Inject
Expand All @@ -53,6 +55,7 @@ public RemapperExtensionHolder(RemapperParameters remapperParameters) {
public abstract Property<String> getRemapperExtensionClassName();

@Nested
@Optional
public RemapperParameters getRemapperParameters() {
return remapperParameters;
}
Expand Down Expand Up @@ -81,7 +84,12 @@ private RemapperExtension<?> newInstance(ObjectFactory objectFactory) {
try {
Class<? extends RemapperExtension> remapperExtensionClass = Class.forName(getRemapperExtensionClassName().get())
.asSubclass(RemapperExtension.class);
return objectFactory.newInstance(remapperExtensionClass, getRemapperParameters());

if (remapperParameters == RemapperParameters.None.INSTANCE) {
return objectFactory.newInstance(remapperExtensionClass);
}

return objectFactory.newInstance(remapperExtensionClass, remapperParameters);
} catch (Exception e) {
throw new RuntimeException("Failed to create remapper extension", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import org.gradle.api.Plugin
import org.gradle.api.Project

import net.fabricmc.loom.LoomGradleExtension
import net.fabricmc.loom.api.remapping.RemapperParameters

class TestPlugin implements Plugin<Project> {
@Override
Expand All @@ -36,5 +37,7 @@ class TestPlugin implements Plugin<Project> {
extension.addRemapperExtension(TestRemapperExtension.class, TestRemapperExtension.Params.class) { TestRemapperExtension.Params p ->
p.replacements.put("Hello World!", "Hello Loom!")
}
extension.addRemapperExtension(TestTinyRemapperExtension.class, RemapperParameters.None.class) { RemapperParameters.None p ->
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

package net.fabricmc.loom.test.integration.buildSrc.remapext

import javax.inject.Inject

import org.gradle.api.provider.MapProperty
import org.objectweb.asm.ClassVisitor

Expand All @@ -33,9 +35,16 @@ import net.fabricmc.loom.api.remapping.RemapperParameters
import net.fabricmc.loom.util.Constants

abstract class TestRemapperExtension implements RemapperExtension<Params> {
final Params parameters

@Inject
TestRemapperExtension(Params parameters) {
this.parameters = parameters
}

@Override
ClassVisitor insertVisitor(String className, RemapperContext remapperContext, ClassVisitor classVisitor) {
def replacements = getParameters().replacements.get()
def replacements = parameters.replacements.get()
return new StringReplacementClassVisitor(Constants.ASM_VERSION, classVisitor, replacements)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2023 FabricMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package net.fabricmc.loom.test.integration.buildSrc.remapext

import org.objectweb.asm.ClassVisitor

import net.fabricmc.loom.api.remapping.RemapperContext
import net.fabricmc.loom.api.remapping.RemapperExtension
import net.fabricmc.loom.api.remapping.RemapperParameters
import net.fabricmc.loom.api.remapping.TinyRemapperExtension
import net.fabricmc.tinyremapper.TinyRemapper

class TestTinyRemapperExtension implements RemapperExtension<RemapperParameters.None>, TinyRemapperExtension {
@Override
ClassVisitor insertVisitor(String className, RemapperContext remapperContext, ClassVisitor classVisitor) {
return classVisitor
}

@Override
TinyRemapper.AnalyzeVisitorProvider getAnalyzeVisitorProvider() {
return null
}

@Override
TinyRemapper.ApplyVisitorProvider getPreApplyVisitor() {
return null
}

@Override
TinyRemapper.ApplyVisitorProvider getPostApplyVisitor() {
return null
}
}

0 comments on commit ffac2a3

Please sign in to comment.