001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs.optional.clearcase;
020:
021: import org.apache.tools.ant.BuildException;
022: import org.apache.tools.ant.Project;
023: import org.apache.tools.ant.taskdefs.Execute;
024: import org.apache.tools.ant.types.Commandline;
025:
026: /**
027: * Task to perform mklabel command to ClearCase.
028: * <p>
029: * The following attributes are interpreted:
030: * <table border="1">
031: * <tr>
032: * <th>Attribute</th>
033: * <th>Values</th>
034: * <th>Required</th>
035: * </tr>
036: * <tr>
037: * <td>viewpath</td>
038: * <td>Path to the ClearCase view file or directory that the command will operate on</td>
039: * <td>No</td>
040: * <tr>
041: * <tr>
042: * <td>replace</td>
043: * <td>Replace a label of the same type on the same branch</td>
044: * <td>No</td>
045: * <tr>
046: * <tr>
047: * <td>recurse</td>
048: * <td>Process each subdirectory under viewpath</td>
049: * <td>No</td>
050: * <tr>
051: * <tr>
052: * <td>version</td>
053: * <td>Identify a specific version to attach the label to</td>
054: * <td>No</td>
055: * <tr>
056: * <tr>
057: * <td>typename</td>
058: * <td>Name of the label type</td>
059: * <td>Yes</td>
060: * <tr>
061: * <tr>
062: * <td>vob</td>
063: * <td>Name of the VOB</td>
064: * <td>No</td>
065: * <tr>
066: * <tr>
067: * <td>comment</td>
068: * <td>Specify a comment. Only one of comment or cfile may be used.</td>
069: * <td>No</td>
070: * <tr>
071: * <tr>
072: * <td>commentfile</td>
073: * <td>Specify a file containing a comment. Only one of comment or cfile may be used.</td>
074: * <td>No</td>
075: * <tr>
076: * <tr>
077: * <td>failonerr</td>
078: * <td>Throw an exception if the command fails. Default is true</td>
079: * <td>No</td>
080: * <tr>
081: * </table>
082: *
083: */
084: public class CCMklabel extends ClearCase {
085: private boolean mReplace = false;
086: private boolean mRecurse = false;
087: private String mVersion = null;
088: private String mTypeName = null;
089: private String mVOB = null;
090: private String mComment = null;
091: private String mCfile = null;
092:
093: /**
094: * Executes the task.
095: * <p>
096: * Builds a command line to execute cleartool and then calls Exec's run method
097: * to execute the command line.
098: * @throws BuildException if the command fails and failonerr is set to true
099: */
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(
108: "Required attribute TypeName not specified");
109: }
110:
111: // Default the viewpath to basedir if it is not specified
112: if (getViewPath() == null) {
113: setViewPath(aProj.getBaseDir().getPath());
114: }
115:
116: // build the command line from what we got. the format is
117: // cleartool mklabel [options...] [viewpath ...]
118: // as specified in the CLEARTOOL help
119: commandLine.setExecutable(getClearToolCommand());
120: commandLine.createArgument().setValue(COMMAND_MKLABEL);
121:
122: checkOptions(commandLine);
123:
124: if (!getFailOnErr()) {
125: getProject().log(
126: "Ignoring any errors that occur for: "
127: + getViewPathBasename(),
128: Project.MSG_VERBOSE);
129: }
130: result = run(commandLine);
131: if (Execute.isFailure(result) && getFailOnErr()) {
132: String msg = "Failed executing: " + commandLine.toString();
133: throw new BuildException(msg, getLocation());
134: }
135: }
136:
137: /**
138: * Check the command line options.
139: */
140: private void checkOptions(Commandline cmd) {
141: if (getReplace()) {
142: // -replace
143: cmd.createArgument().setValue(FLAG_REPLACE);
144: }
145:
146: if (getRecurse()) {
147: // -recurse
148: cmd.createArgument().setValue(FLAG_RECURSE);
149: }
150:
151: if (getVersion() != null) {
152: // -version
153: getVersionCommand(cmd);
154: }
155:
156: if (getComment() != null) {
157: // -c
158: getCommentCommand(cmd);
159: } else {
160: if (getCommentFile() != null) {
161: // -cfile
162: getCommentFileCommand(cmd);
163: } else {
164: cmd.createArgument().setValue(FLAG_NOCOMMENT);
165: }
166: }
167:
168: if (getTypeName() != null) {
169: // type
170: getTypeCommand(cmd);
171: }
172:
173: // viewpath
174: cmd.createArgument().setValue(getViewPath());
175: }
176:
177: /**
178: * Set the replace flag
179: *
180: * @param replace the status to set the flag to
181: */
182: public void setReplace(boolean replace) {
183: mReplace = replace;
184: }
185:
186: /**
187: * Get replace flag status
188: *
189: * @return boolean containing status of replace flag
190: */
191: public boolean getReplace() {
192: return mReplace;
193: }
194:
195: /**
196: * Set recurse flag
197: *
198: * @param recurse the status to set the flag to
199: */
200: public void setRecurse(boolean recurse) {
201: mRecurse = recurse;
202: }
203:
204: /**
205: * Get recurse flag status
206: *
207: * @return boolean containing status of recurse flag
208: */
209: public boolean getRecurse() {
210: return mRecurse;
211: }
212:
213: /**
214: * Set the version flag
215: *
216: * @param version the status to set the flag to
217: */
218: public void setVersion(String version) {
219: mVersion = version;
220: }
221:
222: /**
223: * Get version flag status
224: *
225: * @return boolean containing status of version flag
226: */
227: public String getVersion() {
228: return mVersion;
229: }
230:
231: /**
232: * Set comment string
233: *
234: * @param comment the comment string
235: */
236: public void setComment(String comment) {
237: mComment = comment;
238: }
239:
240: /**
241: * Get comment string
242: *
243: * @return String containing the comment
244: */
245: public String getComment() {
246: return mComment;
247: }
248:
249: /**
250: * Set comment file
251: *
252: * @param cfile the path to the comment file
253: */
254: public void setCommentFile(String cfile) {
255: mCfile = cfile;
256: }
257:
258: /**
259: * Get comment file
260: *
261: * @return String containing the path to the comment file
262: */
263: public String getCommentFile() {
264: return mCfile;
265: }
266:
267: /**
268: * Set the type-name
269: *
270: * @param tn the type name
271: */
272: public void setTypeName(String tn) {
273: mTypeName = tn;
274: }
275:
276: /**
277: * Get type-name
278: *
279: * @return String containing type name
280: */
281: public String getTypeName() {
282: return mTypeName;
283: }
284:
285: /**
286: * Set the VOB name
287: *
288: * @param vob the VOB name
289: */
290: public void setVOB(String vob) {
291: mVOB = vob;
292: }
293:
294: /**
295: * Get VOB name
296: *
297: * @return String containing VOB name
298: */
299: public String getVOB() {
300: return mVOB;
301: }
302:
303: /**
304: * Get the 'version' command
305: *
306: * @param cmd CommandLine containing the command line string with or
307: * without the version flag and string appended
308: */
309: private void getVersionCommand(Commandline cmd) {
310: if (getVersion() != null) {
311: /* Had to make two separate commands here because if a space is
312: inserted between the flag and the value, it is treated as a
313: Windows filename with a space and it is enclosed in double
314: quotes ("). This breaks clearcase.
315: */
316: cmd.createArgument().setValue(FLAG_VERSION);
317: cmd.createArgument().setValue(getVersion());
318: }
319: }
320:
321: /**
322: * Get the 'comment' command
323: *
324: * @param cmd containing the command line string with or
325: * without the comment flag and string appended
326: */
327: private void getCommentCommand(Commandline cmd) {
328: if (getComment() != null) {
329: /* Had to make two separate commands here because if a space is
330: inserted between the flag and the value, it is treated as a
331: Windows filename with a space and it is enclosed in double
332: quotes ("). This breaks clearcase.
333: */
334: cmd.createArgument().setValue(FLAG_COMMENT);
335: cmd.createArgument().setValue(getComment());
336: }
337: }
338:
339: /**
340: * Get the 'commentfile' command
341: *
342: * @param cmd containing the command line string with or
343: * without the commentfile flag and file appended
344: */
345: private void getCommentFileCommand(Commandline cmd) {
346: if (getCommentFile() != null) {
347: /* Had to make two separate commands here because if a space is
348: inserted between the flag and the value, it is treated as a
349: Windows filename with a space and it is enclosed in double
350: quotes ("). This breaks clearcase.
351: */
352: cmd.createArgument().setValue(FLAG_COMMENTFILE);
353: cmd.createArgument().setValue(getCommentFile());
354: }
355: }
356:
357: /**
358: * Get the type-name
359: *
360: * @param cmd containing the command line string with or
361: * without the type-name
362: */
363: private void getTypeCommand(Commandline cmd) {
364: String typenm = null;
365:
366: if (getTypeName() != null) {
367: typenm = getTypeName();
368: if (getVOB() != null) {
369: typenm += "@" + getVOB();
370: }
371: cmd.createArgument().setValue(typenm);
372: }
373: }
374:
375: /**
376: * -replace flag -- replace another label of the same type
377: */
378: public static final String FLAG_REPLACE = "-replace";
379: /**
380: * -recurse flag -- process all subdirectories
381: */
382: public static final String FLAG_RECURSE = "-recurse";
383: /**
384: * -version flag -- attach label to specified version
385: */
386: public static final String FLAG_VERSION = "-version";
387: /**
388: * -c flag -- comment to attach to the file
389: */
390: public static final String FLAG_COMMENT = "-c";
391: /**
392: * -cfile flag -- file containing a comment to attach to the file
393: */
394: public static final String FLAG_COMMENTFILE = "-cfile";
395: /**
396: * -nc flag -- no comment is specified
397: */
398: public static final String FLAG_NOCOMMENT = "-nc";
399:
400: }
|