1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
package nl.toolforge.karma.core.cmd.impl; |
20 |
|
|
21 |
|
import nl.toolforge.core.util.file.MyFileUtils; |
22 |
|
import nl.toolforge.karma.core.KarmaRuntimeException; |
23 |
|
import nl.toolforge.karma.core.cmd.CommandDescriptor; |
24 |
|
import nl.toolforge.karma.core.cmd.CommandException; |
25 |
|
import nl.toolforge.karma.core.cmd.DefaultCommand; |
26 |
|
import nl.toolforge.karma.core.cmd.util.AntLogger; |
27 |
|
import nl.toolforge.karma.core.cmd.util.BuildEnvironment; |
28 |
|
import nl.toolforge.karma.core.manifest.Manifest; |
29 |
|
import nl.toolforge.karma.core.manifest.ManifestException; |
30 |
|
import nl.toolforge.karma.core.module.Module; |
31 |
|
import nl.toolforge.karma.core.module.ModuleTypeException; |
32 |
|
import org.apache.commons.io.FileUtils; |
33 |
|
import org.apache.commons.logging.Log; |
34 |
|
import org.apache.commons.logging.LogFactory; |
35 |
|
import org.apache.tools.ant.Project; |
36 |
|
import org.apache.tools.ant.ProjectHelper; |
37 |
|
import org.apache.tools.ant.helper.ProjectHelperImpl; |
38 |
|
import org.apache.tools.ant.taskdefs.Delete; |
39 |
|
import org.apache.tools.ant.taskdefs.Mkdir; |
40 |
|
import org.apache.tools.ant.types.FileSet; |
41 |
|
|
42 |
|
import java.io.BufferedReader; |
43 |
|
import java.io.BufferedWriter; |
44 |
|
import java.io.File; |
45 |
|
import java.io.FileWriter; |
46 |
|
import java.io.IOException; |
47 |
|
import java.io.InputStreamReader; |
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
public abstract class AbstractBuildCommand extends DefaultCommand { |
58 |
|
|
59 |
0 |
private static final Log logger = LogFactory.getLog(AbstractBuildCommand.class); |
60 |
|
|
61 |
0 |
protected Module module = null; |
62 |
|
|
63 |
0 |
private File tempBuildFileLocation = null; |
64 |
0 |
private Project project = null; |
65 |
0 |
private BuildEnvironment env = null; |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
public AbstractBuildCommand(CommandDescriptor descriptor) { |
73 |
0 |
super(descriptor); |
74 |
0 |
} |
75 |
|
|
76 |
|
public void execute() throws CommandException { |
77 |
|
|
78 |
0 |
if (!getContext().isManclass="keyword">ifestLoaded()) { |
79 |
0 |
throw new CommandException(ManifestException.NO_ACTIVE_MANIFEST); |
80 |
|
} |
81 |
|
|
82 |
0 |
if (getCommandLine().hasOption("m")) { |
83 |
0 |
String moduleName = getCommandLine().getOptionValue("m"); |
84 |
|
|
85 |
|
try { |
86 |
|
|
87 |
|
|
88 |
0 |
module = getCurrentManifest().getModule(moduleName); |
89 |
|
|
90 |
0 |
if (module == null) { |
91 |
0 |
throw new CommandException(ManifestException.MODULE_NOT_FOUND, class="keyword">new Object[]{module}); |
92 |
|
} |
93 |
|
|
94 |
0 |
if (!getCurrentManclass="keyword">ifest().isLocal(module)) { |
95 |
0 |
throw new CommandException(ManifestException.MODULE_NOT_LOCAL, class="keyword">new Object[]{module}); |
96 |
|
} |
97 |
|
|
98 |
|
|
99 |
0 |
} catch (ManifestException m) { |
100 |
0 |
throw new CommandException(m.getErrorCode(), m.getMessageArguments()); |
101 |
0 |
} |
102 |
|
} |
103 |
|
|
104 |
|
|
105 |
|
|
106 |
0 |
env = new BuildEnvironment(getCurrentManifest(), module); |
107 |
0 |
} |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
protected final Manifest getCurrentManifest() { |
115 |
0 |
return getContext().getCurrentManifest(); |
116 |
|
} |
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
protected Module getCurrentModule() { |
124 |
|
|
125 |
0 |
if (module == null) { |
126 |
0 |
throw new KarmaRuntimeException("Module is null. Execute method has not been called by subclass."); |
127 |
0 |
} |
128 |
0 |
return module; |
129 |
0 |
} |
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
protected BuildEnvironment getBuildEnvironment() { |
135 |
0 |
return env; |
136 |
|
} |
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
protected final File getCompileDirectory() throws ModuleTypeException { |
144 |
|
|
145 |
0 |
if (module == null) { |
146 |
0 |
throw new IllegalArgumentException("Module cannot be null."); |
147 |
|
} |
148 |
|
|
149 |
0 |
File base = env.getModuleBuildRootDirectory(); |
150 |
|
|
151 |
0 |
if (module.getType().equals(Module.JAVA_WEB_APPLICATION)) { |
152 |
0 |
return new File(base, "build/WEB-INF/classes"); |
153 |
|
} else { |
154 |
0 |
return new File(base, "build"); |
155 |
|
} |
156 |
|
} |
157 |
|
|
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
protected Project getAntProject(String buildFile) throws CommandException { |
166 |
|
|
167 |
0 |
ProjectHelper helper = new ProjectHelperImpl(); |
168 |
|
try { |
169 |
0 |
File tmp = getBuildFile(buildFile); |
170 |
0 |
helper.parse(getProjectInstance(), tmp); |
171 |
0 |
setBuildFileLocation(tmp); |
172 |
0 |
} catch (IOException e) { |
173 |
0 |
throw new CommandException(e, CommandException.BUILD_FAILED, class="keyword">new Object[] {getCurrentModule().getName()}); |
174 |
0 |
} |
175 |
|
|
176 |
0 |
return project; |
177 |
|
} |
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
protected Project getProjectInstance() throws CommandException { |
187 |
|
|
188 |
0 |
if (project == null) { |
189 |
|
|
190 |
|
|
191 |
0 |
AntLogger logger = new AntLogger(this); |
192 |
|
|
193 |
|
|
194 |
|
|
195 |
0 |
logger.setOutputPrintStream(System.out); |
196 |
|
|
197 |
|
|
198 |
0 |
String logLevel = System.getProperty("antloglevel"); |
199 |
0 |
int antLogLevel = Project.MSG_WARN; |
200 |
0 |
if (logLevel.equalsIgnoreCase("debug")) { |
201 |
0 |
antLogLevel = Project.MSG_DEBUG; |
202 |
0 |
} else if (logLevel.equalsIgnoreCase("info")) { |
203 |
0 |
antLogLevel = Project.MSG_INFO; |
204 |
|
} |
205 |
0 |
logger.setMessageOutputLevel(antLogLevel); |
206 |
|
|
207 |
|
|
208 |
|
|
209 |
0 |
project = new Project(); |
210 |
0 |
project.addBuildListener(logger); |
211 |
0 |
project.init(); |
212 |
|
} |
213 |
|
|
214 |
0 |
return project; |
215 |
|
} |
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
public void executeMkdir(File dir) throws CommandException { |
221 |
|
|
222 |
0 |
if (project == null) { |
223 |
0 |
project = getProjectInstance(); |
224 |
|
} |
225 |
|
|
226 |
0 |
Mkdir mkdir = new Mkdir(); |
227 |
0 |
mkdir.setProject(project); |
228 |
0 |
mkdir.setDir(dir); |
229 |
0 |
mkdir.execute(); |
230 |
0 |
} |
231 |
|
|
232 |
|
|
233 |
|
|
234 |
|
|
235 |
|
public void executeDelete(File dir, String includes) throws CommandException { |
236 |
|
|
237 |
0 |
if (project == null) { |
238 |
0 |
project = getProjectInstance(); |
239 |
|
} |
240 |
|
|
241 |
0 |
if (dir != null && dir.exists()) { |
242 |
|
|
243 |
|
|
244 |
|
|
245 |
0 |
Delete delete = new Delete(); |
246 |
0 |
delete.setProject(project); |
247 |
|
|
248 |
0 |
FileSet fileset = new FileSet(); |
249 |
0 |
fileset.setDir(dir); |
250 |
0 |
fileset.setIncludes(includes); |
251 |
|
|
252 |
0 |
delete.addFileset(fileset); |
253 |
0 |
delete.execute(); |
254 |
|
} |
255 |
0 |
} |
256 |
|
|
257 |
|
public void executeDelete(File dir) throws CommandException { |
258 |
|
|
259 |
0 |
if (project == null) { |
260 |
0 |
project = getProjectInstance(); |
261 |
|
} |
262 |
|
|
263 |
|
try { |
264 |
|
|
265 |
|
|
266 |
|
|
267 |
0 |
Delete delete = new Delete(); |
268 |
0 |
delete.setProject(project); |
269 |
|
|
270 |
0 |
if (dir.equals(new File("."))) { |
271 |
0 |
throw new KarmaRuntimeException("We don't do that stuff here ..."); |
272 |
|
} |
273 |
|
|
274 |
0 |
delete.setDir(dir); |
275 |
0 |
delete.execute(); |
276 |
0 |
} catch (RuntimeException r) { |
277 |
0 |
throw new CommandException(CommandException.BUILD_FAILED, class="keyword">new Object[]{r.getMessage()}); |
278 |
0 |
} |
279 |
0 |
} |
280 |
|
|
281 |
|
private final File getBuildFile(String buildFile) throws IOException { |
282 |
|
|
283 |
0 |
File tmp = null; |
284 |
|
|
285 |
0 |
tmp = MyFileUtils.createTempDirectory(); |
286 |
|
|
287 |
0 |
ClassLoader loader = this.getClass().getClassLoader(); |
288 |
|
|
289 |
0 |
BufferedReader in = |
290 |
|
new BufferedReader(class="keyword">new InputStreamReader(loader.getResourceAsStream("ant/" + buildFile))); |
291 |
0 |
BufferedWriter out = |
292 |
|
new BufferedWriter(class="keyword">new FileWriter(class="keyword">new File(tmp, buildFile))); |
293 |
|
|
294 |
|
String str; |
295 |
0 |
while ((str = in.readLine()) != null) { |
296 |
0 |
out.write(str); |
297 |
|
} |
298 |
0 |
out.close(); |
299 |
0 |
in.close(); |
300 |
|
|
301 |
|
|
302 |
|
|
303 |
0 |
return new File(tmp, buildFile); |
304 |
|
} |
305 |
|
|
306 |
|
private void setBuildFileLocation(File tmpBuildFileLocation) { |
307 |
0 |
this.tempBuildFileLocation = tmpBuildFileLocation; |
308 |
0 |
} |
309 |
|
|
310 |
|
|
311 |
|
|
312 |
|
|
313 |
|
public final void cleanUp() { |
314 |
|
|
315 |
|
try { |
316 |
0 |
if (tempBuildFileLocation != null) { |
317 |
0 |
FileUtils.deleteDirectory(tempBuildFileLocation.getParentFile()); |
318 |
|
} |
319 |
0 |
} catch (IOException e) { |
320 |
0 |
logger.warn("Could not remove temporary directory for Ant build file."); |
321 |
0 |
} |
322 |
0 |
} |
323 |
|
|
324 |
|
} |