Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Config Cache - TreeTask & ListTask #71

Merged
merged 7 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,5 @@ allprojects {
}

dependencyGuard {
configuration("classpath") {
it.tree = true
}
configuration("classpath")
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ internal class DependencyGuardListReportWriter(
* @return Whether changes were detected
*/
internal fun writeReport(
buildDirOutputFile: File,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a legacy artifact from the original Tree Diff practices where we would delegate to the Gradle Dependencies task, and then have the output file be the input for this task.

This is no longer required. We have the contents of the newly computed dependency list in memory, and we can compare with that.

projectDirOutputFile: File,
report: DependencyGuardReportData,
shouldBaseline: Boolean,
Expand All @@ -24,8 +23,6 @@ internal class DependencyGuardListReportWriter(
modules = modules
)

buildDirOutputFile.writeText(reportContent)

val projectDirOutputFileExists = projectDirOutputFile.exists()
return if (shouldBaseline || !projectDirOutputFileExists) {
projectDirOutputFile.writeText(reportContent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.gradle.api.provider.MapProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction

internal abstract class DependencyGuardListTask : DefaultTask() {
Expand Down Expand Up @@ -60,11 +61,8 @@ internal abstract class DependencyGuardListTask : DefaultTask() {
@get:Input
abstract val monitoredConfigurationsMap: MapProperty<DependencyGuardConfiguration, Provider<ResolvedComponentResult>>

@get:Input
abstract val buildDirectory: DirectoryProperty

@get:Input
abstract val projectDirectory: DirectoryProperty
@get:OutputDirectory
abstract val projectDirectoryDependenciesDir: DirectoryProperty

@Suppress("NestedBlockDepth")
@TaskAction
Expand Down Expand Up @@ -122,14 +120,10 @@ internal abstract class DependencyGuardListTask : DefaultTask() {
artifacts = dependencyGuardConfig.artifacts,
modules = dependencyGuardConfig.modules
)

return reportWriter.writeReport(
buildDirOutputFile = OutputFileUtils.buildDirOutputFile(
buildDirectory = buildDirectory.get(),
configurationName = report.configurationName,
reportType = reportType,
),
projectDirOutputFile = OutputFileUtils.projectDirOutputFile(
projectDirectory = projectDirectory.get(),
projectDirectory = projectDirectoryDependenciesDir.get(),
configurationName = report.configurationName,
reportType = reportType,
),
Expand Down Expand Up @@ -176,8 +170,8 @@ internal abstract class DependencyGuardListTask : DefaultTask() {
this.projectPath.set(project.path)
this.monitoredConfigurationsMap.set(resolveMonitoredConfigurationsMap(project, extension.configurations))
this.shouldBaseline.set(shouldBaseline)
this.buildDirectory.set(project.layout.buildDirectory)
this.projectDirectory.set(project.layout.projectDirectory)
val projectDirDependenciesDir = OutputFileUtils.projectDirDependenciesDir(project)
this.projectDirectoryDependenciesDir.set(projectDirDependenciesDir)

declareCompatibilities()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,44 @@ import com.dropbox.gradle.plugins.dependencyguard.internal.ConfigurationValidato
import com.dropbox.gradle.plugins.dependencyguard.internal.getResolvedComponentResult
import com.dropbox.gradle.plugins.dependencyguard.internal.projectConfigurations
import com.dropbox.gradle.plugins.dependencyguard.internal.utils.DependencyGuardTreeDiffer
import com.dropbox.gradle.plugins.dependencyguard.internal.utils.OutputFileUtils
import com.dropbox.gradle.plugins.dependencyguard.internal.utils.Tasks.declareCompatibilities
import java.io.File
import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.artifacts.result.ResolvedComponentResult
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction

internal abstract class DependencyTreeDiffTask : DefaultTask() {

private lateinit var dependencyGuardTreeDiffer: DependencyGuardTreeDiffer

init {
group = DependencyGuardPlugin.DEPENDENCY_GUARD_TASK_GROUP

this.doLast {
dependencyGuardTreeDiffer.performDiff()
}
}

@get:Input
abstract val resolvedComponentResult: Property<ResolvedComponentResult>
abstract val resolvedComponentResult: Property<Provider<ResolvedComponentResult>>

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

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

@get:Input
abstract val shouldBaseline: Property<Boolean>

@get:OutputFile
abstract val buildDirOutputFile: Property<File>

@get:OutputFile
abstract val projectDirOutputFile: Property<File>

fun setParams(
project: Project,
Expand All @@ -38,15 +53,18 @@ internal abstract class DependencyTreeDiffTask : DefaultTask() {
project,
listOf(configurationName)
)

this.dependencyGuardTreeDiffer = DependencyGuardTreeDiffer(
project = project,
configurationName = configurationName,
shouldBaseline = shouldBaseline,
)

val projectDependenciesDir = OutputFileUtils.projectDirDependenciesDir(project)
val projectDirOutputFile: File = DependencyGuardTreeDiffer.projectDirOutputFile(projectDependenciesDir, configurationName)
val buildDirOutputFile: File = DependencyGuardTreeDiffer.buildDirOutputFile(project.layout.buildDirectory.get(), configurationName)
val projectPath = project.path
val resolvedComponentResult = project.projectConfigurations.getResolvedComponentResult(configurationName)

this.resolvedComponentResult.set(resolvedComponentResult)
this.projectDirOutputFile.set(projectDirOutputFile)
this.buildDirOutputFile.set(buildDirOutputFile)
this.configurationName.set(configurationName)
this.projectPath.set(projectPath)
this.shouldBaseline.set(shouldBaseline)

declareCompatibilities()
}
Expand All @@ -56,9 +74,17 @@ internal abstract class DependencyTreeDiffTask : DefaultTask() {
// USES INTERNAL API
val asciiRenderer = AsciiDependencyReportRenderer2()

val resolvedComponentResult = resolvedComponentResult.get()
asciiRenderer.setOutputFile(dependencyGuardTreeDiffer.buildDirOutputFile)
val resolvedComponentResult: Provider<ResolvedComponentResult> = resolvedComponentResult.get()
asciiRenderer.setOutputFile(buildDirOutputFile.get())
asciiRenderer.prepareVisit()
asciiRenderer.render(resolvedComponentResult)
asciiRenderer.render(resolvedComponentResult.get())

DependencyGuardTreeDiffer(
projectPath = projectPath.get(),
configurationName = configurationName.get(),
shouldBaseline = shouldBaseline.get(),
projectDirOutputFile = projectDirOutputFile.get(),
buildDirOutputFile = buildDirOutputFile.get(),
).performDiff()
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
package com.dropbox.gradle.plugins.dependencyguard.internal.utils

import com.dropbox.gradle.plugins.dependencyguard.internal.DependencyGuardReportType
import org.gradle.api.GradleException
import org.gradle.api.Project
import java.io.File
import org.gradle.api.GradleException
import org.gradle.api.file.Directory

internal class DependencyGuardTreeDiffer(
project: Project,
private val configurationName: String,
private val shouldBaseline: Boolean,
private val projectDirOutputFile: File,
private val buildDirOutputFile: File,
private val projectPath: String,
) {
companion object {
fun projectDirOutputFile(projectDirectory: Directory, configurationName: String): File =
OutputFileUtils.projectDirOutputFile(
projectDirectory = projectDirectory,
configurationName = configurationName,
reportType = DependencyGuardReportType.TREE,
)

private val projectDirOutputFile: File = OutputFileUtils.projectDirOutputFile(
projectDirectory = project.layout.projectDirectory,
configurationName = configurationName,
reportType = DependencyGuardReportType.TREE,
)

val buildDirOutputFile: File = OutputFileUtils.buildDirOutputFile(
buildDirectory = project.layout.buildDirectory.get(),
configurationName = configurationName,
reportType = DependencyGuardReportType.TREE,
)

private val projectPath = project.path
fun buildDirOutputFile(buildDirectory: Directory, configurationName: String): File = OutputFileUtils.buildDirOutputFile(
buildDirectory = buildDirectory,
configurationName = configurationName,
reportType = DependencyGuardReportType.TREE,
)
}

@Suppress("NestedBlockDepth")
fun performDiff() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
package com.dropbox.gradle.plugins.dependencyguard.internal.utils

import com.dropbox.gradle.plugins.dependencyguard.internal.DependencyGuardReportType
import org.gradle.api.file.Directory
import java.io.File
import org.gradle.api.Project
import org.gradle.api.file.Directory

internal object OutputFileUtils {

fun projectDirDependenciesDir(
project: Project,
): Directory {
val dependenciesDir = project.layout.projectDirectory
.dir("dependencies")
dependenciesDir
.asFile
.apply {
if (!exists()) {
// Create the directory if it does not exist
mkdirs()
}
}
return dependenciesDir
}

fun buildDirOutputFile(
buildDirectory: Directory,
configurationName: String,
reportType: DependencyGuardReportType,
): File {
val configurationNameAndSuffix = "$configurationName${reportType.fileSuffix}"
return buildDirectory
.dir("tmp/dependency-guard")
.file("$configurationNameAndSuffix.txt")
.asFile
.apply {
parentFile.apply {
if (!exists()) {
// Create the "dependencies" directory if it does not exist
// Create the directory if it does not exist
mkdirs()
}
}
Expand All @@ -32,18 +49,15 @@ internal object OutputFileUtils {
): File {
val configurationNameAndSuffix = "${reportType.filePrefix}$configurationName${reportType.fileSuffix}"
return projectDirectory
.dir("dependencies")
.file("$configurationNameAndSuffix.txt")
.asFile
.apply {
parentFile.apply {
if (!exists()) {
// Create the "dependencies" directory if it does not exist
// Create the directory if it does not exist
mkdirs()
}
}
}
}

private const val DIRECTORY_NAME = "dependency-guard"
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ internal class DependencyGuardReportDataTest {
}

private class TestDelegate {
private val buildDirOutputFile = File.createTempFile("buildDir", ".txt")
private val projectDirOutputFile = File.createTempFile("projectDir", ".txt")
private val reportWriter = DependencyGuardListReportWriter(
artifacts = true,
Expand All @@ -154,7 +153,6 @@ internal class DependencyGuardReportDataTest {

fun whenReportWritten(report: DependencyGuardReportData, shouldBaseline: Boolean): DependencyListDiffResult {
return reportWriter.writeReport(
buildDirOutputFile = buildDirOutputFile,
projectDirOutputFile = projectDirOutputFile,
report = report,
shouldBaseline = shouldBaseline,
Expand Down
1 change: 1 addition & 0 deletions sample/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencyGuard {
// All dependencies included in Production Release APK
configuration("releaseRuntimeClasspath") {
modules = true
tree = true
allowedFilter = {
// Disallow dependencies with a name containing "test"
!it.contains("test")
Expand Down
54 changes: 54 additions & 0 deletions sample/app/dependencies/releaseRuntimeClasspath.tree.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
+--- project :sample:module1
| +--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.10
| | +--- org.jetbrains.kotlin:kotlin-stdlib:1.6.10
| | | +--- org.jetbrains:annotations:13.0
| | | \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.6.10
| | \--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.6.10
| | \--- org.jetbrains.kotlin:kotlin-stdlib:1.6.10 (*)
| \--- project :sample:module2
| +--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.6.10 (*)
| \--- org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2
| \--- org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:1.5.2
| +--- org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.30 -> 1.6.10 (*)
| \--- org.jetbrains.kotlin:kotlin-stdlib-common:1.5.30 -> 1.6.10
\--- androidx.activity:activity:1.4.0
+--- androidx.annotation:annotation:1.1.0 -> 1.2.0
+--- androidx.core:core:1.7.0
| +--- androidx.annotation:annotation:1.2.0
| +--- androidx.annotation:annotation-experimental:1.1.0
| +--- androidx.lifecycle:lifecycle-runtime:2.3.1
| | +--- androidx.arch.core:core-runtime:2.1.0
| | | +--- androidx.annotation:annotation:1.1.0 -> 1.2.0
| | | \--- androidx.arch.core:core-common:2.1.0
| | | \--- androidx.annotation:annotation:1.1.0 -> 1.2.0
| | +--- androidx.lifecycle:lifecycle-common:2.3.1
| | | \--- androidx.annotation:annotation:1.1.0 -> 1.2.0
| | +--- androidx.arch.core:core-common:2.1.0 (*)
| | \--- androidx.annotation:annotation:1.1.0 -> 1.2.0
| +--- androidx.versionedparcelable:versionedparcelable:1.1.1
| | +--- androidx.annotation:annotation:1.1.0 -> 1.2.0
| | \--- androidx.collection:collection:1.0.0
| | \--- androidx.annotation:annotation:1.0.0 -> 1.2.0
| +--- androidx.collection:collection:1.0.0 (*)
| \--- androidx.concurrent:concurrent-futures:1.0.0
| +--- com.google.guava:listenablefuture:1.0
| \--- androidx.annotation:annotation:1.1.0 -> 1.2.0
+--- androidx.lifecycle:lifecycle-runtime:2.3.1 (*)
+--- androidx.lifecycle:lifecycle-viewmodel:2.3.1
| \--- androidx.annotation:annotation:1.1.0 -> 1.2.0
+--- androidx.savedstate:savedstate:1.1.0
| +--- androidx.annotation:annotation:1.1.0 -> 1.2.0
| +--- androidx.arch.core:core-common:2.0.1 -> 2.1.0 (*)
| \--- androidx.lifecycle:lifecycle-common:2.0.0 -> 2.3.1 (*)
+--- androidx.lifecycle:lifecycle-viewmodel-savedstate:2.3.1
| +--- androidx.annotation:annotation:1.0.0 -> 1.2.0
| +--- androidx.savedstate:savedstate:1.1.0 (*)
| +--- androidx.lifecycle:lifecycle-livedata-core:2.3.1
| | +--- androidx.arch.core:core-common:2.1.0 (*)
| | +--- androidx.arch.core:core-runtime:2.1.0 (*)
| | \--- androidx.lifecycle:lifecycle-common:2.3.1 (*)
| \--- androidx.lifecycle:lifecycle-viewmodel:2.3.1 (*)
+--- org.jetbrains.kotlin:kotlin-stdlib:1.5.31 -> 1.6.10 (*)
+--- androidx.collection:collection:1.0.0 (*)
\--- androidx.tracing:tracing:1.0.0
\--- androidx.annotation:annotation:1.1.0 -> 1.2.0