From 6b572bd2e159dbef790192ab5f780eb49996e2b9 Mon Sep 17 00:00:00 2001 From: Rob Spieldenner Date: Mon, 20 Mar 2017 14:53:00 -0700 Subject: [PATCH 1/2] Remove duplicate messages from output --- CHANGELOG.md | 5 +++++ .../nebula/dependencybase/DependencyManagement.kt | 2 +- .../dependencybase/DependencyManagementSpec.groovy | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f2b4e8d..d8beb43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +0.2.1 / 2017-03-20 +================== + +* Remove duplicate messages + 0.2.0 / 2017-03-15 ================== diff --git a/src/main/kotlin/com/netflix/nebula/dependencybase/DependencyManagement.kt b/src/main/kotlin/com/netflix/nebula/dependencybase/DependencyManagement.kt index 4ec6c09..ba9b5d7 100644 --- a/src/main/kotlin/com/netflix/nebula/dependencybase/DependencyManagement.kt +++ b/src/main/kotlin/com/netflix/nebula/dependencybase/DependencyManagement.kt @@ -40,7 +40,7 @@ class DependencyManagement { fun addPluginMessage(message: String) = pluginMessages.add(message) fun getReason(configuration: String, coordinate: String): String { - val recs = reasons.filter { it.configuration == configuration && it.coordinate == coordinate }.reversed() + val recs = reasons.filter { it.configuration == configuration && it.coordinate == coordinate }.distinct().reversed() val reason = recs.joinToString(transform = Reason::getReason) return reason diff --git a/src/test/groovy/com/netflix/nebula/dependencybase/DependencyManagementSpec.groovy b/src/test/groovy/com/netflix/nebula/dependencybase/DependencyManagementSpec.groovy index b7d2105..19c7866 100644 --- a/src/test/groovy/com/netflix/nebula/dependencybase/DependencyManagementSpec.groovy +++ b/src/test/groovy/com/netflix/nebula/dependencybase/DependencyManagementSpec.groovy @@ -39,6 +39,20 @@ class DependencyManagementSpec extends Specification { management.getReason("compile", coordinate) == "recommend 1.0.0 via TestRecommender" } + def "only display same reason once"() { + given: + def management = new DependencyManagement() + final coordinate = "test.nebula:foo" + + when: + management.addRecommendation("compile", coordinate, "1.0.0", "TestRecommender", "test") + management.addRecommendation("compile", coordinate, "1.0.0", "TestRecommender", "test") + + then: + management.reasons.size() == 2 + management.getReason("compile", coordinate) == "recommend 1.0.0 via TestRecommender" + } + def "handle non semver versions"() { given: def management = new DependencyManagement() From be1dd2a3c93dab3dd5cbfa9ea73b0be7f2fa3733 Mon Sep 17 00:00:00 2001 From: Rob Spieldenner Date: Wed, 22 Mar 2017 14:25:39 -0700 Subject: [PATCH 2/2] reduce memory footprint, only collect when displaying dependencyInsightEnhanced --- CHANGELOG.md | 3 +- .../dependencybase/DependencyBasePlugin.kt | 23 +++++++++--- .../dependencybase/DependencyManagement.kt | 23 +++++++++--- ...DependencyBasePluginIntegrationSpec.groovy | 36 ++++++++++++++----- 4 files changed, 66 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8beb43..46f63ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,8 @@ -0.2.1 / 2017-03-20 +0.2.1 / 2017-03-22 ================== * Remove duplicate messages +* Only collect messages if `dependencyInsightEnhanced` task is present 0.2.0 / 2017-03-15 ================== diff --git a/src/main/kotlin/com/netflix/nebula/dependencybase/DependencyBasePlugin.kt b/src/main/kotlin/com/netflix/nebula/dependencybase/DependencyBasePlugin.kt index 0301325..4417db5 100644 --- a/src/main/kotlin/com/netflix/nebula/dependencybase/DependencyBasePlugin.kt +++ b/src/main/kotlin/com/netflix/nebula/dependencybase/DependencyBasePlugin.kt @@ -16,17 +16,25 @@ package com.netflix.nebula.dependencybase import com.netflix.nebula.dependencybase.tasks.NebulaDependencyInsightReportTask +import groovy.lang.Closure import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.artifacts.Configuration +import org.gradle.api.execution.TaskExecutionGraph -class DependencyBasePlugin : Plugin { - private val dependencyManagement : DependencyManagement = DependencyManagement() +class DependencyBasePlugin: Plugin { + val dependencyManagement: DependencyManagement = DependencyManagement() + lateinit var insightTask: NebulaDependencyInsightReportTask override fun apply(project: Project) { initializeDependencyBase(project) enableForceCollection(project) setupDependencyInsightEnhanced(project) + project.gradle.taskGraph.whenReady( groovyClosure { taskGraph : TaskExecutionGraph -> + if (!taskGraph.hasTask(insightTask)) { + dependencyManagement.disableMessageStore() + } + }) } private fun initializeDependencyBase(project: Project) { @@ -45,7 +53,14 @@ class DependencyBasePlugin : Plugin { } private fun setupDependencyInsightEnhanced(project: Project) { - val depInsightEnhancedTask = project.tasks.create("dependencyInsightEnhanced", NebulaDependencyInsightReportTask::class.java) - depInsightEnhancedTask.reasonLookup = dependencyManagement + insightTask = project.tasks.create("dependencyInsightEnhanced", NebulaDependencyInsightReportTask::class.java) + insightTask.reasonLookup = dependencyManagement + } +} + +inline fun S.groovyClosure(crossinline call: (a: T) -> Unit) = object : Closure(this) { + @Suppress("unused") + fun doCall(a: T) { + call(a) } } diff --git a/src/main/kotlin/com/netflix/nebula/dependencybase/DependencyManagement.kt b/src/main/kotlin/com/netflix/nebula/dependencybase/DependencyManagement.kt index ba9b5d7..669ad40 100644 --- a/src/main/kotlin/com/netflix/nebula/dependencybase/DependencyManagement.kt +++ b/src/main/kotlin/com/netflix/nebula/dependencybase/DependencyManagement.kt @@ -20,24 +20,27 @@ import com.netflix.nebula.dependencybase.internal.* class DependencyManagement { val reasons: MutableList = mutableListOf() val pluginMessages: MutableSet = mutableSetOf() + private var shouldStoreReason: Boolean = true fun addRecommendation(configuration: String, coordinate: String, version: String, source: String, plugin: String) { - reasons.add(Recommendation(configuration, coordinate, version, source)) + if (shouldStoreReason) reasons.add(Recommendation(configuration, coordinate, version, source)) } fun addLock(configuration: String, coordinate: String, version: String, source: String, plugin: String) { - reasons.add(Lock(configuration, coordinate, version, source)) + if (shouldStoreReason) reasons.add(Lock(configuration, coordinate, version, source)) } fun addForce(configuration: String, coordinate: String) { - reasons.add(Force(configuration, coordinate)) + if (shouldStoreReason) reasons.add(Force(configuration, coordinate)) } fun addReason(configuration: String, coordinate: String, message: String, plugin: String) { - reasons.add(DefaultReason(configuration, coordinate, message)) + if (shouldStoreReason) reasons.add(DefaultReason(configuration, coordinate, message)) } - fun addPluginMessage(message: String) = pluginMessages.add(message) + fun addPluginMessage(message: String) { + if (shouldStoreReason) pluginMessages.add(message) + } fun getReason(configuration: String, coordinate: String): String { val recs = reasons.filter { it.configuration == configuration && it.coordinate == coordinate }.distinct().reversed() @@ -47,4 +50,14 @@ class DependencyManagement { } fun getGlobalMessages(): String = pluginMessages.joinToString(separator = System.lineSeparator()) + + fun disableMessageStore() { + shouldStoreReason = false + reasons.clear() + pluginMessages.clear() + } + + fun enableMessageStore() { + shouldStoreReason = true + } } diff --git a/src/test/groovy/com/netflix/nebula/dependencybase/RecommendationDependencyBasePluginIntegrationSpec.groovy b/src/test/groovy/com/netflix/nebula/dependencybase/RecommendationDependencyBasePluginIntegrationSpec.groovy index c94ea5e..f9d5435 100644 --- a/src/test/groovy/com/netflix/nebula/dependencybase/RecommendationDependencyBasePluginIntegrationSpec.groovy +++ b/src/test/groovy/com/netflix/nebula/dependencybase/RecommendationDependencyBasePluginIntegrationSpec.groovy @@ -28,8 +28,6 @@ class RecommendationDependencyBasePluginIntegrationSpec extends IntegrationSpec def results = runTasks("dependencyInsightEnhanced", "--configuration", "compileClasspath", "--dependency", "foo") then: - println results?.standardError - println results?.standardOutput results.standardOutput.contains "test.nebula:foo:1.0.0 (recommend 1.0.0 via NebulaTest)" } @@ -68,6 +66,7 @@ class RecommendationDependencyBasePluginIntegrationSpec extends IntegrationSpec } def "detect complete substitute foo to bar and give insight"() { + given: def graph = new DependencyGraphBuilder() .addModule("test.nebula:foo:1.0.0") .addModule("test.nebula:bar:2.0.0") @@ -109,6 +108,25 @@ class RecommendationDependencyBasePluginIntegrationSpec extends IntegrationSpec results.standardOutput.contains "test.nebula:bar:2.0.0 (possible replacement of test.nebula:foo)" } + def "only collect dependency insight if dependencyInsightEnhanced is on task graph"() { + given: + setup1Dependency() + + buildFile << """\ + task messageCount { + doLast { + println "Message count: \${project.nebulaDependencyBase.reasons.size()}" + } + } + """.stripIndent() + + when: + def results = runTasks("dependencies", "--configuration", "compileClasspath", "messageCount") + + then: + results.standardOutput.contains "Message count: 0" + } + def setup1Dependency() { def graph = new DependencyGraphBuilder().addModule("test.nebula:foo:1.0.0").build() def generator = new GradleDependencyGenerator(graph) @@ -134,9 +152,9 @@ class RecommendationDependencyBasePluginIntegrationSpec extends IntegrationSpec } } } - + project.nebulaDependencyBase.addRecommendation("compileClasspath", "test.nebula:foo", "1.0.0", "NebulaTest", "test") - + dependencies { compile "test.nebula:foo" } @@ -156,11 +174,11 @@ class RecommendationDependencyBasePluginIntegrationSpec extends IntegrationSpec id "java" } apply plugin: "nebula.dependency-base" - + repositories { ${generator.mavenRepositoryBlock} } - + project.nebulaDependencyBase.addRecommendation("compileClasspath", "test.nebula:foo", "2.0.0", "NebulaTest", "test") configurations.all { @@ -168,7 +186,7 @@ class RecommendationDependencyBasePluginIntegrationSpec extends IntegrationSpec force "test.nebula:foo:1.0.0" } } - + dependencies { compile "test.nebula:foo" } @@ -202,10 +220,10 @@ class RecommendationDependencyBasePluginIntegrationSpec extends IntegrationSpec } } } - + project.nebulaDependencyBase.addRecommendation("compileClasspath", "test.nebula:foo", "1.0.0", "NebulaTest", "test") project.nebulaDependencyBase.addRecommendation("compileClasspath", "test.nebula:bar", "2.0.0", "NebulaTest", "test") - + repositories { ${generator.mavenRepositoryBlock} }