Skip to content

Commit

Permalink
Merge pull request #3 from rspieldenner/reduce_memory
Browse files Browse the repository at this point in the history
Reduce memory
  • Loading branch information
rspieldenner authored Mar 22, 2017
2 parents c8f0aea + be1dd2a commit 815201c
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
0.2.1 / 2017-03-22
==================

* Remove duplicate messages
* Only collect messages if `dependencyInsightEnhanced` task is present

0.2.0 / 2017-03-15
==================

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Project> {
private val dependencyManagement : DependencyManagement = DependencyManagement()
class DependencyBasePlugin: Plugin<Project> {
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) {
Expand All @@ -45,7 +53,14 @@ class DependencyBasePlugin : Plugin<Project> {
}

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,T> S.groovyClosure(crossinline call: (a: T) -> Unit) = object : Closure<Unit>(this) {
@Suppress("unused")
fun doCall(a: T) {
call(a)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,44 @@ import com.netflix.nebula.dependencybase.internal.*
class DependencyManagement {
val reasons: MutableList<Reason> = mutableListOf()
val pluginMessages: MutableSet<String> = 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 }.reversed()
val recs = reasons.filter { it.configuration == configuration && it.coordinate == coordinate }.distinct().reversed()
val reason = recs.joinToString(transform = Reason::getReason)

return reason
}

fun getGlobalMessages(): String = pluginMessages.joinToString(separator = System.lineSeparator())

fun disableMessageStore() {
shouldStoreReason = false
reasons.clear()
pluginMessages.clear()
}

fun enableMessageStore() {
shouldStoreReason = true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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"
}
Expand All @@ -156,19 +174,19 @@ 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 {
resolutionStrategy {
force "test.nebula:foo:1.0.0"
}
}
dependencies {
compile "test.nebula:foo"
}
Expand Down Expand Up @@ -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}
}
Expand Down

0 comments on commit 815201c

Please sign in to comment.