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 CreateBaseline 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>comment</td>
  34. * <td>Specify a comment. Only one of comment or cfile may be
  35. used.</td>
  36. * <td>No</td>
  37. * </tr>
  38. * <tr>
  39. * <td>commentfile</td>
  40. * <td>Specify a file containing a comment. Only one of comment or
  41. cfile may be used.</td>
  42. * <td>No</td>
  43. * </tr>
  44. * <tr>
  45. * <td>baselinerootname</td>
  46. * <td>Specify the name to be associated with the baseline.</td>
  47. * <td>Yes</td>
  48. * </tr>
  49. * <tr>
  50. * <td>nowarn</td>
  51. * <td>Suppress warning messages</td>
  52. * <td>No</td>
  53. * <tr>
  54. * <tr>
  55. * <td>identical</td>
  56. * <td>Allows the baseline to be created even if it is identical to the
  57. previous baseline.</td>
  58. * <td>No</td>
  59. * </tr>
  60. * <tr>
  61. * <td>full</td>
  62. * <td>Creates a full baseline.</td>
  63. * <td>No</td>
  64. * </tr>
  65. * <tr>
  66. * <td>nlabel</td>
  67. * <td>Allows the baseline to be created without a label.</td>
  68. * <td>No</td>
  69. * </tr>
  70. * <tr>
  71. * <td>failonerr</td>
  72. * <td>Throw an exception if the command fails. Default is true</td>
  73. * <td>No</td>
  74. * <tr>
  75. * </table>
  76. *
  77. */
  78. public class CCMkbl extends ClearCase {
  79. private String mComment = null;
  80. private String mCfile = null;
  81. private String mBaselineRootName = null;
  82. private boolean mNwarn = false;
  83. private boolean mIdentical = true;
  84. private boolean mFull = false;
  85. private boolean mNlabel = false;
  86. /**
  87. * Executes the task.
  88. * <p>
  89. * Builds a command line to execute cleartool and then calls Exec's run method
  90. * to execute the command line.
  91. * @throws BuildException if the command fails and failonerr is set to true
  92. */
  93. public void execute() throws BuildException {
  94. Commandline commandLine = new Commandline();
  95. Project aProj = getProject();
  96. int result = 0;
  97. // Default the viewpath to basedir if it is not specified
  98. if (getViewPath() == null) {
  99. setViewPath(aProj.getBaseDir().getPath());
  100. }
  101. // build the command line from what we got. the format is
  102. // cleartool checkin [options...] [viewpath ...]
  103. // as specified in the CLEARTOOL.EXE help
  104. commandLine.setExecutable(getClearToolCommand());
  105. commandLine.createArgument().setValue(COMMAND_MKBL);
  106. checkOptions(commandLine);
  107. if (!getFailOnErr()) {
  108. getProject().log("Ignoring any errors that occur for: "
  109. + getBaselineRootName(), Project.MSG_VERBOSE);
  110. }
  111. result = run(commandLine);
  112. if (Execute.isFailure(result) && getFailOnErr()) {
  113. String msg = "Failed executing: " + commandLine.toString();
  114. throw new BuildException(msg, getLocation());
  115. }
  116. }
  117. /**
  118. * Check the command line options.
  119. */
  120. private void checkOptions(Commandline cmd) {
  121. if (getComment() != null) {
  122. // -c
  123. getCommentCommand(cmd);
  124. } else {
  125. if (getCommentFile() != null) {
  126. // -cfile
  127. getCommentFileCommand(cmd);
  128. } else {
  129. cmd.createArgument().setValue(FLAG_NOCOMMENT);
  130. }
  131. }
  132. if (getIdentical()) {
  133. // -identical
  134. cmd.createArgument().setValue(FLAG_IDENTICAL);
  135. }
  136. if (getFull()) {
  137. // -full
  138. cmd.createArgument().setValue(FLAG_FULL);
  139. } else {
  140. // -incremental
  141. cmd.createArgument().setValue(FLAG_INCREMENTAL);
  142. }
  143. if (getNlabel()) {
  144. // -nlabel
  145. cmd.createArgument().setValue(FLAG_NLABEL);
  146. }
  147. // baseline_root_name
  148. cmd.createArgument().setValue(getBaselineRootName());
  149. }
  150. /**
  151. * Set comment string
  152. *
  153. * @param comment the comment string
  154. */
  155. public void setComment(String comment) {
  156. mComment = comment;
  157. }
  158. /**
  159. * Get comment string
  160. *
  161. * @return String containing the comment
  162. */
  163. public String getComment() {
  164. return mComment;
  165. }
  166. /**
  167. * Set comment file
  168. *
  169. * @param cfile the path to the comment file
  170. */
  171. public void setCommentFile(String cfile) {
  172. mCfile = cfile;
  173. }
  174. /**
  175. * Get comment file
  176. *
  177. * @return String containing the path to the comment file
  178. */
  179. public String getCommentFile() {
  180. return mCfile;
  181. }
  182. /**
  183. * Set baseline_root_name
  184. *
  185. * @param baselineRootName the name of the baseline
  186. */
  187. public void setBaselineRootName(String baselineRootName) {
  188. mBaselineRootName = baselineRootName;
  189. }
  190. /**
  191. * Get baseline_root_name
  192. *
  193. * @return String containing the name of the baseline
  194. */
  195. public String getBaselineRootName() {
  196. return mBaselineRootName;
  197. }
  198. /**
  199. /**
  200. * Set the nowarn flag
  201. *
  202. * @param nwarn the status to set the flag to
  203. */
  204. public void setNoWarn(boolean nwarn) {
  205. mNwarn = nwarn;
  206. }
  207. /**
  208. * Get nowarn flag status
  209. *
  210. * @return boolean containing status of nwarn flag
  211. */
  212. public boolean getNoWarn() {
  213. return mNwarn;
  214. }
  215. /**
  216. * Set the identical flag
  217. *
  218. * @param identical the status to set the flag to
  219. */
  220. public void setIdentical(boolean identical) {
  221. mIdentical = identical;
  222. }
  223. /**
  224. * Get identical flag status
  225. *
  226. * @return boolean containing status of identical flag
  227. */
  228. public boolean getIdentical() {
  229. return mIdentical;
  230. }
  231. /**
  232. * Set the full flag
  233. *
  234. * @param full the status to set the flag to
  235. */
  236. public void setFull(boolean full) {
  237. mFull = full;
  238. }
  239. /**
  240. * Get full flag status
  241. *
  242. * @return boolean containing status of full flag
  243. */
  244. public boolean getFull() {
  245. return mFull;
  246. }
  247. /**
  248. * Set the nlabel flag
  249. *
  250. * @param nlabel the status to set the flag to
  251. */
  252. public void setNlabel(boolean nlabel) {
  253. mNlabel = nlabel;
  254. }
  255. /**
  256. * Get nlabel status
  257. *
  258. * @return boolean containing status of nlabel flag
  259. */
  260. public boolean getNlabel() {
  261. return mNlabel;
  262. }
  263. /**
  264. * Get the 'comment' command
  265. *
  266. * @param cmd containing the command line string with or
  267. * without the comment flag and string appended
  268. */
  269. private void getCommentCommand(Commandline cmd) {
  270. if (getComment() != null) {
  271. /* Had to make two separate commands here because if a space is
  272. inserted between the flag and the value, it is treated as a
  273. Windows filename with a space and it is enclosed in double
  274. quotes ("). This breaks clearcase.
  275. */
  276. cmd.createArgument().setValue(FLAG_COMMENT);
  277. cmd.createArgument().setValue(getComment());
  278. }
  279. }
  280. /**
  281. * Get the 'commentfile' command
  282. *
  283. * @param cmd CommandLine containing the command line string with or
  284. * without the commentfile flag and file appended
  285. */
  286. private void getCommentFileCommand(Commandline cmd) {
  287. if (getCommentFile() != null) {
  288. /* Had to make two separate commands here because if a space is
  289. inserted between the flag and the value, it is treated as a
  290. Windows filename with a space and it is enclosed in double
  291. quotes ("). This breaks clearcase.
  292. */
  293. cmd.createArgument().setValue(FLAG_COMMENTFILE);
  294. cmd.createArgument().setValue(getCommentFile());
  295. }
  296. }
  297. /**
  298. * -c flag -- comment to attach to the file
  299. */
  300. public static final String FLAG_COMMENT = "-c";
  301. /**
  302. * -cfile flag -- file containing a comment to attach to the file
  303. */
  304. public static final String FLAG_COMMENTFILE = "-cfile";
  305. /**
  306. * -nc flag -- no comment is specified
  307. */
  308. public static final String FLAG_NOCOMMENT = "-nc";
  309. /**
  310. * -identical flag -- allows the file to be checked in even if it is identical to the original
  311. */
  312. public static final String FLAG_IDENTICAL = "-identical";
  313. /**
  314. * -incremental flag -- baseline to be created is incremental
  315. */
  316. public static final String FLAG_INCREMENTAL = "-incremental";
  317. /**
  318. * -full flag -- baseline to be created is full
  319. */
  320. public static final String FLAG_FULL = "-full";
  321. /**
  322. * -nlabel -- baseline to be created without a label
  323. */
  324. public static final String FLAG_NLABEL = "-nlabel";
  325. }