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

Add support for Versions Catalogs gradle/libs.versions.toml #555

Closed
wants to merge 40 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
b396c20
refreshVersionsCatalog generates a Versions Catalog
jmfayard Aug 7, 2021
771db0c
Merge remote-tracking branch 'origin/main' into refreshVersionsCatalog
jmfayard Feb 12, 2022
12b57e6
refreshVersionsCatalog works again
jmfayard Feb 12, 2022
7fd805a
Versions Catalog are stable in Gradle 7.4
jmfayard Feb 12, 2022
1cff52c
versions catalog: use kebab-case
jmfayard Feb 12, 2022
9b7402d
versions catalog: filter out built-in dependencies
jmfayard Feb 12, 2022
f4b3f04
sample-kotlin
jmfayard Feb 12, 2022
0cdc912
pr: self-review
jmfayard Feb 12, 2022
6515c49
ci: fix test
jmfayard Feb 12, 2022
92b393d
./gradlew refreshVersionsMigrate completed
jmfayard Feb 12, 2022
a3bae6e
Polish refreshVersionsCatalog
jmfayard Feb 12, 2022
7728fd2
feat(VersionsCatalog): parse TOML lines
jmfayard Feb 25, 2022
e28ff07
feat(VersionsCatalog): Implement Toml Updater
jmfayard Feb 25, 2022
f40b9e1
feat(VersionsCatalog): Enable in task refreshVersions
jmfayard Feb 25, 2022
5ded7bd
feat(VersionsCatalog): refreshVersionsCatalog --versions --all
jmfayard Feb 25, 2022
ed6a4a9
ci: fix test
jmfayard Feb 25, 2022
9d084a8
tests: move tests to the right package
jmfayard Feb 25, 2022
499f1b4
pr: self-review
jmfayard Feb 25, 2022
cb4ff90
feat: dependencies from versions catalog are not hard-coded
jmfayard Feb 25, 2022
9732622
refactor: helpers for TomlLine
jmfayard Feb 26, 2022
3f6450b
feat(refreshVersionsCatalog): add plugins
jmfayard Feb 26, 2022
316fed3
refactor: Classes VersionCatalogs Toml TomlSection
jmfayard Feb 26, 2022
747fd31
feat(VersionsCatalog): generate libs.toml incrementally
jmfayard Feb 26, 2022
3eb128f
feat(VersionsCatalog): create versions from rules
jmfayard Feb 26, 2022
563ac47
ci: fix tests
jmfayard Feb 26, 2022
ac553bf
feat(VersionsCatalog): support in refreshVersionsCleanup
jmfayard Feb 27, 2022
bc08604
test: incremental merge of properties in libs.versions.toml
jmfayard Feb 27, 2022
ab83f57
feat(migrate): don't migrate build files inside resources or build fo…
jmfayard Feb 28, 2022
c1c9a19
feat(migrate): option --toml use libraries from libs.versions.toml first
jmfayard Feb 28, 2022
67062e7
feat(migrate): support custom project.buildFileName
jmfayard Mar 2, 2022
224179f
refactor: clean-up OutputFile.kt
jmfayard Mar 2, 2022
0a3c287
feat: skip Configuration Cache for refreshVersions tasks
jmfayard Mar 2, 2022
7f49f16
pr(migrate): skip build files that can't be read
jmfayard Mar 3, 2022
4b01f2f
Merge remote-tracking branch 'origin/main' into update-toml
jmfayard Mar 6, 2022
5b3bd78
fix(versions-catalog): NPE when a BOM was used and no version available
jmfayard Apr 28, 2022
a4f3026
feat(migration): support dependencies without versions
jmfayard Apr 28, 2022
68e8af4
feat(versions-catalog): support version="none" when generating libs.v…
jmfayard Apr 28, 2022
20643fe
test: fix tests
jmfayard Apr 28, 2022
8d8521b
ci: fix bom
jmfayard Apr 28, 2022
bb241f3
Merge remote-tracking branch 'origin/main' into gradle-versions-catalogs
jmfayard Jun 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.fayard.buildSrcLibs

