Detect unused icons

This commit is contained in:
ligi 2023-03-13 10:09:23 +01:00
parent 42b4bbf35e
commit a431abd60c
No known key found for this signature in database
GPG Key ID: 8E81894010ABF23D
2 changed files with 26 additions and 4 deletions

View File

@ -26,6 +26,7 @@ private val allChainFiles = allFiles.filter { !it.isDirectory }
private val allIconFilesList = iconsPath.listFiles() ?: error("${iconsPath.absolutePath} must contain the icon json files - but it does not")
private val allIconFiles = allIconFilesList.filter { !it.isDirectory }
private val allUsedIcons = mutableSetOf<String>()
fun main(args: Array<String>) {
@ -34,7 +35,7 @@ fun main(args: Array<String>) {
val verbose = argsList.contains("verbose").also { argsList.remove("verbose") }
if (argsList.firstOrNull() == "singleChainCheck") {
val file = File(File(".."), args.last())
if (file.exists() && file.parentFile == chainsPath ) {
if (file.exists() && file.parentFile == chainsPath) {
println("checking single chain " + args.last())
checkChain(file, true, verbose)
}
@ -129,13 +130,26 @@ private fun doChecks(doRPCConnect: Boolean, doIconDownload: Boolean, verbose: Bo
checkIcon(it, doIconDownload, allIconCIDs, verbose)
}
val unusedIconDownload = mutableSetOf<String>()
iconsDownloadPath.listFiles()?.forEach {
if (!allIconCIDs.contains(it.name)) throw UnreferencedIcon(it.name, iconsDownloadPath)
if (!allIconCIDs.contains(it.name)) unusedIconDownload.add(it.name)
}
if (unusedIconDownload.isNotEmpty()) {
throw UnreferencedIcon(unusedIconDownload.joinToString (" ") , iconsDownloadPath)
}
allFiles.filter { it.isDirectory }.forEach { _ ->
error("should not contain a directory")
}
val unusedIcons = mutableSetOf<String>()
iconsPath.listFiles().forEach {
if (!allUsedIcons.contains(it.name.toString().removeSuffix(".json"))) {
unusedIcons.add(it.toString())
}
}
if (unusedIcons.isNotEmpty()) {
error("error: unused icons ${unusedIcons.joinToString(" ")}")
}
}
fun checkIcon(icon: File, withIconDownload: Boolean, allIconCIDs: MutableSet<String>, verbose: Boolean) {
@ -255,9 +269,13 @@ fun checkChain(chainFile: File, connectRPC: Boolean, verbose: Boolean = false) {
}
jsonObject["icon"]?.let {
if (it !is String) {
error("icon must be string")
}
if (!File(iconsPath, "$it.json").exists()) {
error("The Icon $it does not exist - was used in ${chainFile.name}")
}
allUsedIcons.add(it)
}
val nameRegex = Regex("^[a-zA-Z0-9\\-\\.\\(\\) ]+$")
@ -312,6 +330,10 @@ fun checkChain(chainFile: File, connectRPC: Boolean, verbose: Boolean = false) {
throw (ExplorerMustHaveName())
}
if (explorer["icon"] != null) {
allUsedIcons.add(explorer["icon"].toString())
}
val url = explorer["url"]
if (url == null || url !is String || httpPrefixes.none { prefix -> url.startsWith(prefix) }) {
throw (ExplorerMustWithHttpsOrHttp())

View File

@ -53,4 +53,4 @@ class ChainNameMustBeString: Exception("Name must be string")
class IllegalName(type: String,name: String): Exception("Invalid $type: $name")
class UnreferencedIcon(fileName: String, iconsDownloadPath: File): Exception("Found file $fileName in $iconsDownloadPath that is not referenced")
class UnreferencedIcon(fileName: String, iconsDownloadPath: File): Exception("Found unreference $fileName in $iconsDownloadPath")