Skip to content

Commit

Permalink
Added mcmod.info and pack.mcmeta generation
Browse files Browse the repository at this point in the history
Added tests
  • Loading branch information
Pabilo8 committed Aug 11, 2023
1 parent c128997 commit 6998151
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 13 deletions.
16 changes: 16 additions & 0 deletions src/main/java/pl/pabilo8/modworks/annotations/MCModInfo.java
Original file line number Diff line number Diff line change
@@ -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
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package pl.pabilo8.modworks.annotations.model;

/**
* @author Pabilo8
* @since 11.08.2023
*/
public @interface GeneratedItemModel
{
String name();
}
Original file line number Diff line number Diff line change
@@ -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<? extends TypeElement> 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();
}

}
10 changes: 10 additions & 0 deletions src/main/java/pl/pabilo8/modworks/utils/GeneralUtils.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
}
9 changes: 0 additions & 9 deletions src/main/java/pl/pabilo8/modworks/utils/JsonUtils.java

This file was deleted.

11 changes: 8 additions & 3 deletions src/test/java/GeneralUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
}
}
100 changes: 100 additions & 0 deletions src/test/java/McModInfoProcessorTest.java
Original file line number Diff line number Diff line change
@@ -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<JavaFileObject> 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(" ","");
}
}
14 changes: 14 additions & 0 deletions src/test/resources/jsons/mcmod.info
Original file line number Diff line number Diff line change
@@ -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": []
}
8 changes: 7 additions & 1 deletion src/test/resources/test/HelloWorld.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test;

import pl.pabilo8.modworks.annotations.sound.ModSound;
import pl.pabilo8.modworks.annotations.MCModInfo;

/**
* @author Pabilo8
Expand All @@ -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
{

}
}

0 comments on commit 6998151

Please sign in to comment.