Set version on build in sponge plugin

This commit is contained in:
Luck
2016-08-06 12:25:23 +02:00
Unverified
parent d88657f369
commit 4ca1e26f0f
9 changed files with 131 additions and 28 deletions
@@ -37,9 +37,8 @@ import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@Getter
@Plugin(id = "luckperms", name = "LuckPerms", version = LPSpongePlugin.VERSION, authors = {"Luck"}, description = "A permissions plugin")
@Plugin(id = "luckperms", name = "LuckPerms", version = "MAGIC", authors = {"Luck"}, description = "A permissions plugin")
public class LPSpongePlugin implements LuckPermsPlugin {
static final String VERSION = "1.5"; // TODO load this from pom
@Inject
private Logger logger;
@@ -142,7 +141,7 @@ public class LPSpongePlugin implements LuckPermsPlugin {
@Override
public String getVersion() {
return VERSION;
return "MAGIC";
}
@Override
@@ -0,0 +1,45 @@
package me.lucko.luckperms.utils;
import de.icongmbh.oss.maven.plugin.javassist.ClassTransformer;
import javassist.*;
import javassist.bytecode.AnnotationsAttribute;
import javassist.bytecode.annotation.Annotation;
import javassist.bytecode.annotation.StringMemberValue;
import me.lucko.luckperms.LPSpongePlugin;
import java.util.Properties;
/**
* Sets the version value of the plugin.
* Most of the code in this class was taken from:
* https://github.com/icon-Systemhaus-GmbH/javassist-maven-plugin/blob/master/README.textile
*/
public class VersionUtil extends ClassTransformer {
private String version = null;
@Override
protected boolean shouldTransform(CtClass clazz) throws NotFoundException {
CtClass pluginClass = ClassPool.getDefault().get(LPSpongePlugin.class.getName());
return !clazz.equals(pluginClass) && clazz.subtypeOf(pluginClass);
}
@Override
protected void applyTransformations(CtClass clazz) throws Exception {
AnnotationsAttribute attribute = (AnnotationsAttribute) clazz.getClassFile().getAttribute(AnnotationsAttribute.visibleTag);
Annotation annotation = attribute.getAnnotation("org.spongepowered.api.plugin.Plugin");
StringMemberValue versionValue = (StringMemberValue) annotation.getMemberValue("version");
versionValue.setValue(version);
attribute.setAnnotation(annotation);
CtMethod getVersionMethod = clazz.getDeclaredMethod("getVersion");
CtMethod hackedVersionMethod = CtNewMethod.make("public String getVersion() { return \"" + this.version + "\"; }", clazz);
clazz.removeMethod(getVersionMethod);
clazz.addMethod(hackedVersionMethod);
}
@Override
public void configure(final Properties properties) {
assert properties != null;
version = properties.getProperty("version");
}
}