From 699815104290554188cdb847a67541c8693deb5b Mon Sep 17 00:00:00 2001 From: Pabilo8 Date: Sat, 12 Aug 2023 01:37:16 +0200 Subject: [PATCH] Added mcmod.info and pack.mcmeta generation Added tests --- .../modworks/annotations/MCModInfo.java | 16 +++ .../annotations/model/GeneratedItemModel.java | 10 ++ .../processors/MCModInfoProcessor.java | 77 ++++++++++++++ .../pabilo8/modworks/utils/GeneralUtils.java | 10 ++ .../pl/pabilo8/modworks/utils/JsonUtils.java | 9 -- src/test/java/GeneralUtilsTest.java | 11 +- src/test/java/McModInfoProcessorTest.java | 100 ++++++++++++++++++ src/test/resources/jsons/mcmod.info | 14 +++ src/test/resources/test/HelloWorld.java | 8 +- 9 files changed, 242 insertions(+), 13 deletions(-) create mode 100644 src/main/java/pl/pabilo8/modworks/annotations/MCModInfo.java create mode 100644 src/main/java/pl/pabilo8/modworks/annotations/model/GeneratedItemModel.java create mode 100644 src/main/java/pl/pabilo8/modworks/processors/MCModInfoProcessor.java delete mode 100644 src/main/java/pl/pabilo8/modworks/utils/JsonUtils.java create mode 100644 src/test/java/McModInfoProcessorTest.java create mode 100644 src/test/resources/jsons/mcmod.info diff --git a/src/main/java/pl/pabilo8/modworks/annotations/MCModInfo.java b/src/main/java/pl/pabilo8/modworks/annotations/MCModInfo.java new file mode 100644 index 0000000..964b64b --- /dev/null +++ b/src/main/java/pl/pabilo8/modworks/annotations/MCModInfo.java @@ -0,0 +1,16 @@ +package pl.pabilo8.modworks.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * @author Pabilo8 + * @since 11.08.2023 + */ +@Retention(RetentionPolicy.SOURCE) +@Target({ElementType.TYPE}) +public @interface MCModInfo +{ +} diff --git a/src/main/java/pl/pabilo8/modworks/annotations/model/GeneratedItemModel.java b/src/main/java/pl/pabilo8/modworks/annotations/model/GeneratedItemModel.java new file mode 100644 index 0000000..70a68f8 --- /dev/null +++ b/src/main/java/pl/pabilo8/modworks/annotations/model/GeneratedItemModel.java @@ -0,0 +1,10 @@ +package pl.pabilo8.modworks.annotations.model; + +/** + * @author Pabilo8 + * @since 11.08.2023 + */ +public @interface GeneratedItemModel +{ + String name(); +} diff --git a/src/main/java/pl/pabilo8/modworks/processors/MCModInfoProcessor.java b/src/main/java/pl/pabilo8/modworks/processors/MCModInfoProcessor.java new file mode 100644 index 0000000..f582ead --- /dev/null +++ b/src/main/java/pl/pabilo8/modworks/processors/MCModInfoProcessor.java @@ -0,0 +1,77 @@ +package pl.pabilo8.modworks.processors; + +import com.google.auto.service.AutoService; +import com.google.gson.stream.JsonWriter; +import pl.pabilo8.modworks.utils.GeneralUtils; + +import javax.annotation.processing.Processor; +import javax.annotation.processing.RoundEnvironment; +import javax.annotation.processing.SupportedAnnotationTypes; +import javax.annotation.processing.SupportedSourceVersion; +import javax.lang.model.SourceVersion; +import javax.lang.model.element.TypeElement; +import javax.tools.Diagnostic.Kind; +import java.io.IOException; +import java.util.Set; + +/** + * Added + */ +@SupportedAnnotationTypes({ + "pl.pabilo8.modworks.annotations.MCModInfo", +}) +@SupportedSourceVersion(SourceVersion.RELEASE_8) +@AutoService(Processor.class) +public class MCModInfoProcessor extends AbstractModProcessor +{ + @Override + protected boolean doProcessing(Set annotations, RoundEnvironment roundEnv) + { + if(Boolean.parseBoolean(getOption("modworks.mcmod"))) + { + try(JsonWriter wr = GeneralUtils.writeJSON(processingEnv, DIR_RESOURCES+"/mcmod.info")) + { + wr.beginObject(); + wr.name("modid").value(MODID); + addProperty(wr, "name"); + addProperty(wr, "description"); + addProperty(wr, "version"); + addProperty(wr, "mcversion"); + addProperty(wr, "url"); + + //TODO: 11.08.2023 Automated approach + addProperty(wr, "updateUrl"); + addArray(wr, "authorList"); + + addProperty(wr, "credits"); + addProperty(wr, "logoFile"); + addArray(wr, "screenshots"); + addArray(wr, "dependencies"); + wr.endObject(); + + } catch(IOException e) + { + processingEnv.getMessager().printMessage(Kind.NOTE, + "Could not build the mcmod.info file, "+e); + } + + + } + return true; + } + + private void addProperty(JsonWriter wr, String name) throws IOException + { + wr.name(name).value(getOption("modworks.mcmod."+GeneralUtils.toSnakeCase(name))); + } + + private void addArray(JsonWriter wr, String name) throws IOException + { + wr.name(name).beginArray(); + for(String value : getOption("modworks.mcmod."+GeneralUtils.toSnakeCase(name)).split(",")) + if(!value.isEmpty()) + wr.value(value); + wr.endArray(); + } + +} diff --git a/src/main/java/pl/pabilo8/modworks/utils/GeneralUtils.java b/src/main/java/pl/pabilo8/modworks/utils/GeneralUtils.java index 6417576..3381980 100644 --- a/src/main/java/pl/pabilo8/modworks/utils/GeneralUtils.java +++ b/src/main/java/pl/pabilo8/modworks/utils/GeneralUtils.java @@ -1,5 +1,7 @@ package pl.pabilo8.modworks.utils; +import com.google.gson.stream.JsonWriter; + import javax.annotation.processing.ProcessingEnvironment; import javax.tools.FileObject; import javax.tools.StandardLocation; @@ -27,4 +29,12 @@ public static void writeStream(OutputStream stream, String text, Object... param { stream.write(String.format(text, params).getBytes(StandardCharsets.UTF_8)); } + + public static JsonWriter writeJSON(ProcessingEnvironment env, String path) throws IOException + { + FileObject file = getFile(env, path); + JsonWriter writer = new JsonWriter(file.openWriter()); + writer.setIndent("\t"); + return writer; + } } diff --git a/src/main/java/pl/pabilo8/modworks/utils/JsonUtils.java b/src/main/java/pl/pabilo8/modworks/utils/JsonUtils.java deleted file mode 100644 index b2184b1..0000000 --- a/src/main/java/pl/pabilo8/modworks/utils/JsonUtils.java +++ /dev/null @@ -1,9 +0,0 @@ -package pl.pabilo8.modworks.utils; - -/** - * @author Pabilo8 - * @since 31.07.2023 - */ -public class JsonUtils -{ -} diff --git a/src/test/java/GeneralUtilsTest.java b/src/test/java/GeneralUtilsTest.java index bf651e5..b7c3e9f 100644 --- a/src/test/java/GeneralUtilsTest.java +++ b/src/test/java/GeneralUtilsTest.java @@ -2,7 +2,6 @@ import pl.pabilo8.modworks.utils.GeneralUtils; import java.io.ByteArrayOutputStream; -import java.io.OutputStream; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -23,8 +22,14 @@ void toSnakeCase() void writeStream() { ByteArrayOutputStream stream = new ByteArrayOutputStream(); - assertDoesNotThrow(() -> GeneralUtils.writeStream(stream,"[{('%s %s')}]","hello","world")); + assertDoesNotThrow(() -> GeneralUtils.writeStream(stream, "[{('%s %s')}]", "hello", "world")); - assertEquals(stream.toString(),"[{('hello world')}]"); + assertEquals(stream.toString(), "[{('hello world')}]"); + } + + @Test + void writeJSON() + { + //TODO: 11.08.2023 test } } \ No newline at end of file diff --git a/src/test/java/McModInfoProcessorTest.java b/src/test/java/McModInfoProcessorTest.java new file mode 100644 index 0000000..92def0a --- /dev/null +++ b/src/test/java/McModInfoProcessorTest.java @@ -0,0 +1,100 @@ +import com.google.common.collect.ImmutableList; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; +import com.google.testing.compile.*; +import org.junit.After; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import pl.pabilo8.modworks.processors.MCModInfoProcessor; + +import javax.tools.JavaFileObject; +import javax.tools.StandardLocation; + +import java.io.IOException; +import java.lang.Compiler; +import java.util.Arrays; +import java.util.Optional; +import java.util.stream.Collectors; + +import static com.google.common.truth.Truth.assertAbout; +import static com.google.common.truth.Truth.assertThat; +import static com.google.testing.compile.Compiler.javac; +import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +/** + * @author Pabilo8 + * @since 01.08.2023 + */ +class McModInfoProcessorTest +{ + public static final JavaFileObject SOURCE = JavaFileObjects.forResource("test/HelloWorld.java"); + public static final JavaFileObject MCMOD = JavaFileObjects.forResource("jsons/mcmod.info"); + + @Test + void doProcessing() + { + final CharSequence[] text = new CharSequence[1]; + assertDoesNotThrow(() -> text[0] = MCMOD.getCharContent(true)); + + assertAbout(javaSource()) + .that(SOURCE) + .withCompilerOptions("-Amodworks.modid=testmod") + .withCompilerOptions("-Amodworks.mcmod=true") + .processedWith(new MCModInfoProcessor()) + .compilesWithoutError() + .and() + .generatesFileNamed(StandardLocation.SOURCE_OUTPUT, "resources", "mcmod.info"); + } + + @After + @Test + void checkFileContents() + { + Compilation compile = javac() + .withProcessors(new MCModInfoProcessor()) + .withOptions( + "-Amodworks.modid=testmod", + "-Amodworks.mcmod=true", + "-Amodworks.flat_json=true", + "-Amodworks.mcmod.name=Gradle Test Mod", + "-Amodworks.mcmod.description=Test description", + "-Amodworks.mcmod.version=0.1.0", + "-Amodworks.mcmod.mcversion=1.12.2", + "-Amodworks.mcmod.url=https://example.com", + "-Amodworks.mcmod.author_list=Pabilo8,Carver,Schaeferd,Bastian,GabrielV,Automated Carver Device" + ) + .compile(SOURCE); + + CompilationSubject.assertThat(compile).succeeded(); + + Optional mcmodGenerated = compile.generatedFile(StandardLocation.SOURCE_OUTPUT, "resources", "mcmod.info"); + Assertions.assertTrue(mcmodGenerated.isPresent()); + + Assertions.assertDoesNotThrow( + () -> { + String original = strip(getContent(MCMOD)); + String generated = strip(getContent(mcmodGenerated.get())); + + Assertions.assertEquals( + original, generated + ); + } + ); + } + + private String getContent(JavaFileObject fob) throws IOException + { + return String.valueOf(fob.getCharContent(true)); + } + + private String strip(String text) + { + return Arrays.stream(text.split("\n")) + .map(String::trim).collect(Collectors.joining()) + .replaceAll(" ",""); + } +} \ No newline at end of file diff --git a/src/test/resources/jsons/mcmod.info b/src/test/resources/jsons/mcmod.info new file mode 100644 index 0000000..7b4c4fa --- /dev/null +++ b/src/test/resources/jsons/mcmod.info @@ -0,0 +1,14 @@ +{ + "modid": "testmod", + "name": "Gradle Test Mod", + "description": "Test description", + "version": "0.1.0", + "mcversion": "1.12.2", + "url": "https://example.com", + "updateUrl": "", + "authorList": ["Pabilo8", "Carver", "Schaeferd", "Bastian", "GabrielV", "Automated Carver Device"], + "credits": "", + "logoFile": "", + "screenshots": [], + "dependencies": [] +} \ No newline at end of file diff --git a/src/test/resources/test/HelloWorld.java b/src/test/resources/test/HelloWorld.java index 5237896..b66294c 100644 --- a/src/test/resources/test/HelloWorld.java +++ b/src/test/resources/test/HelloWorld.java @@ -1,6 +1,7 @@ package test; import pl.pabilo8.modworks.annotations.sound.ModSound; +import pl.pabilo8.modworks.annotations.MCModInfo; /** * @author Pabilo8 @@ -13,9 +14,14 @@ public class HelloWorld @ModSound(name = "test3", subtitle = "*") public static int device2; - public static void main(String[] args) { System.out.println("Hello World!"); } + + @MCModInfo() + public static class Mod + { + + } } \ No newline at end of file