-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
423 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 11 additions & 28 deletions
39
plugin/src/main/kotlin/de/fayard/BuildSrcVersionsPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,34 @@ | ||
package de.fayard | ||
|
||
import com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask | ||
import com.github.benmanes.gradle.versions.updates.resolutionstrategy.ComponentSelectionWithCurrent | ||
import de.fayard.PluginConfig.isNonStable | ||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
import org.gradle.kotlin.dsl.create | ||
import org.gradle.kotlin.dsl.getByType | ||
|
||
open class BuildSrcVersionsPlugin : Plugin<Project> { | ||
|
||
override fun apply(project: Project) = project.run { | ||
|
||
configureBenManesVersions() | ||
|
||
extensions.create(BuildSrcVersionsExtension::class, PluginConfig.EXTENSION_NAME, BuildSrcVersionsExtensionImpl::class) | ||
val extension = extensions.create(BuildSrcVersionsExtension::class, PluginConfig.EXTENSION_NAME, BuildSrcVersionsExtensionImpl::class) | ||
(extension as BuildSrcVersionsExtensionImpl).upstream = configureBenManesVersions() | ||
extension.rejectVersionIf { | ||
isNonStable(candidate.version) | ||
} | ||
|
||
tasks.create("buildSrcVersions", BuildSrcVersionsTask::class) { | ||
group = "Help" | ||
description = "Update buildSrc/src/main/kotlin/{Versions.kt,Libs.kt}" | ||
dependsOn(":dependencyUpdates") | ||
outputs.upToDateWhen { false } | ||
} | ||
|
||
Unit | ||
} | ||
|
||
fun Project.configureBenManesVersions(): DependencyUpdatesTask { | ||
val rejectedKeywordsRegexps: List<Regex> by lazy { | ||
project.extensions.getByType<BuildSrcVersionsExtension>().rejectedVersionKeywords | ||
.map { qualifier -> Regex("(?i).*[.-]$qualifier[.\\d-]*") } | ||
fun Project.configureBenManesVersions(): DependencyUpdatesTask = | ||
tasks.maybeCreate("dependencyUpdates", DependencyUpdatesTask::class.java).also { task: DependencyUpdatesTask -> | ||
task.checkForGradleUpdate = true | ||
task.outputFormatter = "json" | ||
} | ||
|
||
val benManesVersions: DependencyUpdatesTask = | ||
tasks.maybeCreate("dependencyUpdates", DependencyUpdatesTask::class.java) | ||
|
||
benManesVersions.outputFormatter = "json" | ||
benManesVersions.checkForGradleUpdate = true | ||
benManesVersions.resolutionStrategy { | ||
|
||
componentSelection { | ||
all { | ||
if (rejectedKeywordsRegexps.any { it.matches(candidate.version) }) { | ||
reject("Release candidate") | ||
} | ||
} | ||
} | ||
|
||
} | ||
return benManesVersions | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package de.fayard | ||
|
||
import java.io.File | ||
|
||
data class Section(val name: String, val lines: MutableList<Pair<String, String>>) { | ||
operator fun get(key: String): String? { | ||
return lines.firstOrNull { it.first == key }?.second | ||
} | ||
} | ||
|
||
object EditorConfig { | ||
const val NAME = ".editorconfig" | ||
const val INDENT_STYLE = "indent_style" | ||
const val INDENT_SIZE = "indent_size" | ||
const val SPACE = "space" | ||
const val TAB = "tab" | ||
|
||
fun findIndentForKotlin(fromDir: File) : String? { | ||
return findEditorConfig(fromDir) | ||
.flatMap { file -> file.parseSections() } | ||
.findIndentForKotlinFiles() | ||
} | ||
|
||
fun List<Section>.findIndentForKotlinFiles() : String? { | ||
val section = this.sortedByDescending { it.priority() } | ||
.filter { s -> | ||
s[INDENT_STYLE] != null && s.priority() > 0 | ||
}.firstOrNull() | ||
return section?.findIndent() | ||
} | ||
|
||
fun Section.findIndent() : String? { | ||
val size = this[INDENT_SIZE]?.toIntOrNull() | ||
val style = this[INDENT_STYLE] | ||
return when { | ||
style == TAB -> "\t" | ||
style == SPACE && size != null -> List(size) { " " }.joinToString(separator = "") | ||
else -> null | ||
} | ||
} | ||
|
||
|
||
fun File.isRootEditorConfig() : Boolean = when { | ||
name != NAME -> false | ||
this.exists().not() -> false | ||
else -> readLines().any { it.replace(" ", "").contains("root=true") } | ||
} | ||
|
||
fun Section.priority() = when { | ||
name.contains("kt") -> 4 | ||
name.contains("gradle") -> 3 | ||
name.contains("java") -> 2 | ||
name == "*" -> 1 | ||
else -> -1 | ||
} | ||
|
||
fun File.parseSections(): List<Section> { | ||
assert(name == NAME) | ||
assert(canRead()) | ||
val result = mutableListOf<Section>() | ||
var currentSection: Section? = null | ||
readLines().map { line -> | ||
line.substringBefore("#").substringBefore(";") | ||
}.forEach { line -> | ||
val section = line.parseSection() | ||
val split = line.split("=").map { it.trim() } | ||
when { | ||
section != null -> { | ||
currentSection = Section(section, mutableListOf()) | ||
result.add(currentSection!!) | ||
} | ||
currentSection == null || line.isBlank() || split.size != 2 -> { } | ||
else -> currentSection!!.lines.add(split.first() to split.last()) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
fun String.parseSection() : String? { | ||
val section = this.substringAfter("[", "").substringBefore("]", "").trim() | ||
return if (section.isBlank()) null else section | ||
} | ||
|
||
fun findEditorConfig(fromDir: File) : List<File> { | ||
val result = mutableListOf<File>() | ||
var current: File? = fromDir | ||
while (current != null) { | ||
val ec = current.resolve(NAME) | ||
if (ec.exists()) { | ||
assert(ec.name == NAME) | ||
result += ec | ||
} | ||
if (ec.isRootEditorConfig()) { | ||
break | ||
} | ||
current = current.parentFile | ||
} | ||
return result | ||
} | ||
} |
Oops, something went wrong.