From 22e5ae82f94422ee7c869d7bdcf237a0cd638b5c Mon Sep 17 00:00:00 2001 From: Inaki Villar Date: Thu, 25 Apr 2024 22:38:05 +0200 Subject: [PATCH] update dv api --- README.md | 13 ++--- build.gradle.kts | 3 +- .../gradleprocess/InfoGradleProcessPlugin.kt | 52 ++++++++++++++++--- .../gradleprocess/output/DevelocityValues.kt | 22 ++++++++ .../gradleprocess/output/EnterpriseValues.kt | 25 +++++++++ .../InfoGradleProcessPluginTest.kt | 2 +- ...nfoGradleProcessPluginWtihBuildScanTest.kt | 51 ++++++++++++++++++ 7 files changed, 152 insertions(+), 16 deletions(-) create mode 100644 src/main/kotlin/io/github/cdsap/gradleprocess/output/DevelocityValues.kt create mode 100644 src/main/kotlin/io/github/cdsap/gradleprocess/output/EnterpriseValues.kt diff --git a/README.md b/README.md index d41e62a..7dc334e 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Apply the plugin in the main `build.gradle(.kts)` configuration file: Using the plugins DSL: ``` groovy plugins { - id("io.github.cdsap.gradleprocess") version "0.1.1" + id("io.github.cdsap.gradleprocess") version "0.1.2" } ``` @@ -20,7 +20,7 @@ buildscript { gradlePluginPortal() } dependencies { - classpath("io.github.cdsap:infogradleprocess:0.1.1") + classpath("io.github.cdsap:infogradleprocess:0.1.2") } } @@ -31,7 +31,7 @@ apply(plugin = "io.github.cdsap.gradleprocess") Using the plugins DSL: ``` groovy plugins { - id "io.github.cdsap.gradleprocess" version "0.1.1" + id "io.github.cdsap.gradleprocess" version "0.1.2" } ``` @@ -43,7 +43,7 @@ buildscript { gradlePluginPortal() } dependencies { - classpath "io.github.cdsap:infogradleprocess:0.1.1" + classpath "io.github.cdsap:infogradleprocess:0.1.2" } } @@ -77,5 +77,6 @@ BUILD SUCCESSFUL in 35s * Gradle 7.5+ ## Libraries -* com.gradle.enterprise:com.gradle.enterprise.gradle.plugin -* com.jakewharton.picnic:picnic +* `com.gradle:develocity-gradle-plugin` +* `com.gradle.enterprise:com.gradle.enterprise.gradle.plugin` +* `com.jakewharton.picnic:picnic` diff --git a/build.gradle.kts b/build.gradle.kts index 299d097..27362eb 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -6,7 +6,7 @@ plugins { } group = "io.github.cdsap" -version = "0.1.1" +version = "0.1.2" java { toolchain { @@ -19,6 +19,7 @@ dependencies { implementation("io.github.cdsap:commandline-value-source:0.1.0") implementation("com.jakewharton.picnic:picnic:0.6.0") implementation("com.gradle.enterprise:com.gradle.enterprise.gradle.plugin:3.12.3") + implementation("com.gradle:develocity-gradle-plugin:3.17.2") testImplementation("junit:junit:4.13.2") } tasks.withType().configureEach { diff --git a/src/main/kotlin/io/github/cdsap/gradleprocess/InfoGradleProcessPlugin.kt b/src/main/kotlin/io/github/cdsap/gradleprocess/InfoGradleProcessPlugin.kt index e359167..bc74598 100644 --- a/src/main/kotlin/io/github/cdsap/gradleprocess/InfoGradleProcessPlugin.kt +++ b/src/main/kotlin/io/github/cdsap/gradleprocess/InfoGradleProcessPlugin.kt @@ -1,27 +1,38 @@ package io.github.cdsap.gradleprocess +import com.gradle.develocity.agent.gradle.DevelocityConfiguration import com.gradle.scan.plugin.BuildScanExtension -import io.github.cdsap.gradleprocess.output.BuildScanOutput +import io.github.cdsap.gradleprocess.output.DevelocityValues +import io.github.cdsap.gradleprocess.output.EnterpriseValues import io.github.cdsap.jdk.tools.parser.ConsolidateProcesses +import io.github.cdsap.jdk.tools.parser.model.Process import io.github.cdsap.jdk.tools.parser.model.TypeProcess import io.github.cdsap.valuesourceprocess.jInfo import io.github.cdsap.valuesourceprocess.jStat import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.api.provider.Provider class InfoGradleProcessPlugin : Plugin { private val nameProcess = "GradleDaemon" override fun apply(target: Project) { target.gradle.rootProject { val buildScanExtension = extensions.findByType(com.gradle.scan.plugin.BuildScanExtension::class.java) - if (buildScanExtension != null) { - buildScanReporting(project, buildScanExtension) + + val develocityConfiguration = extensions.findByType(DevelocityConfiguration::class.java) + val enterpriseExtension = extensions.findByType(com.gradle.scan.plugin.BuildScanExtension::class.java) + + if (develocityConfiguration != null) { + buildScanDevelocityReporting(project, develocityConfiguration) + } else if (enterpriseExtension != null) { + buildScanEnterpriseReporting(project, enterpriseExtension) } else { consoleReporting(target) } } } + private fun consoleReporting(project: Project) { project.gradle.sharedServices.registerIfAbsent( "gradleProcessService", InfoGradleProcessBuildService::class.java @@ -31,16 +42,41 @@ class InfoGradleProcessPlugin : Plugin { }.get() } - private fun buildScanReporting( + private fun buildScanEnterpriseReporting( project: Project, buildScanExtension: BuildScanExtension ) { - val jStat = project.jStat(nameProcess) - val jInfo = project.jInfo(nameProcess) + val (jStat, jInfo) = providerPair(project) buildScanExtension.buildFinished { - val processes = ConsolidateProcesses().consolidate(jStat.get(), jInfo.get(), TypeProcess.Kotlin) - BuildScanOutput(buildScanExtension, processes).addProcessesInfoToBuildScan() + val processes = processes(jStat, jInfo) + EnterpriseValues(buildScanExtension, processes).addProcessesInfoToBuildScan() } } + + private fun buildScanDevelocityReporting( + project: Project, + buildScanExtension: DevelocityConfiguration + ) { + val (jStat, jInfo) = providerPair(project) + + buildScanExtension.buildScan.buildFinished { + val processes = processes(jStat, jInfo) + DevelocityValues(buildScanExtension, processes).addProcessesInfoToBuildScan() + } + } + + private fun processes( + jStat: Provider, + jInfo: Provider + ): List { + val processes = ConsolidateProcesses().consolidate(jStat.get(), jInfo.get(), TypeProcess.Kotlin) + return processes + } + + private fun providerPair(project: Project): Pair, Provider> { + val jStat = project.jStat(nameProcess) + val jInfo = project.jInfo(nameProcess) + return Pair(jStat, jInfo) + } } diff --git a/src/main/kotlin/io/github/cdsap/gradleprocess/output/DevelocityValues.kt b/src/main/kotlin/io/github/cdsap/gradleprocess/output/DevelocityValues.kt new file mode 100644 index 0000000..68fc428 --- /dev/null +++ b/src/main/kotlin/io/github/cdsap/gradleprocess/output/DevelocityValues.kt @@ -0,0 +1,22 @@ +package io.github.cdsap.gradleprocess.output + +import com.gradle.develocity.agent.gradle.DevelocityConfiguration +import io.github.cdsap.jdk.tools.parser.model.Process +class DevelocityValues( + private val develocityConfiguration: DevelocityConfiguration, + private val processes: List, +) { + + fun addProcessesInfoToBuildScan() { + processes.map { + develocityConfiguration.buildScan { + value("Gradle-Process-${it.pid}-max", "${it.max} GB") + value("Gradle-Process-${it.pid}-usage", "${it.usage} GB") + value("Gradle-Process-${it.pid}-capacity", "${it.capacity} GB") + value("Gradle-Process-${it.pid}-uptime", "${it.uptime} minutes") + value("Gradle-Process-${it.pid}-gcTime", "${it.gcTime} minutes") + value("Gradle-Process-${it.pid}-gcType", it.typeGc) + } + } + } +} diff --git a/src/main/kotlin/io/github/cdsap/gradleprocess/output/EnterpriseValues.kt b/src/main/kotlin/io/github/cdsap/gradleprocess/output/EnterpriseValues.kt new file mode 100644 index 0000000..cbc57e3 --- /dev/null +++ b/src/main/kotlin/io/github/cdsap/gradleprocess/output/EnterpriseValues.kt @@ -0,0 +1,25 @@ +package io.github.cdsap.gradleprocess.output + +import com.gradle.develocity.agent.gradle.DevelocityConfiguration +import io.github.cdsap.jdk.tools.parser.model.Process + +import com.gradle.scan.plugin.BuildScanExtension + +class EnterpriseValues( + private val buildScanExtension: BuildScanExtension, + private val processes: List, +) { + + fun addProcessesInfoToBuildScan() { + processes.map { + with(buildScanExtension) { + value("Gradle-Process-${it.pid}-max", "${it.max} GB") + value("Gradle-Process-${it.pid}-usage", "${it.usage} GB") + value("Gradle-Process-${it.pid}-capacity", "${it.capacity} GB") + value("Gradle-Process-${it.pid}-uptime", "${it.uptime} minutes") + value("Gradle-Process-${it.pid}-gcTime", "${it.gcTime} minutes") + value("Gradle-Process-${it.pid}-gcType", it.typeGc) + } + } + } +} diff --git a/src/test/kotlin/io/github/cdsap/gradleprocess/InfoGradleProcessPluginTest.kt b/src/test/kotlin/io/github/cdsap/gradleprocess/InfoGradleProcessPluginTest.kt index 474f8ea..c97601d 100644 --- a/src/test/kotlin/io/github/cdsap/gradleprocess/InfoGradleProcessPluginTest.kt +++ b/src/test/kotlin/io/github/cdsap/gradleprocess/InfoGradleProcessPluginTest.kt @@ -10,7 +10,7 @@ import org.junit.rules.TemporaryFolder class InfoGradleProcessPluginTest { - private val gradleVersions = listOf("7.6", "8.0.1", "8.1.1") + private val gradleVersions = listOf("7.6.2", "8.1.1", "8.6", "8.7") @Rule @JvmField diff --git a/src/test/kotlin/io/github/cdsap/gradleprocess/InfoGradleProcessPluginWtihBuildScanTest.kt b/src/test/kotlin/io/github/cdsap/gradleprocess/InfoGradleProcessPluginWtihBuildScanTest.kt index 91fea08..e5ba588 100644 --- a/src/test/kotlin/io/github/cdsap/gradleprocess/InfoGradleProcessPluginWtihBuildScanTest.kt +++ b/src/test/kotlin/io/github/cdsap/gradleprocess/InfoGradleProcessPluginWtihBuildScanTest.kt @@ -65,4 +65,55 @@ class InfoGradleProcessPluginWtihBuildScanTest { TestCase.assertTrue(secondBuild.output.contains("Configuration cache entry reused.")) } } + + @Test + fun testPluginIsCompatibleWithConfigurationCacheWithDevelocity() { + Assume.assumeTrue( + "Gradle Enterprise URL and Access Key are set", + System.getenv("GE_URL") != null && System.getenv("GE_API_KEY") != null + ) + + testProjectDir.newFile("settings.gradle").appendText( + """ + plugins { + id 'com.gradle.develocity' version '3.17.2' + } + develocity { + server = "${System.getenv("GE_URL")}" + accessKey="${System.getenv("GE_API_KEY")}" + buildScan { + publishing { true } + } + } + """.trimIndent() + ) + testProjectDir.newFile("build.gradle").appendText( + """ + plugins { + id 'org.jetbrains.kotlin.jvm' version '1.7.21' + id 'application' + id 'io.github.cdsap.gradleprocess' + } + repositories { + mavenCentral() + } + """.trimIndent() + ) + listOf("8.1.1").forEach { + val firstBuild = GradleRunner.create() + .withProjectDir(testProjectDir.root) + .withArguments("compileKotlin", "--configuration-cache") + .withPluginClasspath() + .withGradleVersion(it) + .build() + val secondBuild = GradleRunner.create() + .withProjectDir(testProjectDir.root) + .withArguments("compileKotlin", "--configuration-cache") + .withPluginClasspath() + .withGradleVersion(it) + .build() + TestCase.assertTrue(firstBuild.output.contains("Configuration cache entry stored")) + TestCase.assertTrue(secondBuild.output.contains("Configuration cache entry reused.")) + } + } }