1. /*
  2. * Copyright 2003-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. * Task to perform mklabel command to ClearCase.
  24. * <p>
  25. * The following attributes are interpreted:
  26. * <table border="1">
  27. * <tr>
  28. * <th>Attribute</th>
  29. * <th>Values</th>
  30. * <th>Required</th>
  31. * </tr>
  32. * <tr>
  33. * <td>viewpath</td>
  34. * <td>Path to the ClearCase view file or directory that the command will operate on</td>
  35. * <td>No</td>
  36. * <tr>
  37. * <tr>
  38. * <td>replace</td>
  39. * <td>Replace a label of the same type on the same branch</td>
  40. * <td>No</td>
  41. * <tr>
  42. * <tr>
  43. * <td>recurse</td>
  44. * <td>Process each subdirectory under viewpath</td>
  45. * <td>No</td>
  46. * <tr>
  47. * <tr>
  48. * <td>version</td>
  49. * <td>Identify a specific version to attach the label to</td>
  50. * <td>No</td>
  51. * <tr>
  52. * <tr>
  53. * <td>typename</td>
  54. * <td>Name of the label type</td>
  55. * <td>Yes</td>
  56. * <tr>
  57. * <tr>
  58. * <td>vob</td>
  59. * <td>Name of the VOB</td>
  60. * <td>No</td>
  61. * <tr>
  62. * <tr>
  63. * <td>comment</td>
  64. * <td>Specify a comment. Only one of comment or cfile may be used.</td>
  65. * <td>No</td>
  66. * <tr>
  67. * <tr>
  68. * <td>commentfile</td>
  69. * <td>Specify a file containing a comment. Only one of comment or cfile may be used.</td>
  70. * <td>No</td>
  71. * <tr>
  72. * <tr>
  73. * <td>failonerr</td>
  74. * <td>Throw an exception if the command fails. Default is true</td>
  75. * <td>No</td>
  76. * <tr>
  77. * </table>
  78. *
  79. */
  80. public class CCMklabel extends ClearCase {
  81. private boolean mReplace = false;
  82. private boolean mRecurse = false;
  83. private String mVersion = null;
  84. private String mTypeName = null;
  85. private String mVOB = null;
  86. private String mComment = null;
  87. private String mCfile = null;
  88. /**
  89. * Executes the task.
  90. * <p>
  91. * Builds a command line to execute cleartool and then calls Exec's run method
  92. * to execute the command line.
  93. * @throws BuildException if the command fails and failonerr is set to true
  94. */
  95. public void execute() throws BuildException {
  96. Commandline commandLine = new Commandline();
  97. Project aProj = getProject();
  98. int result = 0;
  99. // Check for required attributes
  100. if (getTypeName() == null) {
  101. throw new BuildException("Required attribute TypeName not specified");
  102. }
  103. // Default the viewpath to basedir if it is not specified
  104. if (getViewPath() == null) {
  105. setViewPath(aProj.getBaseDir().getPath());
  106. }
  107. // build the command line from what we got. the format is
  108. // cleartool mklabel [options...] [viewpath ...]
  109. // as specified in the CLEARTOOL help
  110. commandLine.setExecutable(getClearToolCommand());
  111. commandLine.createArgument().setValue(COMMAND_MKLABEL);
  112. checkOptions(commandLine);
  113. if (!getFailOnErr()) {
  114. getProject().log("Ignoring any errors that occur for: "
  115. + getViewPathBasename(), Project.MSG_VERBOSE);
  116. }
  117. result = run(commandLine);
  118. if (Execute.isFailure(result) && getFailOnErr()) {
  119. String msg = "Failed executing: " + commandLine.toString();
  120. throw new BuildException(msg, getLocation());
  121. }
  122. }
  123. /**
  124. * Check the command line options.
  125. */
  126. private void checkOptions(Commandline cmd) {
  127. if (getReplace()) {
  128. // -replace
  129. cmd.createArgument().setValue(FLAG_REPLACE);
  130. }
  131. if (getRecurse()) {
  132. // -recurse
  133. cmd.createArgument().setValue(FLAG_RECURSE);
  134. }
  135. if (getVersion() != null) {
  136. // -version
  137. getVersionCommand(cmd);
  138. }
  139. if (getComment() != null) {
  140. // -c
  141. getCommentCommand(cmd);
  142. } else {
  143. if (getCommentFile() != null) {
  144. // -cfile
  145. getCommentFileCommand(cmd);
  146. } else {
  147. cmd.createArgument().setValue(FLAG_NOCOMMENT);
  148. }
  149. }
  150. if (getTypeName() != null) {
  151. // type
  152. getTypeCommand(cmd);
  153. }
  154. // viewpath
  155. cmd.createArgument().setValue(getViewPath());
  156. }
  157. /**
  158. * Set the replace flag
  159. *
  160. * @param replace the status to set the flag to
  161. */
  162. public void setReplace(boolean replace) {
  163. mReplace = replace;
  164. }
  165. /**
  166. * Get replace flag status
  167. *
  168. * @return boolean containing status of replace flag
  169. */
  170. public boolean getReplace() {
  171. return mReplace;
  172. }
  173. /**
  174. * Set recurse flag
  175. *
  176. * @param recurse the status to set the flag to
  177. */
  178. public void setRecurse(boolean recurse) {
  179. mRecurse = recurse;
  180. }
  181. /**
  182. * Get recurse flag status
  183. *
  184. * @return boolean containing status of recurse flag
  185. */
  186. public boolean getRecurse() {
  187. return mRecurse;
  188. }
  189. /**
  190. * Set the version flag
  191. *
  192. * @param version the status to set the flag to
  193. */
  194. public void setVersion(String version) {
  195. mVersion = version;
  196. }
  197. /**
  198. * Get version flag status
  199. *
  200. * @return boolean containing status of version flag
  201. */
  202. public String getVersion() {
  203. return mVersion;
  204. }
  205. /**
  206. * Set comment string
  207. *
  208. * @param comment the comment string
  209. */
  210. public void setComment(String comment) {
  211. mComment = comment;
  212. }
  213. /**
  214. * Get comment string
  215. *
  216. * @return String containing the comment
  217. */
  218. public String getComment() {
  219. return mComment;
  220. }
  221. /**
  222. * Set comment file
  223. *
  224. * @param cfile the path to the comment file
  225. */
  226. public void setCommentFile(String cfile) {
  227. mCfile = cfile;
  228. }
  229. /**
  230. * Get comment file
  231. *
  232. * @return String containing the path to the comment file
  233. */
  234. public String getCommentFile() {
  235. return mCfile;
  236. }
  237. /**
  238. * Set the type-name
  239. *
  240. * @param tn the type name
  241. */
  242. public void setTypeName(String tn) {
  243. mTypeName = tn;
  244. }
  245. /**
  246. * Get type-name
  247. *
  248. * @return String containing type name
  249. */
  250. public String getTypeName() {
  251. return mTypeName;
  252. }
  253. /**
  254. * Set the VOB name
  255. *
  256. * @param vob the VOB name
  257. */
  258. public void setVOB(String vob) {
  259. mVOB = vob;
  260. }
  261. /**
  262. * Get VOB name
  263. *
  264. * @return String containing VOB name
  265. */
  266. public String getVOB() {
  267. return mVOB;
  268. }
  269. /**
  270. * Get the 'version' command
  271. *
  272. * @param cmd CommandLine containing the command line string with or
  273. * without the version flag and string appended
  274. */
  275. private void getVersionCommand(Commandline cmd) {
  276. if (getVersion() != null) {
  277. /* Had to make two separate commands here because if a space is
  278. inserted between the flag and the value, it is treated as a
  279. Windows filename with a space and it is enclosed in double
  280. quotes ("). This breaks clearcase.
  281. */
  282. cmd.createArgument().setValue(FLAG_VERSION);
  283. cmd.createArgument().setValue(getVersion());
  284. }
  285. }
  286. /**
  287. * Get the 'comment' command
  288. *
  289. * @param cmd containing the command line string with or
  290. * without the comment flag and string appended
  291. */
  292. private void getCommentCommand(Commandline cmd) {
  293. if (getComment() != null) {
  294. /* Had to make two separate commands here because if a space is
  295. inserted between the flag and the value, it is treated as a
  296. Windows filename with a space and it is enclosed in double
  297. quotes ("). This breaks clearcase.
  298. */
  299. cmd.createArgument().setValue(FLAG_COMMENT);
  300. cmd.createArgument().setValue(getComment());
  301. }
  302. }
  303. /**
  304. * Get the 'commentfile' command
  305. *
  306. * @param cmd containing the command line string with or
  307. * without the commentfile flag and file appended
  308. */
  309. private void getCommentFileCommand(Commandline cmd) {
  310. if (getCommentFile() != 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_COMMENTFILE);
  317. cmd.createArgument().setValue(getCommentFile());
  318. }
  319. }
  320. /**
  321. * Get the type-name
  322. *
  323. * @param cmd containing the command line string with or
  324. * without the type-name
  325. */
  326. private void getTypeCommand(Commandline cmd) {
  327. String typenm = null;
  328. if (getTypeName() != null) {
  329. typenm = getTypeName();
  330. if (getVOB() != null) {
  331. typenm += "@" + getVOB();
  332. }
  333. cmd.createArgument().setValue(typenm);
  334. }
  335. }
  336. /**
  337. * -replace flag -- replace another label of the same type
  338. */
  339. public static final String FLAG_REPLACE = "-replace";
  340. /**
  341. * -recurse flag -- process all subdirectories
  342. */
  343. public static final String FLAG_RECURSE = "-recurse";
  344. /**
  345. * -version flag -- attach label to specified version
  346. */
  347. public static final String FLAG_VERSION = "-version";
  348. /**
  349. * -c flag -- comment to attach to the file
  350. */
  351. public static final String FLAG_COMMENT = "-c";
  352. /**
  353. * -cfile flag -- file containing a comment to attach to the file
  354. */
  355. public static final String FLAG_COMMENTFILE = "-cfile";
  356. /**
  357. * -nc flag -- no comment is specified
  358. */
  359. public static final String FLAG_NOCOMMENT = "-nc";
  360. }