From 016876c02c23483957a9f9add675b73fca6ce3f8 Mon Sep 17 00:00:00 2001 From: Sam Edwards Date: Mon, 2 Dec 2024 11:48:02 -0500 Subject: [PATCH] Historical data param, git tagName (#36) * Almost there. * Added Historical Data Param * Use tag or branch name. * Updated sorting * Historical Support * Progress * proper historical sorting and git tag --- .github/CODEOWNERS | 3 +-- gradle.properties | 2 +- .../com/squareup/invert/InvertExtension.kt | 13 ++++++++++ .../invert/internal/GitDataCollector.kt | 13 ++++++++++ .../internal/report/InvertReportWriter.kt | 10 ++++--- .../invert/internal/tasks/InvertTask.kt | 26 +++++++++++++++++-- .../tasks/ProjectMetadataCollector.kt | 2 ++ .../com/squareup/invert/models/Typealiases.kt | 1 + .../invert/models/js/MetadataJsReportModel.kt | 2 ++ .../invert/common/CollectedDataRepo.kt | 2 +- .../invert/common/utils/FormattingUtils.kt | 14 ++++++---- 11 files changed, 74 insertions(+), 14 deletions(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 40b77f8..4142628 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1,3 +1,2 @@ -examples/jvm-with-anvil/ Anvil examples/scopes/ @handstandsam -examples/app/ AppTeam \ No newline at end of file +examples/app/ @handstandsam diff --git a/gradle.properties b/gradle.properties index dd40e49..4e77a5b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -org.gradle.jvmargs=-Xmx8g +org.gradle.jvmargs=-Xmx16g kotlin.code.style=official android.useAndroidX=true diff --git a/invert-gradle-plugin/src/main/kotlin/com/squareup/invert/InvertExtension.kt b/invert-gradle-plugin/src/main/kotlin/com/squareup/invert/InvertExtension.kt index d3f54c8..0b2717b 100644 --- a/invert-gradle-plugin/src/main/kotlin/com/squareup/invert/InvertExtension.kt +++ b/invert-gradle-plugin/src/main/kotlin/com/squareup/invert/InvertExtension.kt @@ -6,6 +6,7 @@ import com.squareup.invert.internal.NoOpOwnershipCollector import com.squareup.invert.models.ConfigurationName import org.gradle.api.Project import org.gradle.api.tasks.Input +import org.gradle.api.tasks.InputFile /** * Extension for configuring the [InvertGradlePlugin] @@ -45,10 +46,18 @@ open class InvertExtension(project: Project) { internal val includeConfigurationProperty = objects.property(InvertIncludeConfigurationCalculator::class.java) + @get:InputFile + @get:Input + internal val historicalDataFileProperty = objects.property(String::class.java) + fun ownershipCollector(ownershipCollector: InvertOwnershipCollector) { ownershipCollectorProperty.set(ownershipCollector) } + fun historicalData(historicalDataFile: String) { + this.historicalDataFileProperty.set(historicalDataFile) + } + fun includeSubproject(invertShouldIncludeSubProject: (subproject: Project) -> Boolean) { includeSubProjectCalculatorProperty.set(object : InvertIncludeSubProjectCalculator { override fun invoke( @@ -79,6 +88,10 @@ open class InvertExtension(project: Project) { statCollectors.add(statCollector) } + internal fun getHistoricalDataFilePath(): String? { + return historicalDataFileProperty.orNull + } + internal fun getStatCollectors(): Collection { return statCollectors } diff --git a/invert-gradle-plugin/src/main/kotlin/com/squareup/invert/internal/GitDataCollector.kt b/invert-gradle-plugin/src/main/kotlin/com/squareup/invert/internal/GitDataCollector.kt index 1b36457..e468438 100644 --- a/invert-gradle-plugin/src/main/kotlin/com/squareup/invert/internal/GitDataCollector.kt +++ b/invert-gradle-plugin/src/main/kotlin/com/squareup/invert/internal/GitDataCollector.kt @@ -54,6 +54,19 @@ internal class GitDataCollector(private val gitProjectRootDir: File) { return exec("git rev-parse --abbrev-ref HEAD", gitProjectRootDir).stdOut.lines()[0] } + fun currentTag(): GitBranch? { + val cmdResult = exec("git describe --tags --exact-match", gitProjectRootDir).stdOut.lines()[0] + return if (cmdResult.contains("fatal: no tag exactly matches") || cmdResult.isEmpty()) { + null + } else { + cmdResult + } + } + + fun currentBranchOrTag(): GitBranch { + return currentTag() ?: currentBranch() + } + /** * * Example output of the command is: diff --git a/invert-gradle-plugin/src/main/kotlin/com/squareup/invert/internal/report/InvertReportWriter.kt b/invert-gradle-plugin/src/main/kotlin/com/squareup/invert/internal/report/InvertReportWriter.kt index b05e503..bc9ef15 100644 --- a/invert-gradle-plugin/src/main/kotlin/com/squareup/invert/internal/report/InvertReportWriter.kt +++ b/invert-gradle-plugin/src/main/kotlin/com/squareup/invert/internal/report/InvertReportWriter.kt @@ -44,6 +44,11 @@ class InvertReportWriter( val globalStats = computeGlobalTotals(allProjectsStatsData, collectedOwnershipInfo) + val historicalDataWithCurrent = (historicalData + HistoricalData( + reportMetadata = reportMetadata, + statTotalsAndMetadata = CollectedStatTotalsJsReportModel(globalStats) + )).sortedBy { it.reportMetadata.latestCommitTime } + // JSON Report InvertJsonReportWriter(invertLogger, rootBuildReportsDir).createInvertJsonReport( reportMetadata = reportMetadata, @@ -53,7 +58,7 @@ class InvertReportWriter( allPluginsData = collectedPlugins, allOwnersData = collectedOwners, globalStats = globalStats, - historicalData = historicalData, + historicalData = historicalDataWithCurrent, ) // HTML/JS Report @@ -67,11 +72,10 @@ class InvertReportWriter( collectedOwnershipInfo = collectedOwnershipInfo, allProjectsConfigurationsData = collectedConfigurations, globalStatTotals = CollectedStatTotalsJsReportModel(globalStats), - historicalData = historicalData, + historicalData = historicalDataWithCurrent, ) } - /** * This provides a warning to the user to let them know that a module was found as a dependency * but was not scanned itself. In order to get a full picture of the project, all should diff --git a/invert-gradle-plugin/src/main/kotlin/com/squareup/invert/internal/tasks/InvertTask.kt b/invert-gradle-plugin/src/main/kotlin/com/squareup/invert/internal/tasks/InvertTask.kt index da1293c..41bd8d0 100644 --- a/invert-gradle-plugin/src/main/kotlin/com/squareup/invert/internal/tasks/InvertTask.kt +++ b/invert-gradle-plugin/src/main/kotlin/com/squareup/invert/internal/tasks/InvertTask.kt @@ -11,7 +11,10 @@ import com.squareup.invert.internal.report.GradleProjectAnalysisCombiner import com.squareup.invert.internal.report.InvertReportWriter import com.squareup.invert.logging.GradleInvertLogger import com.squareup.invert.logging.InvertLogger +import com.squareup.invert.models.InvertSerialization.InvertJson +import com.squareup.invert.models.js.HistoricalData import kotlinx.coroutines.runBlocking +import kotlinx.serialization.builtins.ListSerializer import org.gradle.api.DefaultTask import org.gradle.api.Project import org.gradle.api.artifacts.repositories.UrlArtifactRepository @@ -20,6 +23,7 @@ import org.gradle.api.provider.ListProperty import org.gradle.api.provider.Property import org.gradle.api.tasks.Input import org.gradle.api.tasks.Internal +import org.gradle.api.tasks.Optional import org.gradle.api.tasks.OutputDirectory import org.gradle.api.tasks.TaskAction import java.io.File @@ -41,6 +45,10 @@ abstract class InvertTask : DefaultTask() { @get:Internal abstract var statCollectors: List? + @get:Optional + @get:Input + abstract val historicalDataFileProperty: Property + @get:Input abstract val projectPath: Property @@ -86,6 +94,20 @@ abstract class InvertTask : DefaultTask() { ) ) + val historicalDataFile: File? = historicalDataFileProperty.orNull?.let { File(it) } + val historicalData: List = + if (historicalDataFile?.isFile == true && historicalDataFile.length() > 0) { + try { + val fileContents = historicalDataFile.readText() + InvertJson.decodeFromString(ListSerializer(HistoricalData.serializer()), fileContents) + } catch (e: Exception) { + invertLogger().warn("Failed to read historical data file: $e") + listOf() + } + } else { + listOf() + } + InvertReportWriter( invertLogger = invertLogger(), rootBuildReportsDir = invertReportDir, @@ -96,7 +118,7 @@ abstract class InvertTask : DefaultTask() { collectedDependencies = allCollectedData.collectedDependencies, collectedConfigurations = allCollectedData.collectedConfigurations, collectedPlugins = allCollectedData.collectedPlugins, - historicalData = emptyList(), + historicalData = historicalData, ) } } @@ -117,7 +139,7 @@ abstract class InvertTask : DefaultTask() { InvertFileUtils.REPORTS_SLASH_INVERT_PATH ) ) - + this.historicalDataFileProperty.set(extension.getHistoricalDataFilePath()) this.statCollectors = extension.getStatCollectors().toList() this.mavenRepoUrls.set( diff --git a/invert-gradle-plugin/src/main/kotlin/com/squareup/invert/internal/tasks/ProjectMetadataCollector.kt b/invert-gradle-plugin/src/main/kotlin/com/squareup/invert/internal/tasks/ProjectMetadataCollector.kt index b72791f..2a68c2b 100644 --- a/invert-gradle-plugin/src/main/kotlin/com/squareup/invert/internal/tasks/ProjectMetadataCollector.kt +++ b/invert-gradle-plugin/src/main/kotlin/com/squareup/invert/internal/tasks/ProjectMetadataCollector.kt @@ -28,6 +28,7 @@ object ProjectMetadataCollector { val gitDataCollector = GitDataCollector(gitProjectDir) val currentBranch: GitBranch = gitDataCollector.currentBranch() + val currentTag: GitBranch? = gitDataCollector.currentTag() val currentBranchHash = gitDataCollector.gitShaOfBranch(currentBranch, logger) val latestCommitTimestamp = gitDataCollector.latestCommitTimestamp() @@ -49,6 +50,7 @@ object ProjectMetadataCollector { latestCommitTimeFormatted = formatter.format(Instant.ofEpochSecond(latestCommitTimestamp)), latestCommitGitSha = currentBranchHash, branchName = currentBranch, + tagName = currentTag, latestCommitSha = currentBranchHash, remoteRepoGit = remoteGitRepoUrl, remoteRepoUrl = remoteRepoUrl, diff --git a/invert-models/src/commonMain/kotlin/com/squareup/invert/models/Typealiases.kt b/invert-models/src/commonMain/kotlin/com/squareup/invert/models/Typealiases.kt index 6250c5a..aed62ea 100644 --- a/invert-models/src/commonMain/kotlin/com/squareup/invert/models/Typealiases.kt +++ b/invert-models/src/commonMain/kotlin/com/squareup/invert/models/Typealiases.kt @@ -7,5 +7,6 @@ typealias ModulePath = String typealias OwnerName = String typealias GitSha = String typealias GitBranch = String +typealias GitTag = String typealias StatKey = String typealias ExtraKey = String diff --git a/invert-models/src/commonMain/kotlin/com/squareup/invert/models/js/MetadataJsReportModel.kt b/invert-models/src/commonMain/kotlin/com/squareup/invert/models/js/MetadataJsReportModel.kt index cff0691..faf89f7 100644 --- a/invert-models/src/commonMain/kotlin/com/squareup/invert/models/js/MetadataJsReportModel.kt +++ b/invert-models/src/commonMain/kotlin/com/squareup/invert/models/js/MetadataJsReportModel.kt @@ -2,6 +2,7 @@ package com.squareup.invert.models.js import com.squareup.invert.models.GitBranch import com.squareup.invert.models.GitSha +import com.squareup.invert.models.GitTag import kotlinx.serialization.Serializable /** @@ -17,6 +18,7 @@ data class MetadataJsReportModel( val latestCommitTimeFormatted: String, val latestCommitGitSha: GitSha?, val branchName: GitBranch?, + val tagName: GitTag?, val remoteRepoGit: String, val remoteRepoUrl: String, val mavenRepoUrls: List diff --git a/invert-report/src/jsMain/kotlin/com/squareup/invert/common/CollectedDataRepo.kt b/invert-report/src/jsMain/kotlin/com/squareup/invert/common/CollectedDataRepo.kt index 8de6bc8..09248d6 100644 --- a/invert-report/src/jsMain/kotlin/com/squareup/invert/common/CollectedDataRepo.kt +++ b/invert-report/src/jsMain/kotlin/com/squareup/invert/common/CollectedDataRepo.kt @@ -57,7 +57,7 @@ class CollectedDataRepo( val historicalData: Flow?> = _historicalData.onEach { loadJsOfType(JsReportFileKey.HISTORICAL_DATA) }.map { - it?.sortedBy { it.reportMetadata.currentTime } + it?.sortedBy { it.reportMetadata.latestCommitTime } } private val _directDependenciesData: MutableStateFlow = diff --git a/invert-report/src/jsMain/kotlin/com/squareup/invert/common/utils/FormattingUtils.kt b/invert-report/src/jsMain/kotlin/com/squareup/invert/common/utils/FormattingUtils.kt index e8d382e..7bda3f3 100644 --- a/invert-report/src/jsMain/kotlin/com/squareup/invert/common/utils/FormattingUtils.kt +++ b/invert-report/src/jsMain/kotlin/com/squareup/invert/common/utils/FormattingUtils.kt @@ -7,11 +7,15 @@ import kotlinx.datetime.toLocalDateTime object FormattingUtils { fun Int.formatDecimalSeparator(): String { - return toString() - .reversed() - .chunked(3) - .joinToString(",") - .reversed() + if (this < 1000) { + return this.toString() + } else { + return toString() + .reversed() + .chunked(3) + .joinToString(",") + .reversed() + } } internal fun MetadataJsReportModel.dateDisplayStr(): String {