Skip to content

Commit

Permalink
Historical data param, git tagName (#36)
Browse files Browse the repository at this point in the history
* Almost there.

* Added Historical Data Param

* Use tag or branch name.

* Updated sorting

* Historical Support

* Progress

* proper historical sorting and git tag
  • Loading branch information
handstandsam authored Dec 2, 2024
1 parent 00b0355 commit 016876c
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 14 deletions.
3 changes: 1 addition & 2 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
examples/jvm-with-anvil/ Anvil
examples/scopes/ @handstandsam
examples/app/ AppTeam
examples/app/ @handstandsam
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
org.gradle.jvmargs=-Xmx8g
org.gradle.jvmargs=-Xmx16g
kotlin.code.style=official
android.useAndroidX=true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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?>(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(
Expand Down Expand Up @@ -79,6 +88,10 @@ open class InvertExtension(project: Project) {
statCollectors.add(statCollector)
}

internal fun getHistoricalDataFilePath(): String? {
return historicalDataFileProperty.orNull
}

internal fun getStatCollectors(): Collection<StatCollector> {
return statCollectors
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -53,7 +58,7 @@ class InvertReportWriter(
allPluginsData = collectedPlugins,
allOwnersData = collectedOwners,
globalStats = globalStats,
historicalData = historicalData,
historicalData = historicalDataWithCurrent,
)

// HTML/JS Report
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -41,6 +45,10 @@ abstract class InvertTask : DefaultTask() {
@get:Internal
abstract var statCollectors: List<StatCollector>?

@get:Optional
@get:Input
abstract val historicalDataFileProperty: Property<String?>

@get:Input
abstract val projectPath: Property<String>

Expand Down Expand Up @@ -86,6 +94,20 @@ abstract class InvertTask : DefaultTask() {
)
)

val historicalDataFile: File? = historicalDataFileProperty.orNull?.let { File(it) }
val historicalData: List<HistoricalData> =
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,
Expand All @@ -96,7 +118,7 @@ abstract class InvertTask : DefaultTask() {
collectedDependencies = allCollectedData.collectedDependencies,
collectedConfigurations = allCollectedData.collectedConfigurations,
collectedPlugins = allCollectedData.collectedPlugins,
historicalData = emptyList(),
historicalData = historicalData,
)
}
}
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -49,6 +50,7 @@ object ProjectMetadataCollector {
latestCommitTimeFormatted = formatter.format(Instant.ofEpochSecond(latestCommitTimestamp)),
latestCommitGitSha = currentBranchHash,
branchName = currentBranch,
tagName = currentTag,
latestCommitSha = currentBranchHash,
remoteRepoGit = remoteGitRepoUrl,
remoteRepoUrl = remoteRepoUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -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<String>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class CollectedDataRepo(
val historicalData: Flow<List<HistoricalData>?> = _historicalData.onEach {
loadJsOfType(JsReportFileKey.HISTORICAL_DATA)
}.map {
it?.sortedBy { it.reportMetadata.currentTime }
it?.sortedBy { it.reportMetadata.latestCommitTime }
}

private val _directDependenciesData: MutableStateFlow<DirectDependenciesJsReportModel?> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 016876c

Please sign in to comment.