1. /*
  2. * Copyright 2001-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.io.File;
  24. import java.util.Vector;
  25. import org.apache.tools.ant.BuildException;
  26. import org.apache.tools.ant.DirectoryScanner;
  27. import org.apache.tools.ant.Project;
  28. import org.apache.tools.ant.types.FileSet;
  29. /** Adds specified files to Perforce.
  30. *
  31. * <b>Example Usage:</b>
  32. * <table border="1">
  33. * <th>Function</th><th>Command</th>
  34. * <tr><td>Add files using P4USER, P4PORT and P4CLIENT settings specified</td>
  35. * <td><P4add <br>P4view="//projects/foo/main/source/..." <br>P4User="fbloggs"
  36. * <br>P4Port="km01:1666"
  37. * <br>P4Client="fbloggsclient"><br><fileset basedir="dir" includes="**/*.java"><br>
  38. * </p4add></td></tr>
  39. * <tr><td>Add files using P4USER, P4PORT and P4CLIENT settings defined in environment</td><td>
  40. * <P4add P4view="//projects/foo/main/source/..." /><br><fileset basedir="dir"
  41. * includes="**/*.java"><br></p4add></td></tr>
  42. * <tr><td>Specify the length of command line arguments to pass to each invocation of p4</td>
  43. * <td><p4add Commandlength="450"></td></tr>
  44. * </table>
  45. *
  46. *
  47. * @ant.task category="scm"
  48. */
  49. public class P4Add extends P4Base {
  50. private static final int DEFAULT_CMD_LENGTH = 450;
  51. private int changelist;
  52. private String addCmd = "";
  53. private Vector filesets = new Vector();
  54. private int cmdLength = DEFAULT_CMD_LENGTH;
  55. /**
  56. * positive integer specifying the maximum length
  57. * of the commandline when calling Perforce to add the files.
  58. * Defaults to 450, higher values mean faster execution,
  59. * but also possible failures.
  60. * @param len maximum length of command line default is 450.
  61. * @throws BuildException if trying to set the command line length to 0 or less.
  62. */
  63. public void setCommandlength(int len) throws BuildException {
  64. if (len <= 0) {
  65. throw new BuildException("P4Add: Commandlength should be a positive number");
  66. }
  67. this.cmdLength = len;
  68. }
  69. /**
  70. * If specified the open files are associated with the
  71. * specified pending changelist number; otherwise the open files are
  72. * associated with the default changelist.
  73. *
  74. * @param changelist the change list number
  75. *
  76. * @throws BuildException if trying to set a change list number <=0.
  77. */
  78. public void setChangelist(int changelist) throws BuildException {
  79. if (changelist <= 0) {
  80. throw new BuildException("P4Add: Changelist# should be a positive number");
  81. }
  82. this.changelist = changelist;
  83. }
  84. /**
  85. * files to add
  86. *
  87. * @param set the FileSet that one wants to add to Perforce Source Control
  88. */
  89. public void addFileset(FileSet set) {
  90. filesets.addElement(set);
  91. }
  92. /**
  93. * run the task.
  94. *
  95. * @throws BuildException if the execution of the Perforce command fails.
  96. */
  97. public void execute() throws BuildException {
  98. if (P4View != null) {
  99. addCmd = P4View;
  100. }
  101. P4CmdOpts = (changelist > 0) ? ("-c " + changelist) : "";
  102. StringBuffer filelist = new StringBuffer();
  103. for (int i = 0; i < filesets.size(); i++) {
  104. FileSet fs = (FileSet) filesets.elementAt(i);
  105. DirectoryScanner ds = fs.getDirectoryScanner(getProject());
  106. //File fromDir = fs.getDir(project);
  107. String[] srcFiles = ds.getIncludedFiles();
  108. if (srcFiles != null) {
  109. for (int j = 0; j < srcFiles.length; j++) {
  110. File f = new File(ds.getBasedir(), srcFiles[j]);
  111. filelist.append(" ").append('"').append(f.getAbsolutePath()).append('"');
  112. if (filelist.length() > cmdLength) {
  113. execP4Add(filelist);
  114. filelist = new StringBuffer();
  115. }
  116. }
  117. if (filelist.length() > 0) {
  118. execP4Add(filelist);
  119. }
  120. } else {
  121. log("No files specified to add!", Project.MSG_WARN);
  122. }
  123. }
  124. }
  125. private void execP4Add(StringBuffer list) {
  126. log("Execing add " + P4CmdOpts + " " + addCmd + list, Project.MSG_INFO);
  127. execP4Command("-s add " + P4CmdOpts + " " + addCmd + list, new SimpleP4OutputHandler(this));
  128. }
  129. }