-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
DependencyGraphGeneratorExtension.kt
159 lines (149 loc) · 7.47 KB
/
DependencyGraphGeneratorExtension.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.vanniktech.dependency.graph.generator
import groovy.lang.Closure
import guru.nidi.graphviz.attribute.Label
import guru.nidi.graphviz.attribute.Style
import guru.nidi.graphviz.engine.Format
import guru.nidi.graphviz.engine.Format.PNG
import guru.nidi.graphviz.engine.Format.SVG
import guru.nidi.graphviz.engine.Graphviz
import guru.nidi.graphviz.model.Link
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
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.Optional
import java.util.Locale
/**
* Extension for dependency graph generation.
* @since 0.1.0
*/
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: 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: 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.
* @since 0.1.0
*/
data class Generator @JvmOverloads constructor(
/**
* 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.
*/
@get:Input var name: String = "",
/** Return true when you want to include this [ResolvedDependency], false otherwise. */
@get:Nested var include: (ResolvedDependency) -> Boolean = { true },
/** Return true when you want to include the children of this [ResolvedDependency], false otherwise. */
@get:Nested var children: (ResolvedDependency) -> Boolean = { true },
/** Allows to change the [MutableNode] for the given [ResolvedDependency]. */
@get:Nested var dependencyNode: (MutableNode, ResolvedDependency) -> MutableNode = { node, _ -> node },
/** Allows to change the [MutableNode] for the given [Project]. */
@get:Nested var projectNode: (MutableNode, Project) -> MutableNode = { node, _ -> node },
/** Allows to change the [Link] between two [DependencyContainer]. */
@get:Nested var link: (link: Link, from: DependencyContainer, to: DependencyContainer) -> Link = { it, _, _ -> it },
/** Optional label that can be displayed wrapped around the graph. */
@get:Internal var label: Label? = null, // Not serializable making it unusable as an Input.
/** Return true when you want to include this [Configuration], false otherwise. */
@get:Nested 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. */
@get:Nested var includeProject: (Project) -> Boolean = { true },
/** Return the output [Format]s you'd like to have generated. */
@get:Nested var outputFormats: List<Format> = listOf(PNG, SVG),
/** Allows you to mutate the [MutableGraph] and add things as needed. */
@get:Nested var graph: (MutableGraph) -> MutableGraph = { it },
/** Allows you to configure the [Graphviz] instance. */
@get:Nested var graphviz: (Graphviz) -> Graphviz = { it },
) {
/** Gradle task name that is associated with this generator. */
@get:Internal val gradleTaskName = "generateDependencyGraph${
name.replaceFirstChar {
when {
it.isLowerCase() -> it.titlecase(Locale.getDefault())
else -> it.toString()
}
}
}"
@get:Internal internal val outputFileName = "dependency-graph${name.toHyphenCase().nonEmptyPrepend("-")}"
@get:Internal internal val outputFileNameDot = "$outputFileName.dot"
@get:[Optional Input] internal val rawLabel: String?
get() = label?.toString()
companion object {
/** Default behavior which will include everything as is. */
@JvmStatic val ALL = Generator()
}
}
/**
* ProjectGenerator allows you to filter and tweak between projects dependencies.
* @since 0.6.0
*/
data class ProjectGenerator @JvmOverloads constructor(
/**
* 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.
*/
@get:Input var name: String = "",
/** Allows to change the [MutableNode] for the given [Project]. */
@get:Nested var projectNode: (MutableNode, Project) -> MutableNode = { node, _ -> node },
/** Allows to change the [Link] between two [Project]s and for the given [Configuration]. */
@get:Nested var link: (link: Link, from: Project, to: Project, configuration: Configuration) -> Link = { it, _, _, configuration ->
if (configuration.isImplementation()) it.with(Style.DOTTED) else it
},
/** Return true when you want to include this [Project], false otherwise. */
@get:Nested var includeProject: (Project) -> Boolean = { true },
/** Return true when you want to include this [Configuration], false otherwise. */
@get:Nested var includeConfiguration: (Configuration) -> Boolean = {
// Filter out test configurations by default. Similar to how it's done in Generator.
// We also filter out any configuration that starts with ios.
// This is convenient when using Kotlin Multiplatform, and exporting more modules since transitive dependencies are not included by default.
// https://kotlinlang.org/docs/multiplatform-build-native-binaries.html#export-dependencies-to-binaries
!it.name.contains("test", ignoreCase = true) && !it.name.startsWith("ios")
},
/** Return the output [Format]s you'd like to have generated. */
@get:Nested var outputFormats: List<Format> = listOf(PNG, SVG),
/** Allows you to mutate the [MutableGraph] and add things as needed. */
@get:Nested var graph: (MutableGraph) -> MutableGraph = { it },
/** Allows you to configure the [Graphviz] instance. */
@get:Nested var graphviz: (Graphviz) -> Graphviz = { it },
) {
/** Gradle task name that is associated with this generator. */
@get:Internal val gradleTaskName = "generateProjectDependencyGraph${
name.replaceFirstChar {
when {
it.isLowerCase() -> it.titlecase(Locale.getDefault())
else -> it.toString()
}
}
}"
@get:Internal internal val outputFileName = "project-dependency-graph${name.toHyphenCase().nonEmptyPrepend("-")}"
@get:Internal internal val outputFileNameDot = "$outputFileName.dot"
companion object {
/** Default behavior which will include everything as is. */
@JvmStatic val ALL = ProjectGenerator()
}
}
}