BuildStamp Ant Task

From Stack Overflow
Jump to: navigation, search
/*
 * Created on May 2, 2003
 *
 */
package org.ninjasoft.ant;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import java.util.*;
import java.io.*;
import java.util.regex.*;

public class BuildStamp extends Task {
    private static final String PATTERN = "\\s*public\\s+static\\s+final\\s+String\\s+BUILD_DATE.*";
    private static final String LINE = "public static final String BUILD_DATE = ";
    private String dir = null;
    private String stamp = null;
    private Pattern pattern;
    private boolean debug = false;

    /**
     * Run this task
     * @see org.apache.tools.ant.Task#execute()
     */
    public void execute() throws BuildException {
        super.execute();
        if (this.dir == null)
            throw new BuildException("dir attribute must be given");
        if (this.stamp == null)
            throw new BuildException("stamp attribute must be given");
        if (this.debug)
            super.log("Using RegEx \"" + PATTERN + "\"");
        pattern = Pattern.compile(PATTERN);
        if (this.debug)
            super.log("Starting in " + dir);
        Vector matches = findMatchingFiles(new File(dir), new Vector());
        super.log("" + matches.size() + " file match(es) regex");
        for (Iterator i = matches.iterator(); i.hasNext(); )
            replace((File) i.next());
    }

    private Vector findMatchingFiles(File f, Vector v) {
        // If this is a java file, scan it
        if (f.isFile() && f.getName().toLowerCase().endsWith(".java")) {
            if (this.debug)
                super.log("Checking " + f.getAbsolutePath());
            try{
                String line;
                LineNumberReader in = new LineNumberReader(new FileReader(f));
                while ((line = in.readLine()) != null) {
                    Matcher m = pattern.matcher(line);
                    if (m.matches()) {
                        if (this.debug)
                            super.log("File " + f.getName() + " matches");
                        v.add(f);
                        break;
                    }
                }
                in.close();
            }catch(Exception e){
                super.log("Error reading " + f.getName() + ": " + e.getMessage());
            }
        // If this is a directory, recuse to children
        }else if (f.isDirectory()) {
            if (this.debug)
                super.log("Traversing to " + f.getAbsolutePath());
            File[] children = f.listFiles();
            for (int i=0; i<children.length; i++) {
                v = findMatchingFiles(children[i], v);
            }
        }
        return v;
    }

    private void replace(File f) {
        File tempFile = new File(f.getAbsolutePath() + ".stamp");
        super.log("Stamping " + f.getName());
        try{
            String line;
            LineNumberReader in = new LineNumberReader(new FileReader(f));
            PrintWriter out = new PrintWriter(new FileOutputStream(tempFile));
            while ((line = in.readLine()) != null) {
                Matcher m = pattern.matcher(line);
                if (m.matches()) {
                    out.print(LINE);
                    out.print("\"");
                    out.print(this.stamp);
                    out.println("\";");
                }else{
                    out.println(line);
                }
            }
            in.close();
            out.close();
            f.delete();
            tempFile.renameTo(f);
        }catch(Exception e){
            super.log("Error reading " + f.getName() + ": " + e.getMessage());
        }

    }

    /**
     * Return the description of this task
     * @see org.apache.tools.ant.Task#getDescription()
     */
    public String getDescription() {
        return "Replace public static final String BUILD_STAMP... with the actual build stamp";
    }

    public void setDir(String s) {
        this.dir = s;
    }

    public void setStamp(String s) {
        this.stamp = s;
    }

    public void setDebug(boolean b) {
        this.debug = b;
    }

}
Personal tools