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

Use NamedDomainObjectContainer to configure the Extension. #92

Merged
merged 2 commits into from
May 14, 2019
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
2 changes: 2 additions & 0 deletions code_quality_tools/detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ style:
active: false
MaxLineLength:
active: false
UnnecessaryApply:
active: false

comments:
UndocumentedPublicClass:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.vanniktech.dependency.graph.generator

import groovy.lang.Closure
import guru.nidi.graphviz.attribute.Label
import guru.nidi.graphviz.engine.Format
import guru.nidi.graphviz.engine.Format.PNG
import guru.nidi.graphviz.engine.Format.SVG
import guru.nidi.graphviz.model.MutableGraph
import guru.nidi.graphviz.model.MutableNode
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ResolvedDependency
Expand All @@ -14,17 +16,29 @@ import org.gradle.api.artifacts.ResolvedDependency
* Extension for dependency graph generation.
* @since 0.1.0
*/
open class DependencyGraphGeneratorExtension {
open class DependencyGraphGeneratorExtension(project: Project) {
/**
* Generator extensions. By default this will yield a graph showing every project and library dependencies.
* @since 0.1.0
*/
var generators: List<Generator> = listOf(Generator.ALL)
var generators: NamedDomainObjectContainer<Generator> = project.container(Generator::class.java) { Generator(it) }.apply {
add(Generator.ALL)
}

fun generators(closure: Closure<*>) {
generators.configure(closure)
}

/**
* ProjectGenerator extensions. By default this will yield a graph showing every project and it's project dependencies.
*/
var projectGenerators: List<ProjectGenerator> = listOf(ProjectGenerator.ALL)
var projectGenerators: NamedDomainObjectContainer<ProjectGenerator> = project.container(ProjectGenerator::class.java) { ProjectGenerator(it) }.apply {
add(ProjectGenerator.ALL)
}

fun projectGenerators(closure: Closure<*>) {
projectGenerators.configure(closure)
}

/**
* Generator allows you to filter and tweak between projects- as well as library dependencies.
Expand All @@ -35,29 +49,29 @@ open class DependencyGraphGeneratorExtension {
* The name of this type of generator that should be in lowerCamelCase.
* The task name as well as the output files will use this name.
*/
val name: String = "",
var name: String = "",
/** Return true when you want to include this dependency, false otherwise. */
val include: (ResolvedDependency) -> Boolean = { true },
var include: (ResolvedDependency) -> Boolean = { true },
/** Return true when you want to include the children of this dependency, false otherwise. */
val children: (ResolvedDependency) -> Boolean = { true },
var children: (ResolvedDependency) -> Boolean = { true },
/** Allows to change the node for the given dependency. */
val dependencyNode: (MutableNode, ResolvedDependency) -> MutableNode = { node, _ -> node },
var dependencyNode: (MutableNode, ResolvedDependency) -> MutableNode = { node, _ -> node },
/** Allows to change the node for the given project. */
val projectNode: (MutableNode, Project) -> MutableNode = { node, _ -> node },
var projectNode: (MutableNode, Project) -> MutableNode = { node, _ -> node },
/** Optional label that can be displayed wrapped around the graph. */
val label: Label? = null,
var label: Label? = null,
/** Return true when you want to include this configuration, false otherwise. */
val includeConfiguration: (Configuration) -> Boolean = {
var includeConfiguration: (Configuration) -> Boolean = {
// By default we'll include everything that's on the compileClassPath except test, UnitTest and AndroidTest configurations.
val raw = it.name.replace("compileClasspath", "", ignoreCase = true)
it.name.contains("compileClassPath", ignoreCase = true) && listOf("test", "AndroidTest", "UnitTest").none { raw.contains(it) }
},
/** Return true when you want to include this project, false otherwise. */
val includeProject: (Project) -> Boolean = { true },
var includeProject: (Project) -> Boolean = { true },
/** Return the output formats you'd like to be generated. */
val outputFormats: List<Format> = listOf(PNG, SVG),
var outputFormats: List<Format> = listOf(PNG, SVG),
/** Allows you to mutate the graph and add things as needed. */
val graph: (MutableGraph) -> MutableGraph = { it }
var graph: (MutableGraph) -> MutableGraph = { it }
) {
/** Gradle task name that is associated with this generator. */
val gradleTaskName = "generateDependencyGraph${name.capitalize()}"
Expand All @@ -79,15 +93,15 @@ open class DependencyGraphGeneratorExtension {
* The name of this type of generator that should be in lowerCamelCase.
* The task name as well as the output files will use this name.
*/
val name: String = "",
var name: String = "",
/** Allows to change the node for the given project. */
val projectNode: (MutableNode, Project) -> MutableNode = { node, _ -> node },
var projectNode: (MutableNode, Project) -> MutableNode = { node, _ -> node },
/** Return true when you want to include this project, false otherwise. */
val includeProject: (Project) -> Boolean = { true },
var includeProject: (Project) -> Boolean = { true },
/** Return the output formats you'd like to be generated. */
val outputFormats: List<Format> = listOf(PNG, SVG),
var outputFormats: List<Format> = listOf(PNG, SVG),
/** Allows you to mutate the graph and add things as needed. */
val graph: (MutableGraph) -> MutableGraph = { it }
var graph: (MutableGraph) -> MutableGraph = { it }
) {
/** Gradle task name that is associated with this generator. */
val gradleTaskName = "generateProjectDependencyGraph${name.capitalize()}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,28 @@ package com.vanniktech.dependency.graph.generator

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.util.GradleVersion
import java.io.File

open class DependencyGraphGeneratorPlugin : Plugin<Project> {
override fun apply(project: Project) {
val extension = project.extensions.create("dependencyGraphGenerator", DependencyGraphGeneratorExtension::class.java)

if (GradleVersion.current() >= GradleVersion.version("4.9")) {
extension.generators.forEach {
project.tasks.register(it.gradleTaskName, DependencyGraphGeneratorTask::class.java, it.configureTask(project))
}

extension.projectGenerators.forEach {
project.tasks.register(it.gradleTaskName, ProjectDependencyGraphGeneratorTask::class.java, it.configureTask(project))
val extension = project.extensions.create("dependencyGraphGenerator", DependencyGraphGeneratorExtension::class.java, project)

extension.generators.all { generator ->
project.tasks.register(generator.gradleTaskName, DependencyGraphGeneratorTask::class.java) {
it.generator = generator
it.group = "reporting"
it.description = "Generates a dependency graph${generator.name.nonEmptyPrepend(" for ")}"
it.outputDirectory = File(project.buildDir, "reports/dependency-graph/")
}
} else {
project.afterEvaluate { _ ->
extension.generators.forEach {
project.tasks.create(it.gradleTaskName, DependencyGraphGeneratorTask::class.java, it.configureTask(project))
}

extension.projectGenerators.forEach {
project.tasks.create(it.gradleTaskName, ProjectDependencyGraphGeneratorTask::class.java, it.configureTask(project))
}
}
}
}

private fun DependencyGraphGeneratorExtension.Generator.configureTask(project: Project): (DependencyGraphGeneratorTask) -> Unit {
val name = name.nonEmptyPrepend(" for ")

return {
it.generator = this
it.group = "reporting"
it.description = "Generates a dependency graph$name"
it.outputDirectory = File(project.buildDir, "reports/dependency-graph/")
}
}

private fun DependencyGraphGeneratorExtension.ProjectGenerator.configureTask(project: Project): (ProjectDependencyGraphGeneratorTask) -> Unit {
val name = name.nonEmptyPrepend(" for ")

return {
it.projectGenerator = this
it.group = "reporting"
it.description = "Generates a project dependency graph$name"
it.outputDirectory = File(project.buildDir, "reports/project-dependency-graph/")
extension.projectGenerators.all { projectGenerator ->
project.tasks.register(projectGenerator.gradleTaskName, ProjectDependencyGraphGeneratorTask::class.java) {
it.projectGenerator = projectGenerator
it.group = "reporting"
it.description = "Generates a project dependency graph${projectGenerator.name.nonEmptyPrepend(" for ")}"
it.outputDirectory = File(project.buildDir, "reports/project-dependency-graph/")
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,8 @@ class DependencyGraphGeneratorPluginTest {
assertThat(task.outputDirectory).hasToString(File(singleProject.buildDir, "reports/project-dependency-graph/").toString())
}

@Test fun integrationTestGradle410() {
integrationTest("4.10")
}

@Test fun integrationTestGradle49() {
integrationTest("4.9")
}

@Test fun integrationTestGradle46() {
integrationTest("4.6")
}

@Test fun integrationTestGradle40() {
integrationTest("4.0")
}

@Test fun integrationTestGradle33() {
integrationTest("3.3")
@Test fun integrationTestGradle50() {
integrationTest("5.0")
}

private fun integrationTest(gradleVersion: String) {
Expand Down Expand Up @@ -203,7 +187,7 @@ class DependencyGraphGeneratorPluginTest {

GradleRunner.create()
.withPluginClasspath()
.withGradleVersion("4.6")
.withGradleVersion("5.0")
.withProjectDir(testProjectDir.root)
.withArguments("generateDependencyGraph", "generateProjectDependencyGraph")
.forwardStdError(stdErrorWriter)
Expand Down