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 mkelem.
  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 file or 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>nowarn</td>
  50. * <td>Suppress warning messages</td>
  51. * <td>No</td>
  52. * <tr>
  53. * <tr>
  54. * <td>nocheckout</td>
  55. * <td>Do not checkout after element creation</td>
  56. * <td>No</td>
  57. * <tr>
  58. * <tr>
  59. * <td>checkin</td>
  60. * <td>Checkin element after creation</td>
  61. * <td>No</td>
  62. * <tr>
  63. * <tr>
  64. * <td>preservetime</td>
  65. * <td>Preserve the modification time (for checkin)</td>
  66. * <td>No</td>
  67. * <tr>
  68. * <tr>
  69. * <td>master</td>
  70. * <td>Assign mastership of the main branch to the current site</td>
  71. * <td>No</td>
  72. * <tr>
  73. * <tr>
  74. * <td>eltype</td>
  75. * <td>Element type to use during element creation</td>
  76. * <td>No</td>
  77. * <tr>
  78. * <tr>
  79. * <td>failonerr</td>
  80. * <td>Throw an exception if the command fails. Default is true</td>
  81. * <td>No</td>
  82. * <tr>
  83. * </table>
  84. *
  85. */
  86. public class CCMkelem extends ClearCase {
  87. private String mComment = null;
  88. private String mCfile = null;
  89. private boolean mNwarn = false;
  90. private boolean mPtime = false;
  91. private boolean mNoco = false;
  92. private boolean mCheckin = false;
  93. private boolean mMaster = false;
  94. private String mEltype = null;
  95. /**
  96. * Executes the task.
  97. * <p>
  98. * Builds a command line to execute cleartool and then calls Exec's run method
  99. * to execute the command line.
  100. * @throws BuildException if the command fails and failonerr is set to true
  101. */
  102. public void execute() throws BuildException {
  103. Commandline commandLine = new Commandline();
  104. Project aProj = getProject();
  105. int result = 0;
  106. // Default the viewpath to basedir if it is not specified
  107. if (getViewPath() == null) {
  108. setViewPath(aProj.getBaseDir().getPath());
  109. }
  110. // build the command line from what we got. the format is
  111. // cleartool mkelem [options...] [viewpath ...]
  112. // as specified in the CLEARTOOL.EXE help
  113. commandLine.setExecutable(getClearToolCommand());
  114. commandLine.createArgument().setValue(COMMAND_MKELEM);
  115. checkOptions(commandLine);
  116. if (!getFailOnErr()) {
  117. getProject().log("Ignoring any errors that occur for: "
  118. + getViewPathBasename(), Project.MSG_VERBOSE);
  119. }
  120. result = run(commandLine);
  121. if (Execute.isFailure(result) && getFailOnErr()) {
  122. String msg = "Failed executing: " + commandLine.toString();
  123. throw new BuildException(msg, getLocation());
  124. }
  125. }
  126. /**
  127. * Check the command line options.
  128. */
  129. private void checkOptions(Commandline cmd) {
  130. if (getComment() != null) {
  131. // -c
  132. getCommentCommand(cmd);
  133. } else {
  134. if (getCommentFile() != null) {
  135. // -cfile
  136. getCommentFileCommand(cmd);
  137. } else {
  138. cmd.createArgument().setValue(FLAG_NOCOMMENT);
  139. }
  140. }
  141. if (getNoWarn()) {
  142. // -nwarn
  143. cmd.createArgument().setValue(FLAG_NOWARN);
  144. }
  145. /*
  146. * Should choose either -ci or -nco.
  147. */
  148. if (getNoCheckout() && getCheckin()) {
  149. throw new BuildException("Should choose either [nocheckout | checkin]");
  150. }
  151. if (getNoCheckout()) {
  152. // -nco
  153. cmd.createArgument().setValue(FLAG_NOCHECKOUT);
  154. }
  155. if (getCheckin()) {
  156. // -ci
  157. cmd.createArgument().setValue(FLAG_CHECKIN);
  158. if (getPreserveTime()) {
  159. // -ptime
  160. cmd.createArgument().setValue(FLAG_PRESERVETIME);
  161. }
  162. }
  163. if (getMaster()) {
  164. // -master
  165. cmd.createArgument().setValue(FLAG_MASTER);
  166. }
  167. if (getEltype() != null) {
  168. // -eltype
  169. getEltypeCommand(cmd);
  170. }
  171. // viewpath
  172. cmd.createArgument().setValue(getViewPath());
  173. }
  174. /**
  175. * Sets the comment string.
  176. *
  177. * @param comment the comment string
  178. */
  179. public void setComment(String comment) {
  180. mComment = comment;
  181. }
  182. /**
  183. * Get comment string
  184. *
  185. * @return String containing the comment
  186. */
  187. public String getComment() {
  188. return mComment;
  189. }
  190. /**
  191. * Specifies a file containing a comment.
  192. *
  193. * @param cfile the path to the comment file
  194. */
  195. public void setCommentFile(String cfile) {
  196. mCfile = cfile;
  197. }
  198. /**
  199. * Get comment file
  200. *
  201. * @return String containing the path to the comment file
  202. */
  203. public String getCommentFile() {
  204. return mCfile;
  205. }
  206. /**
  207. * If true, suppress warning messages.
  208. *
  209. * @param nwarn the status to set the flag to
  210. */
  211. public void setNoWarn(boolean nwarn) {
  212. mNwarn = nwarn;
  213. }
  214. /**
  215. * Get nowarn flag status
  216. *
  217. * @return boolean containing status of nwarn flag
  218. */
  219. public boolean getNoWarn() {
  220. return mNwarn;
  221. }
  222. /**
  223. * If true, preserve the modification time.
  224. *
  225. * @param ptime the status to set the flag to
  226. */
  227. public void setPreserveTime(boolean ptime) {
  228. mPtime = ptime;
  229. }
  230. /**
  231. * Get preservetime flag status
  232. *
  233. * @return boolean containing status of preservetime flag
  234. */
  235. public boolean getPreserveTime() {
  236. return mPtime;
  237. }
  238. /**
  239. * If true, do not checkout element after creation.
  240. *
  241. * @param co the status to set the flag to
  242. */
  243. public void setNoCheckout(boolean co) {
  244. mNoco = co;
  245. }
  246. /**
  247. * Get no checkout flag status
  248. *
  249. * @return boolean containing status of noco flag
  250. */
  251. public boolean getNoCheckout() {
  252. return mNoco;
  253. }
  254. /**
  255. * If true, checkin the element after creation
  256. *
  257. * @param ci the status to set the flag to
  258. */
  259. public void setCheckin(boolean ci) {
  260. mCheckin = ci;
  261. }
  262. /**
  263. * Get ci flag status
  264. *
  265. * @return boolean containing status of ci flag
  266. */
  267. public boolean getCheckin() {
  268. return mCheckin;
  269. }
  270. /**
  271. * If true, changes mastership of the main branch
  272. * to the current site
  273. *
  274. * @param master the status to set the flag to
  275. */
  276. public void setMaster(boolean master) {
  277. mMaster = master;
  278. }
  279. /**
  280. * Get master flag status
  281. *
  282. * @return boolean containing status of master flag
  283. */
  284. public boolean getMaster() {
  285. return mMaster;
  286. }
  287. /**
  288. * Specifies the element type to use.
  289. *
  290. * @param eltype to create element
  291. */
  292. public void setEltype(String eltype) {
  293. mEltype = eltype;
  294. }
  295. /**
  296. * Get element type
  297. *
  298. * @return String containing the element type
  299. */
  300. public String getEltype() {
  301. return mEltype;
  302. }
  303. /**
  304. * Get the 'comment' command
  305. *
  306. * @param cmd containing the command line string with or
  307. * without the comment flag and string appended
  308. */
  309. private void getCommentCommand(Commandline cmd) {
  310. if (getComment() != 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_COMMENT);
  317. cmd.createArgument().setValue(getComment());
  318. }
  319. }
  320. /**
  321. * Get the 'commentfile' command
  322. *
  323. * @param cmd containing the command line string with or
  324. * without the commentfile flag and file appended
  325. */
  326. private void getCommentFileCommand(Commandline cmd) {
  327. if (getCommentFile() != null) {
  328. /* Had to make two separate commands here because if a space is
  329. inserted between the flag and the value, it is treated as a
  330. Windows filename with a space and it is enclosed in double
  331. quotes ("). This breaks clearcase.
  332. */
  333. cmd.createArgument().setValue(FLAG_COMMENTFILE);
  334. cmd.createArgument().setValue(getCommentFile());
  335. }
  336. }
  337. /**
  338. * Get the 'element type' command
  339. *
  340. * @param cmd containing the command line string with or
  341. * without the comment flag and string appended
  342. */
  343. private void getEltypeCommand(Commandline cmd) {
  344. if (getEltype() != null) {
  345. /* Had to make two separate commands here because if a space is
  346. inserted between the flag and the value, it is treated as a
  347. Windows filename with a space and it is enclosed in double
  348. quotes ("). This breaks clearcase.
  349. */
  350. cmd.createArgument().setValue(FLAG_ELTYPE);
  351. cmd.createArgument().setValue(getEltype());
  352. }
  353. }
  354. /**
  355. * -c flag -- comment to attach to the file
  356. */
  357. public static final String FLAG_COMMENT = "-c";
  358. /**
  359. * -cfile flag -- file containing a comment to attach to the file
  360. */
  361. public static final String FLAG_COMMENTFILE = "-cfile";
  362. /**
  363. * -nc flag -- no comment is specified
  364. */
  365. public static final String FLAG_NOCOMMENT = "-nc";
  366. /**
  367. * -nwarn flag -- suppresses warning messages
  368. */
  369. public static final String FLAG_NOWARN = "-nwarn";
  370. /**
  371. * -ptime flag -- preserves the modification time on checkin
  372. */
  373. public static final String FLAG_PRESERVETIME = "-ptime";
  374. /**
  375. * -nco flag -- do not checkout element after creation
  376. */
  377. public static final String FLAG_NOCHECKOUT = "-nco";
  378. /**
  379. * -ci flag -- checkin element after creation
  380. */
  381. public static final String FLAG_CHECKIN = "-ci";
  382. /**
  383. * -master flag -- change mastership of main branch to current site
  384. */
  385. public static final String FLAG_MASTER = "-master";
  386. /**
  387. * -eltype flag -- element type to use during creation
  388. */
  389. public static final String FLAG_ELTYPE = "-eltype";
  390. }