Skip to content

Commit

Permalink
Use Set instead of List where possible to avoid possible duplicates. …
Browse files Browse the repository at this point in the history
…We had a case where there was a duplicate module somehow.
  • Loading branch information
handstandsam committed Jan 24, 2025
1 parent c7b822c commit 0e2baac
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ object CollectedStatAggregator {
}
}
return origAllCollectedData.copy(
collectedStats = projectPathToCollectedStatsForProject.values.toList()
collectedStats = projectPathToCollectedStatsForProject.values.toSet()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ package com.squareup.invert.internal.models
* A Combined view of all data collected for all projects.
*/
data class InvertCombinedCollectedData(
val collectedConfigurations: List<CollectedConfigurationsForProject>,
val collectedDependencies: List<CollectedDependenciesForProject>,
val collectedOwners: List<CollectedOwnershipForProject>,
val collectedStats: List<CollectedStatsForProject>,
val collectedPlugins: List<CollectedPluginsForProject>,
val collectedConfigurations: Set<CollectedConfigurationsForProject>,
val collectedDependencies: Set<CollectedDependenciesForProject>,
val collectedOwners: Set<CollectedOwnershipForProject>,
val collectedStats: Set<CollectedStatsForProject>,
val collectedPlugins: Set<CollectedPluginsForProject>,
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import java.io.File
*/
object GradleProjectAnalysisCombiner {
fun combineAnalysisResults(resultDirPaths: List<String>): InvertCombinedCollectedData {
val collectedConfigurations = mutableListOf<CollectedConfigurationsForProject>()
val collectedDependencies = mutableListOf<CollectedDependenciesForProject>()
val collectedOwners = mutableListOf<CollectedOwnershipForProject>()
val collectedStats = mutableListOf<CollectedStatsForProject>()
val collectedPlugins = mutableListOf<CollectedPluginsForProject>()
val collectedConfigurations = mutableSetOf<CollectedConfigurationsForProject>()
val collectedDependencies = mutableSetOf<CollectedDependenciesForProject>()
val collectedOwners = mutableSetOf<CollectedOwnershipForProject>()
val collectedStats = mutableSetOf<CollectedStatsForProject>()
val collectedPlugins = mutableSetOf<CollectedPluginsForProject>()

resultDirPaths
.map { File(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ class InvertReportWriter(
) {
fun writeProjectData(
reportMetadata: MetadataJsReportModel,
collectedOwners: List<CollectedOwnershipForProject>,
collectedStats: List<CollectedStatsForProject>,
collectedDependencies: List<CollectedDependenciesForProject>,
collectedConfigurations: List<CollectedConfigurationsForProject>,
collectedPlugins: List<CollectedPluginsForProject>,
historicalData: List<HistoricalData>,
collectedOwners: Set<CollectedOwnershipForProject>,
collectedStats: Set<CollectedStatsForProject>,
collectedDependencies: Set<CollectedDependenciesForProject>,
collectedConfigurations: Set<CollectedConfigurationsForProject>,
collectedPlugins: Set<CollectedPluginsForProject>,
historicalData: Set<HistoricalData>,
) {
val collectedOwnershipInfo = InvertJsReportUtils.buildModuleToOwnerMap(collectedOwners)
val allProjectsStatsData = InvertJsReportUtils.buildModuleToStatsMap(collectedStats)
Expand All @@ -47,7 +47,7 @@ class InvertReportWriter(
val historicalDataWithCurrent = (historicalData + HistoricalData(
reportMetadata = reportMetadata,
statTotalsAndMetadata = CollectedStatTotalsJsReportModel(globalStats)
)).sortedBy { it.reportMetadata.latestCommitTime }
)).sortedBy { it.reportMetadata.latestCommitTime }.toSet()

// JSON Report
InvertJsonReportWriter(invertLogger, rootBuildReportsDir).createInvertJsonReport(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ object InvertJsReportUtils {
* Takes all [CollectedOwnershipForProject] collected by Invert, and creates the JS Report Model.
*/
fun buildModuleToOwnerMap(
collectedOwners: List<CollectedOwnershipForProject>
collectedOwners: Set<CollectedOwnershipForProject>
): OwnershipJsReportModel {
return OwnershipJsReportModel(
modules = collectedOwners
Expand Down Expand Up @@ -125,7 +125,7 @@ object InvertJsReportUtils {
/**
* Takes all [CollectedStatsForProject] collected by Invert, and creates the JS Report Model.
*/
fun buildModuleToStatsMap(collectedStats: List<CollectedStatsForProject>): StatsJsReportModel {
fun buildModuleToStatsMap(collectedStats: Set<CollectedStatsForProject>): StatsJsReportModel {
val statInfos: Map<StatKey, StatMetadata> = mutableSetOf<StatMetadata>()
.also { statInfos ->
collectedStats.forEach { collectedStatsForProject ->
Expand All @@ -148,7 +148,7 @@ object InvertJsReportUtils {
/**
* Takes all [CollectedPluginsForProject] collected by Invert, and creates the JS Report Model.
*/
fun toCollectedPlugins(allPlugins: List<CollectedPluginsForProject>): PluginsJsReportModel {
fun toCollectedPlugins(allPlugins: Set<CollectedPluginsForProject>): PluginsJsReportModel {
return PluginsJsReportModel(
plugins = mutableMapOf<GradlePluginId, MutableList<ModulePath>>()
.also { resultingDepIdToModuleUsageInfo ->
Expand All @@ -175,7 +175,7 @@ object InvertJsReportUtils {
* Takes all [CollectedDependenciesForProject] collected by Invert, and creates the JS Report Model.
*/
fun toInvertedDependenciesJsReportModel(
collectedDependenciesForProjects: List<CollectedDependenciesForProject>
collectedDependenciesForProjects: Set<CollectedDependenciesForProject>
): DependenciesJsReportModel {
return DependenciesJsReportModel(
mutableMapOf<DependencyId, MutableMap<ModulePath, MutableList<ConfigurationName>>>()
Expand All @@ -197,7 +197,7 @@ object InvertJsReportUtils {
}

fun toCollectedConfigurations(
allProjectsConfigurationsData: List<CollectedConfigurationsForProject>
allProjectsConfigurationsData: Set<CollectedConfigurationsForProject>
): ConfigurationsJsReportModel {
val allConfigurationNames = mutableSetOf<String>()
val moduleToAllConfigurationNames = mutableMapOf<ModulePath, Set<ConfigurationName>>()
Expand Down Expand Up @@ -227,7 +227,7 @@ object InvertJsReportUtils {
}

fun toDirectDependenciesJsReportModel(
allProjectsDependencyData: List<CollectedDependenciesForProject>
allProjectsDependencyData: Set<CollectedDependenciesForProject>
): DirectDependenciesJsReportModel {
return DirectDependenciesJsReportModel(
allProjectsDependencyData.associate { it.path to it.directDependencies }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.squareup.invert.models.js.PluginsJsReportModel
import com.squareup.invert.models.js.StatsJsReportModel
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.builtins.SetSerializer
import java.io.File

class InvertJsReportWriter(
Expand All @@ -28,16 +29,16 @@ class InvertJsReportWriter(
private val rootBuildHtmlReportDir = File(rootBuildReportsDir, InvertFileUtils.JS_FOLDER_NAME)

fun createInvertHtmlReport(
allProjectsDependencyData: List<CollectedDependenciesForProject>,
allProjectsConfigurationsData: List<CollectedConfigurationsForProject>,
allProjectsDependencyData: Set<CollectedDependenciesForProject>,
allProjectsConfigurationsData: Set<CollectedConfigurationsForProject>,
allProjectsStatsData: StatsJsReportModel,
directDependencies: DirectDependenciesJsReportModel,
invertedDependencies: DependenciesJsReportModel,
allPluginsData: List<CollectedPluginsForProject>,
allPluginsData: Set<CollectedPluginsForProject>,
collectedOwnershipInfo: OwnershipJsReportModel,
globalStatTotals: CollectedStatTotalsJsReportModel,
reportMetadata: MetadataJsReportModel,
historicalData: List<HistoricalData>,
historicalData: Set<HistoricalData>,
) {
val pluginsReport = InvertJsReportUtils.toCollectedPlugins(allPluginsData)
val modulesList = allProjectsDependencyData.map { it.path }
Expand All @@ -56,7 +57,7 @@ class InvertJsReportWriter(

writeJsFileInDir(
fileKey = JsReportFileKey.HISTORICAL_DATA,
serializer = ListSerializer(HistoricalData.serializer()),
serializer = SetSerializer(HistoricalData.serializer()),
value = historicalData
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.squareup.invert.models.js.StatTotalAndMetadata
import com.squareup.invert.models.js.StatsJsReportModel
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.builtins.SetSerializer
import java.io.File

class InvertJsonReportWriter(
Expand All @@ -25,14 +26,14 @@ class InvertJsonReportWriter(
) {
private val rootBuildJsonReportsDir = File(rootBuildReportsDir, InvertFileUtils.JSON_FOLDER_NAME)
fun createInvertJsonReport(
allConfigurationsData: List<CollectedConfigurationsForProject>,
allProjectsDependencyData: List<CollectedDependenciesForProject>,
allConfigurationsData: Set<CollectedConfigurationsForProject>,
allProjectsDependencyData: Set<CollectedDependenciesForProject>,
allProjectsStatsData: StatsJsReportModel,
allPluginsData: List<CollectedPluginsForProject>,
allOwnersData: List<CollectedOwnershipForProject>,
allPluginsData: Set<CollectedPluginsForProject>,
allOwnersData: Set<CollectedOwnershipForProject>,
globalStats: Map<StatKey, StatTotalAndMetadata>,
reportMetadata: MetadataJsReportModel,
historicalData: List<HistoricalData>,
historicalData: Set<HistoricalData>,
) {
writeJsonFileInDir(
jsonFileKey = InvertPluginFileKey.METADATA,
Expand All @@ -41,12 +42,12 @@ class InvertJsonReportWriter(
)
writeJsonFileInDir(
jsonFileKey = InvertPluginFileKey.PLUGINS,
serializer = ListSerializer(CollectedPluginsForProject.serializer()),
serializer = SetSerializer(CollectedPluginsForProject.serializer()),
value = allPluginsData
)
writeJsonFileInDir(
jsonFileKey = InvertPluginFileKey.HISTORICAL_DATA,
serializer = ListSerializer(HistoricalData.serializer()),
serializer = SetSerializer(HistoricalData.serializer()),
value = historicalData
)

Expand All @@ -58,13 +59,13 @@ class InvertJsonReportWriter(

writeJsonFileInDir(
jsonFileKey = InvertPluginFileKey.CONFIGURATIONS,
serializer = ListSerializer(CollectedConfigurationsForProject.serializer()),
serializer = SetSerializer(CollectedConfigurationsForProject.serializer()),
value = allConfigurationsData
)

writeJsonFileInDir(
jsonFileKey = InvertPluginFileKey.DEPENDENCIES,
serializer = ListSerializer(CollectedDependenciesForProject.serializer()),
serializer = SetSerializer(CollectedDependenciesForProject.serializer()),
value = allProjectsDependencyData
)

Expand All @@ -76,7 +77,7 @@ class InvertJsonReportWriter(

writeJsonFileInDir(
jsonFileKey = InvertPluginFileKey.OWNERS,
serializer = ListSerializer(CollectedOwnershipForProject.serializer()),
serializer = SetSerializer(CollectedOwnershipForProject.serializer()),
value = allOwnersData
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ abstract class InvertTask : DefaultTask() {
)

val historicalDataFile: File? = historicalDataFileProperty.orNull?.let { File(it) }
val historicalData: List<HistoricalData> =
val historicalData: Set<HistoricalData> =
if (historicalDataFile?.isFile == true && historicalDataFile.length() > 0) {
try {
val fileContents = historicalDataFile.readText()
Expand All @@ -106,7 +106,7 @@ abstract class InvertTask : DefaultTask() {
}
} else {
listOf()
}
}.toSet()

InvertReportWriter(
invertLogger = invertLogger(),
Expand Down

0 comments on commit 0e2baac

Please sign in to comment.