Skip to content

Commit

Permalink
Moved metadata collection into the InvertTask itself and created a si…
Browse files Browse the repository at this point in the history
…ngle entry point to write both the JS and JSON reports.
  • Loading branch information
handstandsam committed Jul 11, 2024
1 parent 0bd102c commit 78baee1
Show file tree
Hide file tree
Showing 9 changed files with 320 additions and 316 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.squareup.invert.internal.tasks.InvertCollectDependenciesTask
import com.squareup.invert.internal.tasks.InvertCollectOwnershipTask
import com.squareup.invert.internal.tasks.InvertCollectStatsTask
import com.squareup.invert.internal.tasks.InvertCollectTask
import com.squareup.invert.internal.tasks.InvertProjectMetadataTask
import com.squareup.invert.internal.tasks.InvertTask
import org.gradle.api.Plugin
import org.gradle.api.Project
Expand All @@ -32,14 +31,6 @@ class InvertGradlePlugin : Plugin<Project> {
if (!rootProject.isRootProject()) {
throw IllegalStateException("Cannot apply $ID to a non-root project")
}
val metadataTask = rootProject.tasks.register(
InvertProjectMetadataTask.TASK_NAME,
InvertProjectMetadataTask::class.java
) {
it.setParams(
rootProject = rootProject,
)
}

val invertCleanTask = registerInvertCleanTask(rootProject)
rootProject.afterEvaluate { rootProject ->
Expand All @@ -59,7 +50,6 @@ class InvertGradlePlugin : Plugin<Project> {
).absolutePath
}
)
reportTask.dependsOn(metadataTask)
}

