001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixcore.generator.iwrpgen;
021:
022: import java.io.File;
023: import java.io.FileWriter;
024: import java.io.IOException;
025: import java.io.PrintWriter;
026: import java.util.ArrayList;
027: import java.util.List;
028:
029: import org.apache.tools.ant.BuildException;
030: import org.apache.tools.ant.Project;
031: import org.apache.tools.ant.taskdefs.Execute;
032: import org.apache.tools.ant.taskdefs.LogStreamHandler;
033: import org.apache.tools.ant.taskdefs.MatchingTask;
034: import org.apache.tools.ant.types.Commandline;
035: import org.apache.tools.ant.types.Path;
036: import org.apache.tools.ant.types.Reference;
037:
038: /**
039: *
040: * @author mleidig@schlund.de
041: *
042: */
043: public class AptTask extends MatchingTask {
044:
045: private File lastAptRunFile = new File("build/.lastaptrun");
046: private Path srcDir;
047: private File destDir;
048: private File preprocessDir;
049: private String factory;
050: private Path classPath;
051: private String encoding;
052:
053: @Override
054: public void execute() throws BuildException {
055: long lastAptRun = lastAptRunFile.lastModified();
056: List<File> modList = getModifiedFiles(lastAptRun);
057: if (modList.size() > 0) {
058: log("Processing " + modList.size() + " source file"
059: + (modList.size() > 1 ? "s" : ""));
060: Commandline cmd = new Commandline();
061: cmd.setExecutable("apt");
062: cmd.createArgument().setValue("-J-Xmx512m");
063: cmd.createArgument().setValue("-classpath");
064: classPath.addExisting(classPath.concatSystemClasspath());
065: cmd.createArgument().setPath(classPath);
066: cmd.createArgument().setValue("-sourcepath");
067: cmd.createArgument().setPath(srcDir);
068: cmd.createArgument().setValue("-nocompile");
069: cmd.createArgument().setValue("-encoding");
070: cmd.createArgument().setValue(encoding);
071: cmd.createArgument().setValue("-factory");
072: cmd.createArgument().setValue(factory);
073: cmd.createArgument().setValue("-s");
074: cmd.createArgument().setFile(preprocessDir);
075: int firstFileIndex = cmd.size();
076: for (File file : modList)
077: cmd.createArgument().setValue(file.getAbsolutePath());
078: log(cmd.toString(), Project.MSG_DEBUG);
079: callApt(cmd.getCommandline(), firstFileIndex);
080: }
081: lastAptRunFile.delete();
082: try {
083: lastAptRunFile.createNewFile();
084: } catch (IOException x) {
085: }
086: }
087:
088: private List<File> getModifiedFiles(long lastAptRun)
089: throws BuildException {
090: IWrapperFileScanner fileScanner = new IWrapperFileScanner();
091: List<File> modList = new ArrayList<File>();
092: String[] dirs = srcDir.list();
093: for (int i = 0; i < dirs.length; i++) {
094: File dir = getProject().resolveFile(dirs[i]);
095: if (!dir.exists())
096: throw new BuildException(
097: "Source directory doesn't exist: "
098: + dir.getAbsolutePath(), getLocation());
099: List<File> newFiles = fileScanner.getChangedFiles(dir,
100: destDir, lastAptRun);
101: modList.addAll(newFiles);
102: }
103: if (fileScanner.getScanCount() > 0)
104: System.out.println(fileScanner.printStatistics());
105: return modList;
106: }
107:
108: private void callApt(String[] args, int firstFileIndex) {
109: File tmpFile = null;
110: try {
111: String[] commandArray = null;
112: if (Commandline.toString(args).length() > 4096
113: && firstFileIndex >= 0) {
114: PrintWriter out = null;
115: try {
116: tmpFile = File.createTempFile("pfx-aptfiles-",
117: ".tmp", null);
118: log("Commandline too long, using temporary file '"
119: + tmpFile.getAbsolutePath() + "'.",
120: Project.MSG_DEBUG);
121: tmpFile.deleteOnExit();
122: out = new PrintWriter(new FileWriter(tmpFile));
123: for (int i = firstFileIndex; i < args.length; i++) {
124: if (args[i].indexOf(" ") > -1) {
125: args[i] = args[i].replace(
126: File.separatorChar, '/');
127: out.println("\"" + args[i] + "\"");
128: } else
129: out.println(args[i]);
130: }
131: out.flush();
132: commandArray = new String[firstFileIndex + 1];
133: System.arraycopy(args, 0, commandArray, 0,
134: firstFileIndex);
135: commandArray[firstFileIndex] = "@" + tmpFile;
136: } catch (IOException x) {
137: throw new BuildException(
138: "Error creating temporary file", x,
139: getLocation());
140: } finally {
141: out.close();
142: }
143: } else {
144: commandArray = args;
145: }
146: try {
147: Execute exec = new Execute(new LogStreamHandler(this ,
148: Project.MSG_INFO, Project.MSG_WARN));
149: exec.setAntRun(getProject());
150: exec.setWorkingDirectory(getProject().getBaseDir());
151: exec.setCommandline(commandArray);
152: int ret = exec.execute();
153: if (ret != 0)
154: throw new BuildException(
155: "Error while executing apt (exit value: "
156: + ret + ").");
157: } catch (IOException x) {
158: throw new BuildException("Error invoking apt", x,
159: getLocation());
160: }
161: } finally {
162: if (tmpFile != null && tmpFile.exists())
163: tmpFile.delete();
164: }
165: }
166:
167: public void setSrcdir(Path srcDir) {
168: this .srcDir = srcDir;
169: }
170:
171: public void setDestdir(File destDir) {
172: this .destDir = destDir;
173: }
174:
175: public void setPreprocessdir(File preprocessDir) {
176: this .preprocessDir = preprocessDir;
177: }
178:
179: public void setFactory(String factory) {
180: this .factory = factory;
181: }
182:
183: public void setClasspathRef(Reference ref) {
184: classPath = new Path(getProject());
185: classPath.createPath().setRefid(ref);
186: }
187:
188: public void setEncoding(String encoding) {
189: this.encoding = encoding;
190: }
191:
192: }
|