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. /*
  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. import org.apache.tools.ant.util.StringUtils;
  26. /**
  27. * This method syncs an existing Perforce label against the Perforce client
  28. * or against a set of files/revisions.
  29. *
  30. *
  31. * Example Usage:
  32. * <pre>
  33. * <p4labelsync name="MyLabel-${TSTAMP}-${DSTAMP}"
  34. * view="//depot/...#head;//depot2/file1#25" />
  35. * </pre>
  36. *
  37. *
  38. * @ant.task category="scm"
  39. */
  40. public class P4Labelsync extends P4Base {
  41. protected String name;
  42. private boolean add; /* -a */
  43. private boolean delete; /* -n */
  44. private boolean simulationmode; /* -n */
  45. /**
  46. * -a flag of p4 labelsync - preserve files which exist in the label,
  47. * but not in the current view
  48. * @return add attribute
  49. * if set to true the task will not remove any files from the label
  50. * only add files which were not there previously or update these where the revision has changed
  51. * the add attribute is the -a flag of p4 labelsync
  52. */
  53. public boolean isAdd() {
  54. return add;
  55. }
  56. /**
  57. * -a flag of p4 labelsync - preserve files which exist in the label,
  58. * but not in the current view
  59. * @param add if set to true the task will not remove any files from the label
  60. * only add files which were not there previously or update these where the revision has changed
  61. * the add attribute is the -a flag of p4 labelsync
  62. */
  63. public void setAdd(boolean add) {
  64. this.add = add;
  65. }
  66. /**
  67. * -d flag of p4 labelsync; indicates an intention of deleting from the label
  68. * the files specified in the view
  69. * @return delete attribute
  70. */
  71. public boolean isDelete() {
  72. return delete;
  73. }
  74. /**
  75. * -d flag of p4 labelsync; indicates an intention of deleting from the label
  76. * the files specified in the view
  77. * @param delete indicates intention of deleting from the label
  78. * the files specified in the view
  79. */
  80. public void setDelete(boolean delete) {
  81. this.delete = delete;
  82. }
  83. /**
  84. * The name of the label; optional, default "AntLabel"
  85. * @param name of the label
  86. */
  87. public void setName(String name) {
  88. this.name = name;
  89. }
  90. /**
  91. * -n flag of p4 labelsync - display changes without actually doing them
  92. * @return -n flag of p4 labelsync
  93. */
  94. public boolean isSimulationmode() {
  95. return simulationmode;
  96. }
  97. /**
  98. * -n flag of p4 labelsync - display changes without actually doing them
  99. * @param simulationmode display changes without actually doing them
  100. */
  101. public void setSimulationmode(boolean simulationmode) {
  102. this.simulationmode = simulationmode;
  103. }
  104. /**
  105. * do the work
  106. * @throws BuildException if the label name is not supplied
  107. */
  108. public void execute() throws BuildException {
  109. log("P4Labelsync exec:", Project.MSG_INFO);
  110. if (P4View != null && P4View.length() >= 1) {
  111. P4View = StringUtils.replace(P4View, ":", "\n\t");
  112. P4View = StringUtils.replace(P4View, ";", "\n\t");
  113. }
  114. if (P4View == null) {
  115. P4View = "";
  116. }
  117. if (name == null || name.length() < 1) {
  118. throw new BuildException("name attribute is compulsory for labelsync");
  119. }
  120. if (this.isSimulationmode()) {
  121. P4CmdOpts = P4CmdOpts + " -n";
  122. }
  123. if (this.isDelete()) {
  124. P4CmdOpts = P4CmdOpts + " -d";
  125. }
  126. if (this.isAdd()) {
  127. P4CmdOpts = P4CmdOpts + " -a";
  128. }
  129. execP4Command("-s labelsync -l " + name + " " + P4CmdOpts + " " + P4View,
  130. new SimpleP4OutputHandler(this));
  131. }
  132. }