import de.fayard.refreshVersions.core.internal.skipConfigurationCache
import org.gradle.api.DefaultTask
import org.gradle.api.Plugin
import org.gradle.api.Project
Expand All @@ -14,6 +15,7 @@ class BuildSrcLibsPlugin : Plugin<Project> {
group = "refreshVersions"
description = "Update buildSrc/src/main/kotlin/Libs.kt"
outputs.upToDateWhen { false }
skipConfigurationCache()
}
project.tasks.register<DefaultTask>(
name = "buildSrcVersions"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ package de.fayard.buildSrcLibs
import com.squareup.kotlinpoet.FileSpec
import de.fayard.buildSrcLibs.internal.*
import de.fayard.refreshVersions.core.addMissingEntriesInVersionsProperties
import de.fayard.refreshVersions.core.internal.Case
import de.fayard.refreshVersions.core.internal.MEANING_LESS_NAMES
import de.fayard.refreshVersions.core.internal.OutputFile
import de.fayard.refreshVersions.core.internal.checkModeAndNames
import de.fayard.refreshVersions.core.internal.computeAliases
import de.fayard.refreshVersions.core.internal.findDependencies
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction

Expand All @@ -17,7 +22,7 @@ open class BuildSrcLibsTask : DefaultTask() {

@TaskAction
fun taskActionInitializeBuildSrc() {
OutputFile.checkWhichFilesExist(project.rootDir)
OutputFile.checkWhichFilesExist()
project.file(OutputFile.OUTPUT_DIR.path).also {
if (it.isDirectory.not()) it.mkdirs()
}
Expand All @@ -42,11 +47,10 @@ open class BuildSrcLibsTask : DefaultTask() {

@TaskAction
fun taskUpdateLibsKt() {
val outputDir = project.file(OutputFile.OUTPUT_DIR.path)
val outputDir = OutputFile.OUTPUT_DIR.file

val allDependencies = project.findDependencies()
val resolvedUseFqdn = computeUseFqdnFor(
libraries = allDependencies,
val resolvedUseFqdn = allDependencies.computeAliases(
configured = emptyList(),
byDefault = MEANING_LESS_NAMES
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package de.fayard.buildSrcLibs.internal

import com.squareup.kotlinpoet.*
import com.squareup.kotlinpoet.CodeBlock
import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.PropertySpec
import com.squareup.kotlinpoet.TypeSpec
import de.fayard.refreshVersions.core.internal.Deps
import de.fayard.refreshVersions.core.internal.Library

internal fun kotlinpoet(
deps: Deps
deps: Deps,
): FileSpec {
val libraries: List<Library> = deps.libraries
val indent = " "
Expand Down Expand Up @@ -36,11 +42,10 @@ internal fun kotlinpoet(
}



internal fun constStringProperty(
name: String,
initializer: CodeBlock,
kdoc: CodeBlock? = null
kdoc: CodeBlock? = null,
): PropertySpec = PropertySpec.builder(name, String::class)
.addModifiers(KModifier.CONST)
.initializer(initializer)
Expand Down
22 changes: 0 additions & 22 deletions plugins/buildSrcLibs/src/test/kotlin/CaseTest.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import org.gradle.api.artifacts.ExternalDependency

@InternalRefreshVersionsApi
fun addMissingEntriesInVersionsProperties(project: Project) {
require(project == project.rootProject) { "Expected a rootProject but got $project" }
OutputFile.checkWhichFilesExist(project.rootDir)
OutputFile.checkWhichFilesExist()
val configurationsWithHardcodedDependencies = project.findHardcodedDependencies()

val versionsMap = RefreshVersionsConfigHolder.readVersionsMap()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
package de.fayard.refreshVersions.core

import de.fayard.refreshVersions.core.internal.OutputFile
import de.fayard.refreshVersions.core.internal.RefreshVersionsConfigHolder
import de.fayard.refreshVersions.core.internal.SettingsPluginsUpdater.removeCommentsAddedByUs
import de.fayard.refreshVersions.core.internal.TomlUpdater
import de.fayard.refreshVersions.core.internal.VersionCatalogs
import de.fayard.refreshVersions.core.internal.VersionCatalogs.LIBS_VERSIONS_TOML
import de.fayard.refreshVersions.core.internal.versions.VersionsPropertiesModel
import de.fayard.refreshVersions.core.internal.versions.VersionsPropertiesModel.Section
import de.fayard.refreshVersions.core.internal.versions.readFromFile
import de.fayard.refreshVersions.core.internal.versions.writeTo
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
import java.io.File

open class RefreshVersionsCleanupTask : DefaultTask() {

@TaskAction
fun cleanUpVersionsProperties() {
OutputFile.checkWhichFilesExist()
val model = VersionsPropertiesModel.readFromFile(RefreshVersionsConfigHolder.versionsPropertiesFile)

val sectionsWithoutAvailableUpdates = model.sections.map { section ->
Expand All @@ -23,18 +29,13 @@ open class RefreshVersionsCleanupTask : DefaultTask() {
}
val newModel = model.copy(sections = sectionsWithoutAvailableUpdates)
newModel.writeTo(RefreshVersionsConfigHolder.versionsPropertiesFile)
OutputFile.VERSIONS_PROPERTIES.logFileWasModified()
}

@TaskAction
fun cleanUpSettings() {
val settingsFiles = listOf(
"settings.gradle",
"settings.gradle.kts",
"buildSrc/settings.gradle",
"buildSrc/settings.gradle.kts"
).mapNotNull { path ->
project.file(path).takeIf { it.exists() }
}
val settingsFiles = OutputFile.settingsFiles
.filter { it.existed }

settingsFiles.forEach { settingsFile ->
val initialContent = settingsFile.readText()
Expand All @@ -46,5 +47,16 @@ open class RefreshVersionsCleanupTask : DefaultTask() {
settingsFile.writeText(newContent)
}
}

settingsFiles.forEach { it.logFileWasModified() }
}

@TaskAction
fun cleanUpVersionsCatalog() {
if (VersionCatalogs.isSupported()) {
val file = File(LIBS_VERSIONS_TOML)
TomlUpdater(file, emptyList()).cleanupComments(file)
OutputFile.GRADLE_VERSIONS_CATALOG.logFileWasModified()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package de.fayard.refreshVersions.core
import de.fayard.refreshVersions.core.extensions.gradle.isBuildSrc
import de.fayard.refreshVersions.core.extensions.gradle.isRootProject
import de.fayard.refreshVersions.core.internal.InternalRefreshVersionsApi
import de.fayard.refreshVersions.core.internal.OutputFile
import de.fayard.refreshVersions.core.internal.RefreshVersionsConfigHolder
import de.fayard.refreshVersions.core.internal.skipConfigurationCache
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.register
Expand All @@ -14,6 +16,7 @@ open class RefreshVersionsCorePlugin : Plugin<Project> {

override fun apply(project: Project) {
check(project.isRootProject) { "ERROR: de.fayard.refreshVersions.core should not be applied manually" }
OutputFile.rootDir = project.rootDir
if (project.isBuildSrc.not()) {
// In the case where this runs in includedBuilds, the task configuration lambda may (will) run
// after RefreshVersionsConfigHolder content is cleared (via its ClearStaticStateBuildService),
Expand All @@ -23,6 +26,7 @@ open class RefreshVersionsCorePlugin : Plugin<Project> {
project.tasks.register<RefreshVersionsTask>(name = "refreshVersions") {
group = "refreshVersions"
description = "Search for new dependencies versions and update $versionsFileName"
skipConfigurationCache()
}

project.tasks.register<RefreshVersionsCleanupTask>(name = "refreshVersionsCleanup") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package de.fayard.refreshVersions.core

import de.fayard.refreshVersions.core.extensions.gradle.getVersionsCatalog
import de.fayard.refreshVersions.core.internal.*
import de.fayard.refreshVersions.core.internal.RefreshVersionsConfigHolder.settings
import de.fayard.refreshVersions.core.internal.VersionCatalogs.LIBS_VERSIONS_TOML
import de.fayard.refreshVersions.core.internal.problems.log
import de.fayard.refreshVersions.core.internal.versions.VersionsPropertiesModel
import de.fayard.refreshVersions.core.internal.versions.writeWithNewVersions
Expand Down Expand Up @@ -46,14 +48,17 @@ open class RefreshVersionsTask : DefaultTask() {

@TaskAction
fun taskActionRefreshVersions() {
OutputFile.checkWhichFilesExist(project.rootDir)
OutputFile.checkWhichFilesExist()

if (FeatureFlag.userSettings.isNotEmpty()) {
logger.lifecycle("Feature flags: " + FeatureFlag.userSettings)
}
//TODO: Filter using known grouping strategies to only use the main artifact to resolve latest version, this
// will reduce the number of repositories lookups, improving performance a little more.

val versionsCatalogMapping: Set<ModuleId.Maven> =
VersionCatalogs.dependencyAliases(project.getVersionsCatalog()).keys

runBlocking {
val lintUpdatingProblemsAsync = async {
configureLintIfRunningOnAnAndroidProject(settings, RefreshVersionsConfigHolder.readVersionsMap())
Expand All @@ -65,7 +70,8 @@ open class RefreshVersionsTask : DefaultTask() {
httpClient = httpClient,
project = project,
versionMap = RefreshVersionsConfigHolder.readVersionsMap(),
versionKeyReader = RefreshVersionsConfigHolder.versionKeyReader
versionKeyReader = RefreshVersionsConfigHolder.versionKeyReader,
versionsCatalogMapping = versionsCatalogMapping,
)
}
VersionsPropertiesModel.writeWithNewVersions(result.dependenciesUpdates)
Expand All @@ -74,6 +80,10 @@ open class RefreshVersionsTask : DefaultTask() {
settingsPluginsUpdates = result.settingsPluginsUpdates,
buildSrcSettingsPluginsUpdates = result.buildSrcSettingsPluginsUpdates
)
val libsToml = project.file(LIBS_VERSIONS_TOML)
if (VersionCatalogs.isSupported()) {
TomlUpdater(libsToml, result.dependenciesUpdates).updateNewVersions(libsToml)
}

warnAboutRefreshVersionsIfSettingIfAny()
warnAboutHardcodedVersionsIfAny(result.dependenciesWithHardcodedVersions)
Expand All @@ -83,6 +93,9 @@ open class RefreshVersionsTask : DefaultTask() {
logger.log(problem)
}
OutputFile.VERSIONS_PROPERTIES.logFileWasModified()
if (libsToml.canRead()) {
OutputFile.GRADLE_VERSIONS_CATALOG.logFileWasModified()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
package de.fayard.refreshVersions.core.extensions.gradle

import de.fayard.refreshVersions.core.internal.InternalRefreshVersionsApi
import org.gradle.api.Project
import org.gradle.api.UnknownDomainObjectException
import org.gradle.api.artifacts.VersionCatalogsExtension
import org.gradle.kotlin.dsl.getByType

internal val Project.isRootProject: Boolean get() = this == rootProject

internal val Project.isBuildSrc: Boolean get() = isRootProject && name == "buildSrc"

@InternalRefreshVersionsApi
fun Project.getVersionsCatalog() = try {
project.extensions.getByType<VersionCatalogsExtension>().named("libs")
} catch (e: UnknownDomainObjectException) {
null
}
Loading