4
0
Fork 0
mirror of https://github.com/Anvilcraft/modpacktools synced 2024-05-19 20:04:07 +02:00

add listrelations and other small changes

This commit is contained in:
LordMZTE 2020-08-06 22:14:14 +02:00
parent d93359798e
commit 91e3d1360b
5 changed files with 118 additions and 27 deletions

View file

@ -52,6 +52,7 @@ dependencies {
//CLI
"net.sourceforge.argparse4j:argparse4j:0.8.1", //CLI argument parser
"com.github.ajalt:mordant:1.2.1", //CLI text formatting
"com.jakewharton.fliptables:fliptables:1.1.0", //Text Table
//Other
"org.slf4j:slf4j-simple:2.0.0-alpha1",

View file

@ -3,6 +3,7 @@
package ley.anvil.modpacktools
import com.github.ajalt.mordant.TermColors
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import ley.anvil.modpacktools.command.CommandLoader
import ley.anvil.modpacktools.command.ICommand
@ -22,7 +23,7 @@ import java.util.concurrent.TimeUnit.MICROSECONDS
val CONFIG by lazy {Config("modpacktoolsconfig.toml")}
val LOADER by lazy {CommandLoader("ley.anvil.modpacktools.commands")}
val MPJH by lazy {ModpackJsonHandler(File(CONFIG.config.pathOrException<String>("Locations/src"), "modpack.json"))}
val GSON by lazy {GsonBuilder().setPrettyPrinting().create()}
val GSON: Gson by lazy {GsonBuilder().setPrettyPrinting().create()}
//TODO thinks term has no color support on win with edited registry. probably no big deal
val TERMC by lazy {TermColors()}

View file

@ -2,8 +2,6 @@ package ley.anvil.modpacktools.commands
import j2html.TagCreator.*
import j2html.utils.CSSMin.compressCss
import ley.anvil.addonscript.wrapper.ASWrapper
import ley.anvil.addonscript.wrapper.ArtifactDestination
import ley.anvil.addonscript.wrapper.MetaData
import ley.anvil.modpacktools.MPJH
import ley.anvil.modpacktools.TERMC
@ -57,7 +55,7 @@ object CreateModlist : ICommand {
override fun execute(args: Namespace): CommandReturn {
val outFile = args.get<File>("file")
val all = args.get<Boolean>("all") ?: false
val all = args.getBoolean("all")
val sorting: Comparator<MetaData> = when(args.get<Sorting>("sorting")!!) {
Sorting.NAME -> comparing<MetaData, String> {it.name}
Sorting.DESCRIPTION -> comparing<MetaData, String> {it.description?.getOrNull(0) ?: ""}
@ -145,21 +143,7 @@ object CreateModlist : ICommand {
return success("Wrote HTML file")
}
private fun getMods(all: Boolean, sorting: Comparator<MetaData>): List<MetaData> {
println("Getting mods...")
val asJson = MPJH.asWrapper
val mods = mutableListOf<MetaData>()
val toGet = mutableListOf<ArtifactDestination>()
for(rel in asJson!!.defaultVersion.getRelations(arrayOf("included"), /*null means all*/ if(all) null else arrayOf("mod"))) {
if(rel.hasLocalMeta())
mods.add(rel.localMeta)
else if(rel.hasFile() && rel.file.isArtifact)
toGet.add(rel.file.artifact)
}
mods.addAll(ASWrapper.getMetaData(toGet.toTypedArray()).values)
return mods.sortedWith(sorting)
}
private fun getMods(all: Boolean, sorting: Comparator<MetaData>): List<MetaData> = MPJH.getModMetas(if(all) null else arrayOf("mod")).sortedWith(sorting)
enum class Format {
HTML, CSV

View file

@ -0,0 +1,86 @@
package ley.anvil.modpacktools.commands
import com.jakewharton.fliptables.FlipTable
import ley.anvil.modpacktools.MPJH
import ley.anvil.modpacktools.command.CommandReturn
import ley.anvil.modpacktools.command.CommandReturn.Companion.success
import ley.anvil.modpacktools.command.ICommand
import ley.anvil.modpacktools.command.LoadCommand
import net.sourceforge.argparse4j.ArgumentParsers
import net.sourceforge.argparse4j.impl.Arguments.storeTrue
import net.sourceforge.argparse4j.inf.ArgumentParser
import net.sourceforge.argparse4j.inf.Namespace
@LoadCommand
object ListRelations : ICommand {
override val name: String = "listrelations"
override val helpMessage: String = "Lists the relations of this mod pack"
override val parser: ArgumentParser = ArgumentParsers.newFor("ListRelations")
.build()
.description(helpMessage)
.apply {
addArgument("-c", "--csv")
.help("Doesn't format as a table but instead as csv (separated by ;)")
.action(storeTrue())
}.apply {
addArgument("-n", "--nolimit")
.help("does not limit the size of the authors list")
.action(storeTrue())
}.apply {
addArgument("-d", "--description")
.help("adds the description of relations to the list")
.action(storeTrue())
}
override fun execute(args: Namespace): CommandReturn {
val metas = MPJH.getModMetas().sortedBy {it.name}
if(args.getBoolean("csv")) {
metas.forEach {
println("${it.name};${it.contributors.keys.joinToString()};${it.website}".let {s ->
if(args.getBoolean("description"))
s + ";${it.description?.joinToString() ?: ""}"
else s
})
}
} else {
val data = mutableListOf<Array<out String>>()
metas.forEach {
data.add(run {
val row = mutableListOf(
it.name ?: "",
it.contributors.keys.joinToString().let {s ->
//Limit size
if(args.getBoolean("nolimit") || s.length < 50)
s
else
s.substring(0..47) + "..."
},
it.website ?: ""
)
if(args.getBoolean("description"))
row.add(it.description?.joinToString(" ") ?: "")
row.toTypedArray()
})
}
println(FlipTable.of(run {
val header = mutableListOf(
"Name",
"Contributors",
"Website"
)
if(args.getBoolean("description"))
header.add("Description")
header.toTypedArray()
},
data.toTypedArray()))
}
return success()
}
}

View file

@ -2,20 +2,39 @@ package ley.anvil.modpacktools.util
import ley.anvil.addonscript.v1.AddonscriptJSON
import ley.anvil.addonscript.wrapper.ASWrapper
import ley.anvil.addonscript.wrapper.ArtifactDestination
import ley.anvil.addonscript.wrapper.MetaData
import ley.anvil.modpacktools.MPJH
import java.io.File
import java.io.FileReader
class ModpackJsonHandler(val modpackJsonFile: File) {
//Null if no file exists
val asWrapper: ASWrapper? get() {
return if(modpackJsonFile.exists()) {
val reader = FileReader(modpackJsonFile)
val ret = ASWrapper(AddonscriptJSON.read(reader, AddonscriptJSON::class.java))
reader.close()
ret
} else null
val asWrapper: ASWrapper?
get() {
return if(modpackJsonFile.exists()) {
val reader = FileReader(modpackJsonFile)
val ret = ASWrapper(AddonscriptJSON.read(reader, AddonscriptJSON::class.java))
reader.close()
ret
} else null
}
fun getModMetas(types: Array<String>? = null): List<MetaData> {
val asJson = MPJH.asWrapper
val mods = mutableListOf<MetaData>()
val toGet = mutableListOf<ArtifactDestination>()
for(rel in asJson!!.defaultVersion.getRelations(arrayOf("included"), /*TODO TILERA MAKE THIS NONSESE TAKE A PREDICATE AND NOT A LIST*/types)) {
if(rel.hasLocalMeta())
mods.add(rel.localMeta)
else if(rel.hasFile() && rel.file.isArtifact)
toGet.add(rel.file.artifact)
}
mods.addAll(ASWrapper.getMetaData(toGet.toTypedArray()).values)
return mods
}
val json: AddonscriptJSON?
get() = asWrapper?.json
}
}