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: * Performs ClearCase mkelem.
028: *
029: * <p>
030: * The following attributes are interpreted:
031: * <table border="1">
032: * <tr>
033: * <th>Attribute</th>
034: * <th>Values</th>
035: * <th>Required</th>
036: * </tr>
037: * <tr>
038: * <td>viewpath</td>
039: * <td>Path to the ClearCase view file or directory that the command will operate on</td>
040: * <td>Yes</td>
041: * <tr>
042: * <tr>
043: * <td>comment</td>
044: * <td>Specify a comment. Only one of comment or cfile may be used.</td>
045: * <td>No</td>
046: * <tr>
047: * <tr>
048: * <td>commentfile</td>
049: * <td>Specify a file containing a comment. Only one of comment or cfile may be used.</td>
050: * <td>No</td>
051: * <tr>
052: * <tr>
053: * <td>nowarn</td>
054: * <td>Suppress warning messages</td>
055: * <td>No</td>
056: * <tr>
057: * <tr>
058: * <td>nocheckout</td>
059: * <td>Do not checkout after element creation</td>
060: * <td>No</td>
061: * <tr>
062: * <tr>
063: * <td>checkin</td>
064: * <td>Checkin element after creation</td>
065: * <td>No</td>
066: * <tr>
067: * <tr>
068: * <td>preservetime</td>
069: * <td>Preserve the modification time (for checkin)</td>
070: * <td>No</td>
071: * <tr>
072: * <tr>
073: * <td>master</td>
074: * <td>Assign mastership of the main branch to the current site</td>
075: * <td>No</td>
076: * <tr>
077: * <tr>
078: * <td>eltype</td>
079: * <td>Element type to use during element creation</td>
080: * <td>No</td>
081: * <tr>
082: * <tr>
083: * <td>failonerr</td>
084: * <td>Throw an exception if the command fails. Default is true</td>
085: * <td>No</td>
086: * <tr>
087: * </table>
088: *
089: */
090: public class CCMkelem extends ClearCase {
091: private String mComment = null;
092: private String mCfile = null;
093: private boolean mNwarn = false;
094: private boolean mPtime = false;
095: private boolean mNoco = false;
096: private boolean mCheckin = false;
097: private boolean mMaster = false;
098: private String mEltype = null;
099:
100: /**
101: * Executes the task.
102: * <p>
103: * Builds a command line to execute cleartool and then calls Exec's run method
104: * to execute the command line.
105: * @throws BuildException if the command fails and failonerr is set to true
106: */
107: public void execute() throws BuildException {
108: Commandline commandLine = new Commandline();
109: Project aProj = getProject();
110: int result = 0;
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 mkelem [options...] [viewpath ...]
119: // as specified in the CLEARTOOL.EXE help
120: commandLine.setExecutable(getClearToolCommand());
121: commandLine.createArgument().setValue(COMMAND_MKELEM);
122:
123: checkOptions(commandLine);
124:
125: if (!getFailOnErr()) {
126: getProject().log(
127: "Ignoring any errors that occur for: "
128: + getViewPathBasename(),
129: Project.MSG_VERBOSE);
130: }
131: result = run(commandLine);
132: if (Execute.isFailure(result) && getFailOnErr()) {
133: String msg = "Failed executing: " + commandLine.toString();
134: throw new BuildException(msg, getLocation());
135: }
136: }
137:
138: /**
139: * Check the command line options.
140: */
141: private void checkOptions(Commandline cmd) {
142: if (getComment() != null) {
143: // -c
144: getCommentCommand(cmd);
145: } else {
146: if (getCommentFile() != null) {
147: // -cfile
148: getCommentFileCommand(cmd);
149: } else {
150: cmd.createArgument().setValue(FLAG_NOCOMMENT);
151: }
152: }
153:
154: if (getNoWarn()) {
155: // -nwarn
156: cmd.createArgument().setValue(FLAG_NOWARN);
157: }
158: /*
159: * Should choose either -ci or -nco.
160: */
161: if (getNoCheckout() && getCheckin()) {
162: throw new BuildException(
163: "Should choose either [nocheckout | checkin]");
164: }
165: if (getNoCheckout()) {
166: // -nco
167: cmd.createArgument().setValue(FLAG_NOCHECKOUT);
168: }
169: if (getCheckin()) {
170: // -ci
171: cmd.createArgument().setValue(FLAG_CHECKIN);
172: if (getPreserveTime()) {
173: // -ptime
174: cmd.createArgument().setValue(FLAG_PRESERVETIME);
175: }
176: }
177: if (getMaster()) {
178: // -master
179: cmd.createArgument().setValue(FLAG_MASTER);
180: }
181: if (getEltype() != null) {
182: // -eltype
183: getEltypeCommand(cmd);
184: }
185: // viewpath
186: cmd.createArgument().setValue(getViewPath());
187: }
188:
189: /**
190: * Sets the comment string.
191: *
192: * @param comment the comment string
193: */
194: public void setComment(String comment) {
195: mComment = comment;
196: }
197:
198: /**
199: * Get comment string
200: *
201: * @return String containing the comment
202: */
203: public String getComment() {
204: return mComment;
205: }
206:
207: /**
208: * Specifies a file containing a comment.
209: *
210: * @param cfile the path to the comment file
211: */
212: public void setCommentFile(String cfile) {
213: mCfile = cfile;
214: }
215:
216: /**
217: * Get comment file
218: *
219: * @return String containing the path to the comment file
220: */
221: public String getCommentFile() {
222: return mCfile;
223: }
224:
225: /**
226: * If true, suppress warning messages.
227: *
228: * @param nwarn the status to set the flag to
229: */
230: public void setNoWarn(boolean nwarn) {
231: mNwarn = nwarn;
232: }
233:
234: /**
235: * Get nowarn flag status
236: *
237: * @return boolean containing status of nwarn flag
238: */
239: public boolean getNoWarn() {
240: return mNwarn;
241: }
242:
243: /**
244: * If true, preserve the modification time.
245: *
246: * @param ptime the status to set the flag to
247: */
248: public void setPreserveTime(boolean ptime) {
249: mPtime = ptime;
250: }
251:
252: /**
253: * Get preservetime flag status
254: *
255: * @return boolean containing status of preservetime flag
256: */
257: public boolean getPreserveTime() {
258: return mPtime;
259: }
260:
261: /**
262: * If true, do not checkout element after creation.
263: *
264: * @param co the status to set the flag to
265: */
266: public void setNoCheckout(boolean co) {
267: mNoco = co;
268: }
269:
270: /**
271: * Get no checkout flag status
272: *
273: * @return boolean containing status of noco flag
274: */
275: public boolean getNoCheckout() {
276: return mNoco;
277: }
278:
279: /**
280: * If true, checkin the element after creation
281: *
282: * @param ci the status to set the flag to
283: */
284: public void setCheckin(boolean ci) {
285: mCheckin = ci;
286: }
287:
288: /**
289: * Get ci flag status
290: *
291: * @return boolean containing status of ci flag
292: */
293: public boolean getCheckin() {
294: return mCheckin;
295: }
296:
297: /**
298: * If true, changes mastership of the main branch
299: * to the current site
300: *
301: * @param master the status to set the flag to
302: */
303: public void setMaster(boolean master) {
304: mMaster = master;
305: }
306:
307: /**
308: * Get master flag status
309: *
310: * @return boolean containing status of master flag
311: */
312: public boolean getMaster() {
313: return mMaster;
314: }
315:
316: /**
317: * Specifies the element type to use.
318: *
319: * @param eltype to create element
320: */
321: public void setEltype(String eltype) {
322: mEltype = eltype;
323: }
324:
325: /**
326: * Get element type
327: *
328: * @return String containing the element type
329: */
330: public String getEltype() {
331: return mEltype;
332: }
333:
334: /**
335: * Get the 'comment' command
336: *
337: * @param cmd containing the command line string with or
338: * without the comment flag and string appended
339: */
340: private void getCommentCommand(Commandline cmd) {
341: if (getComment() != null) {
342: /* Had to make two separate commands here because if a space is
343: inserted between the flag and the value, it is treated as a
344: Windows filename with a space and it is enclosed in double
345: quotes ("). This breaks clearcase.
346: */
347: cmd.createArgument().setValue(FLAG_COMMENT);
348: cmd.createArgument().setValue(getComment());
349: }
350: }
351:
352: /**
353: * Get the 'commentfile' command
354: *
355: * @param cmd containing the command line string with or
356: * without the commentfile flag and file appended
357: */
358: private void getCommentFileCommand(Commandline cmd) {
359: if (getCommentFile() != null) {
360: /* Had to make two separate commands here because if a space is
361: inserted between the flag and the value, it is treated as a
362: Windows filename with a space and it is enclosed in double
363: quotes ("). This breaks clearcase.
364: */
365: cmd.createArgument().setValue(FLAG_COMMENTFILE);
366: cmd.createArgument().setValue(getCommentFile());
367: }
368: }
369:
370: /**
371: * Get the 'element type' command
372: *
373: * @param cmd containing the command line string with or
374: * without the comment flag and string appended
375: */
376: private void getEltypeCommand(Commandline cmd) {
377: if (getEltype() != null) {
378: /* Had to make two separate commands here because if a space is
379: inserted between the flag and the value, it is treated as a
380: Windows filename with a space and it is enclosed in double
381: quotes ("). This breaks clearcase.
382: */
383: cmd.createArgument().setValue(FLAG_ELTYPE);
384: cmd.createArgument().setValue(getEltype());
385: }
386: }
387:
388: /**
389: * -c flag -- comment to attach to the file
390: */
391: public static final String FLAG_COMMENT = "-c";
392: /**
393: * -cfile flag -- file containing a comment to attach to the file
394: */
395: public static final String FLAG_COMMENTFILE = "-cfile";
396: /**
397: * -nc flag -- no comment is specified
398: */
399: public static final String FLAG_NOCOMMENT = "-nc";
400: /**
401: * -nwarn flag -- suppresses warning messages
402: */
403: public static final String FLAG_NOWARN = "-nwarn";
404: /**
405: * -ptime flag -- preserves the modification time on checkin
406: */
407: public static final String FLAG_PRESERVETIME = "-ptime";
408: /**
409: * -nco flag -- do not checkout element after creation
410: */
411: public static final String FLAG_NOCHECKOUT = "-nco";
412: /**
413: * -ci flag -- checkin element after creation
414: */
415: public static final String FLAG_CHECKIN = "-ci";
416: /**
417: * -master flag -- change mastership of main branch to current site
418: */
419: public static final String FLAG_MASTER = "-master";
420: /**
421: * -eltype flag -- element type to use during creation
422: */
423: public static final String FLAG_ELTYPE = "-eltype";
424: }
|