Skip to content

Commit

Permalink
Refactor getting the detekt version for readability (#2387)
Browse files Browse the repository at this point in the history
  • Loading branch information
arturbosch committed Mar 3, 2020
1 parent e896b00 commit feeb98b
Showing 1 changed file with 7 additions and 18 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? {
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 null
}
fun readVersion(resource: URL): String? = resource.openStream()
.use { Manifest(it).mainAttributes.getValue("DetektVersion") }

private fun readDetektVersionInManifest(resource: URL) =
resource.openStream().use {
Manifest(it).mainAttributes
.getValue("DetektVersion")
}
return Detektor::class.java.classLoader.getResources("META-INF/MANIFEST.MF")
.asSequence()
.mapNotNull { runCatching { readVersion(it) }.getOrNull() }
.firstOrNull()
}

0 comments on commit feeb98b

Please sign in to comment.