1 | /*
|
---|
2 | * Copyright 2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.taskdefs.optional.clearcase;
|
---|
19 |
|
---|
20 | import org.apache.tools.ant.BuildException;
|
---|
21 | import org.apache.tools.ant.Project;
|
---|
22 | import org.apache.tools.ant.taskdefs.Execute;
|
---|
23 | import org.apache.tools.ant.types.Commandline;
|
---|
24 | import org.apache.tools.ant.taskdefs.condition.Os;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Task to perform mkattr command to ClearCase.
|
---|
28 | * <p>
|
---|
29 | * The following attributes are interpreted:
|
---|
30 | * <table border="1">
|
---|
31 | * <tr>
|
---|
32 | * <th>Attribute</th>
|
---|
33 | * <th>Values</th>
|
---|
34 | * <th>Required</th>
|
---|
35 | * </tr>
|
---|
36 | * <tr>
|
---|
37 | * <td>viewpath</td>
|
---|
38 | * <td>Path to the ClearCase view file or directory that the command will operate on</td>
|
---|
39 | * <td>Yes</td>
|
---|
40 | * <tr>
|
---|
41 | * <tr>
|
---|
42 | * <td>replace</td>
|
---|
43 | * <td>Replace the value of the attribute if it already exists</td>
|
---|
44 | * <td>No</td>
|
---|
45 | * <tr>
|
---|
46 | * <tr>
|
---|
47 | * <td>recurse</td>
|
---|
48 | * <td>Process each subdirectory under viewpath</td>
|
---|
49 | * <td>No</td>
|
---|
50 | * <tr>
|
---|
51 | * <tr>
|
---|
52 | * <td>version</td>
|
---|
53 | * <td>Identify a specific version to attach the attribute to</td>
|
---|
54 | * <td>No</td>
|
---|
55 | * <tr>
|
---|
56 | * <tr>
|
---|
57 | * <td>typename</td>
|
---|
58 | * <td>Name of the attribute type</td>
|
---|
59 | * <td>Yes</td>
|
---|
60 | * <tr>
|
---|
61 | * <tr>
|
---|
62 | * <td>typevalue</td>
|
---|
63 | * <td>Value to attach to the attribute type</td>
|
---|
64 | * <td>Yes</td>
|
---|
65 | * <tr>
|
---|
66 | * <tr>
|
---|
67 | * <td>comment</td>
|
---|
68 | * <td>Specify a comment. Only one of comment or cfile may be used.</td>
|
---|
69 | * <td>No</td>
|
---|
70 | * <tr>
|
---|
71 | * <tr>
|
---|
72 | * <td>commentfile</td>
|
---|
73 | * <td>Specify a file containing a comment. Only one of comment or cfile may be used.</td>
|
---|
74 | * <td>No</td>
|
---|
75 | * <tr>
|
---|
76 | * <tr>
|
---|
77 | * <td>failonerr</td>
|
---|
78 | * <td>Throw an exception if the command fails. Default is true</td>
|
---|
79 | * <td>No</td>
|
---|
80 | * <tr>
|
---|
81 | * </table>
|
---|
82 | *
|
---|
83 | */
|
---|
84 | public class CCMkattr extends ClearCase {
|
---|
85 | private boolean mReplace = false;
|
---|
86 | private boolean mRecurse = false;
|
---|
87 | private String mVersion = null;
|
---|
88 | private String mTypeName = null;
|
---|
89 | private String mTypeValue = null;
|
---|
90 | private String mComment = null;
|
---|
91 | private String mCfile = null;
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Executes the task.
|
---|
95 | * <p>
|
---|
96 | * Builds a command line to execute cleartool and then calls Exec's run method
|
---|
97 | * to execute the command line.
|
---|
98 | * @throws BuildException if the command fails and failonerr is set to true
|
---|
99 | */
|
---|
100 | public void execute() throws BuildException {
|
---|
101 | Commandline commandLine = new Commandline();
|
---|
102 | Project aProj = getProject();
|
---|
103 | int result = 0;
|
---|
104 |
|
---|
105 | // Check for required attributes
|
---|
106 | if (getTypeName() == null) {
|
---|
107 | throw new BuildException("Required attribute TypeName not specified");
|
---|
108 | }
|
---|
109 | if (getTypeValue() == null) {
|
---|
110 | throw new BuildException("Required attribute TypeValue not specified");
|
---|
111 | }
|
---|
112 | // Default the viewpath to basedir if it is not specified
|
---|
113 | if (getViewPath() == null) {
|
---|
114 | setViewPath(aProj.getBaseDir().getPath());
|
---|
115 | }
|
---|
116 |
|
---|
117 | // build the command line from what we got. the format is
|
---|
118 | // cleartool mkattr [options...] [viewpath ...]
|
---|
119 | // as specified in the CLEARTOOL help
|
---|
120 | commandLine.setExecutable(getClearToolCommand());
|
---|
121 | commandLine.createArgument().setValue(COMMAND_MKATTR);
|
---|
122 |
|
---|
123 | checkOptions(commandLine);
|
---|
124 |
|
---|
125 | if (!getFailOnErr()) {
|
---|
126 | getProject().log("Ignoring any errors that occur for: "
|
---|
127 | + getViewPathBasename(), Project.MSG_VERBOSE);
|
---|
128 | }
|
---|
129 |
|
---|
130 | // For debugging
|
---|
131 | // System.out.println(commandLine.toString());
|
---|
132 |
|
---|
133 | result = run(commandLine);
|
---|
134 | if (Execute.isFailure(result) && getFailOnErr()) {
|
---|
135 | String msg = "Failed executing: " + commandLine.toString();
|
---|
136 | throw new BuildException(msg, getLocation());
|
---|
137 | }
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Check the command line options.
|
---|
143 | */
|
---|
144 | private void checkOptions(Commandline cmd) {
|
---|
145 | if (getReplace()) {
|
---|
146 | // -replace
|
---|
147 | cmd.createArgument().setValue(FLAG_REPLACE);
|
---|
148 | }
|
---|
149 |
|
---|
150 | if (getRecurse()) {
|
---|
151 | // -recurse
|
---|
152 | cmd.createArgument().setValue(FLAG_RECURSE);
|
---|
153 | }
|
---|
154 |
|
---|
155 | if (getVersion() != null) {
|
---|
156 | // -version
|
---|
157 | getVersionCommand(cmd);
|
---|
158 | }
|
---|
159 |
|
---|
160 | if (getComment() != null) {
|
---|
161 | // -c
|
---|
162 | getCommentCommand(cmd);
|
---|
163 | } else {
|
---|
164 | if (getCommentFile() != null) {
|
---|
165 | // -cfile
|
---|
166 | getCommentFileCommand(cmd);
|
---|
167 | } else {
|
---|
168 | cmd.createArgument().setValue(FLAG_NOCOMMENT);
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | if (getTypeName() != null) {
|
---|
173 | // type
|
---|
174 | getTypeCommand(cmd);
|
---|
175 | }
|
---|
176 | if (getTypeValue() != null) {
|
---|
177 | // type value
|
---|
178 | getTypeValueCommand(cmd);
|
---|
179 | }
|
---|
180 | // viewpath
|
---|
181 | cmd.createArgument().setValue(getViewPath());
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Set the replace flag
|
---|
187 | *
|
---|
188 | * @param replace the status to set the flag to
|
---|
189 | */
|
---|
190 | public void setReplace(boolean replace) {
|
---|
191 | mReplace = replace;
|
---|
192 | }
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * Get replace flag status
|
---|
196 | *
|
---|
197 | * @return boolean containing status of replace flag
|
---|
198 | */
|
---|
199 | public boolean getReplace() {
|
---|
200 | return mReplace;
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Set recurse flag
|
---|
205 | *
|
---|
206 | * @param recurse the status to set the flag to
|
---|
207 | */
|
---|
208 | public void setRecurse(boolean recurse) {
|
---|
209 | mRecurse = recurse;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Get recurse flag status
|
---|
214 | *
|
---|
215 | * @return boolean containing status of recurse flag
|
---|
216 | */
|
---|
217 | public boolean getRecurse() {
|
---|
218 | return mRecurse;
|
---|
219 | }
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * Set the version flag
|
---|
223 | *
|
---|
224 | * @param version the status to set the flag to
|
---|
225 | */
|
---|
226 | public void setVersion(String version) {
|
---|
227 | mVersion = version;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Get version flag status
|
---|
232 | *
|
---|
233 | * @return boolean containing status of version flag
|
---|
234 | */
|
---|
235 | public String getVersion() {
|
---|
236 | return mVersion;
|
---|
237 | }
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * Set comment string
|
---|
241 | *
|
---|
242 | * @param comment the comment string
|
---|
243 | */
|
---|
244 | public void setComment(String comment) {
|
---|
245 | mComment = comment;
|
---|
246 | }
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * Get comment string
|
---|
250 | *
|
---|
251 | * @return String containing the comment
|
---|
252 | */
|
---|
253 | public String getComment() {
|
---|
254 | return mComment;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * Set comment file
|
---|
259 | *
|
---|
260 | * @param cfile the path to the comment file
|
---|
261 | */
|
---|
262 | public void setCommentFile(String cfile) {
|
---|
263 | mCfile = cfile;
|
---|
264 | }
|
---|
265 |
|
---|
266 | /**
|
---|
267 | * Get comment file
|
---|
268 | *
|
---|
269 | * @return String containing the path to the comment file
|
---|
270 | */
|
---|
271 | public String getCommentFile() {
|
---|
272 | return mCfile;
|
---|
273 | }
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Set the attribute type-name
|
---|
277 | *
|
---|
278 | * @param tn the type name
|
---|
279 | */
|
---|
280 | public void setTypeName(String tn) {
|
---|
281 | mTypeName = tn;
|
---|
282 | }
|
---|
283 |
|
---|
284 | /**
|
---|
285 | * Get attribute type-name
|
---|
286 | *
|
---|
287 | * @return String containing type name
|
---|
288 | */
|
---|
289 | public String getTypeName() {
|
---|
290 | return mTypeName;
|
---|
291 | }
|
---|
292 |
|
---|
293 | /**
|
---|
294 | * Set the attribute type-value
|
---|
295 | *
|
---|
296 | * @param tv the type value
|
---|
297 | */
|
---|
298 | public void setTypeValue(String tv) {
|
---|
299 | mTypeValue = tv;
|
---|
300 | }
|
---|
301 |
|
---|
302 | /**
|
---|
303 | * Get the attribute type-value
|
---|
304 | *
|
---|
305 | * @return String containing type value
|
---|
306 | */
|
---|
307 | public String getTypeValue() {
|
---|
308 | return mTypeValue;
|
---|
309 | }
|
---|
310 |
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * Get the 'version' command
|
---|
314 | *
|
---|
315 | * @param cmd CommandLine containing the command line string with or
|
---|
316 | * without the version flag and string appended
|
---|
317 | */
|
---|
318 | private void getVersionCommand(Commandline cmd) {
|
---|
319 | if (getVersion() != null) {
|
---|
320 | /* Had to make two separate commands here because if a space is
|
---|
321 | inserted between the flag and the value, it is treated as a
|
---|
322 | Windows filename with a space and it is enclosed in double
|
---|
323 | quotes ("). This breaks clearcase.
|
---|
324 | */
|
---|
325 | cmd.createArgument().setValue(FLAG_VERSION);
|
---|
326 | cmd.createArgument().setValue(getVersion());
|
---|
327 | }
|
---|
328 | }
|
---|
329 |
|
---|
330 | /**
|
---|
331 | * Get the 'comment' command
|
---|
332 | *
|
---|
333 | * @param cmd containing the command line string with or
|
---|
334 | * without the comment flag and string appended
|
---|
335 | */
|
---|
336 | private void getCommentCommand(Commandline cmd) {
|
---|
337 | if (getComment() != null) {
|
---|
338 | /* Had to make two separate commands here because if a space is
|
---|
339 | inserted between the flag and the value, it is treated as a
|
---|
340 | Windows filename with a space and it is enclosed in double
|
---|
341 | quotes ("). This breaks clearcase.
|
---|
342 | */
|
---|
343 | cmd.createArgument().setValue(FLAG_COMMENT);
|
---|
344 | cmd.createArgument().setValue(getComment());
|
---|
345 | }
|
---|
346 | }
|
---|
347 |
|
---|
348 | /**
|
---|
349 | * Get the 'commentfile' command
|
---|
350 | *
|
---|
351 | * @param cmd containing the command line string with or
|
---|
352 | * without the commentfile flag and file appended
|
---|
353 | */
|
---|
354 | private void getCommentFileCommand(Commandline cmd) {
|
---|
355 | if (getCommentFile() != null) {
|
---|
356 | /* Had to make two separate commands here because if a space is
|
---|
357 | inserted between the flag and the value, it is treated as a
|
---|
358 | Windows filename with a space and it is enclosed in double
|
---|
359 | quotes ("). This breaks clearcase.
|
---|
360 | */
|
---|
361 | cmd.createArgument().setValue(FLAG_COMMENTFILE);
|
---|
362 | cmd.createArgument().setValue(getCommentFile());
|
---|
363 | }
|
---|
364 | }
|
---|
365 |
|
---|
366 | /**
|
---|
367 | * Get the attribute type-name
|
---|
368 | *
|
---|
369 | * @param cmd containing the command line string with or
|
---|
370 | * without the type-name
|
---|
371 | */
|
---|
372 | private void getTypeCommand(Commandline cmd) {
|
---|
373 | String typenm = getTypeName();
|
---|
374 |
|
---|
375 | if (typenm != null) {
|
---|
376 | cmd.createArgument().setValue(typenm);
|
---|
377 | }
|
---|
378 | }
|
---|
379 |
|
---|
380 | /**
|
---|
381 | * Get the attribute type-value
|
---|
382 | *
|
---|
383 | * @param cmd containing the command line string with or
|
---|
384 | * without the type-value
|
---|
385 | */
|
---|
386 | private void getTypeValueCommand(Commandline cmd) {
|
---|
387 | String typevl = getTypeValue();
|
---|
388 |
|
---|
389 | if (typevl != null) {
|
---|
390 | if (Os.isFamily("windows")) {
|
---|
391 | typevl = "\\\"" + typevl + "\\\""; // Windows quoting of the value
|
---|
392 | } else {
|
---|
393 | typevl = "\"" + typevl + "\"";
|
---|
394 | }
|
---|
395 | cmd.createArgument().setValue(typevl);
|
---|
396 | }
|
---|
397 | }
|
---|
398 |
|
---|
399 | /**
|
---|
400 | * -replace flag -- replace the existing value of the attribute
|
---|
401 | */
|
---|
402 | public static final String FLAG_REPLACE = "-replace";
|
---|
403 | /**
|
---|
404 | * -recurse flag -- process all subdirectories
|
---|
405 | */
|
---|
406 | public static final String FLAG_RECURSE = "-recurse";
|
---|
407 | /**
|
---|
408 | * -version flag -- attach attribute to specified version
|
---|
409 | */
|
---|
410 | public static final String FLAG_VERSION = "-version";
|
---|
411 | /**
|
---|
412 | * -c flag -- comment to attach to the element
|
---|
413 | */
|
---|
414 | public static final String FLAG_COMMENT = "-c";
|
---|
415 | /**
|
---|
416 | * -cfile flag -- file containing a comment to attach to the file
|
---|
417 | */
|
---|
418 | public static final String FLAG_COMMENTFILE = "-cfile";
|
---|
419 | /**
|
---|
420 | * -nc flag -- no comment is specified
|
---|
421 | */
|
---|
422 | public static final String FLAG_NOCOMMENT = "-nc";
|
---|
423 | }
|
---|
424 |
|
---|