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 |
|
---|
23 | package org.apache.tools.ant.taskdefs.optional.perforce;
|
---|
24 |
|
---|
25 | import org.apache.tools.ant.BuildException;
|
---|
26 | import org.apache.tools.ant.Project;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Requests a new changelist from the Perforce server.
|
---|
30 | * P4Change creates a new changelist in perforce. P4Change sets the property
|
---|
31 | * ${p4.change} with the new changelist number. This should then be passed into
|
---|
32 | * p4edit and p4submit.
|
---|
33 | *
|
---|
34 | *
|
---|
35 | * @see P4Edit
|
---|
36 | * @see P4Submit
|
---|
37 | *
|
---|
38 | * @ant.task category="scm"
|
---|
39 | */
|
---|
40 | public class P4Change extends P4Base {
|
---|
41 |
|
---|
42 | protected String emptyChangeList = null;
|
---|
43 | protected String description = "AutoSubmit By Ant";
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * creates a new Perforce change list
|
---|
47 | * sets the p4.change property to the number of the new change list
|
---|
48 | * @throws BuildException if the word error appears in the output coming from Perforce
|
---|
49 | */
|
---|
50 | public void execute() throws BuildException {
|
---|
51 |
|
---|
52 | if (emptyChangeList == null) {
|
---|
53 | emptyChangeList = getEmptyChangeList();
|
---|
54 | }
|
---|
55 | final Project myProj = getProject();
|
---|
56 |
|
---|
57 | P4Handler handler = new P4HandlerAdapter() {
|
---|
58 | public void process(String line) {
|
---|
59 | if (util.match("/Change/", line)) {
|
---|
60 |
|
---|
61 | //Remove any non-numerical chars - should leave the change number
|
---|
62 | line = util.substitute("s/[^0-9]//g", line);
|
---|
63 |
|
---|
64 | int changenumber = Integer.parseInt(line);
|
---|
65 | log("Change Number is " + changenumber, Project.MSG_INFO);
|
---|
66 | myProj.setProperty("p4.change", "" + changenumber);
|
---|
67 |
|
---|
68 | } else if (util.match("/error/", line)) {
|
---|
69 | throw new BuildException("Perforce Error, check client settings and/or server");
|
---|
70 | }
|
---|
71 |
|
---|
72 | }
|
---|
73 | };
|
---|
74 |
|
---|
75 | handler.setOutput(emptyChangeList);
|
---|
76 |
|
---|
77 | execP4Command("change -i", handler);
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * returns the text of an empty change list
|
---|
82 | * @return the text of an empty change list
|
---|
83 | * @throws BuildException if the text error is displayed
|
---|
84 | * in the Perforce output outside of a comment line
|
---|
85 | */
|
---|
86 | public String getEmptyChangeList() throws BuildException {
|
---|
87 | final StringBuffer stringbuf = new StringBuffer();
|
---|
88 |
|
---|
89 | execP4Command("change -o", new P4HandlerAdapter() {
|
---|
90 | public void process(String line) {
|
---|
91 | if (!util.match("/^#/", line)) {
|
---|
92 | if (util.match("/error/", line)) {
|
---|
93 | log("Client Error", Project.MSG_VERBOSE);
|
---|
94 | throw new BuildException("Perforce Error, "
|
---|
95 | + "check client settings and/or server");
|
---|
96 | } else if (util.match("/<enter description here>/", line)) {
|
---|
97 | // we need to escape the description in case there are /
|
---|
98 | description = backslash(description);
|
---|
99 | line = util.substitute("s/<enter description here>/"
|
---|
100 | + description + "/", line);
|
---|
101 | } else if (util.match("/\\/\\//", line)) {
|
---|
102 | //Match "//" for begining of depot filespec
|
---|
103 | return;
|
---|
104 | }
|
---|
105 | stringbuf.append(line);
|
---|
106 | stringbuf.append("\n");
|
---|
107 | }
|
---|
108 | }
|
---|
109 | });
|
---|
110 | return stringbuf.toString();
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Ensure that a string is backslashing slashes so that it does not
|
---|
115 | * confuse them with Perl substitution delimiter in Oro. Backslashes are
|
---|
116 | * always backslashes in a string unless they escape the delimiter.
|
---|
117 | * @param value the string to backslash for slashes
|
---|
118 | * @return the backslashed string
|
---|
119 | * @see <a href="http://jakarta.apache.org/oro/api/org/apache/oro/text/perl/Perl5Util.html
|
---|
120 | * #substitute(java.lang.String,%20java.lang.String)">Oro</a>
|
---|
121 | */
|
---|
122 | public static final String backslash(String value) {
|
---|
123 | final StringBuffer buf = new StringBuffer(value.length());
|
---|
124 | final int len = value.length();
|
---|
125 | for (int i = 0; i < len; i++) {
|
---|
126 | char c = value.charAt(i);
|
---|
127 | if (c == '/') {
|
---|
128 | buf.append('\\');
|
---|
129 | }
|
---|
130 | buf.append(c);
|
---|
131 | }
|
---|
132 | return buf.toString();
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Description for ChangeList;optional.
|
---|
137 | * If none is specified, it will default to "AutoSubmit By Ant"
|
---|
138 | * @param desc description for the change list
|
---|
139 | */
|
---|
140 | public void setDescription(String desc) {
|
---|
141 | this.description = desc;
|
---|
142 | }
|
---|
143 |
|
---|
144 | } //EoF
|
---|