|
| 1 | +package app.morphe.cli.command |
| 2 | + |
| 3 | +import app.morphe.cli.command.model.PatchBundle |
| 4 | +import app.morphe.cli.command.model.findMatchingBundle |
| 5 | +import app.morphe.cli.command.model.mergeWithBundle |
| 6 | +import app.morphe.cli.command.model.withUpdatedBundle |
| 7 | +import app.morphe.patcher.patch.loadPatchesFromJar |
| 8 | +import kotlinx.serialization.json.Json |
| 9 | +import picocli.CommandLine |
| 10 | +import picocli.CommandLine.Command |
| 11 | +import picocli.CommandLine.Model.CommandSpec |
| 12 | +import picocli.CommandLine.Spec |
| 13 | +import java.io.File |
| 14 | +import java.util.concurrent.Callable |
| 15 | +import java.util.logging.Logger |
| 16 | + |
| 17 | +@Command( |
| 18 | + name = "options-create", |
| 19 | + description = ["Create an options JSON file for the patches and options."], |
| 20 | +) |
| 21 | +internal object OptionsCommand : Callable<Int> { |
| 22 | + |
| 23 | + private const val EXIT_CODE_SUCCESS = 0 |
| 24 | + private const val EXIT_CODE_ERROR = 1 |
| 25 | + |
| 26 | + private val logger = Logger.getLogger(this::class.java.name) |
| 27 | + |
| 28 | + @Spec |
| 29 | + private lateinit var spec: CommandSpec |
| 30 | + |
| 31 | + @CommandLine.Option( |
| 32 | + names = ["-p", "--patches"], |
| 33 | + description = ["One or more paths to MPP files."], |
| 34 | + required = true, |
| 35 | + ) |
| 36 | + @Suppress("unused") |
| 37 | + private fun setPatchesFile(patchesFiles: Set<File>) { |
| 38 | + patchesFiles.firstOrNull { !it.exists() }?.let { |
| 39 | + throw CommandLine.ParameterException(spec.commandLine(), "${it.name} can't be found") |
| 40 | + } |
| 41 | + this.patchesFiles = patchesFiles |
| 42 | + } |
| 43 | + |
| 44 | + private var patchesFiles = emptySet<File>() |
| 45 | + |
| 46 | + @CommandLine.Option( |
| 47 | + names = ["-o", "--out"], |
| 48 | + description = ["Path to the output JSON file."], |
| 49 | + required = true, |
| 50 | + ) |
| 51 | + private lateinit var outputFile: File |
| 52 | + |
| 53 | + @CommandLine.Option( |
| 54 | + names = ["-f", "--filter-package-name"], |
| 55 | + description = ["Filter patches by compatible package name."], |
| 56 | + ) |
| 57 | + private var packageName: String? = null |
| 58 | + |
| 59 | + private val json = Json { prettyPrint = true } |
| 60 | + |
| 61 | + override fun call(): Int { |
| 62 | + return try { |
| 63 | + logger.info("Loading patches") |
| 64 | + |
| 65 | + val patches = loadPatchesFromJar(patchesFiles) |
| 66 | + |
| 67 | + val filtered = packageName?.let { pkg -> |
| 68 | + patches.filter { patch -> |
| 69 | + patch.compatiblePackages?.any { (name, _) -> name == pkg } ?: true |
| 70 | + }.toSet() |
| 71 | + } ?: patches |
| 72 | + |
| 73 | + // Read existing bundles list if the file already exists |
| 74 | + val existingBundles: List<PatchBundle>? = if (outputFile.exists()) { |
| 75 | + try { |
| 76 | + Json.decodeFromString<List<PatchBundle>>(outputFile.readText()) |
| 77 | + } catch (e: Exception) { |
| 78 | + logger.warning("Could not parse existing file, creating fresh: ${e.message}") |
| 79 | + null |
| 80 | + } |
| 81 | + } else null |
| 82 | + |
| 83 | + // Find the bundle matching the current .mpp file(s), merge with it (or create fresh) |
| 84 | + val existingBundle = existingBundles?.findMatchingBundle(patchesFiles) |
| 85 | + val updatedBundle = filtered.mergeWithBundle( |
| 86 | + existing = existingBundle, |
| 87 | + sourceFiles = patchesFiles, |
| 88 | + ) |
| 89 | + |
| 90 | + // Replace the matching entry in the list (or start a new list) |
| 91 | + val updatedBundles = existingBundles?.withUpdatedBundle(updatedBundle) |
| 92 | + ?: listOf(updatedBundle) |
| 93 | + |
| 94 | + outputFile.absoluteFile.parentFile?.mkdirs() |
| 95 | + outputFile.writeText(json.encodeToString(updatedBundles)) |
| 96 | + |
| 97 | + if (existingBundle != null) { |
| 98 | + val existingNames = existingBundle.patches.keys.map { it.lowercase() }.toSet() |
| 99 | + val newNames = updatedBundle.patches.keys.map { it.lowercase() }.toSet() |
| 100 | + val added = newNames - existingNames |
| 101 | + val removed = existingNames - newNames |
| 102 | + val kept = newNames.intersect(existingNames) |
| 103 | + logger.info("Updated bundle in options file at ${outputFile.path}") |
| 104 | + logger.info(" ${kept.size} patches preserved, ${added.size} added, ${removed.size} removed") |
| 105 | + } else { |
| 106 | + logger.info("Created new bundle in options file at ${outputFile.path} with ${updatedBundle.patches.size} patches") |
| 107 | + } |
| 108 | + |
| 109 | + EXIT_CODE_SUCCESS |
| 110 | + } catch (e: Exception) { |
| 111 | + logger.severe("Failed to export options: ${e.message}") |
| 112 | + EXIT_CODE_ERROR |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments