Skip to content

Commit

Permalink
Release 1.5.4
Browse files Browse the repository at this point in the history
  • Loading branch information
Virtlink committed Jul 15, 2024
1 parent 17ae8cf commit 5de98c6
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 96 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ All notable changes to this project are documented in this file, based on [Keep
## [Unreleased]


## [1.5.3] - 2024-07-15
- The Gitonium extension now consists of only lazy properties.


## [1.5.3] - 2024-07-15
- No changes.

Expand Down
11 changes: 7 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ plugins {
}


gitonium {
mainBranch.set("master")
setVersion = false
setSubprojectVersions = false
}

group = "org.metaborg"
version = gitonium.version
description = "A Git-based versioning plugin for Gradle."
extra["isReleaseVersion"] = !version.toString().endsWith("-SNAPSHOT")

Expand Down Expand Up @@ -50,10 +57,6 @@ mavenPublishConvention {
}
}

gitonium {
mainBranch.set("master")
}


// Normally, when you execute a task such as `test` in a multi-project build, you will execute
// all `:test` tasks in all projects. In contrast, when you specifically execute `:test`
Expand Down
37 changes: 35 additions & 2 deletions docs/content/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,47 @@ Apply the gitonium plugin to a project (a `build.gradle(.kts)` file) as follows:
=== "Kotlin"
```kotlin title="build.gradle.kts"
plugins {
id("org.metaborg.gitonium") version "1.3.1"
id("org.metaborg.gitonium") version "<version>"
}

version = gitonium.version
```

=== "Groovy"
```groovy title="build.gradle"
plugins {
id "org.metaborg.gitonium" version "1.3.1"
id "org.metaborg.gitonium" version "<version>"
}

version = gitonium.version
```


## Configuring the plugin
To configure the plugin, configure it before the `version = ` assignment. For example:

=== "Kotlin"
```kotlin title="build.gradle.kts"
plugins {
id("org.metaborg.gitonium") version "<version>"
}

gitonium {
mainBranch.set("master")
}

version = gitonium.version
```

=== "Groovy"
```groovy title="build.gradle"
plugins {
id "org.metaborg.gitonium" version "<version>"
}

gitonium {
mainBranch = "master"
}

version = gitonium.version
```
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pluginManagement {
}

plugins {
id("org.metaborg.convention.settings") version "0.6.8"
id("org.metaborg.convention.settings") version "0.6.7"
}


Expand Down
133 changes: 62 additions & 71 deletions src/main/kotlin/mb/gitonium/GitoniumExtension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,87 +3,85 @@ package mb.gitonium
import org.gradle.api.Project
import org.gradle.api.file.RegularFile
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.internal.enterprise.test.FileProperty
import org.gradle.kotlin.dsl.property
import java.util.regex.Pattern
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import javax.inject.Inject

