source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/optional/perforce/P4Label.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 5.3 KB
Line 
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
23package org.apache.tools.ant.taskdefs.optional.perforce;
24
25import java.text.SimpleDateFormat;
26import java.util.Date;
27import org.apache.tools.ant.BuildException;
28import org.apache.tools.ant.Project;
29import org.apache.tools.ant.util.StringUtils;
30
31/**
32 * Creates a new Perforce label and set contents to reflect current
33 * client file revisions.
34 *
35 * Label name defaults to AntLabel if none set.
36 *
37 * Example Usage:
38 * <pre>
39 * &lt;P4Label name="MyLabel-${TSTAMP}-${DSTAMP}" desc="Auto Build Label" /&gt;
40 * </pre>
41 *
42 *
43 * @ant.task category="scm"
44 */
45public class P4Label extends P4Base {
46
47 protected String name;
48 protected String desc;
49 protected String lock;
50
51 /**
52 * The name of the label; optional, default "AntLabel"
53 * @param name the name of the label
54 */
55 public void setName(String name) {
56 this.name = name;
57 }
58
59 /**
60 *Label Description; optional
61 * @param desc description of the label
62 */
63 public void setDesc(String desc) {
64 this.desc = desc;
65 }
66
67 /**
68 * when set to "locked", Perforce will lock the label once created; optional.
69 * @param lock only admissible value "locked"
70 */
71 public void setLock(String lock) {
72 this.lock = lock;
73 }
74
75 /**
76 * do the work
77 * @throws BuildException if failonerror has been set to true and Perforce fails
78 */
79 public void execute() throws BuildException {
80 log("P4Label exec:", Project.MSG_INFO);
81
82 if (P4View == null || P4View.length() < 1) {
83 log("View not set, assuming //depot/...", Project.MSG_WARN);
84 P4View = "//depot/...";
85 } else {
86 P4View = StringUtils.replace(P4View, ":", "\n\t");
87 P4View = StringUtils.replace(P4View, ";", "\n\t");
88 }
89
90 if (desc == null || desc.length() < 1) {
91 log("Label Description not set, assuming 'AntLabel'",
92 Project.MSG_WARN);
93 desc = "AntLabel";
94 }
95
96 if (lock != null && !lock.equalsIgnoreCase("locked")) {
97 log("lock attribute invalid - ignoring", Project.MSG_WARN);
98 }
99
100 if (name == null || name.length() < 1) {
101 SimpleDateFormat formatter
102 = new SimpleDateFormat("yyyy.MM.dd-hh:mm");
103 Date now = new Date();
104 name = "AntLabel-" + formatter.format(now);
105 log("name not set, assuming '" + name + "'", Project.MSG_WARN);
106 }
107
108
109 //We have to create a unlocked label first
110 String newLabel =
111 "Label: " + name
112 + "\nDescription: " + desc
113 + "\nOptions: unlocked"
114 + "\nView: \n\t" + P4View;
115
116 P4Handler handler = new P4HandlerAdapter() {
117 public void process(String line) {
118 log(line, Project.MSG_VERBOSE);
119 }
120 };
121
122 handler.setOutput(newLabel);
123
124 execP4Command("label -i", handler);
125
126 execP4Command("labelsync -l " + name, new P4HandlerAdapter() {
127 public void process(String line) {
128 log(line, Project.MSG_VERBOSE);
129 }
130 });
131
132
133 log("Created Label " + name + " (" + desc + ") with view:\n" + P4View,
134 Project.MSG_INFO);
135
136 //Now lock if required
137 if (lock != null && lock.equalsIgnoreCase("locked")) {
138
139 log("Modifying lock status to 'locked'", Project.MSG_INFO);
140
141 final StringBuffer labelSpec = new StringBuffer();
142
143 //Read back the label spec from perforce,
144 //Replace Options
145 //Submit back to Perforce
146
147 handler = new P4HandlerAdapter() {
148 public void process(String line) {
149 log(line, Project.MSG_VERBOSE);
150
151 if (util.match("/^Options:/", line)) {
152 line = "Options: " + lock;
153 }
154
155 labelSpec.append(line + "\n");
156 }
157 };
158
159
160 execP4Command("label -o " + name, handler);
161 log(labelSpec.toString(), Project.MSG_DEBUG);
162
163 log("Now locking label...", Project.MSG_VERBOSE);
164 handler = new P4HandlerAdapter() {
165 public void process(String line) {
166 log(line, Project.MSG_VERBOSE);
167 }
168 };
169
170 handler.setOutput(labelSpec.toString());
171 execP4Command("label -i", handler);
172 }
173 }
174}
Note: See TracBrowser for help on using the repository browser.