Skip to content

Commit

Permalink
Merge pull request #81 from jmfayard/buildSrcVersions-80
Browse files Browse the repository at this point in the history
#80 Refine naming convention
  • Loading branch information
Jean-Michel Fayard authored Sep 18, 2019
2 parents 84414da + 8921d1e commit e117dc0
Show file tree
Hide file tree
Showing 26 changed files with 143 additions and 104 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Unreleased

# 0.6.1


- #80 Refine naming convention
- Add plugin version in the output
- Restore indent=FOUR_SPACES when no editorconfig is there
- Add "#" at the beginning of a line in "gradle.properties" so that IntelliJ does not refactor/break the alignment

# 0.6.0

Major new feature:
Expand Down
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//include::includes.adoc[]
:plugin_version: 0.6.0
:plugin_version: 0.6.1
:repo: jmfayard/buildSrcVersions
:branch: 26-buildSrcVersions
:github: https://github.com/{repo}
Expand Down
4 changes: 2 additions & 2 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("de.fayard.buildSrcVersions") version ("0.6.0") // plugin.de.fayard.buildSrcVersions
id("de.fayard.buildSrcVersions") version ("0.6.1") // plugin.de.fayard.buildSrcVersions

id("com.gradle.plugin-publish")
`java-gradle-plugin`
Expand All @@ -11,7 +11,7 @@ plugins {
}


version = "0.6.1" // plugin.de.fayard.buildSrcVersions
version = "0.6.2" // plugin.de.fayard.buildSrcVersions
group = "de.fayard"


