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 java.text.SimpleDateFormat;
  24. import java.util.Date;
  25. import org.apache.tools.ant.BuildException;
  26. import org.apache.tools.ant.Project;
  27. import org.apache.tools.ant.util.StringUtils;
  28. /**
  29. * Creates a new Perforce label and set contents to reflect current
  30. * client file revisions.
  31. *
  32. * Label name defaults to AntLabel if none set.
  33. *
  34. * Example Usage:
  35. * <pre>
  36. * <P4Label name="MyLabel-${TSTAMP}-${DSTAMP}" desc="Auto Build Label" />
  37. * </pre>
  38. *
  39. *
  40. * @ant.task category="scm"
  41. */
  42. public class P4Label extends P4Base {
  43. protected String name;
  44. protected String desc;
  45. protected String lock;
  46. /**
  47. * The name of the label; optional, default "AntLabel"
  48. * @param name the name of the label
  49. */
  50. public void setName(String name) {
  51. this.name = name;
  52. }
  53. /**
  54. *Label Description; optional
  55. * @param desc description of the label
  56. */
  57. public void setDesc(String desc) {
  58. this.desc = desc;
  59. }
  60. /**
  61. * when set to "locked", Perforce will lock the label once created; optional.
  62. * @param lock only admissible value "locked"
  63. */
  64. public void setLock(String lock) {
  65. this.lock = lock;
  66. }
  67. /**
  68. * do the work
  69. * @throws BuildException if failonerror has been set to true and Perforce fails
  70. */
  71. public void execute() throws BuildException {
  72. log("P4Label exec:", Project.MSG_INFO);
  73. if (P4View == null || P4View.length() < 1) {
  74. log("View not set, assuming //depot/...", Project.MSG_WARN);
  75. P4View = "//depot/...";
  76. } else {
  77. P4View = StringUtils.replace(P4View, ":", "\n\t");
  78. P4View = StringUtils.replace(P4View, ";", "\n\t");
  79. }
  80. if (desc == null || desc.length() < 1) {
  81. log("Label Description not set, assuming 'AntLabel'",
  82. Project.MSG_WARN);
  83. desc = "AntLabel";
  84. }
  85. if (lock != null && !lock.equalsIgnoreCase("locked")) {
  86. log("lock attribute invalid - ignoring", Project.MSG_WARN);
  87. }
  88. if (name == null || name.length() < 1) {
  89. SimpleDateFormat formatter
  90. = new SimpleDateFormat("yyyy.MM.dd-hh:mm");
  91. Date now = new Date();
  92. name = "AntLabel-" + formatter.format(now);
  93. log("name not set, assuming '" + name + "'", Project.MSG_WARN);
  94. }
  95. //We have to create a unlocked label first
  96. String newLabel =
  97. "Label: " + name
  98. + "\nDescription: " + desc
  99. + "\nOptions: unlocked"
  100. + "\nView: \n\t" + P4View;
  101. P4Handler handler = new P4HandlerAdapter() {
  102. public void process(String line) {
  103. log(line, Project.MSG_VERBOSE);
  104. }
  105. };
  106. handler.setOutput(newLabel);
  107. execP4Command("label -i", handler);
  108. execP4Command("labelsync -l " + name, new P4HandlerAdapter() {
  109. public void process(String line) {
  110. log(line, Project.MSG_VERBOSE);
  111. }
  112. });
  113. log("Created Label " + name + " (" + desc + ") with view:\n" + P4View,
  114. Project.MSG_INFO);
  115. //Now lock if required
  116. if (lock != null && lock.equalsIgnoreCase("locked")) {
  117. log("Modifying lock status to 'locked'", Project.MSG_INFO);
  118. final StringBuffer labelSpec = new StringBuffer();
  119. //Read back the label spec from perforce,
  120. //Replace Options
  121. //Submit back to Perforce
  122. handler = new P4HandlerAdapter() {
  123. public void process(String line) {
  124. log(line, Project.MSG_VERBOSE);
  125. if (util.match("/^Options:/", line)) {
  126. line = "Options: " + lock;
  127. }
  128. labelSpec.append(line + "\n");
  129. }
  130. };
  131. execP4Command("label -o " + name, handler);
  132. log(labelSpec.toString(), Project.MSG_DEBUG);
  133. log("Now locking label...", Project.MSG_VERBOSE);
  134. handler = new P4HandlerAdapter() {
  135. public void process(String line) {
  136. log(line, Project.MSG_VERBOSE);
  137. }
  138. };
  139. handler.setOutput(labelSpec.toString());
  140. execP4Command("label -i", handler);
  141. }
  142. }
  143. }