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