Expand Down
4 changes: 2 additions & 2 deletions plugin/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
plugin.org.gradle.kotlin.kotlin.dsl=1.2.9
# available=1.3.1
plugin.de.fayard.buildsrcversions=0.6.1
plugin.com.gradle.plugin.publish=0.10.0
plugin.com.gradle.plugin-publish=0.10.0
# available=0.10.1
plugin.com.gradle.build.scan=2.4.1
plugin.com.gradle.build-scan=2.4.1
# available=2.4.2
version.kotlintest.runner.junit5=3.1.9
# available=3.4.1
Expand Down
2 changes: 1 addition & 1 deletion plugin/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pluginManagement {
val resolutionStrategyConfig: String? by extra
resolutionStrategy.eachPlugin {
val property = "plugin.${requested.id.id}".replace("-", ".")
val property = "plugin.${requested.id.id}"
if (extra.has(property) && resolutionStrategyConfig != "false") {
val version = extra.get(property) as String
useVersion(version)
Expand Down
2 changes: 1 addition & 1 deletion plugin/src/main/kotlin/de/fayard/BuildSrcVersionsPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fun Project.useVersionsFromGradleProperties() {
val gradleProperty = PluginConfig.considerGradleProperties(candidate.group, candidate.name)
.firstOrNull { it in properties } ?: return@eachDependency
val message =
"ResolutionStrategy for configuration=$configurationName selected version=${properties[gradleProperty]} from property=$gradleProperty with for dependency=${candidate.group}:${candidate.name}"
"ResolutionStrategy selected version=${properties[gradleProperty]} from property=$gradleProperty with for dependency=${candidate.group}:${candidate.name}"
if (resolutionStrategyConfig == "verbose") println(message)
useVersion(properties[gradleProperty] ?: error(message))
}
Expand Down
15 changes: 8 additions & 7 deletions plugin/src/main/kotlin/de/fayard/BuildSrcVersionsTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ open class BuildSrcVersionsTask : DefaultTask() {
KOTLIN_OBJECT -> false
else -> return
}
val versions = unsortedParsedDependencies.sortedBeautifullyBy { it.versionName }

val kotlinPoetry: KotlinPoetry = kotlinpoet(parsedDependencies, dependencyGraph.gradle, extension)
val kotlinPoetry: KotlinPoetry = kotlinpoet(versions, dependencyGraph.gradle, extension)

if (shouldGenerateLibsKt) {
kotlinPoetry.Libs.writeTo(outputDir)
Expand All @@ -90,16 +91,16 @@ open class BuildSrcVersionsTask : DefaultTask() {
@TaskAction
fun versionsOnlyMode() {
val extension: BuildSrcVersionsExtensionImpl = extension()
val updateGradleProperties = UpdateGradleProperties(extension, parsedDependencies)
val updateGradleProperties = UpdateGradleProperties(extension)

val versionsOnlyMode = when(val mode = extension.versionsOnlyMode) {
null, KOTLIN_OBJECT -> return
else -> mode
}

val dependencies = (parsedDependencies + PluginConfig.gradleVersionsPlugin + PluginConfig.gradleLatestVersion(dependencyGraph))
.sortedBeautifullyBy { it.versionName }
.distinctBy { it.versionName }
val dependencies = (unsortedParsedDependencies + PluginConfig.gradleVersionsPlugin + PluginConfig.gradleLatestVersion(dependencyGraph))
.sortedBeautifullyBy { it.versionProperty }
.distinctBy { it.versionProperty }

if (versionsOnlyMode == GRADLE_PROPERTIES) {
updateGradleProperties.generateVersionProperties(project, dependencies)
Expand Down Expand Up @@ -132,7 +133,7 @@ open class BuildSrcVersionsTask : DefaultTask() {
return@lazy PluginConfig.readGraphFromJsonFile(jsonInput)
}

private val parsedDependencies: List<Dependency> by lazy {
private val unsortedParsedDependencies: List<Dependency> by lazy {
val useFdqnByDefault = extension().useFqqnFor.map { PluginConfig.escapeVersionsKt(it) }
parseGraph(dependencyGraph, useFdqnByDefault + PluginConfig.MEANING_LESS_NAMES)
.map { d -> d.maybeUpdate(update) }
Expand All @@ -148,7 +149,7 @@ open class BuildSrcVersionsTask : DefaultTask() {

private fun extension(): BuildSrcVersionsExtensionImpl {
val extension: BuildSrcVersionsExtensionImpl = _extension
if (extension.indent == PluginConfig.DEFAULT_INDENT) {
if (extension.indent == PluginConfig.INDENT_FROM_EDITOR_CONFIG) {
val findIndentForKotlin = EditorConfig.findIndentForKotlin(project.file("buildSrc/src/main/kotlin"))
extension.indent = findIndentForKotlin ?: PluginConfig.DEFAULT_INDENT
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import de.fayard.VersionsOnlyMode
internal open class BuildSrcVersionsExtensionImpl(
override var renameLibs: String = PluginConfig.DEFAULT_LIBS,
override var renameVersions: String = PluginConfig.DEFAULT_VERSIONS,
override var indent: String = PluginConfig.DEFAULT_INDENT,
override var indent: String = PluginConfig.INDENT_FROM_EDITOR_CONFIG,
override var versionsOnlyMode: VersionsOnlyMode? = null,
override var versionsOnlyFile: String? = null
) : BuildSrcVersionsExtension, java.io.Serializable {
Expand Down
14 changes: 12 additions & 2 deletions plugin/src/main/kotlin/de/fayard/internal/DependencyGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,15 @@ data class Dependency(
val projectUrl: String? = "",
val name: String = "",
var escapedName: String = "",
var versionName: String = "",
var mode: VersionMode = VersionMode.MODULE,
val available: AvailableDependency? = null
) {
val versionName: String
get() = PluginConfig.versionKtFor(this)

val versionProperty: String
get() = PluginConfig.versionPropertyFor(this)

fun maybeUpdate(update: Boolean): Dependency {
val newerVersion = newerVersion()
return when {
Expand All @@ -46,6 +52,10 @@ data class Dependency(
}
}

enum class VersionMode {
MODULE, GROUP, GROUP_MODULE
}

data class GradleConfig(
val current: GradleVersion,
val nightly: GradleVersion,
Expand Down Expand Up @@ -76,7 +86,7 @@ data class SingleModeResult(

companion object {
val NEW_FILE = SingleModeResult(0, 0, "")
val BLOC_NOT_FOUND = SingleModeResult(-1, -1, PluginConfig.DEFAULT_INDENT)
val BLOC_NOT_FOUND = SingleModeResult(-1, -1, PluginConfig.INDENT_FROM_EDITOR_CONFIG)
}
}

50 changes: 24 additions & 26 deletions plugin/src/main/kotlin/de/fayard/internal/KotlinPoetry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.gradle.plugin.use.PluginDependencySpec

fun kotlinpoet(versions: List<Dependency>, gradleConfig: GradleConfig, extension: BuildSrcVersionsExtension): KotlinPoetry {


val gradleVersion = constStringProperty(
PluginConfig.GRADLE_LATEST_VERSION,
gradleConfig.current.version,
Expand Down Expand Up @@ -130,45 +131,42 @@ fun parseGraph(
): List<Dependency> {

val dependencies: List<Dependency> = graph.current + graph.exceeded + graph.outdated + graph.unresolved
return dependencies.checkModeAndNames(useFdqnByDefault).findCommonVersions()
}

val map = mutableMapOf<String, Dependency>()
for (d: Dependency in dependencies) {
val key = PluginConfig.escapeVersionsKt(d.name)
val fdqnName = d.fqdnName()


if (key in useFdqnByDefault) {
d.escapedName = fdqnName
} else if (map.containsKey(key)) {
d.escapedName = fdqnName

// also use FDQN for the dependency that conflicts with this one
val other = map[key]!!
other.escapedName = other.fqdnName()
} else {
map[key] = d
d.escapedName = key
fun List<Dependency>.checkModeAndNames(useFdqnByDefault: List<String>): List<Dependency> {
groupBy { d -> d.name }
.forEach { (name, list) ->
for (d: Dependency in list) {
d.mode = when {
d.name in useFdqnByDefault -> VersionMode.GROUP_MODULE
list.size >= 2 -> VersionMode.GROUP_MODULE
else -> VersionMode.MODULE
}
d.escapedName = PluginConfig.escapeVersionsKt(
when (d.mode) {
VersionMode.MODULE -> d.name
VersionMode.GROUP -> d.group
VersionMode.GROUP_MODULE -> "${d.group}_${d.name}"
}
)
}
}
}
return dependencies
.findCommonVersions()
.sortedBeautifullyBy(exceptIf = OutputFile.VERSIONS.existed) { it.versionName }
return this
}

fun Dependency.fqdnName(): String = PluginConfig.escapeVersionsKt("${group}_${name}")


fun List<Dependency>.orderDependencies(): List<Dependency> {
return this.sortedBy { it.gradleNotation() }
}


fun List<Dependency>.findCommonVersions(): List<Dependency> {
val map = groupBy { d -> d.group }
for (deps in map.values) {
val groupTogether = deps.size > 1 && deps.map { it.version }.distinct().size == 1

for (d in deps) {
d.versionName = if (groupTogether) PluginConfig.escapeVersionsKt(d.group) else d.escapedName
if (groupTogether) {
deps.forEach { d -> d.mode = VersionMode.GROUP }
}
}
return this
Expand Down
48 changes: 30 additions & 18 deletions plugin/src/main/kotlin/de/fayard/internal/PluginConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package de.fayard.internal
import com.github.benmanes.gradle.versions.updates.DependencyUpdatesTask
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import de.fayard.internal.VersionMode.GROUP
import de.fayard.internal.VersionMode.GROUP_MODULE
import de.fayard.internal.VersionMode.MODULE
import okio.buffer
import okio.source
import org.gradle.util.GradleVersion
Expand All @@ -14,7 +17,7 @@ object PluginConfig {


const val PLUGIN_ID = "de.fayard.buildSrcVersions"
const val PLUGIN_VERSION = "0.6.1" // plugin.de.fayard.buildSrcVersions
const val PLUGIN_VERSION = "0.6.2" // plugin.de.fayard.buildSrcVersions
const val GRADLE_VERSIONS_PLUGIN_ID = "com.github.ben-manes.versions"
const val GRADLE_VERSIONS_PLUGIN_VERSION = "0.25.0" // Sync with plugin/build.gradle.kts
const val EXTENSION_NAME = "buildSrcVersions"
Expand Down Expand Up @@ -46,17 +49,26 @@ object PluginConfig {
* $ ./gradlew -Pversion.kotlin.stdlib=1.3.50
* **/
fun considerGradleProperties(group: String, module: String): List<String> = listOf(
escapeGradleProperty("version.$group.$module"),
escapeGradleProperty("version.$module"),
escapeGradleProperty("version.$module")
"version.$group..$module",
"version.$group",
"version.$module"
)

/** Naming convention: replace [:-_] with "." **/
@JvmStatic
fun escapeGradleProperty(name: String): String =
name.replace(":", ".").replace("_", ".").replace("-", ".")
fun versionPropertyFor(d: Dependency): String = when (d.mode) {
MODULE -> d.name
GROUP -> d.group
GROUP_MODULE -> "${d.group}..${d.name}"
}

fun versionKtFor(d: Dependency): String = escapeVersionsKt(
when (d.mode) {
MODULE -> d.name
GROUP -> d.group
GROUP_MODULE -> "${d.group}:${d.name}"
}
)

@JvmStatic
fun escapeVersionsKt(name: String): String {
val escapedChars = listOf('-', '.', ':')
return buildString {
Expand All @@ -69,7 +81,12 @@ object PluginConfig {

const val DEFAULT_LIBS = "Libs"
const val DEFAULT_VERSIONS = "Versions"
const val DEFAULT_INDENT = "from-editorconfig-file"
const val INDENT_FROM_EDITOR_CONFIG = "from-editorconfig-file"
const val SPACES4 = " "
const val SPACES2 = " "
const val SPACES0 = ""
const val TAB = "\t"
const val DEFAULT_INDENT = SPACES4
const val BENMANES_REPORT_PATH = "build/dependencyUpdates/report.json"

/** Documentation **/
Expand All @@ -89,7 +106,7 @@ object PluginConfig {
* Found many inspiration for bad libs here https://developer.android.com/jetpack/androidx/migrate
* **/
val MEANING_LESS_NAMES: List<String> = listOf(
"common", "core", "core-testing", "testing", "runtime", "extensions",
"common", "core", "testing", "runtime", "extensions",
"compiler", "migration", "db", "rules", "runner", "monitor", "loader",
"media", "print", "io", "media", "collection", "gradle", "android"
)
Expand Down Expand Up @@ -170,11 +187,6 @@ object PluginConfig {

const val GRADLE_LATEST_VERSION = "gradleLatestVersion"

const val SPACES4 = " "
const val SPACES2 = " "
const val SPACES0 = ""
const val TAB = "\t"

fun supportsTaskAvoidance(): Boolean =
GradleVersion.current() >= GradleVersion.version("5.0")

Expand All @@ -189,16 +201,16 @@ object PluginConfig {

val gradleVersionsPlugin: Dependency = Dependency(
group = "com.github.ben-manes",
name = "gradle-versions-plugin",
name = "$GRADLE_VERSIONS_PLUGIN_ID.gradle.plugin",
version = GRADLE_VERSIONS_PLUGIN_VERSION,
versionName = escapeVersionsKt("$GRADLE_VERSIONS_PLUGIN_ID.gradle.plugin"),
mode = MODULE,
available = null
)

fun gradleLatestVersion(graph: DependencyGraph): Dependency = Dependency(
group = "org.gradle",
name = GRADLE_LATEST_VERSION,
versionName = GRADLE_LATEST_VERSION,
mode = MODULE,
version = graph.gradle.running.version,
available = when {
graph.gradle.running == graph.gradle.current -> null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import de.fayard.BuildSrcVersionsExtension
import org.gradle.api.Project

data class UpdateGradleProperties(
val extension: BuildSrcVersionsExtension,
val dependencies: List<Dependency>
val extension: BuildSrcVersionsExtension
) {

fun generateVersionProperties(project: Project, dependencies: List<Dependency>) = with(UpdateVersionsOnly) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ object UpdateVersionsOnly {
const val newline = "\n"

fun Dependency.asGradleProperty(): String {
val escapedVersionName = PluginConfig.escapeGradleProperty(versionName)
val pluginName = escapedVersionName.substringBeforeLast(".gradle.plugin", "")
val pluginName = versionProperty.substringBeforeLast(".gradle.plugin", "")
val key = when {
pluginName.isNotBlank() -> "plugin.$pluginName"
else -> "version.${escapedVersionName}"
else -> "version.${versionProperty}"
}
val commentPrefix = " available="
val spacing = PluginConfig.spaces(key.length - commentPrefix.length - 1)
Expand Down Expand Up @@ -110,7 +109,7 @@ object UpdateVersionsOnly {
fun regenerateBlock(mode: VersionsOnlyMode, dependencies: List<Dependency>, indent: String): List<String> {
val comment = mode.comment
val result = mutableListOf<String>()
var (before, after) = mode.beforeAfter()
val (before, after) = mode.beforeAfter()

result += PluginConfig.VERSIONS_ONLY_INTRO.map { "$indent$comment $it" }

Expand Down
Loading

0 comments on commit e117dc0

Please sign in to comment.