From 4944f7c9343bbd0b20828446c00265df3b2baf96 Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Tue, 18 Aug 2020 21:46:21 +0200 Subject: [PATCH] add BuilderContainerTagTest.kt move convertAStoManifest into ASUtil.kt convertAStoManifest will throw exception if forge version is invalid add out to gitignore --- .gitignore | 3 +- .../modpacktools/commands/BuildTwitch.kt | 2 +- .../modpacktools/util/addonscript/ASUtil.kt | 82 +++++++++++++++++++ .../modpacktools/util/manifest/ASConverter.kt | 79 ------------------ .../util/BuilderContainerTagTest.kt | 19 +++++ 5 files changed, 104 insertions(+), 81 deletions(-) delete mode 100644 src/main/kotlin/ley/anvil/modpacktools/util/manifest/ASConverter.kt create mode 100644 src/test/kotlin/ley/anvil/modpacktools/util/BuilderContainerTagTest.kt diff --git a/.gitignore b/.gitignore index 4d8a649..a116660 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ +gradle.properties .gradle .idea build -gradle.properties +out diff --git a/src/main/kotlin/ley/anvil/modpacktools/commands/BuildTwitch.kt b/src/main/kotlin/ley/anvil/modpacktools/commands/BuildTwitch.kt index 6c6ea40..57ce6e7 100644 --- a/src/main/kotlin/ley/anvil/modpacktools/commands/BuildTwitch.kt +++ b/src/main/kotlin/ley/anvil/modpacktools/commands/BuildTwitch.kt @@ -9,11 +9,11 @@ import ley.anvil.modpacktools.command.CommandReturn.Companion.fail import ley.anvil.modpacktools.command.CommandReturn.Companion.success import ley.anvil.modpacktools.command.LoadCommand import ley.anvil.modpacktools.util.FileToDownload +import ley.anvil.modpacktools.util.addonscript.convertAStoManifest import ley.anvil.modpacktools.util.addonscript.installFile import ley.anvil.modpacktools.util.arg import ley.anvil.modpacktools.util.downloadFiles import ley.anvil.modpacktools.util.fPrintln -import ley.anvil.modpacktools.util.manifest.convertAStoManifest import ley.anvil.modpacktools.util.toZip import net.sourceforge.argparse4j.impl.Arguments.storeTrue import net.sourceforge.argparse4j.inf.ArgumentParser diff --git a/src/main/kotlin/ley/anvil/modpacktools/util/addonscript/ASUtil.kt b/src/main/kotlin/ley/anvil/modpacktools/util/addonscript/ASUtil.kt index 9347b73..9b228fb 100644 --- a/src/main/kotlin/ley/anvil/modpacktools/util/addonscript/ASUtil.kt +++ b/src/main/kotlin/ley/anvil/modpacktools/util/addonscript/ASUtil.kt @@ -1,7 +1,10 @@ package ley.anvil.modpacktools.util.addonscript +import ley.anvil.addonscript.curse.ManifestJSON +import ley.anvil.addonscript.wrapper.ASWrapper import ley.anvil.modpacktools.TERMC import ley.anvil.modpacktools.util.fPrintln +import ley.anvil.modpacktools.util.manifest.ManifestLinksPair import org.apache.commons.io.FileUtils import java.io.File import java.io.PrintStream @@ -68,3 +71,82 @@ fun installFile(installer: String, file: File, outDir: File): InstallFileSuccess } return InstallFileSuccess(true, "installed $file") } + +/** + * Converts a AS file to a twitch manifest + * + * @param addonscript the file to convert + * @param shouldAddLink if a link for a given relation should be added, does not get called if the relations is a curseforge artifact + */ +@JvmOverloads +fun convertAStoManifest( + addonscript: ASWrapper, + shouldAddLink: (ASWrapper.RelationWrapper) -> Boolean = {true} +): ManifestLinksPair { + val ml = ManifestLinksPair() + val ver = addonscript.defaultVersion + val manifest = ManifestJSON() + + val mcv = ver.version!!.mcversion[0] + manifest.minecraft = ManifestJSON.Minecraft() + manifest.minecraft.version = mcv + manifest.minecraft.modLoaders = mutableListOf() + manifest.files = mutableListOf() + + manifest.manifestType = "minecraftModpack" + manifest.manifestVersion = 1 + manifest.name = addonscript.json.meta!!.name ?: addonscript.json.id + manifest.version = ver.versionName + manifest.author = addonscript.json!!.meta!!.contributors[0].name + + for(rel in ver.getRelations(arrayOf("client"), null)) { + if(rel.isModloader) { + if(rel.relation.id == "forge") { + val forge = ManifestJSON.Modloader() + forge.primary = true + forge.id = "forge-${rel.versions.latestKnown?.split('-')?.getOrNull(1) + ?: throw IllegalArgumentException("Forge version format is invalid.")}" + manifest.minecraft.modLoaders.add(forge) + } else { + println("Curse only allows Forge as a modloader. ${rel.relation.id} is not allowed!") + } + } else if(rel.hasFile()) { + val file = rel.file + //TODO deduplicate this + if(file.file.installer == "internal.jar") { + println("internal.jar is not supported on Curse") + continue + } else if(file.isArtifact && file.artifact.isCurseforge) { + val art = file.artifact + val f = ManifestJSON.File() + f.fileID = art.fileID + f.projectID = art.projectID + f.required = "required" in rel.options + manifest.files.add(f) + } else if(shouldAddLink(rel)) { + ml.links[file.get()] = file.file.installer + } + } + } + + for(file in ver.getFiles(arrayOf("client", "required"), null)) { + if(file.file.installer == "internal.jar") { + println("internal.jar is not supported on Curse") + continue + } else if(file.isArtifact) { + val art = file.artifact + if(art.isCurseforge) { + val f = ManifestJSON.File() + f.fileID = art.fileID + f.projectID = art.projectID + f.required = true + manifest.files.add(f) + } + } else { + ml.links[file.get()] = file.file.installer + } + } + ml.manifest = manifest + + return ml +} diff --git a/src/main/kotlin/ley/anvil/modpacktools/util/manifest/ASConverter.kt b/src/main/kotlin/ley/anvil/modpacktools/util/manifest/ASConverter.kt deleted file mode 100644 index b4d5443..0000000 --- a/src/main/kotlin/ley/anvil/modpacktools/util/manifest/ASConverter.kt +++ /dev/null @@ -1,79 +0,0 @@ -package ley.anvil.modpacktools.util.manifest - -import ley.anvil.addonscript.curse.ManifestJSON -import ley.anvil.addonscript.wrapper.ASWrapper - -/** - * Converts a AS file to a twitch manifest - * - * @param addonscript the file to convert - * @param shouldAddLink if a link for a given relation should be added, does not get called if the relations is a curseforge artifact - */ -@JvmOverloads -fun convertAStoManifest(addonscript: ASWrapper, shouldAddLink: (ASWrapper.RelationWrapper) -> Boolean = {true}): ManifestLinksPair { - val ml = ManifestLinksPair() - val ver = addonscript.defaultVersion - val manifest = ManifestJSON() - - val mcv = ver.version!!.mcversion[0] - manifest.minecraft = ManifestJSON.Minecraft() - manifest.minecraft.version = mcv - manifest.minecraft.modLoaders = mutableListOf() - manifest.files = mutableListOf() - - manifest.manifestType = "minecraftModpack" - manifest.manifestVersion = 1 - manifest.name = addonscript.json.meta!!.name ?: addonscript.json.id - manifest.version = ver.versionName - manifest.author = addonscript.json!!.meta!!.contributors[0].name - - for(rel in ver.getRelations(arrayOf("client"), null)) { - if(rel.isModloader) { - if(rel.relation.id == "forge") { - val forge = ManifestJSON.Modloader() - forge.primary = true - forge.id = "forge-${rel.versions.latestKnown?.split('-')?.get(1)}" - manifest.minecraft.modLoaders.add(forge) - } else { - println("Curse only allows Forge as a modloader. ${rel.relation.id} is not allowed!") - } - } else if(rel.hasFile()) { - val file = rel.file - //TODO deduplicate this - if(file.file.installer == "internal.jar") { - println("internal.jar is not supported on Curse") - continue - } else if(file.isArtifact && file.artifact.isCurseforge) { - val art = file.artifact - val f = ManifestJSON.File() - f.fileID = art.fileID - f.projectID = art.projectID - f.required = "required" in rel.options - manifest.files.add(f) - } else if(shouldAddLink(rel)) { - ml.links[file.get()] = file.file.installer - } - } - } - - for(file in ver.getFiles(arrayOf("client", "required"), null)) { - if(file.file.installer == "internal.jar") { - println("internal.jar is not supported on Curse") - continue - } else if(file.isArtifact) { - val art = file.artifact - if(art.isCurseforge) { - val f = ManifestJSON.File() - f.fileID = art.fileID - f.projectID = art.projectID - f.required = true - manifest.files.add(f) - } - } else { - ml.links[file.get()] = file.file.installer - } - } - ml.manifest = manifest - - return ml -} diff --git a/src/test/kotlin/ley/anvil/modpacktools/util/BuilderContainerTagTest.kt b/src/test/kotlin/ley/anvil/modpacktools/util/BuilderContainerTagTest.kt new file mode 100644 index 0000000..34b51d7 --- /dev/null +++ b/src/test/kotlin/ley/anvil/modpacktools/util/BuilderContainerTagTest.kt @@ -0,0 +1,19 @@ +package ley.anvil.modpacktools.util + +import ley.anvil.modpacktools.util.BuilderContainerTag.Companion.html +import org.junit.Assert.assertEquals +import org.junit.Test + +class BuilderContainerTagTest { + @Test + fun htmlBuilder() { + assertEquals( + "

Hello

", + html { + "body" { + "h1"("Hello") + } + }.render() + ) + } +}