-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added mcmod.info and pack.mcmeta generation
Added tests
- Loading branch information
Showing
9 changed files
with
242 additions
and
13 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
src/main/java/pl/pabilo8/modworks/annotations/MCModInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/pl/pabilo8/modworks/annotations/model/GeneratedItemModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/pl/pabilo8/modworks/processors/MCModInfoProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(" ",""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters