Skip to content

Instantly share code, notes, and snippets.

@AnthonyMastrean
Last active August 29, 2015 14:17
Show Gist options
  • Save AnthonyMastrean/dc7fb3c4a2630a7bb6f0 to your computer and use it in GitHub Desktop.
Save AnthonyMastrean/dc7fb3c4a2630a7bb6f0 to your computer and use it in GitHub Desktop.
Create a Graphviz dot file from a set of Ant files (targets and depends)
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[string[]] $Path
)
BEGIN {
"digraph {"
}
PROCESS {
foreach($item in $Path) {
Select-Xml -XPath '//target' -Path $item `
| Select-Object -ExpandProperty Node `
| Select-Object @{Name="Name";Expression={$_.Name -replace '-', '_'}}, @{Name="Depends";Expression={($_.Depends -replace '-', '_') -split ','}} `
| %{ $name = $_.Name; $deps = $_.Depends; "`t$name;"; if($deps) { $deps | %{ "`t$name -> $_;" } } }
}
}
END {
"}"
}
#!/usr/bin/env bash
# vim: set ft=ruby:
exec /usr/bin/env ruby --disable-gems -x "$0" $*
#!ruby
require 'rexml/document'
class GraphIt
def main()
lines = $stdin.readlines.map(&:chomp)
files = lines.map{ |item| File.open(item) }
docs = files.map{ |item| REXML::Document.new(item) }
elements = docs.map{ |item| REXML::XPath.match(item, '//target') }.flatten
targets = elements.map{ |item| Target.from_element(item) }
graph = Digraph.new(targets)
puts graph.to_dot
end
end
class Target
def self.from_element(element)
name = element.attributes['name']
depends = (element.attributes['depends'] || '').split(',')
Target.new(name, depends)
end
attr_reader :name, :depends
def initialize(name, depends)
@name = name || fail()
@depends = depends || []
end
def to_dot
name = @name.gsub('-', '_')
depends = @depends.map{ |item| item.gsub('-', '_') }
["#{name};", depends.map{ |item| "#{name} -> #{item};" }].flatten
end
end
class Digraph
attr_reader :targets
def initialize(targets)
@targets = targets
end
def to_dot
["digraph {", @targets.map(&:to_dot), "}"].flatten
end
end
if $0 == __FILE__
GraphIt.new.main
end
@AnthonyMastrean
Copy link
Author

$ dot -Tpng -o graph.png <(ls build/*.xml | graphit)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment