1 31 package org.jruby.util.ant; 32 33 import java.io.File ; 34 import java.util.ArrayList ; 35 import java.util.HashMap ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 import java.util.Map ; 39 40 import org.apache.tools.ant.BuildException; 41 import org.apache.tools.ant.DirectoryScanner; 42 import org.apache.tools.ant.Task; 43 import org.apache.tools.ant.types.FileSet; 44 import org.apache.tools.ant.types.Mapper; 45 import org.apache.tools.ant.util.FileNameMapper; 46 import org.apache.tools.ant.util.GlobPatternMapper; 47 import org.apache.tools.ant.util.SourceFileScanner; 48 import org.jruby.main.ASTSerializer; 49 50 54 public class JRubySerialize extends Task { 55 private File destdir = null; 56 private boolean verbose = false; 57 58 private List fileSets = new ArrayList (); 59 private Mapper mapperElement = null; 60 61 public void execute() throws BuildException { 62 Map fileMap = new HashMap (); 63 64 FileNameMapper mapper = null; 65 if (mapperElement != null) { 66 mapper = mapperElement.getImplementation(); 67 } else { 68 mapper = new GlobPatternMapper(); 69 mapper.setFrom("*.rb"); 70 mapper.setTo("*.rb.ast.ser"); 71 } 72 73 SourceFileScanner sfs = new SourceFileScanner(this); 74 75 for (int i = 0, size = fileSets.size(); i < size; i++) { 76 FileSet fs = (FileSet) fileSets.get(i); 77 DirectoryScanner ds = fs.getDirectoryScanner(getProject()); 78 79 File dir = fs.getDir(getProject()); 80 String [] files = ds.getIncludedFiles(); 81 82 files = sfs.restrict(files, dir, destdir, mapper); 83 84 for (int j = 0; j < files.length; j++) { 85 File src = new File (dir, files[j]); 86 File dest = new File (destdir, mapper.mapFileName(files[j])[0]); 87 fileMap.put(src, dest); 88 } 89 } 90 91 if (fileMap.size() > 0) { 92 log( 93 "Serializing " 94 + fileMap.size() 95 + " file" 96 + (fileMap.size() == 1 ? "" : "s") 97 + " to " 98 + destdir.getAbsolutePath()); 99 100 Iterator iter = fileMap.entrySet().iterator(); 101 while (iter.hasNext()) { 102 Map.Entry entry = (Map.Entry ) iter.next(); 103 try { 104 ((File ) entry.getValue()).getParentFile().mkdirs(); 105 if (verbose) System.out.println(entry.getKey()); 106 ASTSerializer.serialize((File ) entry.getKey(), (File ) entry.getValue()); 107 } catch (Exception e) { 108 e.printStackTrace(); 109 } 110 } 111 } 112 } 113 114 public void setDestdir(File destdir) { 115 this.destdir = destdir; 116 } 117 118 121 public void addFileset(FileSet set) { 122 fileSets.add(set); 123 } 124 125 public void setVerbose(boolean verbose) { 126 this.verbose = verbose; 127 } 128 129 132 public Mapper createMapper() throws BuildException { 133 if (mapperElement != null) { 134 throw new BuildException("Cannot define more than one mapper", getLocation()); 135 } 136 mapperElement = new Mapper(getProject()); 137 return mapperElement; 138 } 139 } 140 | Popular Tags |