Skip to content

Commit ab97904

Browse files
Make version a lazily evaluated property (#71)
In this changeset, we allow configuring JxBrowser version with a [lazily evaluated value](https://docs.gradle.org/current/userguide/properties_providers.html) by changing `version` field to be a lazy `Property`. ## Problem The Gradle [documentation states](https://docs.gradle.org/current/userguide/properties_providers.html): > When implementing a custom task or plugin, it’s imperative that you use these lazy properties. We use regular `String` properties instead. That is OK when a version is a string literal in `build.gradle.kts`. But it's not OK, when the value is taken from elsewhere. For example, from the version catalog: ``` jxbrowser { // Calling `get()` this way is a bad practice, because the value may not be available yet. version = libs.versions.jxbrowser.asProvider().get() } ``` ## Solution We change the `version` field from `String` to `Property<String>`. We don't change other fields because they're less likely to come from the lazily evaluated sources. And we want to avoid breaking changes. More on this in the next section. ## Backward compatibility The plug-in stays backward compatible for Gradle 8.2 and newer. No changes are required, because of [`Property::assign`](https://github.com/gradle/gradle/blob/fcef0ec368956438f2c1272b64bad82c996ab236/platforms/core-configuration/kotlin-dsl/src/main/kotlin/org/gradle/kotlin/dsl/PropertyExtensions.kt#L41) extension method. Users of older Gradle versions, will need to replace `version = "8.2.2"` with `version.set("8.2.2")`. Otherwise, the build will fail. ## Version bump Since this change is breaking for some of the users, we bump the version to `2.0.0`.
1 parent 7843dcd commit ab97904

6 files changed

Lines changed: 61 additions & 15 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ A Gradle plug-in that provides convenience methods for adding JxBrowser dependen
88
import com.teamdev.jxbrowser.gradle.Repository
99

1010
plugins {
11-
id("com.teamdev.jxbrowser") version "1.2.1"
11+
id("com.teamdev.jxbrowser") version "2.0.0"
1212
}
1313

1414
jxbrowser {
1515
// The JxBrowser version (required).
1616
// Obtain the latest release version number at https://teamdev.com/jxbrowser/.
1717
version = "8.2.2"
18+
19+
// If you're using Gradle 8.1.1 or older, use the following syntax:
20+
// version.set("8.2.2")
1821

1922
// The location of JxBrowser repository to use (optional).
2023
// It's either North America or Europe.

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import org.jetbrains.kotlin.gradle.dsl.JvmTarget
2222

2323
object BuildSettings {
2424
const val GROUP = "com.teamdev.jxbrowser"
25-
const val VERSION = "1.2.1"
25+
const val VERSION = "2.0.0"
2626
const val JXBROWSER_VERSION = "8.2.2"
2727
val javaVersion = JavaVersion.VERSION_1_8
2828
}

src/main/kotlin/com/teamdev/jxbrowser/gradle/JxBrowserExtension.kt

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ import com.teamdev.jxbrowser.gradle.Environment.isX64Bit
2929
import com.teamdev.jxbrowser.gradle.Environment.jvmArch
3030
import com.teamdev.jxbrowser.gradle.Environment.osName
3131
import com.vdurmont.semver4j.Semver
32+
import org.gradle.api.InvalidUserDataException
3233
import org.gradle.api.Project
34+
import org.gradle.api.provider.Property
3335
import org.gradle.api.provider.Provider
3436

3537
/**
@@ -40,12 +42,20 @@ import org.gradle.api.provider.Provider
4042
* and various JxBrowser dependencies based on your project's needs.
4143
*/
4244
public open class JxBrowserExtension(private val project: Project) {
45+
init {
46+
project.afterEvaluate {
47+
if (!version.isPresent) {
48+
throw InvalidUserDataException("JxBrowser version is not specified.")
49+
}
50+
}
51+
}
52+
4353
/**
4454
* A version of the JxBrowser.
4555
*
4656
* This is a mandatory field.
4757
*/
48-
public var version: String = ""
58+
public val version: Property<String> = project.objects.property(String::class.java)
4959

5060
/**
5161
* The preferred location of the JxBrowser repository.
@@ -175,26 +185,29 @@ public open class JxBrowserExtension(private val project: Project) {
175185
return platformMap.entries.firstOrNull { it.key() }?.value
176186
?: project.providers.provider {
177187
val currentPlatform = "${osName()} ${jvmArch()}"
178-
val errorMessage = "The current $currentPlatform platform is not supported by JxBrowser $version."
188+
val errorMessage = "The current $currentPlatform platform is not supported by JxBrowser ${version.get()}"
179189
throw IllegalStateException(errorMessage)
180190
}
181191
}
182192

183-
private fun artifact(shortName: String) =
184-
project.providers.provider {
185-
check(version.isNotBlank()) { "JxBrowser version is not specified." }
186-
checkArtifactSupported(shortName)
193+
private fun artifact(shortName: String): Provider<String> {
194+
return version.map { versionValue ->
195+
checkArtifactSupported(shortName, versionValue)
187196
if (shortName == "core") {
188-
"$GROUP:jxbrowser:$version"
197+
"$GROUP:jxbrowser:$versionValue"
189198
} else {
190-
"$GROUP:jxbrowser-$shortName:$version"
199+
"$GROUP:jxbrowser-$shortName:$versionValue"
191200
}
192201
}
202+
}
193203

194204
/**
195205
* Checks if the artifact with [shortName] exists in JxBrowser [version].
196206
*/
197-
private fun checkArtifactSupported(shortName: String) {
207+
private fun checkArtifactSupported(
208+
shortName: String,
209+
version: String,
210+
) {
198211
val artifactNameToReleaseVersion =
199212
mapOf(
200213
"compose" to "8.0.0",

src/main/kotlin/com/teamdev/jxbrowser/gradle/JxBrowserPlugin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import org.gradle.api.Project
3636
*
3737
* jxbrowser {
3838
* // Obtain the latest release version number at https://teamdev.com/jxbrowser/.
39-
* version = "7.36"
39+
* version = "8.2.2"
4040
*
4141
* // Use JxBrowser repository at specific location. It's North America by default.
4242
* repository = Repository.NORTH_AMERICA

src/test/kotlin/com/teamdev/jxbrowser/gradle/JxBrowserPluginFunctionalTest.kt

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ package com.teamdev.jxbrowser.gradle
2222

2323
import io.kotest.matchers.collections.shouldContainExactlyInAnyOrder
2424
import io.kotest.matchers.shouldBe
25+
import io.kotest.matchers.string.shouldContain
2526
import org.gradle.testkit.runner.BuildResult
2627
import org.gradle.testkit.runner.GradleRunner
2728
import org.gradle.testkit.runner.TaskOutcome.SUCCESS
@@ -217,14 +218,42 @@ internal class JxBrowserPluginFunctionalTest {
217218
""".trimIndent(),
218219
)
219220

221+
val failure =
222+
assertFails {
223+
GradleRunner.create()
224+
.withProjectDir(testProjectDir)
225+
.withPluginClasspath()
226+
.withArguments("build")
227+
.build()
228+
}
229+
failure.message shouldContain "is not supported by JxBrowser"
230+
}
231+
}
232+
233+
@Test
234+
fun `require JxBrowser version`() {
235+
buildFile.writeText(
236+
"""
237+
plugins {
238+
base
239+
id("com.teamdev.jxbrowser")
240+
}
241+
242+
jxbrowser {
243+
includePreviewBuilds()
244+
}
245+
""".trimIndent(),
246+
)
247+
248+
val failure =
220249
assertFails {
221250
GradleRunner.create()
222251
.withProjectDir(testProjectDir)
223252
.withPluginClasspath()
224-
.withArguments("build")
253+
.withArguments("check")
225254
.build()
226255
}
227-
}
256+
failure.message shouldContain "JxBrowser version is not specified"
228257
}
229258

230259
private fun BuildResult.outcome(taskName: String) = this.task(taskName)!!.outcome

src/test/kotlin/com/teamdev/jxbrowser/gradle/JxBrowserPluginTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ internal class JxBrowserPluginTest {
5454
@Test
5555
fun `resolve dependencies`() {
5656
with(extension) {
57-
version = jxBrowserVersion
57+
version.set(jxBrowserVersion)
5858
val group = "com.teamdev.jxbrowser"
5959

6060
swt.get() shouldBe "$group:jxbrowser-swt:$jxBrowserVersion"
@@ -78,6 +78,7 @@ internal class JxBrowserPluginTest {
7878
mavenRepositoryUrls() shouldContain customRepository
7979

8080
val eapRepository = "https://europe-maven.pkg.dev/jxbrowser/eaps"
81+
extension.version.set("8.2.2")
8182
extension.includePreviewBuilds()
8283
project.evaluationDependsOn(":")
8384
mavenRepositoryUrls() shouldContain eapRepository

0 commit comments

Comments
 (0)