1. /*
  2. * Copyright 2000-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. /*
  18. * Portions of this software are based upon public domain software
  19. * originally written at the National Center for Supercomputing Applications,
  20. * University of Illinois, Urbana-Champaign.
  21. */
  22. package org.apache.tools.ant.taskdefs.optional.perforce;
  23. import org.apache.tools.ant.BuildException;
  24. import org.apache.tools.ant.Project;
  25. /** Synchronize client space to a Perforce depot view.
  26. *
  27. * The API allows additional functionality of the "p4 sync" command
  28. * (such as "p4 sync -f //...#have" or other exotic invocations).</P>
  29. *
  30. * <b>Example Usage:</b>
  31. * <table border="1">
  32. * <th>Function</th><th>Command</th>
  33. * <tr><td>Sync to head using P4USER, P4PORT and P4CLIENT settings specified</td>
  34. * <td><P4Sync <br>P4view="//projects/foo/main/source/..." <br>
  35. * P4User="fbloggs" <br>P4Port="km01:1666" <br>P4Client="fbloggsclient" /></td></tr>
  36. * <tr><td>Sync to head using P4USER, P4PORT and P4CLIENT settings defined in environment</td>
  37. * <td><P4Sync P4view="//projects/foo/main/source/..." /></td></tr>
  38. * <tr><td>Force a re-sync to head, refreshing all files</td>
  39. * <td><P4Sync force="yes" P4view="//projects/foo/main/source/..." /></td></tr>
  40. * <tr><td>Sync to a label</td><td><P4Sync label="myPerforceLabel" /></td></tr>
  41. * </table>
  42. *
  43. * @todo Add decent label error handling for non-exsitant labels
  44. *
  45. *
  46. * @ant.task category="scm"
  47. */
  48. public class P4Sync extends P4Base {
  49. String label;
  50. private String syncCmd = "";
  51. /**
  52. * Label to sync client to; optional.
  53. * @param label name of a label against which one want to sync
  54. * @throws BuildException if label is null or empty string
  55. */
  56. public void setLabel(String label) throws BuildException {
  57. if (label == null && !label.equals("")) {
  58. throw new BuildException("P4Sync: Labels cannot be Null or Empty");
  59. }
  60. this.label = label;
  61. }
  62. /**
  63. * force a refresh of files, if this attribute is set; false by default.
  64. * @param force sync all files, whether they are supposed to be already uptodate or not.
  65. * @throws BuildException if a label is set and force is null
  66. */
  67. public void setForce(String force) throws BuildException {
  68. if (force == null && !label.equals("")) {
  69. throw new BuildException("P4Sync: If you want to force, set force to non-null string!");
  70. }
  71. P4CmdOpts = "-f";
  72. }
  73. /**
  74. * do the work
  75. * @throws BuildException if an error occurs during the execution of the Perforce command
  76. * and failOnError is set to true
  77. */
  78. public void execute() throws BuildException {
  79. if (P4View != null) {
  80. syncCmd = P4View;
  81. }
  82. if (label != null && !label.equals("")) {
  83. syncCmd = syncCmd + "@" + label;
  84. }
  85. log("Execing sync " + P4CmdOpts + " " + syncCmd, Project.MSG_VERBOSE);
  86. execP4Command("-s sync " + P4CmdOpts + " " + syncCmd, new SimpleP4OutputHandler(this));
  87. }
  88. }