add InfoExtension

This commit is contained in:
2024-10-24 10:11:39 +01:00
parent 7f4b602385
commit f226f17d70
4 changed files with 101 additions and 1 deletions

View File

@@ -8,6 +8,10 @@ One Gradle plugin to rule them all, a simple thing to bind the tasks and in auto
The GitExtension enhances versioning by dynamically appending the Git hash before "snapshot" in the version string. For example, `0.0.0-SNAPSHOT` becomes `0.0.0.0a2b3c4d-SNAPSHOT`, ensuring each build reflects its commit origin, prevents overwriting existing versions. This feature aids developers during development by providing clear version identification. The GitExtension enhances versioning by dynamically appending the Git hash before "snapshot" in the version string. For example, `0.0.0-SNAPSHOT` becomes `0.0.0.0a2b3c4d-SNAPSHOT`, ensuring each build reflects its commit origin, prevents overwriting existing versions. This feature aids developers during development by providing clear version identification.
### Extension Info
The InfoExtension provides information for name and version, vendor name, and UTC timestamp.
## Releasing plugin ## Releasing plugin
Run `release.sh` script from `master` branch. Run `release.sh` script from `master` branch.

View File

@@ -1,9 +1,11 @@
package ltd.hlaeja.plugin package ltd.hlaeja.plugin
import ltd.hlaeja.plugin.extension.InfoExtension
import ltd.hlaeja.plugin.extension.GitExtension import ltd.hlaeja.plugin.extension.GitExtension
import ltd.hlaeja.plugin.extension.GitExtension.Companion.PLUGIN_NAME as GIT_PLUGIN_NAME
import org.gradle.api.Plugin import org.gradle.api.Plugin
import org.gradle.api.Project import org.gradle.api.Project
import ltd.hlaeja.plugin.extension.InfoExtension.Companion.PLUGIN_NAME as INFO_PLUGIN_NAME
import ltd.hlaeja.plugin.extension.GitExtension.Companion.PLUGIN_NAME as GIT_PLUGIN_NAME
@Suppress("unused") @Suppress("unused")
class CorePlugin : Plugin<Project> { class CorePlugin : Plugin<Project> {
@@ -13,8 +15,13 @@ class CorePlugin : Plugin<Project> {
*/ */
override fun apply(project: Project) { override fun apply(project: Project) {
gitExtension(project) gitExtension(project)
infoExtension(project)
} }
private fun infoExtension(
project: Project,
): InfoExtension = project.extensions.create(INFO_PLUGIN_NAME, InfoExtension::class.java, project)
private fun gitExtension( private fun gitExtension(
project: Project, project: Project,
): GitExtension = project.extensions.create(GIT_PLUGIN_NAME, GitExtension::class.java, project) ): GitExtension = project.extensions.create(GIT_PLUGIN_NAME, GitExtension::class.java, project)

View File

@@ -0,0 +1,16 @@
package ltd.hlaeja.plugin.extension
import java.time.OffsetDateTime.now
import java.time.ZoneId.of
import java.time.format.DateTimeFormatter.ofPattern
import org.gradle.api.Project
abstract class InfoExtension(private val project: Project) {
companion object {
const val PLUGIN_NAME = "info"
}
val nameVersion get() = "Project Name: ${project.name} Version: ${project.version}"
val utcTimestamp = now().atZoneSameInstant(of("UTC")).format(ofPattern("yyyy-MM-dd HH:mm:ss z")).toString()
val vendorName = "Hlæja Ltd"
}

View File

@@ -0,0 +1,73 @@
package ltd.hlaeja.plugin.extension
import io.mockk.every
import io.mockk.mockkStatic
import io.mockk.unmockkStatic
import java.time.OffsetDateTime
import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
class InfoExtensionTest {
companion object {
const val BASIC_TIMESTAMP = "2002-02-20T02:10:11+01:00"
const val EXTENSION = "info"
const val PLUGIN_ID = "ltd.hlaeja.plugin.hlaeja-core-plugin"
const val PROJECT_NAME = "test-project"
const val SNAPSHOT_VERSION = "0.0.0-SNAPSHOT"
@JvmStatic
@BeforeAll
fun buildUp() {
mockkStatic(OffsetDateTime::class)
every { OffsetDateTime.now() } returns OffsetDateTime.parse(BASIC_TIMESTAMP)
}
@JvmStatic
@AfterAll
fun tearDown() {
unmockkStatic(OffsetDateTime::class)
}
}
lateinit var project: Project
@BeforeEach
fun beforeEach() {
project = ProjectBuilder.builder().withName(PROJECT_NAME).build()
project.version = SNAPSHOT_VERSION
project.pluginManager.apply(PLUGIN_ID)
}
@Test
fun `get vendor`() {
// when
val extension = project.extensions.getByName(EXTENSION) as InfoExtension
// then
assertEquals("Hlæja Ltd", extension.vendorName)
}
@Test
fun `get timestamp`() {
// when
val extension = project.extensions.getByName(EXTENSION) as InfoExtension
// then
assertEquals("2002-02-20 01:10:11 UTC", extension.utcTimestamp)
}
@Test
fun `get build`() {
// when
val extension = project.extensions.getByName(EXTENSION) as InfoExtension
// then
assertEquals("Project Name: test-project Version: 0.0.0-SNAPSHOT", extension.nameVersion)
}
}