diff --git a/src/main/kotlin/ley/anvil/modpacktools/command/AbstractCommand.kt b/src/main/kotlin/ley/anvil/modpacktools/command/AbstractCommand.kt new file mode 100644 index 0000000..9d3ebed --- /dev/null +++ b/src/main/kotlin/ley/anvil/modpacktools/command/AbstractCommand.kt @@ -0,0 +1,35 @@ +package ley.anvil.modpacktools.command + +import net.sourceforge.argparse4j.ArgumentParsers +import net.sourceforge.argparse4j.inf.ArgumentParser + +/** + * Implement this for commands. it is meant to reduce boilerplate. + * + * @param displayName the name of this command to be displayed in the help message + * @param name the internal name of the command. will be the display name in lower case and with _ instead of spaces by default + */ +abstract class AbstractCommand + +@JvmOverloads +constructor( + val displayName: String, + override val name: String = displayName.toLowerCase().replace(' ', '_') +) : ICommand { + override val parser: ArgumentParser by lazy { + ArgumentParsers.newFor(displayName) + .build() + .description(helpMessage) + .apply { + addArgs() + } + } + + /** + * This will be called to add arguments to the arg parser of this command. + * override this to add arguments. + * + * @receiver the parser to add the args to + */ + open fun ArgumentParser.addArgs() {} +} diff --git a/src/main/kotlin/ley/anvil/modpacktools/commands/BuildTechnic.kt b/src/main/kotlin/ley/anvil/modpacktools/commands/BuildTechnic.kt index d5868a8..b087ac2 100644 --- a/src/main/kotlin/ley/anvil/modpacktools/commands/BuildTechnic.kt +++ b/src/main/kotlin/ley/anvil/modpacktools/commands/BuildTechnic.kt @@ -4,16 +4,14 @@ import ley.anvil.addonscript.wrapper.FileOrLink import ley.anvil.modpacktools.CONFIG import ley.anvil.modpacktools.MPJH import ley.anvil.modpacktools.TERMC +import ley.anvil.modpacktools.command.AbstractCommand import ley.anvil.modpacktools.command.CommandReturn import ley.anvil.modpacktools.command.CommandReturn.Companion.fail -import ley.anvil.modpacktools.command.ICommand import ley.anvil.modpacktools.command.LoadCommand import ley.anvil.modpacktools.util.addonscript.installFile import ley.anvil.modpacktools.util.downloadFiles import ley.anvil.modpacktools.util.fPrintln import ley.anvil.modpacktools.util.toZip -import net.sourceforge.argparse4j.ArgumentParsers -import net.sourceforge.argparse4j.inf.ArgumentParser import net.sourceforge.argparse4j.inf.Namespace import org.apache.commons.io.FileUtils import java.io.File @@ -22,14 +20,9 @@ import java.net.URL import java.util.zip.ZipOutputStream @LoadCommand -object BuildTechnic : ICommand { - override val name = "buildtechnic" +object BuildTechnic : AbstractCommand("BuildTechnic") { override val helpMessage = "Builds a technic export" - override val parser: ArgumentParser = ArgumentParsers.newFor("BuildTechnic") - .build() - .description(helpMessage) - private val tempDir by lazy {File(CONFIG.config.pathOrException("Locations/tempDir"))} private val tmp: File by lazy {File(tempDir, "technic")} private val download: File by lazy {File(tempDir, "download")} diff --git a/src/main/kotlin/ley/anvil/modpacktools/commands/BuildTwitch.kt b/src/main/kotlin/ley/anvil/modpacktools/commands/BuildTwitch.kt index 3b26cc8..143e6c5 100644 --- a/src/main/kotlin/ley/anvil/modpacktools/commands/BuildTwitch.kt +++ b/src/main/kotlin/ley/anvil/modpacktools/commands/BuildTwitch.kt @@ -3,17 +3,16 @@ package ley.anvil.modpacktools.commands import ley.anvil.modpacktools.CONFIG import ley.anvil.modpacktools.MPJH import ley.anvil.modpacktools.TERMC +import ley.anvil.modpacktools.command.AbstractCommand import ley.anvil.modpacktools.command.CommandReturn import ley.anvil.modpacktools.command.CommandReturn.Companion.fail import ley.anvil.modpacktools.command.CommandReturn.Companion.success -import ley.anvil.modpacktools.command.ICommand import ley.anvil.modpacktools.command.LoadCommand import ley.anvil.modpacktools.util.addonscript.installFile 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.ArgumentParsers import net.sourceforge.argparse4j.impl.Arguments.storeTrue import net.sourceforge.argparse4j.inf.ArgumentParser import net.sourceforge.argparse4j.inf.Namespace @@ -26,22 +25,19 @@ import java.net.URL import java.util.zip.ZipOutputStream @LoadCommand -object BuildTwitch : ICommand { - override val name: String = "buildtwitch" +object BuildTwitch : AbstractCommand("BuildTwitch") { override val helpMessage: String = "builds a twitch export" - override val parser: ArgumentParser = ArgumentParsers.newFor("BuildTwitch") - .build() - .description(helpMessage) - .apply { - addArgument("-a", "--all") - .help("Downloads all relations instead of just required ones") - .action(storeTrue()) - } private val tempDir by lazy {File(CONFIG.config.pathOrException("Locations/tempDir"))} private val tmp: File by lazy {File(tempDir, "twitch")} private val downloadDir by lazy {File(tempDir, "download")} + override fun ArgumentParser.addArgs() { + addArgument("-a", "--all") + .help("Downloads all relations instead of just required ones") + .action(storeTrue()) + } + override fun execute(args: Namespace): CommandReturn { val wr = MPJH.asWrapper!! val ml = convertAStoManifest(wr) {args.getBoolean("all") || "required" in it.options} diff --git a/src/main/kotlin/ley/anvil/modpacktools/commands/CreateModlist.kt b/src/main/kotlin/ley/anvil/modpacktools/commands/CreateModlist.kt index 67bd210..caee447 100644 --- a/src/main/kotlin/ley/anvil/modpacktools/commands/CreateModlist.kt +++ b/src/main/kotlin/ley/anvil/modpacktools/commands/CreateModlist.kt @@ -18,12 +18,11 @@ import j2html.utils.CSSMin.compressCss import ley.anvil.addonscript.wrapper.MetaData import ley.anvil.modpacktools.MPJH import ley.anvil.modpacktools.TERMC +import ley.anvil.modpacktools.command.AbstractCommand 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 ley.anvil.modpacktools.util.fPrintln -import net.sourceforge.argparse4j.ArgumentParsers import net.sourceforge.argparse4j.impl.Arguments.storeTrue import net.sourceforge.argparse4j.impl.type.CaseInsensitiveEnumNameArgumentType import net.sourceforge.argparse4j.impl.type.FileArgumentType @@ -38,31 +37,26 @@ import java.nio.charset.StandardCharsets import java.util.Comparator.comparing @LoadCommand -object CreateModlist : ICommand { - override val name: String = "createmodlist" +object CreateModlist : AbstractCommand("CreateModlist") { override val helpMessage: String = "This creates a modlist either as html or csv file." - override val parser: ArgumentParser = run { - val parser = ArgumentParsers.newFor("CreateModlist").build() - .description(helpMessage) - parser.addArgument("-s", "--sorting") + override fun ArgumentParser.addArgs() { + addArgument("-s", "--sorting") .type(CaseInsensitiveEnumNameArgumentType(Sorting::class.java)) .setDefault(Sorting.NAME) .help("Determines How mods should be sorted") - parser.addArgument("-a", "--all") + addArgument("-a", "--all") .action(storeTrue()) .help("If this is set, all relations and not only be mods will be in the list") - parser.addArgument("type") + addArgument("type") .type(CaseInsensitiveEnumNameArgumentType(Format::class.java)) .help("What format the mod list should be made in") - parser.addArgument("file") + addArgument("file") .type(FileArgumentType().verifyNotExists()) .help("What file the mod list should be written to") - - parser } override fun execute(args: Namespace): CommandReturn { @@ -107,7 +101,12 @@ object CreateModlist : ICommand { head( style( //Fancy css! - compressCss(IOUtils.toString(ClassLoader.getSystemResourceAsStream("commands/createmodlist/style.css"), StandardCharsets.UTF_8)) + compressCss( + IOUtils.toString( + ClassLoader.getSystemResourceAsStream("commands/createmodlist/style.css"), + StandardCharsets.UTF_8 + ) + ) ) ), body( @@ -164,7 +163,8 @@ object CreateModlist : ICommand { return success("Wrote HTML file") } - private fun getMods(all: Boolean, sorting: Comparator): List = MPJH.getModMetas(if(all) null else arrayOf("mod")).sortedWith(sorting) + private fun getMods(all: Boolean, sorting: Comparator): List = + MPJH.getModMetas(if(all) null else arrayOf("mod")).sortedWith(sorting) enum class Format { HTML, CSV diff --git a/src/main/kotlin/ley/anvil/modpacktools/commands/DownloadMods.kt b/src/main/kotlin/ley/anvil/modpacktools/commands/DownloadMods.kt index dea129a..217dc8f 100644 --- a/src/main/kotlin/ley/anvil/modpacktools/commands/DownloadMods.kt +++ b/src/main/kotlin/ley/anvil/modpacktools/commands/DownloadMods.kt @@ -2,14 +2,13 @@ package ley.anvil.modpacktools.commands import ley.anvil.addonscript.wrapper.FileOrLink import ley.anvil.modpacktools.MPJH +import ley.anvil.modpacktools.command.AbstractCommand 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 ley.anvil.modpacktools.util.DownloadFileTask import ley.anvil.modpacktools.util.downloadFiles import ley.anvil.modpacktools.util.fPrintln -import net.sourceforge.argparse4j.ArgumentParsers import net.sourceforge.argparse4j.impl.Arguments.storeTrue import net.sourceforge.argparse4j.impl.type.FileArgumentType import net.sourceforge.argparse4j.inf.ArgumentParser @@ -19,37 +18,31 @@ import java.net.URL import java.util.stream.Collectors.toMap @LoadCommand -object DownloadMods : ICommand { - override val name: String = "downloadmods" +object DownloadMods : AbstractCommand("DownloadMods") { override val helpMessage: String = "Downloads all mods." - override val parser: ArgumentParser = run { - val parser = ArgumentParsers.newFor("DownloadMods") - .build() - .description(helpMessage) - parser.addArgument("dir") + override fun ArgumentParser.addArgs() { + addArgument("dir") .type(FileArgumentType().verifyCanCreate()) .help("the directory to download the mods to") - parser.addArgument("-f", "--force") + addArgument("-f", "--force") .action(storeTrue()) .help("if true, mods that are already in the download folder will be downloaded again") - parser.addArgument("-a", "--all") + addArgument("-a", "--all") .action(storeTrue()) .help("Downloads not only mods but everything with a dir installer") - - parser } override fun execute(args: Namespace): CommandReturn { val json = MPJH.asWrapper val fileList = mutableListOf() for( - rel in json!!.defaultVersion!!.getRelations( - arrayOf("client"), - if(args.getBoolean("all")) null else arrayOf("mod") - )!! + rel in json!!.defaultVersion!!.getRelations( + arrayOf("client"), + if(args.getBoolean("all")) null else arrayOf("mod") + )!! ) //TODO only client? what if someone wants a server? if(rel.hasFile()) fileList.add(rel.file.get()) diff --git a/src/main/kotlin/ley/anvil/modpacktools/commands/Import.kt b/src/main/kotlin/ley/anvil/modpacktools/commands/Import.kt index f1446b4..a8dbf43 100644 --- a/src/main/kotlin/ley/anvil/modpacktools/commands/Import.kt +++ b/src/main/kotlin/ley/anvil/modpacktools/commands/Import.kt @@ -3,13 +3,12 @@ package ley.anvil.modpacktools.commands import ley.anvil.addonscript.curse.ManifestJSON import ley.anvil.modpacktools.GSON import ley.anvil.modpacktools.MPJH +import ley.anvil.modpacktools.command.AbstractCommand import ley.anvil.modpacktools.command.CommandReturn import ley.anvil.modpacktools.command.CommandReturn.Companion.fail import ley.anvil.modpacktools.command.CommandReturn.Companion.success -import ley.anvil.modpacktools.command.ICommand import ley.anvil.modpacktools.command.LoadCommand import ley.anvil.modpacktools.util.readAsJson -import net.sourceforge.argparse4j.ArgumentParsers import net.sourceforge.argparse4j.impl.type.FileArgumentType import net.sourceforge.argparse4j.inf.ArgumentParser import net.sourceforge.argparse4j.inf.Namespace @@ -17,16 +16,14 @@ import java.io.File import java.io.FileWriter @LoadCommand -object Import : ICommand { - override val name: String = "import" +object Import : AbstractCommand("Import") { override val helpMessage: String = "Converts a given manifest file to a modpackjson file" - override val parser: ArgumentParser = ArgumentParsers.newFor("Import") - .build() - .apply { - addArgument("manifest") - .help("the manifest file to import") - .type(FileArgumentType().verifyIsFile()) - } + + override fun ArgumentParser.addArgs() { + addArgument("manifest") + .help("the manifest file to import") + .type(FileArgumentType().verifyIsFile()) + } override val needsModpackjson: Boolean = false override val needsConfig: Boolean = false diff --git a/src/main/kotlin/ley/anvil/modpacktools/commands/Init.kt b/src/main/kotlin/ley/anvil/modpacktools/commands/Init.kt index 4b6da84..9278531 100644 --- a/src/main/kotlin/ley/anvil/modpacktools/commands/Init.kt +++ b/src/main/kotlin/ley/anvil/modpacktools/commands/Init.kt @@ -2,23 +2,17 @@ package ley.anvil.modpacktools.commands import ley.anvil.addonscript.v1.AddonscriptJSON import ley.anvil.modpacktools.CONFIG +import ley.anvil.modpacktools.command.AbstractCommand 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.inf.ArgumentParser import net.sourceforge.argparse4j.inf.Namespace import java.io.File import java.io.FileWriter @LoadCommand -object Init : ICommand { - override val name: String = "init" - override val helpMessage: String = "initializes the MPT dev environment (currently only creates config file)" - override val parser: ArgumentParser = ArgumentParsers.newFor("Init") - .build() - .description(helpMessage) +object Init : AbstractCommand("Init") { + override val helpMessage: String = "initializes the MPT dev environment" override val needsConfig: Boolean = false override val needsModpackjson: Boolean = false @@ -35,7 +29,7 @@ object Init : ICommand { val asJson = File(srcDir, "modpack.json") - if (!asJson.exists()) { + if(!asJson.exists()) { //create new file val writer = FileWriter(asJson) val addsc = AddonscriptJSON.create() diff --git a/src/main/kotlin/ley/anvil/modpacktools/commands/ListRelations.kt b/src/main/kotlin/ley/anvil/modpacktools/commands/ListRelations.kt index 9c95c7d..5c6aa12 100644 --- a/src/main/kotlin/ley/anvil/modpacktools/commands/ListRelations.kt +++ b/src/main/kotlin/ley/anvil/modpacktools/commands/ListRelations.kt @@ -2,35 +2,31 @@ package ley.anvil.modpacktools.commands import com.jakewharton.fliptables.FlipTable import ley.anvil.modpacktools.MPJH +import ley.anvil.modpacktools.command.AbstractCommand 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" +object ListRelations : AbstractCommand("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 ArgumentParser.addArgs() { + addArgument("-c", "--csv") + .help("Doesn't format as a table but instead as csv (separated by ;)") + .action(storeTrue()) + + addArgument("-n", "--nolimit") + .help("does not limit the size of the authors list") + .action(storeTrue()) + + 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} diff --git a/src/main/kotlin/ley/anvil/modpacktools/commands/Shell.kt b/src/main/kotlin/ley/anvil/modpacktools/commands/Shell.kt index 8113ad4..861a46b 100644 --- a/src/main/kotlin/ley/anvil/modpacktools/commands/Shell.kt +++ b/src/main/kotlin/ley/anvil/modpacktools/commands/Shell.kt @@ -1,23 +1,17 @@ package ley.anvil.modpacktools.commands import ley.anvil.modpacktools.TERMC +import ley.anvil.modpacktools.command.AbstractCommand 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 ley.anvil.modpacktools.runCommand import ley.anvil.modpacktools.util.fPrint -import net.sourceforge.argparse4j.ArgumentParsers -import net.sourceforge.argparse4j.inf.ArgumentParser import net.sourceforge.argparse4j.inf.Namespace @LoadCommand -object Shell : ICommand { - override val name: String = "shell" +object Shell : AbstractCommand("Shell") { override val helpMessage: String = "opens a shell where mpt commands can be entered in a loop." - override val parser: ArgumentParser = ArgumentParsers.newFor("Shell") - .build() - .description(helpMessage) override val needsConfig: Boolean = false override val needsModpackjson: Boolean = false