Skip to content

Commit 8f93511

Browse files
zevclaude
andcommitted
Add ant_graph_task to draw Ant target dependencies
JRuby's rakelib/graph.rake (graph:viz, added in 2011 as an "ant target visualizer") drew the targets of JRuby's Ant build.xml with Graphviz. JRuby no longer has an Ant build and removes it in jruby/jruby#9761, so move the idea here as a reusable feature: - Rake::Ant.dot(project) returns Graphviz DOT text with an edge from each dependency to the target that depends on it, sanitizing - and . in target names as graph.rake did - ant_graph_task(name, buildfile, output) defines a task that ant_imports the build file and pipes the DOT text to `dot -Tpng -x` - CI installs Graphviz so the PNG test runs; it skips without `dot` Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 061cedf commit 8f93511

6 files changed

Lines changed: 84 additions & 0 deletions

File tree

‎.github/workflows/test.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ jobs:
2828
java-version: 21
2929
- name: set up ant
3030
uses: cedx/SetupAnt@v7
31+
- name: set up graphviz
32+
run: sudo apt-get update && sudo apt-get install -y graphviz
3133
- name: set up ruby
3234
uses: ruby/setup-ruby@v1
3335
with:

‎lib/rake/ant.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,5 @@ def self.ant_script
6868
Ant = Rake::Ant
6969

7070
require 'rake/ant/ant'
71+
require 'rake/ant/graph'
7172
require 'rake/ant/rake' if defined?(::Rake)

‎lib/rake/ant/graph.rb‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Rake::Ant
2+
# Graphviz DOT text for the target dependencies of an Ant project, with an
3+
# edge from each dependency to the target that depends on it.
4+
def self.dot(project)
5+
edges = []
6+
project.targets.each do |name, target|
7+
target.dependencies.to_a.each do |dep|
8+
edges << "#{dot_id(dep)} -> #{dot_id(name)}"
9+
end
10+
end
11+
"digraph ant {\n#{edges.map { |edge| "#{edge}\n" }.join}}\n"
12+
end
13+
14+
def self.dot_id(name)
15+
name.gsub(/[-.]/, '_')
16+
end
17+
end

‎lib/rake/ant/rake.rb‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ def ant_task(*args, &block)
44
end
55
end
66

7+
# Defines a task that draws the target dependencies of an Ant build file as
8+
# a PNG. Requires Graphviz's `dot` on the PATH.
9+
def ant_graph_task(name = :'ant:graph', buildfile = 'build.xml', output = 'build_graph.png')
10+
task(name) do
11+
ant_import buildfile
12+
IO.popen(['dot', '-Tpng', '-x', '-o', output], 'w') do |dot|
13+
dot.write Rake::Ant.dot(ant.project)
14+
end
15+
fail "dot exited with #{$?.exitstatus}" unless $?.success?
16+
end
17+
end
18+
719
class FileList
820
def to_str
921
join(',')

‎test/graph_example.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0"?>
2+
<project name="graph_example">
3+
<target name="init"/>
4+
<target name="compile" depends="init"/>
5+
<target name="test.unit" depends="compile"/>
6+
<target name="dist-jar" depends="init,compile"/>
7+
</project>

‎test/test_graph.rb‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require 'ant_test_helper'
2+
require 'tmpdir'
3+
4+
class TestGraph < Minitest::Test
5+
include Ant::TestHelper
6+
include Rake::DSL
7+
8+
BUILDFILE = File.expand_path('graph_example.xml', __dir__)
9+
10+
def setup
11+
@app = Rake.application
12+
Rake.application = Rake::Application.new
13+
end
14+
15+
def teardown
16+
Rake.application = @app
17+
end
18+
19+
def test_dot_has_an_edge_from_each_dependency_to_its_target
20+
ant_import BUILDFILE
21+
dot = Rake::Ant.dot(ant.project)
22+
23+
assert dot.start_with?("digraph ant {\n")
24+
assert dot.end_with?("}\n")
25+
edges = dot.lines[1..-2].map(&:chomp)
26+
assert_equal ["compile -> dist_jar", "compile -> test_unit", "init -> compile", "init -> dist_jar"], edges.sort
27+
end
28+
29+
def test_ant_graph_task_defines_a_task
30+
ant_graph_task :graph, BUILDFILE, 'unused.png'
31+
refute_nil Rake::Task[:graph]
32+
end
33+
34+
def test_ant_graph_task_writes_a_png
35+
skip 'Graphviz dot is not installed' unless system('dot', '-V', [:out, :err] => File::NULL)
36+
37+
Dir.mktmpdir do |dir|
38+
output = File.join(dir, 'build_graph.png')
39+
ant_graph_task :graph, BUILDFILE, output
40+
Rake::Task[:graph].invoke
41+
42+
assert_equal "\x89PNG".b, File.binread(output, 4)
43+
end
44+
end
45+
end

0 commit comments

Comments
 (0)