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: import java.util.ArrayList;
029:
030: import org.apache.tools.ant.Project;
031: import org.apache.tools.ant.BuildException;
032: import org.apache.tools.ant.DirectoryScanner;
033: import org.apache.tools.ant.types.FileSet;
034:
035: /**
036: * P4Fstat--find out which files are under Perforce control and which are not.
037: *
038: * <br><b>Example Usage:</b><br>
039: * <pre>
040: * <project name="p4fstat" default="p4fstat"
041: * basedir="C:\dev\gnu">
042: * <target name="p4fstat" >
043: * <p4fstat showfilter="all">
044: * <fileset dir="depot" includes="**\/*"/>
045: * </p4fstat>
046: * </target>
047: * </project>
048: * </pre>
049: *
050: * @ant.task category="scm"
051: */
052: public class P4Fstat extends P4Base {
053:
054: private int changelist;
055: private String addCmd = "";
056: private Vector filesets = new Vector();
057: private static final int DEFAULT_CMD_LENGTH = 300;
058: private int cmdLength = DEFAULT_CMD_LENGTH;
059: private static final int SHOW_ALL = 0;
060: private static final int SHOW_EXISTING = 1;
061: private static final int SHOW_NON_EXISTING = 2;
062: private int show = SHOW_NON_EXISTING;
063: private FStatP4OutputHandler handler;
064: private StringBuffer filelist;
065: private int fileNum = 0;
066: private int doneFileNum = 0;
067: private boolean debug = false;
068:
069: private static final String EXISTING_HEADER = "Following files exist in perforce";
070: private static final String NONEXISTING_HEADER = "Following files do not exist in perforce";
071:
072: /**
073: * Sets the filter that one wants applied.
074: * <table>
075: * <tr><th>Option</th><th>Meaning</th></tr>
076: * <tr><td>all</td><td>all files under Perforce control or not</td></tr>
077: * <tr><td>existing</td><td>only files under Perforce control</td></tr>
078: * <tr><td>non-existing</td><td>only files not under Perforce control or not</td></tr>
079: * </table>
080: * @param filter should be one of all|existing|non-existing.
081: */
082: public void setShowFilter(String filter) {
083: if (filter.equalsIgnoreCase("all")) {
084: show = SHOW_ALL;
085: } else if (filter.equalsIgnoreCase("existing")) {
086: show = SHOW_EXISTING;
087: } else if (filter.equalsIgnoreCase("non-existing")) {
088: show = SHOW_NON_EXISTING;
089: } else {
090: throw new BuildException(
091: "P4Fstat: ShowFilter should be one of: "
092: + "all, existing, non-existing");
093: }
094: }
095:
096: /**
097: * Sets optionally a change list number.
098: * @param changelist change list that one wants information about.
099: * @throws BuildException if the change list number is negative.
100: */
101: public void setChangelist(int changelist) throws BuildException {
102: if (changelist <= 0) {
103: throw new BuildException(
104: "P4FStat: Changelist# should be a "
105: + "positive number");
106: }
107: this .changelist = changelist;
108: }
109:
110: /**
111: * Adds a fileset to be examined by p4fstat.
112: * @param set the fileset to add.
113: */
114: public void addFileset(FileSet set) {
115: filesets.addElement(set);
116: }
117:
118: /**
119: * Executes the p4fstat task.
120: * @throws BuildException if no files are specified.
121: */
122: public void execute() throws BuildException {
123: handler = new FStatP4OutputHandler(this );
124: if (P4View != null) {
125: addCmd = P4View;
126: }
127: P4CmdOpts = (changelist > 0) ? ("-c " + changelist) : "";
128:
129: filelist = new StringBuffer();
130:
131: for (int i = 0; i < filesets.size(); i++) {
132: FileSet fs = (FileSet) filesets.elementAt(i);
133: DirectoryScanner ds = fs.getDirectoryScanner(getProject());
134:
135: String[] srcFiles = ds.getIncludedFiles();
136: fileNum = srcFiles.length;
137:
138: if (srcFiles != null) {
139: for (int j = 0; j < srcFiles.length; j++) {
140: File f = new File(ds.getBasedir(), srcFiles[j]);
141: filelist.append(" ").append('"').append(
142: f.getAbsolutePath()).append('"');
143: doneFileNum++;
144: if (filelist.length() > cmdLength) {
145:
146: execP4Fstat(filelist);
147: filelist = new StringBuffer();
148: }
149: }
150: if (filelist.length() > 0) {
151: execP4Fstat(filelist);
152: }
153: } else {
154: log("No files specified to query status on!",
155: Project.MSG_WARN);
156: }
157: }
158: if (show == SHOW_ALL || show == SHOW_EXISTING) {
159: printRes(handler.getExisting(), EXISTING_HEADER);
160: }
161: if (show == SHOW_ALL || show == SHOW_NON_EXISTING) {
162: printRes(handler.getNonExisting(), NONEXISTING_HEADER);
163: }
164: }
165:
166: /**
167: * Return the number of files seen.
168: * @return the number of files seen.
169: */
170: public int getLengthOfTask() {
171: return fileNum;
172: }
173:
174: /**
175: * Return the number of passes to make.
176: * IS THIS BEING USED?
177: * @return number of passes (how many filesets).
178: */
179: int getPasses() {
180: return filesets.size();
181: }
182:
183: private void printRes(ArrayList ar, String header) {
184: log(header, Project.MSG_INFO);
185: for (int i = 0; i < ar.size(); i++) {
186: log((String) ar.get(i), Project.MSG_INFO);
187: }
188: }
189:
190: private void execP4Fstat(StringBuffer list) {
191: String l = list.substring(0);
192: if (debug) {
193: log("Executing fstat " + P4CmdOpts + " " + addCmd + l
194: + "\n", Project.MSG_INFO);
195: }
196: execP4Command("fstat " + P4CmdOpts + " " + addCmd + l, handler);
197: }
198:
199: }
|