|
| 1 | +package net.thunderbird.cli.weblate |
| 2 | + |
| 3 | +import net.thunderbird.cli.weblate.api.ComponentConfig |
| 4 | + |
| 5 | +object ComponentConfigDiff { |
| 6 | + |
| 7 | + fun computeConfigDiff(expected: ComponentConfig, actual: ComponentConfig, indentLevel: Int = 0): List<String> { |
| 8 | + return fields.mapNotNull { it.diff(expected, actual, indentLevel) } |
| 9 | + } |
| 10 | + |
| 11 | + private fun <T> value( |
| 12 | + name: String, |
| 13 | + selector: (ComponentConfig) -> T, |
| 14 | + ): DiffField = ValueField(name, selector) |
| 15 | + |
| 16 | + private fun set( |
| 17 | + name: String, |
| 18 | + selector: (ComponentConfig) -> List<String>, |
| 19 | + ): DiffField = SetField(name, selector) |
| 20 | + |
| 21 | + private fun multiline( |
| 22 | + name: String, |
| 23 | + selector: (ComponentConfig) -> String, |
| 24 | + ): DiffField = MultilineField(name, selector) |
| 25 | + |
| 26 | + private val fields: List<DiffField> = listOf( |
| 27 | + value("license") { it.license }, |
| 28 | + value("license_url") { it.licenseUrl }, |
| 29 | + value("agreement") { it.agreement }, |
| 30 | + value("report_source_bugs") { it.reportSourceBugs }, |
| 31 | + value("priority") { it.priority }, |
| 32 | + value("is_glossary") { it.isGlossary }, |
| 33 | + value("glossary_color") { it.glossaryColor }, |
| 34 | + |
| 35 | + value("enable_suggestions") { it.enableSuggestions }, |
| 36 | + value("suggestion_voting") { it.suggestionVoting }, |
| 37 | + value("suggestion_autoaccept") { it.suggestionAutoaccept }, |
| 38 | + |
| 39 | + value("allow_translation_propagation") { it.allowTranslationPropagation }, |
| 40 | + |
| 41 | + value("check_flags") { it.checkFlags }, |
| 42 | + value("variant_regex") { it.variantRegex }, |
| 43 | + set("enforced_checks") { it.enforcedChecks }, |
| 44 | + value("secondary_language") { it.secondaryLanguage }, |
| 45 | + |
| 46 | + value("vcs") { it.vcs }, |
| 47 | + value("repo") { it.repo }, |
| 48 | + value("branch") { it.branch }, |
| 49 | + value("push") { it.push }, |
| 50 | + value("push_branch") { it.pushBranch }, |
| 51 | + value("repoweb") { it.repoweb }, |
| 52 | + value("commit_pending_age") { it.commitPendingAge }, |
| 53 | + value("merge_style") { it.mergeStyle }, |
| 54 | + value("auto_lock_error") { it.autoLockError }, |
| 55 | + |
| 56 | + multiline("commit_message") { it.commitMessage }, |
| 57 | + multiline("add_message") { it.addMessage }, |
| 58 | + multiline("delete_message") { it.deleteMessage }, |
| 59 | + multiline("merge_message") { it.mergeMessage }, |
| 60 | + multiline("addon_message") { it.addonMessage }, |
| 61 | + multiline("pull_message") { it.pullMessage }, |
| 62 | + |
| 63 | + value("language_regex") { it.languageRegex }, |
| 64 | + value("key_filter") { it.keyFilter }, |
| 65 | + |
| 66 | + value("file_format_params.xml_closing_tags") { it.fileFormatParams.xmlClosingTags }, |
| 67 | + |
| 68 | + value("edit_template") { it.editTemplate }, |
| 69 | + value("intermediate") { it.intermediate }, |
| 70 | + value("new_base") { it.newBase }, |
| 71 | + value("new_lang") { it.newLang }, |
| 72 | + value("language_code_style") { it.languageCodeStyle }, |
| 73 | + value("screenshot_filemask") { it.screenshotFilemask }, |
| 74 | + ) |
| 75 | +} |
| 76 | + |
| 77 | +private interface DiffField { |
| 78 | + fun diff(expected: ComponentConfig, actual: ComponentConfig, indentLevel: Int): String? |
| 79 | +} |
| 80 | + |
| 81 | +private class ValueField<T>( |
| 82 | + private val name: String, |
| 83 | + private val selector: (ComponentConfig) -> T, |
| 84 | +) : DiffField { |
| 85 | + override fun diff(expected: ComponentConfig, actual: ComponentConfig, indentLevel: Int): String? { |
| 86 | + val expectedValue = selector(expected) |
| 87 | + val actualValue = selector(actual) |
| 88 | + val indent = " ".repeat(indentLevel * 2) |
| 89 | + |
| 90 | + return if (expectedValue != actualValue) { |
| 91 | + "$indent$name: expected=$expectedValue, actual=$actualValue" |
| 92 | + } else { |
| 93 | + null |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +private class SetField( |
| 99 | + private val name: String, |
| 100 | + private val selector: (ComponentConfig) -> List<String>, |
| 101 | +) : DiffField { |
| 102 | + override fun diff(expected: ComponentConfig, actual: ComponentConfig, indentLevel: Int): String? { |
| 103 | + val expectedValue = selector(expected) |
| 104 | + val actualValue = selector(actual) |
| 105 | + |
| 106 | + return if (expectedValue.toSet() != actualValue.toSet()) { |
| 107 | + listDiff(name, expectedValue, actualValue, indentLevel) |
| 108 | + } else { |
| 109 | + null |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private fun listDiff(name: String, expected: List<String>, actual: List<String>, indentLevel: Int): String { |
| 114 | + val indent = " ".repeat(indentLevel * 2) |
| 115 | + val expectedSet = expected.toSet() |
| 116 | + val actualSet = actual.toSet() |
| 117 | + |
| 118 | + val missing = expected.filter { it !in actualSet } |
| 119 | + val unexpected = actual.filter { it !in expectedSet } |
| 120 | + |
| 121 | + val inner = buildString { |
| 122 | + if (missing.isNotEmpty()) { |
| 123 | + appendLine("missing:") |
| 124 | + missing.forEach { appendLine(" - $it") } |
| 125 | + } |
| 126 | + if (unexpected.isNotEmpty()) { |
| 127 | + appendLine("unexpected:") |
| 128 | + unexpected.forEach { appendLine(" + $it") } |
| 129 | + } |
| 130 | + }.trimEnd() |
| 131 | + |
| 132 | + return if (inner.isEmpty()) { |
| 133 | + "" |
| 134 | + } else { |
| 135 | + buildString { |
| 136 | + appendLine("$indent$name:") |
| 137 | + append(indentText(inner, indentLevel + 1)) |
| 138 | + }.trimEnd() |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +private class MultilineField( |
| 144 | + private val name: String, |
| 145 | + private val selector: (ComponentConfig) -> String, |
| 146 | +) : DiffField { |
| 147 | + override fun diff(expected: ComponentConfig, actual: ComponentConfig, indentLevel: Int): String? { |
| 148 | + val expectedValue = selector(expected) |
| 149 | + val actualValue = selector(actual) |
| 150 | + |
| 151 | + return if (expectedValue != actualValue) { |
| 152 | + multilineDiff(name, expectedValue, actualValue, indentLevel) |
| 153 | + } else { |
| 154 | + null |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private fun multilineDiff(name: String, expected: String, actual: String, indentLevel: Int): String { |
| 159 | + val indent = " ".repeat(indentLevel * 2) |
| 160 | + val expectedLines = expected.lines() |
| 161 | + val actualLines = actual.lines() |
| 162 | + val max = maxOf(expectedLines.size, actualLines.size) |
| 163 | + |
| 164 | + val inner = buildString { |
| 165 | + for (i in 0 until max) { |
| 166 | + val exp = expectedLines.getOrNull(i) |
| 167 | + val act = actualLines.getOrNull(i) |
| 168 | + if (exp != act) { |
| 169 | + val expText = exp ?: "<missing>" |
| 170 | + val actText = act ?: "<missing>" |
| 171 | + appendLine(" [${i + 1}] expected: $expText") |
| 172 | + appendLine(" [${i + 1}] actual : $actText") |
| 173 | + } |
| 174 | + } |
| 175 | + }.trimEnd() |
| 176 | + |
| 177 | + return if (inner.isEmpty()) { |
| 178 | + "" |
| 179 | + } else { |
| 180 | + buildString { |
| 181 | + appendLine("$indent$name:") |
| 182 | + append(indentText(inner, indentLevel + 1)) |
| 183 | + }.trimEnd() |
| 184 | + } |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +/** |
| 189 | + * Indent a multi-line string by a given indent level. Each level equals 2 spaces by default. |
| 190 | + */ |
| 191 | +private fun indentText(text: String, level: Int, spacesPerLevel: Int = 2): String = |
| 192 | + text.lines().joinToString("\n") { " ".repeat(level * spacesPerLevel) + it } |
0 commit comments