Skip to content

Commit

Permalink
Fail early and give tailored message when dot command is not present.
Browse files Browse the repository at this point in the history
  • Loading branch information
vanniktech committed Mar 4, 2018
1 parent 3f05f44 commit 40742ba
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.vanniktech.dependency.graph.generator

import com.vanniktech.dependency.graph.generator.DependencyGraphGeneratorExtension.Generator
import org.apache.tools.ant.taskdefs.condition.Os
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import java.io.File
import java.io.OutputStream

open class DependencyGraphGeneratorTask : DefaultTask() {
lateinit var generator: Generator // TODO does this need to be an input? Quick testing shows no.
Expand All @@ -15,6 +18,23 @@ open class DependencyGraphGeneratorTask : DefaultTask() {
@OutputFile lateinit var imageOutputFile: File

@TaskAction fun run() {
val result = project.exec {
it.standardOutput = NullOutputStream // Don't print the output.
it.executable = "command"
it.args("-v", "dot")
it.isIgnoreExitValue = true
}

if (result.exitValue != 0) {
val message = when {
Os.isFamily(Os.FAMILY_MAC) -> "Please install via: brew install graphviz"
Os.isFamily(Os.FAMILY_UNIX) -> "Please install via: sudo apt-get install graphviz"
else -> "Please find a way to install it."
}

throw GradleException("This task requires dot from graphviz. $message")
}

dotOutputFile.writeText(DotGenerator(project, generator).generateContent())

project.exec {
Expand All @@ -29,4 +49,10 @@ open class DependencyGraphGeneratorTask : DefaultTask() {
it.rename("\\.dot", "")
}
}

internal object NullOutputStream : OutputStream() {
override fun write(b: Int) {
// No-op.
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.vanniktech.dependency.graph.generator

import com.vanniktech.dependency.graph.generator.DependencyGraphGeneratorExtension.Generator.Companion.ALL
import org.assertj.core.api.Java6Assertions.assertThat
import org.gradle.api.internal.project.DefaultProject
import org.gradle.api.plugins.JavaLibraryPlugin
import org.gradle.testfixtures.ProjectBuilder
import org.junit.Before
import org.junit.Test
import java.io.File

class DependencyGraphGeneratorPluginTest {
private lateinit var singleProject: DefaultProject

@Before fun setUp() {
// Casting this to DefaultProject so we can call evaluate later.
singleProject = ProjectBuilder.builder().withName("single").build() as DefaultProject
singleProject.plugins.apply(JavaLibraryPlugin::class.java)
singleProject.repositories.run { add(mavenCentral()) }
singleProject.dependencies.add("api", "org.jetbrains.kotlin:kotlin-stdlib:1.2.30")
singleProject.dependencies.add("implementation", "io.reactivex.rxjava2:rxjava:2.1.10")
}

@Test fun taskProperties() {
singleProject.plugins.apply(DependencyGraphGeneratorPlugin::class.java)

singleProject.evaluate() // Need to call this for afterEvaluate() to pick up.

val task = singleProject.tasks.getByName("generateDependencyGraph") as DependencyGraphGeneratorTask
assertThat(task.generator).isSameAs(ALL)
assertThat(task.group).isEqualTo("reporting")
assertThat(task.description).isEqualTo("Generates a dependency graph")
assertThat(task.inputFile).hasToString(singleProject.buildFile.toString())
assertThat(task.dotOutputFile).hasToString(File(singleProject.buildDir, "dependency-graph.dot").toString())
assertThat(task.imageOutputFile).hasToString(File(singleProject.projectDir, "dependency-graph.png").toString())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import org.junit.Test

class HeaderTest {
@Test fun syntax() {
assertThat(Header("foo")).hasToString("label=\"foo\" fontsize=\"24\" height=\"5\" labelloc=\"t\" labeljust=\"c\"")
assertThat(Header("foo", 10)).hasToString("label=\"foo\" fontsize=\"10\" height=\"5\" labelloc=\"t\" labeljust=\"c\"")
assertThat(Header("foo", 10, 25)).hasToString("label=\"foo\" fontsize=\"10\" height=\"25\" labelloc=\"t\" labeljust=\"c\"")
assertThat(Header("foo", 10, 25, BOTTOM)).hasToString("label=\"foo\" fontsize=\"10\" height=\"25\" labelloc=\"b\" labeljust=\"c\"")
assertThat(Header("1234", 100, 5, BOTTOM, LEFT)).hasToString("label=\"1234\" fontsize=\"100\" height=\"5\" labelloc=\"b\" labeljust=\"l\"")
}
}

0 comments on commit 40742ba

Please sign in to comment.