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.text.SimpleDateFormat;
027: import java.util.Date;
028: import org.apache.tools.ant.BuildException;
029: import org.apache.tools.ant.Project;
030: import org.apache.tools.ant.util.StringUtils;
031:
032: /**
033: * Creates a new Perforce label and set contents to reflect current
034: * client file revisions.
035: *
036: * Label name defaults to AntLabel if none set.
037: *
038: * Example Usage:
039: * <pre>
040: * <P4Label name="MyLabel-${TSTAMP}-${DSTAMP}" desc="Auto Build Label" />
041: * </pre>
042: *
043: * @ant.task category="scm"
044: */
045: public class P4Label extends P4Base {
046:
047: // CheckStyle:VisibilityModifier OFF - bc
048: protected String name;
049: protected String desc;
050: protected String lock;
051:
052: // CheckStyle:VisibilityModifier ON
053:
054: /**
055: * The name of the label; optional, default "AntLabel"
056: * @param name the name of the label
057: */
058: public void setName(String name) {
059: this .name = name;
060: }
061:
062: /**
063: *Label Description; optional
064: * @param desc description of the label
065: */
066: public void setDesc(String desc) {
067: this .desc = desc;
068: }
069:
070: /**
071: * when set to "locked", Perforce will lock the label once created; optional.
072: * @param lock only admissible value "locked"
073: */
074: public void setLock(String lock) {
075: this .lock = lock;
076: }
077:
078: /**
079: * do the work
080: * @throws BuildException if failonerror has been set to true and Perforce fails
081: */
082: public void execute() throws BuildException {
083: log("P4Label exec:", Project.MSG_INFO);
084:
085: if (P4View == null || P4View.length() < 1) {
086: log("View not set, assuming //depot/...", Project.MSG_WARN);
087: P4View = "//depot/...";
088: } else {
089: P4View = StringUtils.replace(P4View, ":", "\n\t");
090: P4View = StringUtils.replace(P4View, ";", "\n\t");
091: }
092:
093: if (desc == null || desc.length() < 1) {
094: log("Label Description not set, assuming 'AntLabel'",
095: Project.MSG_WARN);
096: desc = "AntLabel";
097: }
098:
099: if (lock != null && !lock.equalsIgnoreCase("locked")) {
100: log("lock attribute invalid - ignoring", Project.MSG_WARN);
101: }
102:
103: if (name == null || name.length() < 1) {
104: SimpleDateFormat formatter = new SimpleDateFormat(
105: "yyyy.MM.dd-hh:mm");
106: Date now = new Date();
107: name = "AntLabel-" + formatter.format(now);
108: log("name not set, assuming '" + name + "'",
109: Project.MSG_WARN);
110: }
111:
112: //We have to create a unlocked label first
113: String newLabel = "Label: " + name + "\nDescription: " + desc
114: + "\nOptions: unlocked" + "\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: log("Created Label " + name + " (" + desc + ") with view:\n"
133: + P4View, Project.MSG_INFO);
134:
135: //Now lock if required
136: if (lock != null && lock.equalsIgnoreCase("locked")) {
137:
138: log("Modifying lock status to 'locked'", Project.MSG_INFO);
139:
140: final StringBuffer labelSpec = new StringBuffer();
141:
142: //Read back the label spec from perforce,
143: //Replace Options
144: //Submit back to Perforce
145:
146: handler = new P4HandlerAdapter() {
147: public void process(String line) {
148: log(line, Project.MSG_VERBOSE);
149:
150: if (util.match("/^Options:/", line)) {
151: line = "Options: " + lock;
152: }
153:
154: labelSpec.append(line + "\n");
155: }
156: };
157:
158: execP4Command("label -o " + name, handler);
159: log(labelSpec.toString(), Project.MSG_DEBUG);
160:
161: log("Now locking label...", Project.MSG_VERBOSE);
162: handler = new P4HandlerAdapter() {
163: public void process(String line) {
164: log(line, Project.MSG_VERBOSE);
165: }
166: };
167:
168: handler.setOutput(labelSpec.toString());
169: execP4Command("label -i", handler);
170: }
171: }
172: }
|