Skip to content

Commit

Permalink
Huge changes, too many for one commit... but here we go!
Browse files Browse the repository at this point in the history
  • Loading branch information
handstandsam committed Nov 27, 2024
1 parent 2e559bc commit 63cb709
Show file tree
Hide file tree
Showing 46 changed files with 749 additions and 588 deletions.
43 changes: 35 additions & 8 deletions examples/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ buildscript {
dependencies {
val invertVersion = "0.0.3-dev-SNAPSHOT"
classpath("com.squareup.invert:invert-gradle-plugin:$invertVersion")
// classpath("com.squareup.invert:collectors-anvil-dagger:$invertVersion")
// classpath("com.squareup.invert:collectors-kotlin-java-loc:$invertVersion")
// classpath("com.squareup.invert:owners-github-codeowners:$invertVersion")
classpath("com.squareup.invert:invert-collectors:$invertVersion")
classpath("com.squareup.invert:invert-owners-github:$invertVersion")
}
}

Expand All @@ -26,12 +25,40 @@ allprojects {
}

plugins {
id("com.squareup.invert")
id("com.squareup.invert")
}

invert {
// ownershipCollector(com.squareup.invert.GitHubCodeOwnersInvertOwnershipCollector)
// addStatCollector(com.squareup.invert.DiProvidesAndInjectsStatCollector())
// addStatCollector(com.squareup.invert.suppress.SuppressionsStatCollector())
// addStatCollector(com.squareup.invert.LinesOfCodeStatCollector())

ownershipCollector(com.squareup.invert.owners.GitHubCodeOwnersInvertOwnershipCollector)
addStatCollector(
com.squareup.invert.collectors.linesofcode.LinesOfCodeStatCollector(
name = "Kotlin",
fileExtensions = listOf("kt", "kts"),
)
)
addStatCollector(
com.squareup.invert.collectors.contains.LineContainsStatCollector(
statKey = "wildcard-imports",
statDescription = "Wildcard Imports",
linePredicate = { it.contains("import") && it.contains("*") },
filePredicate = { it.extension == "kt" || it.extension == "kts" },
)
)
}

/**
* Will copy the report js files into invert so we can use them in dev cycles
*/
tasks.register("copyJsFiles") {
doLast {
copy {
from(fileTree("build/reports/invert/js") { include("*.js") }) // Replace "src/js" with your source directory
into("../invert-report/src/jsMain/resources/js") // Replace "build/js" with your target directory
}
}
}