/** Extension for configuring the Gitonium plugin. */
@Suppress("unused")
open class GitoniumExtension(private val project: Project) {
private val LOG: Logger = LoggerFactory.getLogger(GitoniumExtension::class.java)

/**
* Configuration for the Gitonium plugin.
*
* Create an instance like this:
*
* ```kotlin
* extensions.create<GitoniumExtension>("gitonium")
* ```
*/
open class GitoniumExtension @Inject constructor(
/** The project itself. */
project: Project,
/** The Gradle object factory. */
objects: ObjectFactory,
){
/** The prefix to use to match release tags. */
var tagPrefix: String = "release-"
/** The pattern to use to match release tags. Deprecated. */
@Deprecated("Use tagPrefix instead.", ReplaceWith("tagPrefix"))
var tagPattern: Pattern? = null

/** The suffix to use for dirty (release or snapshot) versions; or an empty string to use no suffix. */
var dirtySuffix: String = "dirty"
val tagPrefix: Property<String> = objects.property(String::class.java)
.convention("release-")
/** The major increase for snapshot versions. */
var snapshotMajorIncrease: Int = 0
set(value) {
require(value >= 0) { "Snapshot major increase must be non-negative." }
field = value
}
val snapshotMajorIncrease: Property<Int> = objects.property(Int::class.java)
.convention(0)
/** The minor increase for snapshot versions. */
var snapshotMinorIncrease: Int = 0
set(value) {
require(value >= 0) { "Snapshot minor increase must be non-negative." }
field = value
}
val snapshotMinorIncrease: Property<Int> = objects.property(Int::class.java)
.convention(0)
/** The patch increase for snapshot versions. */
var snapshotPatchIncrease: Int = 1
set(value) {
require(value >= 0) { "Snapshot patch increase must be non-negative." }
field = value
}
val snapshotPatchIncrease: Property<Int> = objects.property(Int::class.java)
.convention(1)
/** The suffix to use for snapshot versions; or an empty string to use no suffix. */
var snapshotSuffix: String = "SNAPSHOT"
val snapshotSuffix: Property<String> = objects.property(String::class.java)
.convention("SNAPSHOT")
/** The suffix to use for dirty (release or snapshot) versions; or an empty string to use no suffix. */
val dirtySuffix: Property<String> = objects.property(String::class.java)
.convention("dirty")
/** Whether to include the branch name in snapshot versions. */
var snapshotIncludeBranch: Boolean = true
val snapshotIncludeBranch: Property<Boolean> = objects.property(Boolean::class.java)
.convention(true)
/** Whether to consider the first parent only when looking for tags across merge commits. */
var firstParentOnly: Boolean = false

/** Whether to set the version on the root project. */
var setVersion: Boolean = true
/** Whether to set the version on the subprojects. */
var setSubprojectVersions: Boolean = true
val firstParentOnly: Property<Boolean> = objects.property(Boolean::class.java)
.convention(false)
/** Whether to check for SNAPSHOT dependencies when publishing a release. */
var checkSnapshotDependenciesInRelease: Boolean = true

val checkSnapshotDependenciesInRelease: Property<Boolean> = objects.property(Boolean::class.java)
.convention(true)
/** Whether to always create a snapshot version string, even if the HEAD points to a release tag. */
var alwaysSnapshotVersion: Boolean = false

val alwaysSnapshotVersion: Property<Boolean> = objects.property(Boolean::class.java)
.convention(false)
/** The name of the main branch. */
val mainBranch: Property<String> = project.objects.property<String>()

val mainBranch: Property<String> = objects.property(String::class.java)
.convention("main")
/** A properties file to write the build and version info to, or unset to not write. */
val buildPropertiesFile: RegularFileProperty = project.objects.fileProperty()
val buildPropertiesFile: RegularFileProperty = objects.fileProperty()
.convention(null as RegularFile?)
/** Whether to set the version on the root project. */
var setVersion: Property<Boolean> = objects.property(Boolean::class.java)
.convention(true)
/** Whether to set the version on the subprojects. */
var setSubprojectVersions: Property<Boolean> = objects.property(Boolean::class.java)
.convention(true)

/** The version info, determined lazily. */
val versionInfo: GitoniumVersion by lazy {
@Suppress("DEPRECATION") val prefix = tagPattern?.let {
// For backwards compatibility, we allow tagPattern to be set if it consists of a prefix and a suffix of `(.+)`.
val patternStr = it.pattern()
if (patternStr.endsWith("(.+)")) {
patternStr.substringBeforeLast("(.+)")
} else {
throw IllegalArgumentException("tagPattern is no longer supported, use tagPrefix.")
}
} ?: tagPrefix

GitoniumVersion.determineVersion(
project.rootDir,
prefix,
dirtySuffix,
snapshotMajorIncrease,
snapshotMinorIncrease,
snapshotPatchIncrease,
snapshotSuffix,
snapshotIncludeBranch,
firstParentOnly,
alwaysSnapshotVersion,
tagPrefix.get(),
dirtySuffix.get(),
snapshotMajorIncrease.get(),
snapshotMinorIncrease.get(),
snapshotPatchIncrease.get(),
snapshotSuffix.get(),
snapshotIncludeBranch.get(),
firstParentOnly.get(),
alwaysSnapshotVersion.get(),
mainBranch.getOrNull(),
)
}
Expand All @@ -100,7 +98,7 @@ open class GitoniumExtension(private val project: Project) {
val version: String by lazy {
val versionString = versionInfo.versionString
if (versionString == null) {
project.logger.warn("Gitonium could not determine version from Git repository, using default version.")
LOG.warn("Gitonium could not determine version from Git repository, using default version.")
return@lazy Project.DEFAULT_VERSION
}
versionString
Expand All @@ -111,12 +109,5 @@ open class GitoniumExtension(private val project: Project) {

/** Whether the repository is dirty (i.e., has uncommitted changes). */
val isDirty: Boolean get() = versionInfo.isDirty

/**
* Sets the convention (default values) for the configuration extension.
*/
fun setConvention() {
mainBranch.convention("main")
buildPropertiesFile.convention(null as RegularFile?)
}
}

31 changes: 15 additions & 16 deletions src/main/kotlin/mb/gitonium/GitoniumPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package mb.gitonium
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.publish.maven.tasks.PublishToMavenRepository
import org.gradle.kotlin.dsl.register
import org.gradle.kotlin.dsl.withType
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
Expand All @@ -14,10 +16,16 @@ import java.util.*
class GitoniumPlugin : Plugin<Project> {
override fun apply(project: Project) {
// Create and add extension
val extension = GitoniumExtension(project)
extension.setConvention()
val extension = GitoniumExtension(project, project.objects)
project.extensions.add("gitonium", extension)

// Configure the version
// The value will be computed and cached when LazyGitoniumVersion.toString() is called for the first time.
project.version = LazyGitoniumVersion(extension, false)
project.subprojects.forEach { subproject ->
subproject.version = LazyGitoniumVersion(extension, true)
}

// Register tasks
registerCheckSnapshotDependenciesTask(project, extension)
registerPrintVersionTask(project)
Expand All @@ -28,19 +36,6 @@ class GitoniumPlugin : Plugin<Project> {
registerPrintVersionTask(subproject)
registerAssertNotDirtyTask(subproject)
registerWriteBuildPropertiesTask(subproject, extension)

subproject.afterEvaluate {
if (extension.setSubprojectVersions) {
subproject.version = LazyGitoniumVersion(extension, true)
}
}
}

project.afterEvaluate {
// Set project version
if (extension.setVersion) {
project.version = LazyGitoniumVersion(extension, false)
}
}
}

Expand All @@ -51,7 +46,6 @@ class GitoniumPlugin : Plugin<Project> {
* @param extension The Gitonium extension, used for the configuration.
*/
private fun registerCheckSnapshotDependenciesTask(project: Project, extension: GitoniumExtension) {
if (!extension.checkSnapshotDependenciesInRelease) return
val checkTask = project.tasks.register<CheckSnapshotDependencies>("checkSnapshotDependencies", extension)
project.tasks.named("checkSnapshotDependencies") {
group = "Verification"
Expand All @@ -62,6 +56,11 @@ class GitoniumPlugin : Plugin<Project> {
dependsOn(checkTask)
}
}
project.gradle.taskGraph.whenReady {
project.tasks.withType<CheckSnapshotDependencies>().configureEach {
onlyIf { extension.checkSnapshotDependenciesInRelease.get() }
}
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/mb/gitonium/LazyGitoniumVersion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class LazyGitoniumVersion(
) {
override fun toString(): String {
return when {
extension.setVersion && !isSubProject -> extension.version
extension.setSubprojectVersions && isSubProject -> extension.version
extension.setVersion.get() && !isSubProject -> extension.version
extension.setSubprojectVersions.get() && isSubProject -> extension.version
else -> Project.DEFAULT_VERSION
}
}
Expand Down

0 comments on commit 5de98c6

Please sign in to comment.