1 |
// Preverify Ant Task |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
package net.douglasthrift.anttasks; |
8 |
|
9 |
import java.io.File; |
10 |
import java.util.ArrayList; |
11 |
|
12 |
import org.apache.tools.ant.BuildException; |
13 |
import org.apache.tools.ant.Task; |
14 |
import org.apache.tools.ant.taskdefs.ExecTask; |
15 |
import org.apache.tools.ant.types.EnumeratedAttribute; |
16 |
import org.apache.tools.ant.types.Path; |
17 |
import org.apache.tools.ant.types.Reference; |
18 |
import org.apache.tools.ant.util.FileUtils; |
19 |
|
20 |
public class Preverify extends Task |
21 |
{ |
22 |
public static class Target extends EnumeratedAttribute |
23 |
{ |
24 |
public String[] getValues() |
25 |
{ |
26 |
return new String[]{"CLDC1.1", "CLDC1.0"}; |
27 |
} |
28 |
} |
29 |
|
30 |
private Path classpath; |
31 |
private File directory; |
32 |
private boolean cldc1_0; |
33 |
private Target target; |
34 |
private boolean nofinalize; |
35 |
private boolean nonative; |
36 |
private boolean nofp; |
37 |
private ArrayList<String> classnames; |
38 |
private Path dirnames; |
39 |
|
40 |
public void setClasspath(Path classpath) |
41 |
{ |
42 |
if (this.classpath == null) |
43 |
this.classpath = classpath; |
44 |
else |
45 |
this.classpath.add(classpath); |
46 |
} |
47 |
|
48 |
public void setClasspathRef(Reference classpathRef) |
49 |
{ |
50 |
this.setClasspath((Path)classpathRef.getReferencedObject(this.getProject())); |
51 |
} |
52 |
|
53 |
public void setDirectory(File directory) |
54 |
{ |
55 |
this.directory = directory; |
56 |
} |
57 |
|
58 |
public void setCldc1_0(boolean cldc1_0) |
59 |
{ |
60 |
this.cldc1_0 = cldc1_0; |
61 |
} |
62 |
|
63 |
public void setCldc(boolean cldc) |
64 |
{ |
65 |
this.setCldc1_0(cldc); |
66 |
} |
67 |
|
68 |
public void setTarget(Target target) |
69 |
{ |
70 |
this.target = target; |
71 |
} |
72 |
|
73 |
public void setNofinalize(boolean nofinalize) |
74 |
{ |
75 |
this.nofinalize = nofinalize; |
76 |
} |
77 |
|
78 |
public void setNonative(boolean nonative) |
79 |
{ |
80 |
this.nonative = nonative; |
81 |
} |
82 |
|
83 |
public void setNofp(boolean nofp) |
84 |
{ |
85 |
this.nofp = nofp; |
86 |
} |
87 |
|
88 |
public void setClassnames(String classnames) |
89 |
{ |
90 |
if (this.classnames == null) |
91 |
this.classnames = new ArrayList<String>(); |
92 |
|
93 |
for (String classname: classnames.split("[ ,]")) |
94 |
this.classnames.add(classname); |
95 |
} |
96 |
|
97 |
public void setDirnames(Path dirnames) |
98 |
{ |
99 |
if (this.dirnames == null) |
100 |
this.dirnames = dirnames; |
101 |
else |
102 |
this.dirnames.add(dirnames); |
103 |
} |
104 |
|
105 |
public void setDirnamesRef(Reference dirnamesRef) |
106 |
{ |
107 |
this.setDirnames((Path)dirnamesRef.getReferencedObject(this.getProject())); |
108 |
} |
109 |
|
110 |
public void addClasspath(Path classpath) |
111 |
{ |
112 |
this.setClasspath(classpath); |
113 |
} |
114 |
|
115 |
public void addDirnames(Path dirnames) |
116 |
{ |
117 |
this.setDirnames(dirnames); |
118 |
} |
119 |
|
120 |
public void execute() |
121 |
{ |
122 |
if (classnames == null && dirnames == null) |
123 |
throw new BuildException("no classnames or dirnames"); |
124 |
|
125 |
if (this.isUpToDate()) |
126 |
return; |
127 |
|
128 |
ExecTask exec = (ExecTask)this.getProject().createTask("exec"); |
129 |
|
130 |
exec.setTaskName(this.getTaskName()); |
131 |
exec.setExecutable("preverify"); |
132 |
exec.setDir(new File(System.getProperty("java.io.tmpdir"))); |
133 |
exec.setFailonerror(true); |
134 |
|
135 |
if (this.classpath != null) |
136 |
{ |
137 |
exec.createArg().setValue("-classpath"); |
138 |
exec.createArg().setPath(this.classpath); |
139 |
} |
140 |
|
141 |
if (this.directory != null) |
142 |
{ |
143 |
exec.createArg().setValue("-d"); |
144 |
exec.createArg().setFile(this.directory); |
145 |
} |
146 |
|
147 |
if (this.cldc1_0) |
148 |
exec.createArg().setValue("-cldc1.0"); |
149 |
|
150 |
if (this.target != null) |
151 |
exec.createArg().setLine("-target " + this.target); |
152 |
|
153 |
if (this.nofinalize) |
154 |
exec.createArg().setValue("-nofinalize"); |
155 |
|
156 |
if (this.nonative) |
157 |
exec.createArg().setValue("-nonative"); |
158 |
|
159 |
if (this.nofp) |
160 |
exec.createArg().setValue("-nofp"); |
161 |
|
162 |
ArrayList<String> files = new ArrayList<String>(); |
163 |
|
164 |
if (this.classnames != null) |
165 |
for (String classname: this.classnames) |
166 |
{ |
167 |
this.log("Preverifying " + classname); |
168 |
exec.createArg().setValue(classname); |
169 |
} |
170 |
|
171 |
if (this.dirnames != null) |
172 |
for (String dirname: this.dirnames.list()) |
173 |
{ |
174 |
this.log("Preverifying " + dirname); |
175 |
exec.createArg().setValue(dirname); |
176 |
} |
177 |
|
178 |
exec.execute(); |
179 |
} |
180 |
|
181 |
// XXX: this thing only really checks jar files right now |
182 |
private boolean isUpToDate() |
183 |
{ |
184 |
FileUtils utils = FileUtils.getFileUtils(); |
185 |
|
186 |
if (this.classnames != null) |
187 |
return false; |
188 |
|
189 |
if (this.dirnames != null) |
190 |
for (String dirname: this.dirnames.list()) |
191 |
{ |
192 |
File file = new File(dirname); |
193 |
|
194 |
if (file.isDirectory()) |
195 |
return false; |
196 |
|
197 |
if (!utils.isUpToDate(file, new File(this.directory != null ? this.directory : new File("output"), file.getName()))) |
198 |
return false; |
199 |
} |
200 |
|
201 |
return true; |
202 |
} |
203 |
} |