afterEvaluate {
rootProject.tasks.findByName("invert")!!.finalizedBy("copyJsFiles")
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.squareup.invert.examples.repository.CategoryRepo
import com.squareup.invert.examples.scopes.AppScope
import javax.inject.Inject


@ContributesBinding(AppScope::class)
class FakeCategoryRepo @Inject constructor(
private val mockAccount: MockAccount
Expand All @@ -17,4 +16,4 @@ class FakeCategoryRepo @Inject constructor(
val categories = mockAccount.getCategories()
return Response.Success(categories)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ class FakeItemRepo @Inject constructor(
return Response.Failure()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import javax.inject.Inject

@ContributesBinding(AppScope::class)
class FakeNetworkGraph @Inject constructor(
override val categoryRepo: CategoryRepo,
override val itemRepo: ItemRepo,
override val userRepo: UserRepo
) : NetworkGraph
override val categoryRepo: CategoryRepo,
override val itemRepo: ItemRepo,
override val userRepo: UserRepo
) : NetworkGraph
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ class FakeUserRepo @Inject constructor(
override suspend fun login(loginRequest: LoginRequest): Response<User> {
return Response.Success(mockAccount.getUser())
}
}
}
18 changes: 18 additions & 0 deletions invert-collectors/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
plugins {
kotlin("jvm")
alias(libs.plugins.vanniktech.maven.publish)
alias(libs.plugins.dokka)
}

java {
withSourcesJar()
}

dependencies {
compileOnly(gradleApi())

implementation(project(":invert-models"))
implementation(project(":invert-gradle-plugin"))

testImplementation(libs.kotlin.test)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.squareup.invert.collectors.contains

import com.squareup.invert.CollectedStat
import com.squareup.invert.InvertProjectData
import com.squareup.invert.StatCollector
import com.squareup.invert.collectors.internal.wrapCodeForMarkdown
import com.squareup.invert.models.Stat
import com.squareup.invert.models.StatDataType
import com.squareup.invert.models.StatMetadata
import java.io.File

open class LineContainsStatCollector(
private val statKey: String,
private val statDescription: String,
private val linePredicate: (String) -> Boolean,
private val filePredicate: (File) -> Boolean = { true },
) : StatCollector {
override fun collect(
invertProjectData: InvertProjectData,
): List<CollectedStat>? {
val codeReferences = mutableListOf<Stat.CodeReferencesStat.CodeReference>()
invertProjectData.projectDir
.walkTopDown()
.filter { it.isFile && it.length() > 0 }
.filter { filePredicate(it) }
.forEach { sourceFile ->
val relativeFilePath = sourceFile.relativeTo(invertProjectData.rootProjectDir).path
sourceFile.readLines()
.map { it.trim() }
.forEachIndexed { index, line ->
if (linePredicate(line)) {
codeReferences.add(
Stat.CodeReferencesStat.CodeReference(
filePath = relativeFilePath,
startLine = index + 1,
endLine = index + 1,
code = line.wrapCodeForMarkdown(),
)
)
}
}
}

return if (codeReferences.isNotEmpty()) {
listOf(
CollectedStat(
metadata = StatMetadata(
key = statKey,
description = statDescription,
dataType = StatDataType.CODE_REFERENCES,
),
stat = Stat.CodeReferencesStat(codeReferences)
)
)
} else {
null
}
}

override fun getName(): String {
return this::class.java.name
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.squareup.invert.collectors.internal

internal fun String.wrapCodeForMarkdown(language: String = "") = buildString {
appendLine("```$language")
appendLine(this@wrapCodeForMarkdown)
appendLine("```")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.squareup.invert.collectors.linesofcode

import com.squareup.invert.CollectedStat
import com.squareup.invert.InvertProjectData
import com.squareup.invert.StatCollector
import com.squareup.invert.models.Stat
import com.squareup.invert.models.StatDataType
import com.squareup.invert.models.StatMetadata
import com.squareup.invert.projectSrcDir
import java.io.File

open class LinesOfCodeStatCollector(
name: String,
private val fileExtensions: List<String>,
keySuffix: String = fileExtensions.joinToString(","),
private val sourcesDirectory: (InvertProjectData) -> File = {
it.projectSrcDir
},
) : StatCollector {

companion object {
const val STAT_CATEGORY_LINES_OF_CODE = "Lines of Code"
}

private val fileCountStatMetadata: StatMetadata = StatMetadata(
key = "file_count_$keySuffix",
description = "File Count - $name",
dataType = StatDataType.NUMERIC,
category = STAT_CATEGORY_LINES_OF_CODE,
)

private val linesOfCodeStatMetadata: StatMetadata = StatMetadata(
key = "lines_of_code_$keySuffix",
description = "Lines of Code - $name",
dataType = StatDataType.NUMERIC,
category = STAT_CATEGORY_LINES_OF_CODE,
)

override fun collect(
invertProjectData: InvertProjectData,
): List<CollectedStat>? {
val matchingSourceFiles = sourcesDirectory(invertProjectData)
.walkTopDown()
.filter { file -> file.isFile }
.filter { fileExtensions.contains(it.extension) }
.toList()

return if (matchingSourceFiles.isNotEmpty()) {
var totalLoc = 0
matchingSourceFiles
.map { it.readLines().filter { line -> line.isNotBlank() } }
.forEach {
totalLoc += it.size
}

listOf(
CollectedStat(
metadata = fileCountStatMetadata,
stat = Stat.NumericStat(
value = matchingSourceFiles.size,
)
),
CollectedStat(
metadata = linesOfCodeStatMetadata,
stat = Stat.NumericStat(
value = totalLoc,
)
)
)
} else {
null
}
}

override fun getName(): String {
return this::class.java.name
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class InvertAllCollectedDataRepo(
private val projectMetadata: MetadataJsReportModel,
) {

val httpsRemoteRepoUrlForCommit: String = "${projectMetadata.remoteRepoUrl}/blob/${projectMetadata.gitSha}"
val httpsRemoteRepoUrlForCommit: String = "${projectMetadata.remoteRepoUrl}/blob/${projectMetadata.latestCommitGitSha}"

val mavenRepoUrls = projectMetadata.mavenRepoUrls

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ internal class GitDataCollector(private val gitProjectRootDir: File) {
return exec("git config --get remote.origin.url", gitProjectRootDir).stdOut.lines()[0]
}

/**
*
* Example output of the command is:
* 1732637664
*/
fun latestCommitTimestamp(): Long {
return exec("git log -1 --format=%ct", gitProjectRootDir).stdOut.lines()[0].toLong()
}

fun gitShaOfBranch(branchName: GitBranch, logger: InvertLogger): GitSha {
val command = buildString {
append("git log -n 1 ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ object NoOpOwnershipCollector : InvertOwnershipCollector {
override fun collect(
rootProjectDir: String,
modulePath: ModulePath
): OwnerInfo? = null
): OwnerInfo? = OwnerInfo("None")
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ 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.models.ConfigurationName
import com.squareup.invert.models.StatDataType
import com.squareup.invert.models.DependencyId
import com.squareup.invert.models.ModulePath
import com.squareup.invert.models.GradlePluginId
import com.squareup.invert.models.ModulePath
import com.squareup.invert.models.Stat
import com.squareup.invert.models.StatDataType
import com.squareup.invert.models.StatKey
import com.squareup.invert.models.StatMetadata
import com.squareup.invert.models.js.ConfigurationsJsReportModel
import com.squareup.invert.models.js.DependenciesJsReportModel
import com.squareup.invert.models.js.DirectDependenciesJsReportModel
import com.squareup.invert.models.js.OwnershipJsReportModel
import com.squareup.invert.models.js.PluginsJsReportModel
import com.squareup.invert.models.js.StatTotalAndMetadata
import com.squareup.invert.models.js.StatsJsReportModel

/**
Expand All @@ -41,7 +42,7 @@ object InvertJsReportUtils {
)
}

fun computeGlobalStats(allProjectsStatsData: StatsJsReportModel): Map<StatMetadata, Int> {
fun computeGlobalStats(allProjectsStatsData: StatsJsReportModel): Map<StatKey, StatTotalAndMetadata> {
val globalStats: Map<StatMetadata, Int> = allProjectsStatsData.statInfos.values
.filter { statInfo ->
when (statInfo.dataType) {
Expand All @@ -54,7 +55,7 @@ object InvertJsReportUtils {
}
}
}
.associateWith { statMetadata ->
.associateWith { statMetadata: StatMetadata ->
val statKey = statMetadata.key
allProjectsStatsData.statsByModule.values.sumOf { statsForModule: Map<StatKey, Stat> ->
val stat: Stat? = statsForModule[statKey]
Expand All @@ -73,7 +74,7 @@ object InvertJsReportUtils {
}
}
}.toMap()
return globalStats
return globalStats.entries.associate { it.key.key to StatTotalAndMetadata(it.key, it.value) }
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import com.squareup.invert.internal.models.InvertPluginFileKey
import com.squareup.invert.logging.InvertLogger
import com.squareup.invert.logging.SystemOutInvertLogger
import com.squareup.invert.models.InvertSerialization.InvertJson
import com.squareup.invert.models.StatKey
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.StatTotalAndMetadata
import com.squareup.invert.models.js.StatsJsReportModel
import kotlinx.serialization.KSerializer
import kotlinx.serialization.builtins.ListSerializer
Expand All @@ -28,7 +30,7 @@ class InvertJsonReportWriter(
allProjectsStatsData: StatsJsReportModel,
allPluginsData: List<CollectedPluginsForProject>,
allOwnersData: List<CollectedOwnershipForProject>,
globalStats: Map<StatMetadata, Int>,
globalStats: Map<StatKey, StatTotalAndMetadata>,
reportMetadata: MetadataJsReportModel,
) {
writeJsonFileInDir(
Expand Down
Loading

0 comments on commit 63cb709

Please sign in to comment.