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 org.apache.tools.ant.BuildException;
027: import org.apache.tools.ant.Project;
028:
029: /** Synchronize client space to a Perforce depot view.
030: *
031: * The API allows additional functionality of the "p4 sync" command
032: * (such as "p4 sync -f //...#have" or other exotic invocations).</P>
033: *
034: * <b>Example Usage:</b>
035: * <table border="1">
036: * <th>Function</th><th>Command</th>
037: * <tr><td>Sync to head using P4USER, P4PORT and P4CLIENT settings specified</td>
038: * <td><P4Sync <br>P4view="//projects/foo/main/source/..." <br>
039: * P4User="fbloggs" <br>P4Port="km01:1666" <br>P4Client="fbloggsclient" /></td></tr>
040: * <tr><td>Sync to head using P4USER, P4PORT and P4CLIENT settings defined in environment</td>
041: * <td><P4Sync P4view="//projects/foo/main/source/..." /></td></tr>
042: * <tr><td>Force a re-sync to head, refreshing all files</td>
043: * <td><P4Sync force="yes" P4view="//projects/foo/main/source/..." /></td></tr>
044: * <tr><td>Sync to a label</td><td><P4Sync label="myPerforceLabel" /></td></tr>
045: * </table>
046: *
047: * @todo Add decent label error handling for non-exsitant labels
048: *
049: * @ant.task category="scm"
050: */
051: public class P4Sync extends P4Base {
052:
053: // CheckStyle:VisibilityModifier OFF - bc
054: String label;
055: private String syncCmd = "";
056:
057: // CheckStyle:VisibilityModifier ON
058:
059: /**
060: * Label to sync client to; optional.
061: * @param label name of a label against which one want to sync
062: * @throws BuildException if label is null or empty string
063: */
064: public void setLabel(String label) throws BuildException {
065: if (label == null || label.equals("")) {
066: throw new BuildException(
067: "P4Sync: Labels cannot be Null or Empty");
068: }
069:
070: this .label = label;
071:
072: }
073:
074: /**
075: * force a refresh of files, if this attribute is set; false by default.
076: * @param force sync all files, whether they are supposed to be already uptodate or not.
077: * @throws BuildException if a label is set and force is null
078: */
079: public void setForce(String force) throws BuildException {
080: if (force == null && !label.equals("")) {
081: throw new BuildException(
082: "P4Sync: If you want to force, set force to non-null string!");
083: }
084: P4CmdOpts = "-f";
085: }
086:
087: /**
088: * do the work
089: * @throws BuildException if an error occurs during the execution of the Perforce command
090: * and failOnError is set to true
091: */
092: public void execute() throws BuildException {
093:
094: if (P4View != null) {
095: syncCmd = P4View;
096: }
097:
098: if (label != null && !label.equals("")) {
099: syncCmd = syncCmd + "@" + label;
100: }
101:
102: log("Execing sync " + P4CmdOpts + " " + syncCmd,
103: Project.MSG_VERBOSE);
104:
105: execP4Command("-s sync " + P4CmdOpts + " " + syncCmd,
106: new SimpleP4OutputHandler(this));
107: }
108: }
|