added renderDependencies task
This commit is contained in:
@@ -23,6 +23,10 @@ exportDependencies {
|
|||||||
configurationName = 'runtime'
|
configurationName = 'runtime'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
renderDependencies {
|
||||||
|
format = "svg"
|
||||||
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
runtime("org.hibernate:hibernate-core:5.4.13.Final")
|
runtime("org.hibernate:hibernate-core:5.4.13.Final")
|
||||||
}
|
}
|
@@ -9,7 +9,8 @@ buildscript {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
import net.woggioni.plugins.DependencyExportPluginExtension
|
import net.woggioni.plugins.ExportDependenciesPluginExtension
|
||||||
|
import net.woggioni.plugins.RenderDependenciesPluginExtension
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
kotlin("jvm") version "1.3.71"
|
kotlin("jvm") version "1.3.71"
|
||||||
@@ -25,6 +26,10 @@ dependencies {
|
|||||||
runtime("org.hibernate:hibernate-core:5.4.13.Final")
|
runtime("org.hibernate:hibernate-core:5.4.13.Final")
|
||||||
}
|
}
|
||||||
|
|
||||||
configure<DependencyExportPluginExtension> {
|
configure<ExportDependenciesPluginExtension> {
|
||||||
configurationName = "runtime"
|
configurationName = "runtime"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
configure<RenderDependenciesPluginExtension> {
|
||||||
|
format = "svg"
|
||||||
|
}
|
@@ -3,6 +3,7 @@
|
|||||||
*/
|
*/
|
||||||
package net.woggioni.plugins
|
package net.woggioni.plugins
|
||||||
|
|
||||||
|
import org.gradle.api.GradleException
|
||||||
import org.gradle.api.Plugin
|
import org.gradle.api.Plugin
|
||||||
import org.gradle.api.Project
|
import org.gradle.api.Project
|
||||||
import org.gradle.api.artifacts.component.ModuleComponentIdentifier
|
import org.gradle.api.artifacts.component.ModuleComponentIdentifier
|
||||||
@@ -13,26 +14,63 @@ import org.gradle.api.artifacts.result.UnresolvedDependencyResult
|
|||||||
import java.io.BufferedWriter
|
import java.io.BufferedWriter
|
||||||
import java.io.OutputStreamWriter
|
import java.io.OutputStreamWriter
|
||||||
import java.nio.file.Files
|
import java.nio.file.Files
|
||||||
|
import java.nio.file.Path
|
||||||
|
import java.nio.file.Paths
|
||||||
|
import kotlin.reflect.KMutableProperty0
|
||||||
|
|
||||||
open class DependencyExportPluginExtension {
|
|
||||||
|
open class ExportDependenciesPluginExtension(project: Project) {
|
||||||
var configurationName: String = "default"
|
var configurationName: String = "default"
|
||||||
|
var outputFile = project.buildDir.toPath().resolve("dependencies.dot")
|
||||||
|
}
|
||||||
|
|
||||||
|
open class RenderDependenciesPluginExtension(project: Project) {
|
||||||
|
var format: String = "xlib"
|
||||||
|
var outputFile = project.buildDir.toPath().resolve("renderedDependencies")
|
||||||
|
var graphvizExecutable: String = "dot"
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Overrider(private val properties : Map<String, Any?>, private val prefix: String) {
|
||||||
|
inline fun <reified T> identity(arg: T): T {
|
||||||
|
return arg
|
||||||
|
}
|
||||||
|
|
||||||
|
fun overrideProperty(
|
||||||
|
property: KMutableProperty0<String>) {
|
||||||
|
overrideProperty(property, ::identity)
|
||||||
|
}
|
||||||
|
|
||||||
|
inline fun <reified V> overrideProperty(
|
||||||
|
property: KMutableProperty0<V>,
|
||||||
|
valueFactory: (String) -> V) {
|
||||||
|
val propertyKey = prefix + "." + property.name
|
||||||
|
val propertyValue = properties[propertyKey] as String?
|
||||||
|
if (propertyValue != null) {
|
||||||
|
property.set(valueFactory(propertyValue))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
object DependencyExporter {
|
object DependencyExporter {
|
||||||
|
|
||||||
fun graphviz(project: Project, configurationName : String) {
|
fun exportDot(project: Project, ext: ExportDependenciesPluginExtension) {
|
||||||
|
val overrider = Overrider(project.properties, "exportDependencies")
|
||||||
|
overrider.overrideProperty(ext::configurationName)
|
||||||
|
overrider.overrideProperty(ext::outputFile) { value -> Paths.get(value) }
|
||||||
|
|
||||||
var sequence = 0
|
var sequence = 0
|
||||||
val map = HashMap<ResolvedComponentResult, Int>()
|
val map = HashMap<ResolvedComponentResult, Int>()
|
||||||
|
|
||||||
val resolutionResult = project.configurations.single {
|
val resolutionResult = project.configurations.single {
|
||||||
it.name == configurationName
|
it.name == ext.configurationName
|
||||||
}.incoming.resolutionResult
|
}.incoming.resolutionResult
|
||||||
project.buildDir.toPath().let {
|
if(!ext.outputFile.isAbsolute) {
|
||||||
Files.createDirectories(it)
|
ext.outputFile = project.buildDir.toPath().resolve(ext.outputFile)
|
||||||
}.let {
|
}
|
||||||
|
Files.createDirectories(ext.outputFile.parent)
|
||||||
BufferedWriter(
|
BufferedWriter(
|
||||||
OutputStreamWriter(
|
OutputStreamWriter(
|
||||||
Files.newOutputStream(project.buildDir.toPath().resolve("dependencies.dot"))))
|
Files.newOutputStream(ext.outputFile))).use { writer ->
|
||||||
}.use { writer ->
|
|
||||||
writer.write("digraph G {")
|
writer.write("digraph G {")
|
||||||
writer.newLine()
|
writer.newLine()
|
||||||
writer.write(" #rankdir=\"LR\";")
|
writer.write(" #rankdir=\"LR\";")
|
||||||
@@ -83,17 +121,47 @@ object DependencyExporter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class DependencyExportPlugin : Plugin<Project> {
|
object DependencyRenderer {
|
||||||
override fun apply(project: Project) {
|
fun render(project: Project, ext: RenderDependenciesPluginExtension, sourceFile : Path) {
|
||||||
val extension = DependencyExportPluginExtension()
|
val overrider = Overrider(project.properties, "renderDependencies")
|
||||||
project.extensions.add(DependencyExportPluginExtension::class.java, "exportDependencies", extension)
|
overrider.overrideProperty(ext::format)
|
||||||
project.tasks.register("exportDependencies") {
|
overrider.overrideProperty(ext::graphvizExecutable)
|
||||||
it.doLast {
|
overrider.overrideProperty(ext::outputFile) { value -> Paths.get(value) }
|
||||||
val propertyKey = "exportDependencies.configurationName"
|
|
||||||
val properties = project.properties
|
if(!ext.outputFile.isAbsolute) {
|
||||||
val configurationName = properties.getOrDefault(propertyKey, extension.configurationName) as String
|
ext.outputFile = project.buildDir.toPath().resolve(ext.outputFile)
|
||||||
DependencyExporter.graphviz(project, configurationName = configurationName)
|
|
||||||
}
|
}
|
||||||
|
val cmd: List<String> = listOf(
|
||||||
|
ext.graphvizExecutable,
|
||||||
|
"-T${ext.format}",
|
||||||
|
"-o${ext.outputFile}",
|
||||||
|
sourceFile.toString()
|
||||||
|
|
||||||
|
)
|
||||||
|
val returnCode = ProcessBuilder(cmd).inheritIO().start().waitFor()
|
||||||
|
if (returnCode != 0) {
|
||||||
|
throw GradleException("Error invoking graphviz")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class DependencyExportPlugin : Plugin<Project> {
|
||||||
|
override fun apply(project: Project) {
|
||||||
|
val dependencyExportExtension = ExportDependenciesPluginExtension(project)
|
||||||
|
project.extensions.add(ExportDependenciesPluginExtension::class.java, "exportDependencies", dependencyExportExtension)
|
||||||
|
val exportDependenciesTask = project.tasks.register("exportDependencies") {
|
||||||
|
it.doLast {
|
||||||
|
DependencyExporter.exportDot(project, dependencyExportExtension)
|
||||||
|
}
|
||||||
|
}.get()
|
||||||
|
|
||||||
|
val renderDependenciesPluginExtension = RenderDependenciesPluginExtension(project)
|
||||||
|
project.extensions.add(RenderDependenciesPluginExtension::class.java, "renderDependencies", renderDependenciesPluginExtension)
|
||||||
|
val renderDependenciesTask = project.tasks.register("renderDependencies") {
|
||||||
|
it.dependsOn(exportDependenciesTask)
|
||||||
|
it.doLast {
|
||||||
|
DependencyRenderer.render(project, renderDependenciesPluginExtension, dependencyExportExtension.outputFile)
|
||||||
|
}
|
||||||
|
}.get()
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user