読者です 読者をやめる 読者になる 読者になる

nokogiri習作 antのビルドファイルの概要を出力

出力結果

targetとコメントを抽出

ruby buildxmltarget.rb apache-tomcat-6.0.18-src/build.xml 
build-prepare []
compile []
   Compile internal server components 
   Copy static resource files 
build-only [build-prepare,compile,package]
package []
   Common Annotations 1.0 JAR File 
   Servlet 2.5 Implementation JAR File 
   JSP 2.1 Implementation JAR File 
   JSP 2.1 EL Implementation JAR File 
   Bootstrap JAR File 
   Tomcat-juli JAR File 
   Catalina Main JAR File 
   Catalina GroupCom/Tribes JAR File 
   Catalina Cluster/HA JAR File 
   Catalina Ant Tasks JAR File 
   Protocol handlers - Coyote 
   Jasper Implementation JAR File 
   Jasper EL Implementation JAR File 
   i18n JARs 
build-docs []
   XSL processing 
   Print friendly version 
deploy [build-only,build-docs]
   Copy scripts 
   Copy static resource files 
   Copy other regular webapps 
   Build classes for examples webapp 
   Add sources for examples 
clean-depend [] Clean depend src components
clean []
proxyflags []
   check proxy parameters. 
setproxy [proxyflags]
testexist []
downloadgz [setproxy,testexist]
   Download and extract the package 
downloadzip [setproxy,testexist]
   Download and extract the package 
downloadfile [setproxy,testexist]
   Download extract the file 
download [] Builds and download dependent components
   Build Tomcat DBCP bundle 
   Build Jasper JDT bundle 
build-tomcat-dbcp []
build-jasper-jdt []
ruby buildxmltarget.rb apache-tomcat-6.0.18-src/dist.xml 
build-webapps-precompile [] Precompile webapps
   JSPC 
fix-webapps []
   Extra build steps for webapps 
   Add release notes to the root webapp 
   Add documents to the tomcat-docs webapp 
   Build JARs for webapps classes 
   Add XML declarations for admin, manager and balancer 
clean [] Clean all components
dist-prepare []
dist-static [dist-prepare]
   Copy the top-level documentation files 
   Copy the contents of each "build" directory 
   Correct permissions and line endings on "bin" scripts 
dist-javadoc [dist-source] Create the Tomcat javadoc
dist-deployer [] Create the Tomcat deployer binary
   Servlet and JSP 
   Digester and dependencies 
   Main build script 
   Copy deployer documentation 
dist-source []
   Tomcat source 
installer [] Create Windows installer
release [clean,dist-static,dist-deployer,installer,package-zip,package-tgz,package-deployer-zip,package-deployer-tgz,dist-source,dist-javadoc,package-docs-tgz,package-src-zip,package-src-tgz] Create a Tomcat 6 packaged distribution
package-zip []
package-deployer-zip []
package-admin-zip []
package-tgz []
package-deployer-tgz []
package-admin-tgz []
package-docs-tgz []
   Package gocs 
package-src-zip []
package-src-tgz []
ruby buildxmltarget.rb apache-tomcat-6.0.18-src/extras.xml 
prepare []
clean []
commons-logging []
webservices []
   Classpath 
   Compile internal server components 
   Catalina Main JAR File 
extras [prepare,commons-logging,webservices]
proxyflags []
   check proxy parameters. 
setproxy [proxyflags]
testexist []
downloadfile [setproxy,testexist]
   Download extract the file 

スクリプト

require 'rubygems'
require 'nokogiri'

if ARGV.length != 1
   STDERR.puts "Usage: #{$0} ant-build-file"
end

buildxml = Nokogiri::XML(File.read(ARGV[0]))

targets = Array.new

buildxml.search("project").first.children.each do |child|
  if child.name == 'target'
    comments = Array.new
    child.children.each do |c|
      comments << c if c.name == "comment"
    end
    targets << [child, comments]
  end
end

targets.each do |pair|
  target = pair[0]
  print "#{target.get_attribute('name')} [#{target.get_attribute('depends')}]"
  print " #{target.get_attribute('description')}" if target.has_attribute?('description')
  puts 
  pair[1].each do |c|
    next if c.text.include?("\n")
    puts "  #{c.text}"
  end
end