Skip to content

Commit

Permalink
Add map function to LazyVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
JavierSegoviaCordoba committed Jun 6, 2023
1 parent 16af831 commit 777577a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Added

- `map` function to `LazyVersion`

### Changed

### Deprecated
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,16 @@ semver: 1.0.1
./gradlew "-Psemver.checkClean=false"
semver: 1.0.0.23+1a2cd5b2 # 1a2cd5b2 is the last commit hash
```

### Utilities

The `project.version` is set as `LazyVersion`, it is possible so after casting it to `LazyVersion`,
it is possible to access to the `map` function which allows modifying the version. This can be
useful
to add a suffix to the version, for example, it is a common practice in Kotlin Compiler plugins to
attach the compatible Kotlin version, for example `1.0.0-1.8.21` or `1.0.0-alpha.2-1.8.21`.

```kotlin
val kotlin: String = getKotlinVersion()
(version as LazyVersion).map { semver -> "$semver-$kotlin" }
```
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public final class com/javiersc/semver/project/gradle/plugin/Commit {

public final class com/javiersc/semver/project/gradle/plugin/LazyVersion {
public fun <init> (Lorg/gradle/api/provider/Provider;)V
public final fun map (Lkotlin/jvm/functions/Function1;)V
public fun toString ()Ljava/lang/String;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ public class LazyVersion(internal val version: Provider<String>) {

private var cachedVersion: String? = null

public fun map(transform: (String) -> String) {
cachedVersion = transform(version.get())
}

override fun toString(): String {
if (cachedVersion == null) cachedVersion = version.get()
cachedVersion ?: version.get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ public class SemverProjectPlugin : Plugin<Project> {

// It is possible third party plugin breaks lazy configuration by calling `project.version`
// too early, applying the calculated version in `afterEvaluate` fix it sometimes.
afterEvaluate { version = LazyVersion(VersionValueSource.register(this)) }
afterEvaluate { it.version = LazyVersion(VersionValueSource.register(this)) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import io.kotest.matchers.comparables.shouldBeGreaterThan
import io.kotest.matchers.comparables.shouldBeLessThan
import io.kotest.matchers.nulls.shouldNotBeNull
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldEndWith
import io.kotest.matchers.string.shouldNotBeEmpty
import io.kotest.matchers.string.shouldStartWith
import java.time.Instant
import kotlin.test.Test
import kotlin.time.Duration.Companion.seconds
Expand Down Expand Up @@ -57,6 +59,9 @@ class SemverIntegrationTest : GradleProjectTest() {
}

tasks.names.contains("printSemver")
(version as LazyVersion).map { version -> "$version-test" }
version.toString().shouldStartWith("1.0.0").shouldEndWith("-test")
afterEvaluate { it.version.toString().shouldStartWith("1.0.0").shouldEndWith("-test") }
}
}
}

0 comments on commit 777577a

Please sign in to comment.