From 982a4fbda0120883733ba7b2a2418c71c26c6a98 Mon Sep 17 00:00:00 2001 From: Yahor Berdnikau Date: Wed, 26 Apr 2023 09:03:45 +0200 Subject: [PATCH] Print in the console report showing amount of tasks used K2 ^KT-57736 Fixed --- .../kotlin/gradle/experimental/TryK2IT.kt | 73 +++++++++++++++++++ .../kotlin/gradle/report/BuildReportType.kt | 6 +- .../gradle/report/BuildReportsService.kt | 31 ++++++++ .../kotlin/gradle/report/ReportingSettings.kt | 1 + .../kotlin/gradle/report/configureReporing.kt | 14 +++- 5 files changed, 119 insertions(+), 6 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/experimental/TryK2IT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/experimental/TryK2IT.kt index 3bcb3f22039..3aa1bd45b65 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/experimental/TryK2IT.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/experimental/TryK2IT.kt @@ -30,6 +30,7 @@ class TryK2IT : KGPBaseTest() { build("--dry-run") { assertOutputContainsExactTimes(EXPERIMENTAL_TRY_K2_WARNING_MESSAGE, 1) + assertOutputContains("No Kotlin compilation tasks have run") } } } @@ -53,6 +54,78 @@ class TryK2IT : KGPBaseTest() { } } + @DisplayName("JVM: build report is not printed") + @JvmGradlePluginTests + @GradleTest + fun buildReportNotPrinted(gradleVersion: GradleVersion) { + project( + "incrementalMultiproject", + gradleVersion, + buildOptions = defaultBuildOptions.copy(logLevel = LogLevel.WARN) + ) { + build("build") { + assertOutputDoesNotContain( + "##### 'kotlin.experimental.tryK2' results (Kotlin/Native not checked) #####" + ) + } + } + } + + @DisplayName("JVM: report is printed at the end of the build") + @JvmGradlePluginTests + @GradleTest + fun buildReport(gradleVersion: GradleVersion) { + project( + "incrementalMultiproject", + gradleVersion, + buildOptions = defaultBuildOptions.copy(logLevel = LogLevel.WARN) + ) { + enableTryK2() + + build("build") { + assertOutputContains( + """ + |##### 'kotlin.experimental.tryK2' results (Kotlin/Native not checked) ##### + |:lib:compileKotlin: 2.0 language version + |:app:compileKotlin: 2.0 language version + |##### 100% (2/2) tasks have compiled with Kotlin 2 ##### + """.trimMargin().normalizeLineEndings() + ) + } + } + } + + @DisplayName("JVM: report is printed at the end of the build in case of compilation error") + @JvmGradlePluginTests + @GradleTest + fun buildReportOnCompilationError(gradleVersion: GradleVersion) { + project( + "incrementalMultiproject", + gradleVersion, + buildOptions = defaultBuildOptions.copy(logLevel = LogLevel.WARN) + ) { + enableTryK2() + + subProject("app").kotlinSourcesDir().resolve("foo/AA.kt").appendText( + """ + | + |ZZZZ + """.trimMargin() + ) + + buildAndFail("build", forceOutput = true) { + assertOutputContains( + """ + |##### 'kotlin.experimental.tryK2' results (Kotlin/Native not checked) ##### + |:lib:compileKotlin: 2.0 language version + |:app:compileKotlin: 2.0 language version + |##### 100% (2/2) tasks have compiled with Kotlin 2 ##### + """.trimMargin().normalizeLineEndings() + ) + } + } + } + @DisplayName("MPP: language version default is changed to 2.0") @MppGradlePluginTests @GradleTest diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/report/BuildReportType.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/report/BuildReportType.kt index ad42f20a8c3..8f15e133ea0 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/report/BuildReportType.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/report/BuildReportType.kt @@ -11,9 +11,11 @@ enum class BuildReportType : Serializable { FILE, HTTP, BUILD_SCAN, - SINGLE_FILE; + SINGLE_FILE, + TRY_K2_CONSOLE, + ; companion object { - const val serialVersionUID: Long = 0 + const val serialVersionUID: Long = 1L } } \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/report/BuildReportsService.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/report/BuildReportsService.kt index 4876b99e45f..a75b40ac6e0 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/report/BuildReportsService.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/report/BuildReportsService.kt @@ -16,6 +16,7 @@ import org.jetbrains.kotlin.build.report.statistics.BuildFinishStatisticsData import org.jetbrains.kotlin.build.report.statistics.CompileStatisticsData import org.jetbrains.kotlin.build.report.statistics.BuildStartParameters import org.jetbrains.kotlin.build.report.statistics.StatTag +import org.jetbrains.kotlin.gradle.dsl.KotlinVersion import org.jetbrains.kotlin.gradle.report.data.BuildExecutionData import org.jetbrains.kotlin.gradle.report.data.BuildOperationRecord import org.jetbrains.kotlin.utils.addToStdlib.measureTimeMillisWithResult @@ -92,6 +93,10 @@ class BuildReportsService { MetricsWriter(singleOutputFile.absoluteFile).process(buildData, log) } + if (reportingSettings.experimentalTryK2ConsoleOutput) { + reportTryK2ToConsole(buildData) + } + //It's expected that bad internet connection can cause a significant delay for big project executorService.shutdown() } @@ -227,6 +232,32 @@ class BuildReportsService { customValues++ } + private fun reportTryK2ToConsole( + data: BuildExecutionData + ) { + val tasksData = data.buildOperationRecord + .filterIsInstance() + .filter { + // Filtering by only KGP tasks and by those that actually do compilation + it.isFromKotlinPlugin && it.kotlinLanguageVersion != null + } + log.warn("##### 'kotlin.experimental.tryK2' results (Kotlin/Native not checked) #####") + if (tasksData.isEmpty()) { + log.warn("No Kotlin compilation tasks have run") + log.warn("#####") + } else { + val tasksCountWithKotlin2 = tasksData.count { + it.kotlinLanguageVersion != null && it.kotlinLanguageVersion >= KotlinVersion.KOTLIN_2_0 + } + val taskWithK2Percent = (tasksCountWithKotlin2 * 100) / tasksData.count() + val statsData = tasksData.map { it.path to it.kotlinLanguageVersion?.version } + statsData.forEach { record -> + log.warn("${record.first}: ${record.second} language version") + } + log.warn("##### $taskWithK2Percent% ($tasksCountWithKotlin2/${tasksData.count()}) tasks have compiled with Kotlin 2 #####") + } + } + private fun readableString(data: CompileStatisticsData): List { val readableString = StringBuilder() if (data.nonIncrementalAttributes.isEmpty()) { diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/report/ReportingSettings.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/report/ReportingSettings.kt index 61c9d6ce964..6fbef7b8ef8 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/report/ReportingSettings.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/report/ReportingSettings.kt @@ -18,6 +18,7 @@ data class ReportingSettings( val httpReportSettings: HttpReportSettings? = null, val buildScanReportSettings: BuildScanSettings? = null, val singleOutputFile: File? = null, + val experimentalTryK2ConsoleOutput: Boolean = false, val includeCompilerArguments: Boolean = false, ) : Serializable { companion object { diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/report/configureReporing.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/report/configureReporing.kt index f17d823832d..d1c5cd84739 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/report/configureReporing.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/report/configureReporing.kt @@ -20,10 +20,15 @@ private val availableMetrics = BuildTime.values().map { it.name } + BuildPerform internal fun reportingSettings(project: Project): ReportingSettings { val properties = PropertiesProvider(project) - val buildReportOutputTypes = properties.buildReportOutputs.map { - BuildReportType.values().firstOrNull { brt -> brt.name == it.trim().toUpperCaseAsciiOnly() } - ?: throw IllegalStateException("Unknown output type: $it") - }.toMutableList() //temporary solution. support old property + val experimentalTryK2Enabled = properties.kotlinExperimentalTryK2.get() + val buildReportOutputTypes = properties.buildReportOutputs + .map { + BuildReportType.values().firstOrNull { brt -> brt.name == it.trim().toUpperCaseAsciiOnly() } + ?: throw IllegalStateException("Unknown output type: $it") + } + .plus(if (experimentalTryK2Enabled) listOf(BuildReportType.TRY_K2_CONSOLE) else emptyList()) + .toMutableList() //temporary solution. support old property + val buildReportMode = when { buildReportOutputTypes.isEmpty() -> BuildReportMode.NONE @@ -82,6 +87,7 @@ internal fun reportingSettings(project: Project): ReportingSettings { buildReportOutputs = buildReportOutputTypes, singleOutputFile = singleOutputFile ?: oldSingleBuildMetric, includeCompilerArguments = properties.buildReportIncludeCompilerArguments, + experimentalTryK2ConsoleOutput = experimentalTryK2Enabled ) }