Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor getting the detekt version for readability #2387

Merged
merged 1 commit into from Mar 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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()
}