Proper graph actions: graph-src and graph-pkg for source dependency graph and quasi-package dependency graph (based on source directories and source dependencies).

git-svn-id: https://simple-build-tool.googlecode.com/svn/trunk@992 d89573ee-9141-11dd-94d4-bdf5e562f29c
This commit is contained in:
dmharrah 2009-09-11 14:09:12 +00:00
parent 4d80a7e8dd
commit a0becc9efb
4 changed files with 60 additions and 25 deletions

View File

@ -260,7 +260,8 @@ abstract class BasicScalaProject extends ScalaProject with BasicDependencyProjec
protected def docAllAction = (doc && docTest) describedAs DocAllDescription
protected def packageAllAction = task { None } dependsOn(`package`, packageTest, packageSrc, packageTestSrc, packageDocs) describedAs PackageAllDescription
protected def graphAction = graphTask(graphPath, mainCompileConditional.analysis).dependsOn(compile)
protected def graphSourcesAction = graphSourcesTask(graphSourcesPath, mainSourceRoots, mainCompileConditional.analysis).dependsOn(compile)
protected def graphPackagesAction = graphPackagesTask(graphPackagesPath, mainSourceRoots, mainCompileConditional.analysis).dependsOn(compile)
protected def incrementVersionAction = task { incrementVersionNumber(); None } describedAs IncrementVersionDescription
protected def releaseAction = (test && packageAll && incrementVersion) describedAs ReleaseDescription
@ -284,7 +285,8 @@ abstract class BasicScalaProject extends ScalaProject with BasicDependencyProjec
lazy val packageProject = packageProjectAction
lazy val docAll = docAllAction
lazy val packageAll = packageAllAction
lazy val graph = graphAction
lazy val graphSrc = graphSourcesAction
lazy val graphPkg = graphPackagesAction
lazy val incrementVersion = incrementVersionAction
lazy val release = releaseAction
lazy val copyResources = copyResourcesAction

View File

@ -7,41 +7,68 @@ import java.io.{File, Writer}
object DotGraph
{
def apply(analysis: BasicCompileAnalysis, outputDirectory: Path, log: Logger) =
def sources(analysis: BasicCompileAnalysis, outputDirectory: Path, sourceRoots: Iterable[Path], log: Logger) =
{
val roots = sourceRoots.toList.map(_.asFile)
val toString = (x: File) => sourceToString(roots, x)
apply(analysis, outputDirectory, toString, toString, log)
}
def packages(analysis: BasicCompileAnalysis, outputDirectory: Path, sourceRoots: Iterable[Path], log: Logger) =
{
val roots = sourceRoots.toList.map(_.asFile)
val packageOnly = (path: String) =>
{
val last = path.lastIndexOf(File.separatorChar)
val packagePath = (if(last > 0) path.substring(0, last) else path).trim
if(packagePath.isEmpty) "" else packagePath.replace(File.separatorChar, '.')
}
val toString = packageOnly compose ((x: File) => sourceToString(roots, x))
apply(analysis, outputDirectory, toString, toString, log)
}
def apply(analysis: BasicCompileAnalysis, outputDirectory: Path, sourceToString: File => String, externalToString: File => String, log: Logger) =
{
val outputDir = outputDirectory.asFile
def generateGraph[Key, Value](fileName: String, graphName: String, graph: Iterable[(Key, scala.collection.Set[Value])],
keyToString: Key => String, valueToString: Value => String) =
{
FileUtilities.write(new File(outputDir, fileName), log)
{
(writer: Writer) =>
{
import scala.collection.mutable.{HashMap, MultiMap, Set}
val mappedGraph = new HashMap[String, Set[String]] with MultiMap[String, String]
for( (key, values) <- graph; keyString = keyToString(key); value <- values)
mappedGraph.add(keyString, valueToString(value))
FileUtilities.write(new File(outputDir, fileName), log) { (writer: Writer) =>
def writeLine(line: String) = FileUtilities.writeLine(writer, line)
writeLine("digraph " + graphName + " {")
for( (dependsOn, dependants) <- graph; dependant <- dependants)
writeLine(valueToString(dependant) + " -> " + keyToString(dependsOn))
for( (dependsOn, dependants) <- mappedGraph; dependant <- dependants)
{
if(dependant != dependsOn && !dependsOn.isEmpty && !dependant.isEmpty)
writeLine("\"" + dependant + "\" -> \"" + dependsOn + "\"")
}
writeLine("}")
None
}
}
}
val srcToString = (p: Path) => sourceToString(p.asFile)
FileUtilities.createDirectory(outputDir, log) orElse
generateGraph(BasicAnalysis.DependenciesFileName, "dependencies", analysis.allDependencies,
sourceToString, sourceToString) orElse
srcToString, srcToString) orElse
generateGraph(BasicAnalysis.ExternalDependenciesFileName, "externalDependencies", analysis.allExternalDependencies,
fileToString, sourceToString)
externalToString, srcToString)
}
private def sourceToString(source: Path) = fileToString(source.asFile)
private def fileToString(file: File) =
def sourceToString(roots: List[File], source: File) =
{
val rawName = file.getName
val name =
val rawName = relativized(roots, source).trim
if(rawName.endsWith(".scala"))
rawName.substring(0, rawName.length - ".scala".length)
else
rawName
"\"" + name + "\""
}
private def relativized(roots: List[File], path: File): String =
{
val relativized = roots.flatMap(root => Path.relativize(root, path))
val shortest = (Int.MaxValue /: relativized)(_ min _.length)
relativized.find(_.length == shortest).getOrElse(path.getName)
}
}

View File

@ -32,7 +32,8 @@ trait ScalaPaths extends PackagePaths
def testAnalysisPath: Path
def mainDocPath: Path
def testDocPath: Path
def graphPath: Path
def graphSourcesPath: Path
def graphPackagesPath: Path
def mainResourcesOutputPath: Path
def testResourcesOutputPath: Path
@ -146,6 +147,8 @@ trait MavenStyleScalaPaths extends BasicScalaPaths with BasicPackagePaths
def docPath = outputPath / docDirectoryName
def graphPath = outputPath / graphDirectoryName
def graphPackagesPath = graphPath / "packages"
def graphSourcesPath = graphPath / "sources"
/** These are the directories that are created when a user makes a new project from sbt.*/
protected def directoriesToCreate: List[Path] =

View File

@ -168,7 +168,10 @@ trait ScalaProject extends SimpleScalaProject with FileTasks with MultiTaskProje
}
private def toTask(testTask: NamedTestTask) = task(testTask.run()) named(testTask.name)
def graphTask(outputDirectory: Path, analysis: CompileAnalysis): Task = task { DotGraph(analysis, outputDirectory, log) }
def graphSourcesTask(outputDirectory: Path, roots: PathFinder, analysis: CompileAnalysis): Task =
task { DotGraph.sources(analysis, outputDirectory, roots.get, log) }
def graphPackagesTask(outputDirectory: Path, roots: PathFinder, analysis: CompileAnalysis): Task =
task { DotGraph.packages(analysis, outputDirectory, roots.get, log) }
def scaladocTask(label: String, sources: PathFinder, outputDirectory: Path, classpath: PathFinder, options: ScaladocOption*): Task =
scaladocTask(label, sources, outputDirectory, classpath, options)
def scaladocTask(label: String, sources: PathFinder, outputDirectory: Path, classpath: PathFinder, options: => Seq[ScaladocOption]): Task =