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

Generator: Allow custom output formats. #51

Merged
merged 3 commits into from
May 25, 2018
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: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ dependencies {
implementation localGroovy()
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
implementation gradleApi()
api "guru.nidi:graphviz-java:0.5.0"
api "guru.nidi:graphviz-java:0.5.2"

testImplementation 'junit:junit:4.12'
testImplementation 'org.assertj:assertj-core:3.10.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.vanniktech.dependency.graph.generator

import com.vanniktech.dependency.graph.generator.DependencyGraphGeneratorExtension.Generator.Companion.ALL
import guru.nidi.graphviz.attribute.Label
import guru.nidi.graphviz.engine.Format
import guru.nidi.graphviz.engine.Format.PNG
import guru.nidi.graphviz.model.MutableNode
import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
Expand Down Expand Up @@ -41,16 +43,15 @@ open class DependencyGraphGeneratorExtension {
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 }
val includeProject: (Project) -> Boolean = { true },
/** Return the output formats you'd like to be generated. */
val outputFormats: List<Format> = listOf(PNG)
) {
/** Gradle task name that is associated with this generator. */
val gradleTaskName = "generateDependencyGraph${name.capitalize()}"
private val outputFileName = "dependency-graph${name.toHyphenCase().nonEmptyPrepend("-")}"
internal val outputFileName = "dependency-graph${name.toHyphenCase().nonEmptyPrepend("-")}"
internal val outputFileNameDot = "$outputFileName.dot"

/** Output file name of the generated png which is stored under the build/reports/dependency-graph/ directory. */
val outputFileNamePng = "$outputFileName.png"

companion object {
/** Default behavior which will include everything as is. */
@JvmStatic val ALL = Generator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ open class DependencyGraphGeneratorPlugin : Plugin<Project> {
task.group = "reporting"
task.description = "Generates a dependency graph${it.name.nonEmptyPrepend(" for ")}"
task.inputFile = project.buildFile
task.outputFileDot = File(project.buildDir, "reports/dependency-graph/${it.outputFileNameDot}")
task.outputFileImage = File(project.buildDir, "reports/dependency-graph/${it.outputFileNamePng}")
task.outputDirectory = File(project.buildDir, "reports/dependency-graph/")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
package com.vanniktech.dependency.graph.generator

import com.vanniktech.dependency.graph.generator.DependencyGraphGeneratorExtension.Generator
import guru.nidi.graphviz.engine.Format.PNG
import guru.nidi.graphviz.engine.Graphviz
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import java.io.File

open class DependencyGraphGeneratorTask : DefaultTask() {
lateinit var generator: Generator // TODO does this need to be an input? Quick testing shows no.
@InputFile lateinit var inputFile: File

@OutputFile lateinit var outputFileDot: File
@OutputFile lateinit var outputFileImage: File
@OutputDirectory lateinit var outputDirectory: File

@TaskAction fun run() {
val graph = DotGenerator(project, generator).generateGraph()
outputFileDot.writeText(graph.toString())
Graphviz.fromGraph(graph).render(PNG).toFile(outputFileImage)
File(outputDirectory, generator.outputFileNameDot).writeText(graph.toString())

val graphviz = Graphviz.fromGraph(graph)

generator.outputFormats.forEach {
graphviz.render(it).toFile(File(outputDirectory, generator.outputFileName))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.vanniktech.dependency.graph.generator

import com.vanniktech.dependency.graph.generator.DependencyGraphGeneratorExtension.Generator.Companion.ALL
import guru.nidi.graphviz.engine.Format.PNG
import org.assertj.core.api.Java6Assertions.assertThat
import org.junit.Test

Expand All @@ -9,4 +10,8 @@ class DependencyGraphGeneratorExtensionTest {
val defaults = DependencyGraphGeneratorExtension()
assertThat(defaults.generators).containsExactly(ALL)
}

@Test fun allDefaults() {
assertThat(ALL.outputFormats).containsExactly(PNG)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ class DependencyGraphGeneratorPluginTest {
assertThat(task.group).isEqualTo("reporting")
assertThat(task.description).isEqualTo("Generates a dependency graph")
assertThat(task.inputFile).hasToString(singleProject.buildFile.toString())
assertThat(task.outputFileDot).hasToString(File(singleProject.buildDir, "reports/dependency-graph/dependency-graph.dot").toString())
assertThat(task.outputFileImage).hasToString(File(singleProject.buildDir, "reports/dependency-graph/dependency-graph.png").toString())
assertThat(task.outputDirectory).hasToString(File(singleProject.buildDir, "reports/dependency-graph/").toString())
}

@Test fun integrationTestGradle46() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class GeneratorTest {
assertThat(generatorFoo.gradleTaskName).isEqualTo("generateDependencyGraphFooBar")
}

@Test fun outputFileName() {
assertThat(ALL.outputFileName).isEqualTo("dependency-graph")
assertThat(generatorFoo.outputFileName).isEqualTo("dependency-graph-foo-bar")
}

@Test fun outputFileNameDot() {
assertThat(ALL.outputFileNameDot).isEqualTo("dependency-graph.dot")
assertThat(generatorFoo.outputFileNameDot).isEqualTo("dependency-graph-foo-bar.dot")
}

@Test fun outputFileNamePng() {
assertThat(ALL.outputFileNamePng).isEqualTo("dependency-graph.png")
assertThat(generatorFoo.outputFileNamePng).isEqualTo("dependency-graph-foo-bar.png")
}
}