Skip to content

Commit

Permalink
Version 0.6.3 fixes #94: Libs.kt is broken by a wrong indent
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Michel Fayard committed Oct 2, 2019
1 parent 1199312 commit 472e3d0
Show file tree
Hide file tree
Showing 19 changed files with 64 additions and 50 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Unreleased

# 0.6.3

Fix #94: Libs.kt is broken by a wrong indent

# 0.6.2

If you want to manage your versions from gradle.properties for better compilation avoidance,
Expand Down
10 changes: 8 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//include::includes.adoc[]
:plugin_version: 0.6.2
// plugin.de.fayard.buildSrcVersions
:plugin_version: 0.6.3
:gradle_version: 5.6.2
:repo: jmfayard/buildSrcVersions
:branch: 26-buildSrcVersions
:github: https://github.com/{repo}
Expand Down Expand Up @@ -56,6 +57,10 @@ plugins {
// Don't put any code before the buildscripts {} and plugins {} block
----

Updating Gradle is usually a good idea. You get less bugs, more features and more build speed, and it's as simple as this:

`$ ./gradlew wrapper --gradle-version {gradle_version}`


If you have to, use instead the legacy `buildscript { ... }` syntax

Expand Down Expand Up @@ -124,6 +129,7 @@ I have a series of articles called `Built with Gradle`:
You can follow me at https://dev.to/jmfayard



== Configuration

No configuration is required, but some things are configurable.
Expand Down
2 changes: 1 addition & 1 deletion RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
- Create a GitHub release https://github.com/jmfayard/buildSrcVersions/releases/new
- Try the plugin in sample projects
- Update `:plugin_version:` in `README.adoc`
- Search for `plugin.de.fayard.buildSrcVersions` everywhere to update the version
- Otherwise update the README
- Increment `version` in `{plugin,sample-kotlin,sample-groovy}/build.kts`
- Increment the version in recent articles that talk about the plugin
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.2") // plugin.de.fayard.buildSrcVersions
id("de.fayard.buildSrcVersions") version ("0.6.3") // plugin.de.fayard.buildSrcVersions

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


version = "0.6.3" // plugin.de.fayard.buildSrcVersions
version = "0.6.4" // plugin.de.fayard.buildSrcVersions
group = "de.fayard"


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ interface BuildSrcVersionsExtension {
* Even better, define an https://editorconfig.org file.
* It will be used if detected.
*/
var indent: String
var indent: String?

/**
* Possible values: KOTLIN_VAL, KOTLIN_OBJECT, GROOVY_DEF, GROOVY_EXT, GRADLE_PROPERTIES
Expand Down
21 changes: 10 additions & 11 deletions plugin/src/main/kotlin/de/fayard/BuildSrcVersionsTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ open class BuildSrcVersionsTask : DefaultTask() {
var update: Boolean = false

@Input
@Optional
@Option(description = "Tabs or Spaces?")
var indent: String = PluginConfig.INDENT_FROM_EDITOR_CONFIG
var indent: String? = null

@TaskAction
fun initializeBuildSrc() {
Expand Down Expand Up @@ -69,7 +70,7 @@ open class BuildSrcVersionsTask : DefaultTask() {
}
val versions = unsortedParsedDependencies.sortedBeautifullyBy { it.versionName }

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

if (shouldGenerateLibsKt) {
kotlinPoetry.Libs.writeTo(outputDir)
Expand Down Expand Up @@ -145,7 +146,7 @@ open class BuildSrcVersionsTask : DefaultTask() {
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) }
.map { d -> d.maybeUpdate(update || extension().alwaysUpdateVersions) }
}

@Input @Optional @Transient
Expand All @@ -154,18 +155,16 @@ open class BuildSrcVersionsTask : DefaultTask() {
fun configure(action: Action<BuildSrcVersionsExtension>) {
val projectExtension = project.extensions.getByType<BuildSrcVersionsExtension>() as BuildSrcVersionsExtensionImpl
this._extension = projectExtension.defensiveCopy()

action.execute(this._extension)
if (_extension.indent == PluginConfig.INDENT_FROM_EDITOR_CONFIG) {
val findIndentForKotlin = EditorConfig.findIndentForKotlin(project.file("buildSrc/src/main/kotlin"))
indent = findIndentForKotlin ?: PluginConfig.DEFAULT_INDENT
}
if (_extension.alwaysUpdateVersions) {
update = true
}
PluginConfig.useRefreshVersions = project.hasProperty("plugin.de.fayard.buildSrcVersions")
}

private fun computeIndent(): String {
val fromEditorConfig = EditorConfig.findIndentForKotlin(project.file("buildSrc/src/main/kotlin"))
val computedIndent = indent ?: extension().indent ?: fromEditorConfig ?: PluginConfig.DEFAULT_INDENT
return if (computedIndent.isBlank()) computedIndent else PluginConfig.DEFAULT_INDENT
}

private fun extension(): BuildSrcVersionsExtensionImpl = _extension

fun BuildSrcVersionsExtension.shouldInitializeBuildSrc() = when(versionsOnlyMode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,22 @@ 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.INDENT_FROM_EDITOR_CONFIG,
override var indent: String? = null,
override var versionsOnlyMode: VersionsOnlyMode? = null,
override var versionsOnlyFile: String? = null
override var versionsOnlyFile: String? = null,
var useFqqnFor: List<String> = emptyList(),
var alwaysUpdateVersions: Boolean = false
) : BuildSrcVersionsExtension, java.io.Serializable {

// Necessary because of https://github.com/jmfayard/buildSrcVersions/issues/92
fun defensiveCopy(): BuildSrcVersionsExtensionImpl = BuildSrcVersionsExtensionImpl(
renameLibs, renameVersions, indent, versionsOnlyMode, versionsOnlyFile
).also {
it.alwaysUpdateVersions = this.alwaysUpdateVersions
it.useFqqnFor = this.useFqqnFor
}
renameLibs, renameVersions, indent, versionsOnlyMode, versionsOnlyFile, useFqqnFor, alwaysUpdateVersions
)

override fun alwaysUpdateVersions() {
this.alwaysUpdateVersions = true
}

var useFqqnFor: List<String> = emptyList()
var alwaysUpdateVersions = false

// Use @Transient for fields that should not be present in toString()
override fun toString(): String = PluginConfig.extensionAdapter.toJson(this)

Expand Down
10 changes: 8 additions & 2 deletions plugin/src/main/kotlin/de/fayard/internal/DependencyGraph.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.fayard.internal

import com.squareup.kotlinpoet.FileSpec
import de.fayard.VersionsOnlyMode

data class KotlinPoetry(
val Libs: FileSpec,
Expand Down Expand Up @@ -83,10 +84,15 @@ data class SingleModeResult(
val endOfBlock: Int,
val indentation: String
) {
fun isBlockNotFound(): Boolean {
return startOfBlock == -1 && endOfBlock == -1
}

fun isNewFile() = startOfBlock == 0 && endOfBlock == 0

companion object {
val NEW_FILE = SingleModeResult(0, 0, "")
val BLOC_NOT_FOUND = SingleModeResult(-1, -1, PluginConfig.INDENT_FROM_EDITOR_CONFIG)
fun blockNotFound(versionMode: VersionsOnlyMode) = SingleModeResult(-1, -1, versionMode.defaultIndent)
fun newFile(versionMode: VersionsOnlyMode) = SingleModeResult(0, 0, versionMode.defaultIndent)
}
}

11 changes: 8 additions & 3 deletions plugin/src/main/kotlin/de/fayard/internal/KotlinPoetry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import org.gradle.plugin.use.PluginDependenciesSpec
import org.gradle.plugin.use.PluginDependencySpec


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


val gradleVersion = constStringProperty(
Expand Down Expand Up @@ -42,12 +47,12 @@ fun kotlinpoet(versions: List<Dependency>, gradleConfig: GradleConfig, extension


val LibsFile = FileSpec.builder("", extension.renameLibs)
.indent(extension.indent)
.indent(indent)
.addType(Libs)
.build()

val VersionsFile = FileSpec.builder("", extension.renameVersions)
.indent(extension.indent)
.indent(indent)
.addType(Versions)
.apply { addMaybeBuildSrcVersions(versions, extension) }
.build()
Expand Down
3 changes: 1 addition & 2 deletions plugin/src/main/kotlin/de/fayard/internal/PluginConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object PluginConfig {


const val PLUGIN_ID = "de.fayard.buildSrcVersions"
const val PLUGIN_VERSION = "0.6.3" // plugin.de.fayard.buildSrcVersions
const val PLUGIN_VERSION = "0.6.4" // 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 @@ -81,7 +81,6 @@ object PluginConfig {

const val DEFAULT_LIBS = "Libs"
const val DEFAULT_VERSIONS = "Versions"
const val INDENT_FROM_EDITOR_CONFIG = "from-editorconfig-file"
const val SPACES4 = " "
const val SPACES2 = " "
const val SPACES0 = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ object UpdateVersionsOnly {
versionsOnlyMode == VersionsOnlyMode.GRADLE_PROPERTIES -> {
val file = versionsOnlyFile ?: fromDir.resolve("gradle.properties")
if (file.canRead().not()) file.createNewFile()
Pair(file, SingleModeResult.NEW_FILE)
Pair(file, SingleModeResult.newFile(versionsOnlyMode))
}
versionsOnlyFile == null || versionsOnlyFile.canRead().not() -> Pair(
fromDir.resolve(versionsOnlyMode.suggestedFilename()),
SingleModeResult.NEW_FILE.copy(indentation = versionsOnlyMode.defaultIndent)
SingleModeResult.newFile(versionsOnlyMode)
)
else -> Pair(
versionsOnlyFile,
parseBuildFile(versionsOnlyFile, versionsOnlyMode) ?: SingleModeResult.BLOC_NOT_FOUND
parseBuildFile(versionsOnlyFile, versionsOnlyMode) ?: SingleModeResult.blockNotFound(versionsOnlyMode)
)
}

Expand Down Expand Up @@ -75,7 +75,7 @@ object UpdateVersionsOnly {

val newBlock = regenerateBlock(versionsOnlyMode, sortedDependencies, indent)

if (parseResult != SingleModeResult.BLOC_NOT_FOUND) {
if (parseResult.isBlockNotFound().not()) {
val lines = file.readLines()
val newLines = lines.subList(0, startOfBlock) + newBlock + lines.subList(endOfBlock + 1, lines.size)
file.writeText(newLines.joinWithNewlines() + "\n")
Expand Down
2 changes: 1 addition & 1 deletion plugin/src/test/kotlin/de/fayard/NonRegression.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class NonRegression : FreeSpec({
val received = approved.resolveSibling(approved.nameWithoutExtension + "-received" + approved.extension)
val message = """
|Files differ. Run:
|$ diff -u ${approved.relativeTo(buildSrcVersionsDir)} ${received.relativeTo(buildSrcVersionsDir)}
| diff -u ${approved.relativeTo(buildSrcVersionsDir)} ${received.relativeTo(buildSrcVersionsDir)}
|""".trimMargin()
return Pair(received, message)
}
Expand Down
9 changes: 4 additions & 5 deletions plugin/src/test/kotlin/de/fayard/VersionsOnlyModeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import de.fayard.VersionsOnlyMode.GRADLE_PROPERTIES
import de.fayard.VersionsOnlyMode.GROOVY_DEF
import de.fayard.VersionsOnlyMode.GROOVY_EXT
import de.fayard.VersionsOnlyMode.KOTLIN_VAL
import de.fayard.internal.PluginConfig
import de.fayard.internal.SingleModeResult
import de.fayard.internal.UpdateVersionsOnly.parseBuildFile
import de.fayard.internal.UpdateVersionsOnly.parseBuildFileOrNew
Expand Down Expand Up @@ -37,17 +36,17 @@ class VersionsOnlyModeTest: FreeSpec({
"always modify gradle.properties" {
val (file1, result1) = parseBuildFileOrNew(null, GRADLE_PROPERTIES, File("."))
file1.name shouldBe "gradle.properties"
result1 shouldBe SingleModeResult.NEW_FILE
result1 shouldBe SingleModeResult.newFile(GRADLE_PROPERTIES)

val (file2, result2) = parseBuildFileOrNew(File("gradle.properties"), GRADLE_PROPERTIES, File("."))
file2.name shouldBe "gradle.properties"
result2 shouldBe SingleModeResult.NEW_FILE
result2 shouldBe SingleModeResult.newFile(GRADLE_PROPERTIES)
}

"Create new file if needed" {
val (file, result) = parseBuildFileOrNew(null, KOTLIN_VAL, tempDir)
file shouldBe tempDir.resolve("build.gradle.kts")
result shouldBe SingleModeResult.NEW_FILE.copy(indentation = PluginConfig.SPACES4)
result shouldBe SingleModeResult.newFile(KOTLIN_VAL)
}


Expand All @@ -70,7 +69,7 @@ class VersionsOnlyModeTest: FreeSpec({
regenerateBuildFile(received, mode, deps)
withClue(
"""Files differ. Run:
|$ diff -u ${validated.relativeTo(buildSrcVersionsDir)} ${received.relativeTo(buildSrcVersionsDir)}
| diff -u ${validated.relativeTo(buildSrcVersionsDir)} ${received.relativeTo(buildSrcVersionsDir)}
|""".trimMargin()
) {
(received.readText() == validated.readText()).shouldBe(true)
Expand Down
2 changes: 1 addition & 1 deletion sample-groovy/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ resolutionStrategyConfig=verbose
# You can edit the rest of the file, it will be kept intact
# See https://github.com/jmfayard/buildSrcVersions/issues/77
plugin.com.github.ben-manes.versions=0.25.0
plugin.de.fayard.buildSrcVersions=0.6.3
plugin.de.fayard.buildSrcVersions=0.6.4
plugin.com.gradle.build-scan=2.4.2
version.gradleLatestVersion=5.6.2
version.guava=15.0
Expand Down
2 changes: 1 addition & 1 deletion sample-kotlin/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ resolutionStrategyConfig=verbose
# You can edit the rest of the file, it will be kept intact
# See https://github.com/jmfayard/buildSrcVersions/issues/77
plugin.com.github.ben-manes.versions=0.25.0
plugin.de.fayard.buildSrcVersions=0.6.3
plugin.de.fayard.buildSrcVersions=0.6.4
plugin.org.jetbrains.kotlin.jvm=1.3.50
plugin.com.gradle.build-scan=2.4.2
version.org.mongodb..mongo-java-driver=3.11.0
Expand Down
2 changes: 1 addition & 1 deletion sample-versionsOnlyMode/GROOVY_DEF.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Generated by ./gradle buildSrcVersions
// See https://github.com/jmfayard/buildSrcVersions/issues/54
def com_github_ben_manes_versions_gradle_plugin = '0.25.0'
def de_fayard_buildsrcversions_gradle_plugin = '0.6.3'
def de_fayard_buildsrcversions_gradle_plugin = '0.6.4'
def org_jetbrains_kotlin_jvm_gradle_plugin = '1.3.50'
def org_jetbrains_kotlin = '1.3.50'
def gradlelatestversion = '5.5.1' // available: '5.6.1'
Expand Down
2 changes: 1 addition & 1 deletion sample-versionsOnlyMode/GROOVY_EXT.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// See https://github.com/jmfayard/buildSrcVersions/issues/54
ext {
com_github_ben_manes_versions_gradle_plugin = '0.25.0'
de_fayard_buildsrcversions_gradle_plugin = '0.6.3'
de_fayard_buildsrcversions_gradle_plugin = '0.6.4'
org_jetbrains_kotlin_jvm_gradle_plugin = '1.3.50'
org_jetbrains_kotlin = '1.3.50'
gradlelatestversion = '5.5.1' // available: '5.6.1'
Expand Down
2 changes: 1 addition & 1 deletion sample-versionsOnlyMode/KOTLIN_VAL.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// Generated by ./gradle buildSrcVersions
// See https://github.com/jmfayard/buildSrcVersions/issues/54
val com_github_ben_manes_versions_gradle_plugin = "0.25.0"
val de_fayard_buildsrcversions_gradle_plugin = "0.6.3"
val de_fayard_buildsrcversions_gradle_plugin = "0.6.4"
val org_jetbrains_kotlin_jvm_gradle_plugin = "1.3.50"
val org_jetbrains_kotlin = "1.3.50"
val gradlelatestversion = "5.5.1" // available: "5.6.1"
Expand Down
2 changes: 1 addition & 1 deletion sample-versionsOnlyMode/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ resolutionStrategyConfig=verbose
# See https://github.com/jmfayard/buildSrcVersions/issues/77
plugin.com.github.ben-manes.versions=0.25.0
plugin.org.lovedev.greeting.kotlin=1.1
plugin.de.fayard.buildSrcVersions=0.6.3
plugin.de.fayard.buildSrcVersions=0.6.4
plugin.org.jetbrains.kotlin.jvm=1.3.50
plugin.ch.tutteli.kotlin.utils=0.29.0
plugin.com.gradle.build-scan=2.4.1
Expand Down

0 comments on commit 472e3d0

Please sign in to comment.