Skip to content

Commit

Permalink
Refactor getting the detekt version for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
arturbosch committed Mar 2, 2020
1 parent 07a83d3 commit 7ac5a2f
Showing 1 changed file with 4 additions and 15 deletions.
@@ -1,6 +1,5 @@
package io.gitlab.arturbosch.detekt.core

import java.io.IOException
import java.net.URL
import java.util.jar.Manifest

Expand All @@ -9,21 +8,11 @@ fun whichOS(): String = System.getProperty("os.name")
fun whichJava(): String = System.getProperty("java.runtime.version")

fun whichDetekt(): String? {
fun readVersion(resource: URL) = resource.openStream()
.use { Manifest(it).mainAttributes.getValue("DetektVersion") }

for (resource in Detektor::class.java.classLoader.getResources("META-INF/MANIFEST.MF")) {
try {
val version = readDetektVersionInManifest(resource)
if (version != null) {
return version
}
} catch (_: IOException) {
// we search for the manifest with the detekt version
}
return runCatching { readVersion(resource) }.getOrNull() ?: continue
}
return null
}

private fun readDetektVersionInManifest(resource: URL) =
resource.openStream().use {
Manifest(it).mainAttributes
.getValue("DetektVersion")
}

0 comments on commit 7ac5a2f

Please sign in to comment.