001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: /*
019: * Portions of this software are based upon public domain software
020: * originally written at the National Center for Supercomputing Applications,
021: * University of Illinois, Urbana-Champaign.
022: */
023:
024: package org.apache.tools.ant.taskdefs.optional.perforce;
025:
026: import java.io.File;
027: import java.util.Vector;
028:
029: import org.apache.tools.ant.Project;
030: import org.apache.tools.ant.BuildException;
031: import org.apache.tools.ant.DirectoryScanner;
032: import org.apache.tools.ant.types.FileSet;
033:
034: /**
035: * Adds specified files to Perforce.
036: *
037: * <b>Example Usage:</b>
038: * <table border="1">
039: * <th>Function</th><th>Command</th>
040: * <tr><td>Add files using P4USER, P4PORT and P4CLIENT settings specified</td>
041: * <td><P4add <br>P4view="//projects/foo/main/source/..." <br>P4User="fbloggs"
042: * <br>P4Port="km01:1666"
043: * <br>P4Client="fbloggsclient"><br><fileset basedir="dir" includes="**/*.java"><br>
044: * </p4add></td></tr>
045: * <tr><td>Add files using P4USER, P4PORT and P4CLIENT settings defined in environment</td><td>
046: * <P4add P4view="//projects/foo/main/source/..." /><br><fileset basedir="dir"
047: * includes="**/*.java"><br></p4add></td></tr>
048: * <tr><td>Specify the length of command line arguments to pass to each invocation of p4</td>
049: * <td><p4add Commandlength="450"></td></tr>
050: * </table>
051: *
052: * @ant.task category="scm"
053: */
054: public class P4Add extends P4Base {
055: private static final int DEFAULT_CMD_LENGTH = 450;
056: private int changelist;
057: private String addCmd = "";
058: private Vector filesets = new Vector();
059: private int cmdLength = DEFAULT_CMD_LENGTH;
060:
061: /**
062: * Set the maximum length
063: * of the commandline when calling Perforce to add the files.
064: * Defaults to 450, higher values mean faster execution,
065: * but also possible failures.
066: * @param len maximum length of command line default is 450.
067: * @throws BuildException if trying to set the command line length to 0 or less.
068: */
069:
070: public void setCommandlength(int len) throws BuildException {
071: if (len <= 0) {
072: throw new BuildException(
073: "P4Add: Commandlength should be a positive number");
074: }
075: this .cmdLength = len;
076: }
077:
078: /**
079: * If specified the open files are associated with the
080: * specified pending changelist number; otherwise the open files are
081: * associated with the default changelist.
082: *
083: * @param changelist the change list number.
084: *
085: * @throws BuildException if trying to set a change list number <=0.
086: */
087: public void setChangelist(int changelist) throws BuildException {
088: if (changelist <= 0) {
089: throw new BuildException(
090: "P4Add: Changelist# should be a positive number");
091: }
092: this .changelist = changelist;
093: }
094:
095: /**
096: * Add a fileset whose files will be added to Perforce.
097: *
098: * @param set the FileSet that one wants to add to Perforce Source Control.
099: */
100: public void addFileset(FileSet set) {
101: filesets.addElement(set);
102: }
103:
104: /**
105: * Run the task.
106: *
107: * @throws BuildException if the execution of the Perforce command fails.
108: */
109: public void execute() throws BuildException {
110: if (P4View != null) {
111: addCmd = P4View;
112: }
113: P4CmdOpts = (changelist > 0) ? ("-c " + changelist) : "";
114:
115: StringBuffer filelist = new StringBuffer();
116:
117: for (int i = 0; i < filesets.size(); i++) {
118: FileSet fs = (FileSet) filesets.elementAt(i);
119: DirectoryScanner ds = fs.getDirectoryScanner(getProject());
120:
121: String[] srcFiles = ds.getIncludedFiles();
122: if (srcFiles != null) {
123: for (int j = 0; j < srcFiles.length; j++) {
124: File f = new File(ds.getBasedir(), srcFiles[j]);
125: filelist.append(" ").append('"').append(
126: f.getAbsolutePath()).append('"');
127: if (filelist.length() > cmdLength) {
128: execP4Add(filelist);
129: filelist = new StringBuffer();
130: }
131: }
132: if (filelist.length() > 0) {
133: execP4Add(filelist);
134: }
135: } else {
136: log("No files specified to add!", Project.MSG_WARN);
137: }
138: }
139: }
140:
141: private void execP4Add(StringBuffer list) {
142: log("Execing add " + P4CmdOpts + " " + addCmd + list,
143: Project.MSG_INFO);
144: execP4Command("-s add " + P4CmdOpts + " " + addCmd + list,
145: new SimpleP4OutputHandler(this));
146: }
147: }
|