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. package org.apache.tools.ant.taskdefs.optional.clearcase;
  18. import org.apache.tools.ant.BuildException;
  19. import org.apache.tools.ant.Project;
  20. import org.apache.tools.ant.taskdefs.Execute;
  21. import org.apache.tools.ant.types.Commandline;
  22. /**
  23. * Performs ClearCase mkdir.
  24. *
  25. * <p>
  26. * The following attributes are interpreted:
  27. * <table border="1">
  28. * <tr>
  29. * <th>Attribute</th>
  30. * <th>Values</th>
  31. * <th>Required</th>
  32. * </tr>
  33. * <tr>
  34. * <td>viewpath</td>
  35. * <td>Path to the ClearCase view directory that the command will operate on</td>
  36. * <td>Yes</td>
  37. * <tr>
  38. * <tr>
  39. * <td>comment</td>
  40. * <td>Specify a comment. Only one of comment or cfile may be used.</td>
  41. * <td>No</td>
  42. * <tr>
  43. * <tr>
  44. * <td>commentfile</td>
  45. * <td>Specify a file containing a comment. Only one of comment or cfile may be used.</td>
  46. * <td>No</td>
  47. * <tr>
  48. * <tr>
  49. * <td>nocheckout</td>
  50. * <td>Do not checkout after element creation</td>
  51. * <td>No</td>
  52. * <tr>
  53. * <tr>
  54. * <td>failonerr</td>
  55. * <td>Throw an exception if the command fails. Default is true</td>
  56. * <td>No</td>
  57. * <tr>
  58. * </table>
  59. *
  60. */
  61. public class CCMkdir extends ClearCase {
  62. private String mComment = null;
  63. private String mCfile = null;
  64. private boolean mNoco = false;
  65. /**
  66. * Executes the task.
  67. * <p>
  68. * Builds a command line to execute cleartool and then calls Exec's run method
  69. * to execute the command line.
  70. * @throws BuildException if the command fails and failonerr is set to true
  71. */
  72. public void execute() throws BuildException {
  73. Commandline commandLine = new Commandline();
  74. Project aProj = getProject();
  75. int result = 0;
  76. // Default the viewpath to basedir if it is not specified
  77. if (getViewPath() == null) {
  78. setViewPath(aProj.getBaseDir().getPath());
  79. }
  80. // build the command line from what we got. the format is
  81. // cleartool mkelem [options...] [viewpath ...]
  82. // as specified in the CLEARTOOL.EXE help
  83. commandLine.setExecutable(getClearToolCommand());
  84. commandLine.createArgument().setValue(COMMAND_MKDIR);
  85. checkOptions(commandLine);
  86. if (!getFailOnErr()) {
  87. getProject().log("Ignoring any errors that occur for: "
  88. + getViewPathBasename(), Project.MSG_VERBOSE);
  89. }
  90. result = run(commandLine);
  91. if (Execute.isFailure(result) && getFailOnErr()) {
  92. String msg = "Failed executing: " + commandLine.toString();
  93. throw new BuildException(msg, getLocation());
  94. }
  95. }
  96. /**
  97. * Check the command line options.
  98. */
  99. private void checkOptions(Commandline cmd) {
  100. if (getComment() != null) {
  101. // -c
  102. getCommentCommand(cmd);
  103. } else {
  104. if (getCommentFile() != null) {
  105. // -cfile
  106. getCommentFileCommand(cmd);
  107. } else {
  108. cmd.createArgument().setValue(FLAG_NOCOMMENT);
  109. }
  110. }
  111. if (getNoCheckout()) {
  112. // -nco
  113. cmd.createArgument().setValue(FLAG_NOCHECKOUT);
  114. }
  115. // viewpath
  116. cmd.createArgument().setValue(getViewPath());
  117. }
  118. /**
  119. * Sets the comment string.
  120. *
  121. * @param comment the comment string
  122. */
  123. public void setComment(String comment) {
  124. mComment = comment;
  125. }
  126. /**
  127. * Get comment string
  128. *
  129. * @return String containing the comment
  130. */
  131. public String getComment() {
  132. return mComment;
  133. }
  134. /**
  135. * Specifies a file containing a comment.
  136. *
  137. * @param cfile the path to the comment file
  138. */
  139. public void setCommentFile(String cfile) {
  140. mCfile = cfile;
  141. }
  142. /**
  143. * Get comment file
  144. *
  145. * @return String containing the path to the comment file
  146. */
  147. public String getCommentFile() {
  148. return mCfile;
  149. }
  150. /**
  151. * If true, do not checkout element after creation.
  152. *
  153. * @param co the status to set the flag to
  154. */
  155. public void setNoCheckout(boolean co) {
  156. mNoco = co;
  157. }
  158. /**
  159. * Get no checkout flag status
  160. *
  161. * @return boolean containing status of noco flag
  162. */
  163. public boolean getNoCheckout() {
  164. return mNoco;
  165. }
  166. /**
  167. * Get the 'comment' command
  168. *
  169. * @param cmd containing the command line string with or
  170. * without the comment flag and string appended
  171. */
  172. private void getCommentCommand(Commandline cmd) {
  173. if (getComment() != null) {
  174. /* Had to make two separate commands here because if a space is
  175. inserted between the flag and the value, it is treated as a
  176. Windows filename with a space and it is enclosed in double
  177. quotes ("). This breaks clearcase.
  178. */
  179. cmd.createArgument().setValue(FLAG_COMMENT);
  180. cmd.createArgument().setValue(getComment());
  181. }
  182. }
  183. /**
  184. * Get the 'commentfile' command
  185. *
  186. * @param cmd containing the command line string with or
  187. * without the commentfile flag and file appended
  188. */
  189. private void getCommentFileCommand(Commandline cmd) {
  190. if (getCommentFile() != null) {
  191. /* Had to make two separate commands here because if a space is
  192. inserted between the flag and the value, it is treated as a
  193. Windows filename with a space and it is enclosed in double
  194. quotes ("). This breaks clearcase.
  195. */
  196. cmd.createArgument().setValue(FLAG_COMMENTFILE);
  197. cmd.createArgument().setValue(getCommentFile());
  198. }
  199. }
  200. /**
  201. * -c flag -- comment to attach to the directory
  202. */
  203. public static final String FLAG_COMMENT = "-c";
  204. /**
  205. * -cfile flag -- file containing a comment to attach to the directory
  206. */
  207. public static final String FLAG_COMMENTFILE = "-cfile";
  208. /**
  209. * -nc flag -- no comment is specified
  210. */
  211. public static final String FLAG_NOCOMMENT = "-nc";
  212. /**
  213. * -nco flag -- do not checkout element after creation
  214. */
  215. public static final String FLAG_NOCHECKOUT = "-nco";
  216. }