subprojectsToRegisterOn.forEach { subproject ->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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>,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.squareup.invert.internal.report

import com.squareup.invert.internal.models.CollectedConfigurationsForProject
import com.squareup.invert.internal.models.CollectedDependenciesForProject
import com.squareup.invert.internal.models.CollectedOwnershipForProject
import com.squareup.invert.internal.models.CollectedPluginsForProject
import com.squareup.invert.internal.models.CollectedStatsForProject
import com.squareup.invert.internal.models.InvertCombinedCollectedData
import com.squareup.invert.internal.models.InvertPluginFileKey
import java.io.File

/**
* Reads from all the individual project reports and combines them.
*/
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>()

resultDirPaths
.map { File(it) }
.forEach { subprojectInvertReportDirFile ->
if (subprojectInvertReportDirFile.exists()) {
File(
subprojectInvertReportDirFile,
InvertPluginFileKey.DEPENDENCIES.filename
).also { file ->
if (file.exists()) {
InvertReportFileUtils.buildModuleToFeaturesMap(file)?.let {
collectedDependencies.add(it)
}
}
}

File(
subprojectInvertReportDirFile,
InvertPluginFileKey.CONFIGURATIONS.filename
).also { file ->
if (file.exists()) {
InvertReportFileUtils.readCollectedConfigurationsForAllModules(file)?.let {
collectedConfigurations.add(it)
}
}
}

File(
subprojectInvertReportDirFile,
InvertPluginFileKey.STATS.filename
).also { file ->
if (file.exists()) {
InvertReportFileUtils.readCollectedStatsForAllProjectsFromDisk(file)?.let {
collectedStats.add(it)
}
}
}

File(
subprojectInvertReportDirFile,
InvertPluginFileKey.OWNERS.filename
).also { file ->
InvertReportFileUtils.readCollectedOwnershipForAllProjectsFromDisk(file)
?.let { collectedOwners.add(it) }
}

File(
subprojectInvertReportDirFile,
InvertPluginFileKey.PLUGINS.filename
).also { file ->
if (file.exists()) {
InvertReportFileUtils.readCollectedPluginsForAllModules(file)?.let {
synchronized(collectedPlugins) {
collectedPlugins.add(it)
}
}
}
}
}
}
return InvertCombinedCollectedData(
collectedConfigurations = collectedConfigurations,
collectedDependencies = collectedDependencies,
collectedOwners = collectedOwners,
collectedStats = collectedStats,
collectedPlugins = collectedPlugins
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.squareup.invert.internal.report

import com.squareup.invert.internal.models.CollectedConfigurationsForProject
import com.squareup.invert.internal.models.CollectedDependenciesForProject
import com.squareup.invert.internal.models.CollectedOwnershipForProject
import com.squareup.invert.internal.models.CollectedPluginsForProject
import com.squareup.invert.internal.models.CollectedStatsForProject
import com.squareup.invert.internal.report.js.InvertJsReportUtils
import com.squareup.invert.internal.report.js.InvertJsReportUtils.computeGlobalStats
import com.squareup.invert.internal.report.js.InvertJsReportWriter
import com.squareup.invert.internal.report.json.InvertJsonReportWriter
import com.squareup.invert.logging.InvertLogger
import com.squareup.invert.models.DependencyId
import com.squareup.invert.models.GradlePath
import com.squareup.invert.models.js.CollectedStatTotalsJsReportModel
import com.squareup.invert.models.js.MetadataJsReportModel
import java.io.File

class InvertReportWriter(
private val invertLogger: InvertLogger,
private val rootBuildReportsDir: File
) {
fun writeProjectData(
reportMetadata: MetadataJsReportModel,
collectedOwners: List<CollectedOwnershipForProject>,
collectedStats: List<CollectedStatsForProject>,
collectedDependencies: List<CollectedDependenciesForProject>,
collectedConfigurations: List<CollectedConfigurationsForProject>,
collectedPlugins: List<CollectedPluginsForProject>,
) {
val collectedOwnershipInfo = InvertJsReportUtils.buildModuleToOwnerMap(collectedOwners)
val allProjectsStatsData = InvertJsReportUtils.buildModuleToStatsMap(collectedStats)
val directDependenciesJsReportModel = InvertJsReportUtils.toDirectDependenciesJsReportModel(collectedDependencies)
val invertedDependenciesJsReportModel = InvertJsReportUtils
.toInvertedDependenciesJsReportModel(collectedDependencies)

assertModuleMatch(
logger = invertLogger,
modulesList = collectedDependencies.map { it.path },
invertedModulesList = invertedDependenciesJsReportModel.getAllModulePaths()
)

val globalStats = computeGlobalStats(allProjectsStatsData)

// JSON Report
InvertJsonReportWriter(invertLogger, rootBuildReportsDir).createInvertJsonReport(
reportMetadata = reportMetadata,
allConfigurationsData = collectedConfigurations,
allProjectsDependencyData = collectedDependencies,
allProjectsStatsData = allProjectsStatsData,
allPluginsData = collectedPlugins,
allOwnersData = collectedOwners,
globalStats = globalStats
)

// HTML/JS Report
InvertJsReportWriter(invertLogger, rootBuildReportsDir).createInvertHtmlReport(
reportMetadata = reportMetadata,
allProjectsDependencyData = collectedDependencies,
allProjectsStatsData = allProjectsStatsData,
directDependencies = directDependenciesJsReportModel,
invertedDependencies = invertedDependenciesJsReportModel,
allPluginsData = collectedPlugins,
collectedOwnershipInfo = collectedOwnershipInfo,
allProjectsConfigurationsData = collectedConfigurations,
globalStatTotals = CollectedStatTotalsJsReportModel(globalStats),
)
}


/**
* 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
* be scanned.
*/
private fun assertModuleMatch(
logger: InvertLogger,
modulesList: List<GradlePath>,
invertedModulesList: List<DependencyId>
) {
if (!invertedModulesList.containsAll(modulesList)) {
val modulesMap = modulesList.groupBy { it }
val invertedModulesMap = invertedModulesList.groupBy { it }
val errorString = buildString {
appendLine("WARNING: Module Mismatch...")
appendLine("The following modules are dependencies, but were not scanned:")
var idx = 1
invertedModulesMap.keys.sorted().forEach { path ->
if (modulesMap[path] == null) {
appendLine("${idx++}. $path")
}
}
}

logger.warn(errorString)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.squareup.invert.models.js.DependenciesJsReportModel
import com.squareup.invert.models.js.DirectDependenciesJsReportModel
import com.squareup.invert.models.js.HomeJsReportModel
import com.squareup.invert.models.js.JsReportFileKey
import com.squareup.invert.models.js.MetadataJsReportModel
import com.squareup.invert.models.js.OwnershipJsReportModel
import com.squareup.invert.models.js.PluginsJsReportModel
import com.squareup.invert.models.js.StatsJsReportModel
Expand All @@ -33,6 +34,7 @@ class InvertJsReportWriter(
allPluginsData: List<CollectedPluginsForProject>,
collectedOwnershipInfo: OwnershipJsReportModel,
globalStatTotals: CollectedStatTotalsJsReportModel,
reportMetadata: MetadataJsReportModel,
) {
val pluginsReport = InvertJsReportUtils.toCollectedPlugins(allPluginsData)
val modulesList = allProjectsDependencyData.map { it.path }
Expand All @@ -43,6 +45,12 @@ class InvertJsReportWriter(
plugins = pluginsReport.plugins.keys.toList().sorted()
)

writeJsFileInDir(
fileKey = JsReportFileKey.METADATA,
serializer = MetadataJsReportModel.serializer(),
value = reportMetadata
)

writeJsFileInDir(
fileKey = JsReportFileKey.OWNERS,
serializer = OwnershipJsReportModel.serializer(),
Expand Down Expand Up @@ -150,6 +158,9 @@ class InvertJsReportWriter(
serializer: KSerializer<T>,
value: T,
) = jsOutputFile.apply {
if (!parentFile.exists()) {
parentFile.mkdirs()
}
writeText(
invertJsGlobalVariableAssignment(
fileKey = fileKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.squareup.invert.logging.InvertLogger
import com.squareup.invert.models.InvertSerialization.InvertJson
import com.squareup.invert.models.StatMetadata
import com.squareup.invert.models.js.CollectedStatTotalsJsReportModel
import com.squareup.invert.models.js.MetadataJsReportModel
import com.squareup.invert.models.js.StatsJsReportModel
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.ListSerializer
Expand All @@ -27,7 +28,13 @@ class InvertJsonReportWriter(
allPluginsData: List<CollectedPluginsForProject>,
allOwnersData: List<CollectedOwnershipForProject>,
globalStats: Map<StatMetadata, Int>,
reportMetadata: MetadataJsReportModel,
) {
writeJsonFileInDir(
jsonFileKey = InvertPluginFileKey.METADATA,
serializer = MetadataJsReportModel.serializer(),
value = reportMetadata
)
writeJsonFileInDir(
jsonFileKey = InvertPluginFileKey.PLUGINS,
serializer = ListSerializer(CollectedPluginsForProject.serializer()),
Expand Down
Loading

0 comments on commit 78baee1

Please sign in to comment.