Last active
August 29, 2015 14:17
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | |
"}" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Author
AnthonyMastrean
commented
Mar 18, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment