Skip to content

Commit

Permalink
#104 v0.8.2 (#127)
Browse files Browse the repository at this point in the history
v0.8.2

The long-term plan for v1.0 is to focus on :refreshVersions instead of :buildSrcVersions

See #104
  • Loading branch information
Jean-Michel Fayard authored Nov 7, 2019
1 parent d02577f commit d6193d1
Show file tree
Hide file tree
Showing 31 changed files with 765 additions and 171 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
# Unreleased

# v0.8.x

The long-term plan for v1.0 is to focus on `:refreshVersions` instead of `:buildSrcVersions`

See https://github.com/jmfayard/buildSrcVersions/issues/104

Starting from release 0.8.0 the plugin is now called and contain only the task `refreshVersions`

```groovy
plugins {
id("de.fayard.refreshVersions").version("0.8.x") // or newer
}
```

We are not quite ready yet to extract the useful parts of `buildSrcVersions` to another plugin,
so if you need the features from buildSrcVersions, stay with this for now:

```groovy
plugins {
id("de.fayard.buildSrcVersions").version("0.7.0")
}
```



# 0.7.0

The plugin will in the future focus on `:refreshVersions`.
Expand Down
2 changes: 2 additions & 0 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pluginBundle {

dependencies {
testImplementation("io.kotlintest:kotlintest-runner-junit5:3.1.9")
implementation(gradleKotlinDsl())

// SYNC WITH plugin/src/main/kotlin/de/fayard/internal/PluginConfig.kt
implementation("com.github.ben-manes:gradle-versions-plugin:0.25.0")
Expand All @@ -61,6 +62,7 @@ dependencies {

tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
kotlinOptions.freeCompilerArgs = listOf("-Xinline-classes")
}

tasks.withType<Test> {
Expand Down
3 changes: 3 additions & 0 deletions plugin/src/main/kotlin/de/fayard/RefreshVersionsExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ interface RefreshVersionsExtension {
***/
fun useFqdnFor(vararg dependencyName: String)

// TODO: remove
var alignVersionsForGroups: MutableList<String>
// TODO: do something with it
var versionsMapping: MutableMap<String, String>

/**
* See [versionsOnlyMode]
Expand Down
69 changes: 49 additions & 20 deletions plugin/src/main/kotlin/de/fayard/RefreshVersionsPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import de.fayard.internal.PluginConfig
import de.fayard.internal.PluginConfig.isNonStable
import de.fayard.internal.PluginsSetup
import de.fayard.internal.RefreshVersionsExtensionImpl
import de.fayard.versions.RefreshVersionsPropertiesExtension
import de.fayard.versions.RefreshVersionsPropertiesTask
import de.fayard.versions.extensions.registerOrCreate
import de.fayard.versions.getVersionProperties
import de.fayard.versions.setupVersionPlaceholdersResolving
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.ModuleVersionSelector
Expand All @@ -13,50 +18,74 @@ import org.gradle.kotlin.dsl.apply
import org.gradle.kotlin.dsl.create
import java.util.Properties


open class RefreshVersionsPlugin : Plugin<Project> {

/**
* Overwrite the default by adding the following line to gradle.properties:
*
* ```
* refreshVersions.useExperimentalUpdater=true
* ```
* **/
internal val Project.useExperimentalUpdater: Boolean
get() = findProperty(PluginConfig.USE_EXPERIMENTAL_UPDATER) == "true"

override fun apply(project: Project) {
check(project == project.rootProject) { "ERROR: plugins de.fayard.refreshVersions must be applied to the root build.gradle(.kts)" }
project.apply(plugin = PluginConfig.GRADLE_VERSIONS_PLUGIN_ID)
project.configure()
project.useVersionsFromProperties()
check(project == project.rootProject) {
"ERROR: plugins de.fayard.refreshVersions must be applied to the root build.gradle(.kts)"
}

if (project.useExperimentalUpdater) {
project.configureExperimentalUpdater()
val properties: Map<String, String> = project.getVersionProperties()
project.allprojects { configurations.all { setupVersionPlaceholdersResolving(properties) } }
} else {
project.apply(plugin = PluginConfig.GRADLE_VERSIONS_PLUGIN_ID)
project.configure()
project.useVersionsFromProperties()
}
}

private fun Project.configureExperimentalUpdater() {
extensions.create<RefreshVersionsPropertiesExtension>(name = PluginConfig.EXTENSION_NAME)
tasks.registerOrCreate<RefreshVersionsPropertiesTask>(name = PluginConfig.REFRESH_VERSIONS) {
group = "Help"
description = "Search for new dependencies versions and update versions.properties"
}
}

fun Project.configure() = with(PluginConfig) {
private fun Project.configure() = with(PluginConfig) {
PluginsSetup.copyPluginsGradleKtsIfNeeded(project)

extensions.create(RefreshVersionsExtension::class, EXTENSION_NAME, RefreshVersionsExtensionImpl::class)

@Suppress("LiftReturnOrAssignment")
if (supportsTaskAvoidance()) {
val provider: TaskProvider<DependencyUpdatesTask> = when {
tasks.findByPath(DEPENDENCY_UPDATES_PATH) == null -> tasks.register(DEPENDENCY_UPDATES_PATH, DependencyUpdatesTask::class.java)
tasks.findByPath(DEPENDENCY_UPDATES_PATH) == null -> tasks.register(
DEPENDENCY_UPDATES_PATH,
DependencyUpdatesTask::class.java
)
else -> tasks.named(DEPENDENCY_UPDATES, DependencyUpdatesTask::class.java)
}

configureGradleVersions = { operation -> provider.configure(operation) }
configureGradleVersions(DependencyUpdatesTask::configureBenManesVersions)

tasks.register(REFRESH_VERSIONS, RefreshVersionsTask::class.java, RefreshVersionsTask::configureRefreshVersions)

} else {
val dependencyUpdatesTask = tasks.maybeCreate(DEPENDENCY_UPDATES, DependencyUpdatesTask::class.java)
configureGradleVersions = { operation -> dependencyUpdatesTask.operation() }
configureGradleVersions(DependencyUpdatesTask::configureBenManesVersions)

tasks.create(REFRESH_VERSIONS, RefreshVersionsTask::class, RefreshVersionsTask::configureRefreshVersions)
}
configureGradleVersions(DependencyUpdatesTask::configureBenManesVersions)
tasks.registerOrCreate(name = REFRESH_VERSIONS, action = RefreshVersionsTask::configureRefreshVersions)
}
}

fun Project.useVersionsFromProperties() {
private fun Project.useVersionsFromProperties() {
@Suppress("UNCHECKED_CAST")
val properties: Map<String, String> = Properties().apply {
val propertiesFile = listOf("versions.properties", "gradle.properties").firstOrNull { project.file(it).canRead() } ?: return
val propertiesFile =
listOf("versions.properties", "gradle.properties").firstOrNull { project.file(it).canRead() } ?: return
load(project.file(propertiesFile).reader())
} as Map<String, String>


val resolutionStrategyConfig = project.findProperty("resolutionStrategyConfig") as? String
if (resolutionStrategyConfig == "false") return
allprojects {
Expand All @@ -80,13 +109,13 @@ fun Project.useVersionsFromProperties() {
}


fun DependencyUpdatesTask.configureBenManesVersions() {
private fun DependencyUpdatesTask.configureBenManesVersions() {
rejectVersionIf { isNonStable(candidate.version) }
checkForGradleUpdate = true
outputFormatter = "json"
}

fun RefreshVersionsTask.configureRefreshVersions() {
private fun RefreshVersionsTask.configureRefreshVersions() {
group = "Help"
description = "Search for available dependencies updates and update gradle.properties"
dependsOn(PluginConfig.DEPENDENCY_UPDATES_PATH)
Expand Down
8 changes: 4 additions & 4 deletions plugin/src/main/kotlin/de/fayard/RefreshVersionsTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ open class RefreshVersionsTask : DefaultTask() {
@TaskAction
fun taskActionGradleProperties() {
val extension: RefreshVersionsExtensionImpl = extension()
val updateGradleProperties = UpdateProperties(extension)
val updateGradleProperties = UpdateProperties()

val specialDependencies =
listOf(PluginConfig.gradleVersionsPlugin, PluginConfig.gradleRefreshVersions, PluginConfig.gradleLatestVersion(dependencyGraph))

val dependencies = (unsortedParsedDependencies + specialDependencies)
val dependencies: List<Dependency> = (unsortedParsedDependencies + specialDependencies)
.sortedBeautifullyBy(extension.orderBy) { it.versionProperty }
.distinctBy { it.versionProperty }

Expand All @@ -51,7 +51,7 @@ open class RefreshVersionsTask : DefaultTask() {

val message = with(PluginConfig) {
"""
|Running plugins.id("$PLUGIN_ID").version("$PLUGIN_VERSION") with useRefreshVersions=${useRefreshVersions} and extension: $extension
|Running plugins.id("$PLUGIN_ID").version("$PLUGIN_VERSION") with configuration: $extension
|See documentation at $issue53PluginConfiguration
|
""".trimMargin()
Expand All @@ -76,7 +76,7 @@ open class RefreshVersionsTask : DefaultTask() {
val projectExtension = project.extensions.getByType<RefreshVersionsExtension>() as RefreshVersionsExtensionImpl
this._extension = projectExtension.defensiveCopy()
action.execute(this._extension)
PluginConfig.useRefreshVersions = project.hasProperty("plugin.de.fayard.buildSrcVersions") || project.hasProperty("plugin.de.refreshVersions")
// TODO: use DEFAULT_MAPPING instead of ALIGN_VERSION_GROUPS
PluginConfig.ALIGN_VERSION_GROUPS.clear()
PluginConfig.ALIGN_VERSION_GROUPS.addAll(_extension.alignVersionsForGroups)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ data class Dependency(

companion object {
fun virtualGroup(dependency: Dependency, withVersion: Boolean = false): String? {
// TODO: use DEFAULT_MAPPING instead of ALIGN_VERSION_GROUPS
val virtualGroup = PluginConfig.ALIGN_VERSION_GROUPS.firstOrNull { "${dependency.group}.${dependency.module}".startsWith(it) }
return when {
virtualGroup == null -> null
Expand Down
38 changes: 22 additions & 16 deletions plugin/src/main/kotlin/de/fayard/internal/PluginConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ import java.io.File

@Suppress("unused")
object PluginConfig {


const val PLUGIN_ID = "de.fayard.refreshVersions"
const val PLUGIN_VERSION = "0.8.2" // plugin.de.fayard.refreshVersions
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 DEPENDENCY_UPDATES = "dependencyUpdates"
const val REFRESH_VERSIONS_UPDATES_PATH = "refreshVersionsUpdates" // TODO: think about a better name
const val USE_EXPERIMENTAL_UPDATER = "refreshVersions.useExperimentalUpdater" // TODO: think about a better name
const val DEPENDENCY_UPDATES_PATH = ":$DEPENDENCY_UPDATES"
const val REFRESH_VERSIONS = "refreshVersions"
const val EXTENSION_NAME = REFRESH_VERSIONS
const val DEFAULT_PROPERTIES_FILE = "versions.properties"
const val AVAILABLE_DEPENDENCIES_FILE = "build/dependencyUpdates/refreshVersions.txt"

/** There is no standard on how to name stable and unstable versions
* This version is a good starting point but you can define you rown
Expand Down Expand Up @@ -54,20 +55,25 @@ object PluginConfig {
"version.$module"
)

/**
* We want to treat all "org.getbrains.kotlinx:kotlinx-coroutines-*" as if they were a maven group
* with one common version, but different from org.jetbrains.kotlinx:kotlinx-serialization*
* For now this list is not part of the public API but feel free to add feedback that you need it.
* Add your use case here https://github.com/jmfayard/buildSrcVersions/issues/102
***/
val ALIGN_VERSION_GROUPS: MutableList<String> = mutableListOf(
"org.jetbrains.kotlinx.kotlinx-coroutines",
"org.jetbrains.kotlinx.kotlinx-serialization",
"com.louiscad.splitties:splitties",
"com.squareup.retrofit2"
// TODO: replace ALIGN_VERSION_GROUPS by DEFAULT_MAPPING
val ALIGN_VERSION_GROUPS: MutableList<String>
get() = DEFAULT_MAPPING.values.toMutableList()

val DEFAULT_MAPPING: Map<String, String> = mapOf(
"org.jetbrains.kotlinx..kotlinx-coroutines" to "org.jetbrains.kotlinx.kotlinx-coroutines",
"org.jetbrains.kotlinx..kotlinx-serialization" to "org.jetbrains.kotlinx.kotlinx-serialization",
"com.louiscad.splitties..splitties" to "com.louiscad.splitties:splitties",
"com.squareup.retrofit2" to "com.squareup.retrofit2"
/**
* TODO: improve DEFAULT_MAPPING according to
* https://github.com/LouisCAD/Splitties/blob/9f4a85e7ecc612d290fd6e4615b3472c05aece51/build.gradle.kts#L151-L167
* for example:
"retrofit2" to "com.squareup.retrofit2",
"kotlin" to "org.jetbrains.kotlin:kotlin",
"moshi" to "com.squareup.moshi"
**/
)


@JvmStatic
fun versionPropertyFor(d: Dependency): String = when (d.mode) {
MODULE -> d.name
Expand Down Expand Up @@ -144,6 +150,8 @@ object PluginConfig {

internal val extensionAdapter: JsonAdapter<RefreshVersionsExtensionImpl> by moshiAdapter()

internal val dependencyAdapter: JsonAdapter<Dependency> by moshiAdapter()

fun readGraphFromJsonFile(jsonInput: File): DependencyGraph {
return dependencyGraphAdapter.fromJson(jsonInput.source().buffer())!!
}
Expand Down Expand Up @@ -229,8 +237,6 @@ object PluginConfig {
return (configured + byDefault + ambiguities + depsFromGroups - groups).distinct().sorted()
}

var useRefreshVersions: Boolean = false

lateinit var configureGradleVersions: (DependencyUpdatesTask.() -> Unit) -> Unit

}
2 changes: 1 addition & 1 deletion plugin/src/main/kotlin/de/fayard/internal/PluginsSetup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object PluginsSetup {
apply(from = "gradle/plugins.gradle.kts")
"""

fun pluginFileContent(): String {
private fun pluginFileContent(): String {
return this::class.java.getResourceAsStream("/$PLUGIN_GRADLE_KTS.txt").reader().readText()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ internal open class RefreshVersionsExtensionImpl(
var useFqqnFor: List<String> = emptyList(),
var alwaysUpdateVersions: Boolean = false,
override var orderBy: OrderBy = OrderBy.GROUP_AND_LENGTH,
override var alignVersionsForGroups: MutableList<String> = PluginConfig.ALIGN_VERSION_GROUPS
// TODO: use DEFAULT_MAPPING instead of ALIGN_VERSION_GROUPS
override var versionsMapping: MutableMap<String, String> = PluginConfig.DEFAULT_MAPPING.toMutableMap()
) : RefreshVersionsExtension, java.io.Serializable {

override var alignVersionsForGroups: MutableList<String>
get() = versionsMapping.values.toMutableList()
set(value) = println("WARNING: alignVersionsForGroups is deprecated, use instead versionsMapping")

// Necessary because of https://github.com/jmfayard/buildSrcVersions/issues/92
fun defensiveCopy(): RefreshVersionsExtensionImpl = RefreshVersionsExtensionImpl(
propertiesFile, useFqqnFor, alwaysUpdateVersions, orderBy, alignVersionsForGroups
propertiesFile, useFqqnFor, alwaysUpdateVersions, orderBy, versionsMapping
)

override fun alwaysUpdateVersions() {
Expand All @@ -25,6 +30,7 @@ internal open class RefreshVersionsExtensionImpl(
override fun toString(): String = PluginConfig.extensionAdapter.toJson(this)

override fun rejectVersionIf(filter: ComponentFilter) {
// TODO: use the filter in RefreshVersionsTask
(PluginConfig.configureGradleVersions) {
this.rejectVersionIf(filter)
}
Expand Down
9 changes: 3 additions & 6 deletions plugin/src/main/kotlin/de/fayard/internal/UpdateProperties.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package de.fayard.internal

import de.fayard.RefreshVersionsExtension
import java.io.File

data class UpdateProperties(
val extension: RefreshVersionsExtension
) {
class UpdateProperties {

fun generateVersionProperties(file: File, dependencies: List<Dependency>) = with(UpdateVersionsOnly) {
PluginConfig.isAndroidProject = dependencies.any { it.group.contains("android") }
Expand All @@ -24,7 +21,7 @@ data class UpdateProperties(
)
}

fun String.wasGeneratedByPlugin(): Boolean = when {
private fun String.wasGeneratedByPlugin(): Boolean = when {
startsWith("module.kotlin") -> true
startsWith("module.android") -> true
startsWith("version.") -> true
Expand All @@ -34,7 +31,7 @@ data class UpdateProperties(
else -> false
}

fun updateGradleProperties(file: File, newLines: List<String>, removeIf: (String) -> Boolean) {
private fun updateGradleProperties(file: File, newLines: List<String>, removeIf: (String) -> Boolean) {
if (!file.exists()) file.createNewFile()

val existingLines = file.readLines().filterNot { line -> removeIf(line) }
Expand Down
25 changes: 25 additions & 0 deletions plugin/src/main/kotlin/de/fayard/versions/ArtifactGrouping.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package de.fayard.versions

import org.gradle.api.Incubating

/**
* We assume each part of the "group" is dot separated (`.`), and each part of the name is dash separated (`-`).
*/
internal enum class ArtifactGroupNaming {
GroupOnly,
GroupLastPart,
GroupFirstTwoParts,
GroupFirstThreeParts,
GroupAndNameFirstPart,
GroupLastPartAndNameSecondPart,
GroupFirstPartAndNameTwoFirstParts
}

internal class ArtifactGroupingRule(
val artifactNamesStartingWith: String,
val groupNaming: ArtifactGroupNaming
) {
init {
require(artifactNamesStartingWith.count { it == ':' } <= 1)
}
}
Loading

0 comments on commit d6193d1

Please sign in to comment.