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. import java.util.Vector;
  26. /** Submits a numbered changelist to Perforce.
  27. *
  28. * <B>Note:</B> P4Submit cannot (yet) submit the default changelist.
  29. * This shouldn't be a problem with the ANT task as the usual flow is
  30. * P4Change to create a new numbered change followed by P4Edit then P4Submit.
  31. *
  32. * Example Usage:-<br>
  33. * <p4submit change="${p4.change}" />
  34. *
  35. *
  36. * @ant.task category="scm"
  37. */
  38. public class P4Submit extends P4Base {
  39. //ToDo: If dealing with default cl need to parse out <enter description here>
  40. /**
  41. * change list number
  42. */
  43. public String change;
  44. /**
  45. * change property
  46. */
  47. private String changeProperty;
  48. /**
  49. * needsresolveproperty
  50. */
  51. private String needsResolveProperty;
  52. /**
  53. * set the change list number to submit
  54. * @param change The changelist number to submit; required.
  55. */
  56. public void setChange(String change) {
  57. this.change = change;
  58. }
  59. /**
  60. * property defining the change number if the change number gets renumbered
  61. * @param changeProperty name of a new property to which the change number
  62. * will be assigned if it changes
  63. * @since ant 1.6.1
  64. */
  65. public void setChangeProperty(String changeProperty) {
  66. this.changeProperty = changeProperty;
  67. }
  68. /**
  69. * property defining the need to resolve the change list
  70. * @param needsResolveProperty a property which will be set if the change needs resolve
  71. * @since ant 1.6.1
  72. */
  73. public void setNeedsResolveProperty(String needsResolveProperty) {
  74. this.needsResolveProperty = needsResolveProperty;
  75. }
  76. /**
  77. * do the work
  78. * @throws BuildException if no change list specified
  79. */
  80. public void execute() throws BuildException {
  81. if (change != null) {
  82. execP4Command("submit -c " + change, (P4HandlerAdapter) new P4SubmitAdapter(this));
  83. } else {
  84. //here we'd parse the output from change -o into submit -i
  85. //in order to support default change.
  86. throw new BuildException("No change specified (no support for default change yet....");
  87. }
  88. }
  89. /**
  90. * internal class used to process the output of p4 submit
  91. */
  92. public class P4SubmitAdapter extends SimpleP4OutputHandler {
  93. public P4SubmitAdapter(P4Base parent) {
  94. super(parent);
  95. }
  96. /**
  97. * process a line of stdout/stderr coming from Perforce
  98. * @param line line of stdout or stderr coming from Perforce
  99. */
  100. public void process(String line) {
  101. super.process(line);
  102. getProject().setProperty("p4.needsresolve", "0");
  103. // this type of output might happen
  104. // Change 18 renamed change 20 and submitted.
  105. if (util.match("/renamed/", line)) {
  106. try {
  107. Vector myarray = new Vector();
  108. util.split(myarray, line);
  109. boolean found = false;
  110. for (int counter = 0; counter < myarray.size(); counter++) {
  111. if (found == true) {
  112. String chnum = (String) myarray.elementAt(counter + 1);
  113. int changenumber = Integer.parseInt(chnum);
  114. log("Perforce change renamed " + changenumber, Project.MSG_INFO);
  115. getProject().setProperty("p4.change", "" + changenumber);
  116. if (changeProperty != null) {
  117. getProject().setNewProperty(changeProperty, chnum);
  118. }
  119. found = false;
  120. }
  121. if (((myarray.elementAt(counter))).equals("renamed")) {
  122. found = true;
  123. }
  124. }
  125. // NumberFormatException or ArrayOutOfBondsException could happen here
  126. } catch (Exception e) {
  127. String msg = "Failed to parse " + line + "\n"
  128. + " due to " + e.getMessage();
  129. throw new BuildException(msg, e, getLocation());
  130. }
  131. }
  132. if (util.match("/p4 submit -c/", line)) {
  133. getProject().setProperty("p4.needsresolve", "1");
  134. if (needsResolveProperty != null) {
  135. getProject().setNewProperty(needsResolveProperty, "true");
  136. }
  137. }
  138. }
  139. }
  140. }