forked from SteamWar/SteamWar
Simplify LinkageProcessor
This commit is contained in:
@@ -28,6 +28,7 @@ version = ""
|
|||||||
tasks.compileJava {
|
tasks.compileJava {
|
||||||
options.encoding = "UTF-8"
|
options.encoding = "UTF-8"
|
||||||
options.isWarnings = false
|
options.isWarnings = false
|
||||||
|
options.compilerArgs.add("-AprojectDir=$projectDir")
|
||||||
}
|
}
|
||||||
|
|
||||||
java {
|
java {
|
||||||
|
|||||||
@@ -36,22 +36,20 @@ import javax.lang.model.element.VariableElement;
|
|||||||
import javax.lang.model.type.DeclaredType;
|
import javax.lang.model.type.DeclaredType;
|
||||||
import javax.lang.model.type.TypeMirror;
|
import javax.lang.model.type.TypeMirror;
|
||||||
import javax.tools.Diagnostic;
|
import javax.tools.Diagnostic;
|
||||||
import javax.tools.FileObject;
|
|
||||||
import javax.tools.StandardLocation;
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.net.URI;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Predicate;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@SupportedAnnotationTypes("de.steamwar.linkage.Linked")
|
@SupportedAnnotationTypes("de.steamwar.linkage.Linked")
|
||||||
public class LinkageProcessor extends AbstractProcessor {
|
public class LinkageProcessor extends AbstractProcessor {
|
||||||
|
|
||||||
|
private File projectDir;
|
||||||
|
|
||||||
private static Context context;
|
private static Context context;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@@ -76,7 +74,16 @@ public class LinkageProcessor extends AbstractProcessor {
|
|||||||
super.init(processingEnv);
|
super.init(processingEnv);
|
||||||
messager = processingEnv.getMessager();
|
messager = processingEnv.getMessager();
|
||||||
className = "LinkageUtils";
|
className = "LinkageUtils";
|
||||||
|
|
||||||
|
projectDir = new File(processingEnv.getOptions().get("projectDir"));
|
||||||
|
mainClass(projectDir);
|
||||||
disabledFeatures();
|
disabledFeatures();
|
||||||
|
|
||||||
|
String name = projectDir.getName();
|
||||||
|
if (name.contains("_")) {
|
||||||
|
name = name.substring(0, name.indexOf("_"));
|
||||||
|
}
|
||||||
|
this.name = name.replaceAll("[^a-zA-Z]", "").toLowerCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
@@ -115,31 +122,13 @@ public class LinkageProcessor extends AbstractProcessor {
|
|||||||
.forEach(disabledFeatures::add);
|
.forEach(disabledFeatures::add);
|
||||||
}
|
}
|
||||||
|
|
||||||
private File getFile(File file, Predicate<File> predicate) {
|
|
||||||
while (file != null) {
|
|
||||||
if (predicate.test(file)) return file;
|
|
||||||
file = file.getParentFile();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
@Override
|
@Override
|
||||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||||
if (processed) return false;
|
if (processed) return false;
|
||||||
processed = true;
|
processed = true;
|
||||||
|
|
||||||
URI uri = processingEnv.getFiler().createResource(StandardLocation.SOURCE_OUTPUT, "", ".ignore").toUri();
|
Writer writer = processingEnv.getFiler().createSourceFile("de.steamwar." + name + ".linkage.LinkageUtils").openWriter();
|
||||||
File file = getFile(new File(uri.toString().substring(7)), f -> {
|
|
||||||
return !f.getName().contains("_") && new File(f, "build.gradle.kts").exists();
|
|
||||||
});
|
|
||||||
|
|
||||||
name = file.getName().replaceAll("[^a-zA-Z]", "").toLowerCase();
|
|
||||||
mainClass(file);
|
|
||||||
|
|
||||||
FileObject fileObject = processingEnv.getFiler().createSourceFile("de.steamwar." + name + ".linkage.LinkageUtils");
|
|
||||||
|
|
||||||
Writer writer = fileObject.openWriter();
|
|
||||||
BuildPlan buildPlan = new BuildPlan("de.steamwar." + name + ".linkage", className);
|
BuildPlan buildPlan = new BuildPlan("de.steamwar." + name + ".linkage", className);
|
||||||
|
|
||||||
Set<TypeElement> disabledElements = new HashSet<>();
|
Set<TypeElement> disabledElements = new HashSet<>();
|
||||||
|
|||||||
Reference in New Issue
Block a user