From dbc0892753aed75bd4c489dd9da2b91f845f41c8 Mon Sep 17 00:00:00 2001 From: LordMZTE Date: Wed, 29 Jul 2020 22:38:28 +0200 Subject: [PATCH] CreateModlist will now accept an "all" option to not only add mods to the list CreateModlist will now use not only client relations if no link is specified, there will be no href in the mod list --- .../modpacktools/commands/CreateModlist.kt | 42 ++++++++++++------- .../anvil/modpacktools/util/FileDownloader.kt | 2 +- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/ley/anvil/modpacktools/commands/CreateModlist.kt b/src/main/kotlin/ley/anvil/modpacktools/commands/CreateModlist.kt index 2445253..36f5fb5 100644 --- a/src/main/kotlin/ley/anvil/modpacktools/commands/CreateModlist.kt +++ b/src/main/kotlin/ley/anvil/modpacktools/commands/CreateModlist.kt @@ -21,7 +21,7 @@ import java.nio.charset.StandardCharsets @LoadCommand object CreateModlist : ICommand { override val name: String = "createmodlist" - override val helpMessage: String = "This creates a modlist either as html or csv file. Syntax: outFile" + override val helpMessage: String = "This creates a modlist either as html or csv file. if the \'all\' option is supplied, not only mods will be included Syntax: [all]" override fun execute(args: Array): CommandReturn { if(!args.checkArgs()) @@ -32,20 +32,21 @@ object CreateModlist : ICommand { if(outFile.exists()) return fail("File already exists!") + val all = args.getOrNull(3) == "all" return if(args[1] == "html") - doHtml(outFile) + doHtml(outFile, all) else - doCsv(outFile) + doCsv(outFile, all) } - private fun doCsv(outFile: File): CommandReturn { + private fun doCsv(outFile: File, all: Boolean): CommandReturn { println("Making CSV file $outFile") val printer = CSVPrinter(FileWriter(outFile), CSVFormat.EXCEL.withDelimiter(';')) printer.printRecord("Name", "Contributors", "Link") printer.println() - for(mod in getMods()) { + for(mod in getMods(all)) { printer.printRecord( mod.name, mod.contributors.keys.joinToString(), @@ -57,7 +58,7 @@ object CreateModlist : ICommand { return success("Wrote CSV file") } - private fun doHtml(outFile: File): CommandReturn { + private fun doHtml(outFile: File, all: Boolean): CommandReturn { println("Making HTML file $outFile") val writer = FileWriter(outFile) val html = html( @@ -75,18 +76,22 @@ object CreateModlist : ICommand { td(b("Contributors")), td(b("Description")) ), - each(getMods()) { + each(getMods(all)) { tr( td(if(it.icon != null) a( img().withSrc(it.icon) .withClass("img") ).withHref(it.website) else null ), - td(a(it.name) - .withHref(it.website) - //Open in new tab - .withRel("noopener noreferrer") - .withTarget("_blank")), + td(run { + val a = a(it.name) + //Open in new tab + .withRel("noopener noreferrer") + .withTarget("_blank") + if(it.website != null) + a.withHref(it.website) + a + }), td(ul( each(it.contributors) {contr -> li(contr.key) @@ -106,13 +111,13 @@ object CreateModlist : ICommand { return success("Wrote HTML file") } - private fun getMods(): List { + private fun getMods(all: Boolean): List { println("Getting mods...") val asJson = MPJH.asWrapper val mods = mutableListOf() val toGet = mutableListOf() - for(rel in asJson!!.defaultVersion.getRelations(arrayOf("client"), arrayOf("mod", "modloader"))) { + 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) @@ -122,6 +127,11 @@ object CreateModlist : ICommand { return mods.sortedBy {m -> m.name?.toLowerCase()} } - private fun Array.checkArgs(): Boolean = this.size >= 3 && this[1] in arrayOf("html", "csv") - + private fun Array.checkArgs(): Boolean = + //must be right length + this.size >= 3 && + //second option must be html or csv + this[1] in arrayOf("html", "csv") && + //3rd option must either be "all" or nothing + this.getOrElse(3) {"all"} == "all" } \ No newline at end of file diff --git a/src/main/kotlin/ley/anvil/modpacktools/util/FileDownloader.kt b/src/main/kotlin/ley/anvil/modpacktools/util/FileDownloader.kt index 8e14901..4f948bf 100644 --- a/src/main/kotlin/ley/anvil/modpacktools/util/FileDownloader.kt +++ b/src/main/kotlin/ley/anvil/modpacktools/util/FileDownloader.kt @@ -87,7 +87,7 @@ open class FileDownloader( val responseCode: Int?, val responseMessage: String?, val exception: Exception? - ) {} + ) } enum class ExistingFileBehaviour {