Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ jobs:
java-version: 21
- name: set up ant
uses: cedx/SetupAnt@v7
- name: set up graphviz
run: sudo apt-get update && sudo apt-get install -y graphviz
- name: set up ruby
uses: ruby/setup-ruby@v1
with:
Expand Down
1 change: 1 addition & 0 deletions lib/rake/ant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@ def self.ant_script
Ant = Rake::Ant

require 'rake/ant/ant'
require 'rake/ant/graph'
require 'rake/ant/rake' if defined?(::Rake)
6 changes: 1 addition & 5 deletions lib/rake/ant/ant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,6 @@ def ant(*args, &block)
Rake::Ant.ant(*args, &block)
end

# Need Rake DSL at top-level for "task" and other methods.
require 'rake/dsl_definition'
include Rake::DSL

def ant_import(filename = 'build.xml')
ant = Rake::Ant.ant

Expand All @@ -220,6 +216,6 @@ def ant_import(filename = 'build.xml')
ant.project.targets.each do |target_name, target|
name = Rake.application.lookup(target_name) ? "ant_" + target_name : target_name

task(name) { target.project.execute_target(target_name) }
Rake::Task.define_task(name) { target.project.execute_target(target_name) }
end
end
17 changes: 17 additions & 0 deletions lib/rake/ant/graph.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Rake::Ant
# Graphviz DOT text for the target dependencies of an Ant project, with an
# edge from each dependency to the target that depends on it.
def self.dot(project)
edges = []
project.targets.each do |name, target|
target.dependencies.to_a.each do |dep|
edges << "#{dot_id(dep)} -> #{dot_id(name)}"
end
end
"digraph ant {\n#{edges.map { |edge| "#{edge}\n" }.join}}\n"
end

def self.dot_id(name)
name.gsub(/[-.]/, '_')
end
end
14 changes: 13 additions & 1 deletion lib/rake/ant/rake.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
def ant_task(*args, &block)
task(*args) do |t|
Rake::Task.define_task(*args) do |t|
ant.define_tasks(&block)
end
end

# Defines a task that draws the target dependencies of an Ant build file as
# a PNG. Requires Graphviz's `dot` on the PATH.
def ant_graph_task(name = :'ant:graph', buildfile = 'build.xml', output = 'build_graph.png')
Rake::Task.define_task(name) do
ant_import buildfile
IO.popen(['dot', '-Tpng', '-x', '-o', output], 'w') do |dot|
dot.write Rake::Ant.dot(ant.project)
end
fail "dot exited with #{$?.exitstatus}" unless $?.success?
end
end

class FileList
def to_str
join(',')
Expand Down
7 changes: 7 additions & 0 deletions test/graph_example.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<project name="graph_example">
<target name="init"/>
<target name="compile" depends="init"/>
<target name="test.unit" depends="compile"/>
<target name="dist-jar" depends="init,compile"/>
</project>
44 changes: 44 additions & 0 deletions test/test_graph.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require 'ant_test_helper'
require 'tmpdir'

class TestGraph < Minitest::Test
include Ant::TestHelper

BUILDFILE = File.expand_path('graph_example.xml', __dir__)

def setup
@app = Rake.application
Rake.application = Rake::Application.new
end

def teardown
Rake.application = @app
end

def test_dot_has_an_edge_from_each_dependency_to_its_target
ant_import BUILDFILE
dot = Rake::Ant.dot(ant.project)

assert dot.start_with?("digraph ant {\n")
assert dot.end_with?("}\n")
edges = dot.lines[1..-2].map(&:chomp)
assert_equal ["compile -> dist_jar", "compile -> test_unit", "init -> compile", "init -> dist_jar"], edges.sort
end

def test_ant_graph_task_defines_a_task
ant_graph_task :graph, BUILDFILE, 'unused.png'
refute_nil Rake::Task[:graph]
end

def test_ant_graph_task_writes_a_png
skip 'Graphviz dot is not installed' unless system('dot', '-V', [:out, :err] => File::NULL)

Dir.mktmpdir do |dir|
output = File.join(dir, 'build_graph.png')
ant_graph_task :graph, BUILDFILE, output
Rake::Task[:graph].invoke

assert_equal "\x89PNG".b, File.binread(output, 4)
end
end
end
3 changes: 1 addition & 2 deletions test/test_rake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def test_sets_file_lists_as_task_attributes_by_joining_them_with_commas

class TestRakeAntTask < Minitest::Test
include Ant::TestHelper
include Rake::DSL

def setup
@app = Rake.application
Expand All @@ -26,7 +25,7 @@ def teardown
def test_creates_a_rake_task_whose_body_defines_ant_tasks
refute_includes ant.properties, "foo"

task :initial
Rake::Task.define_task :initial
ant_task :ant => :initial do
property :name => "foo", :value => "bar"
end
Expand Down
Loading