001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2007, ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol.bootstrappers;
037:
038: import net.sourceforge.cruisecontrol.Bootstrapper;
039: import net.sourceforge.cruisecontrol.CruiseControlException;
040: import net.sourceforge.cruisecontrol.util.Commandline;
041: import net.sourceforge.cruisecontrol.util.ValidationHelper;
042:
043: import org.apache.log4j.Logger;
044:
045: import java.io.File;
046:
047: /**
048: * The MercurialBootstrapper will handle updating the local working repository before the build begins.
049: *
050: * @author <a href="jerome@coffeebreaks.org">Jerome Lacoste</a>
051: * @see <a href="http://www.selenic.com/mercurial">Mercurial web site</a>
052: */
053: public class MercurialBootstrapper implements Bootstrapper {
054: private static final Logger LOG = Logger
055: .getLogger(SVNBootstrapper.class);
056:
057: /** Configuration parameters */
058: private String localWorkingCopy;
059:
060: /**
061: * Sets the local working copy to use when making calls to Mercurial.
062: *
063: * @param localWorkingCopy
064: * String indicating the relative or absolute path to the local working copy of the Mercurial repository
065: * on which to execute the update command.
066: */
067: public void setLocalWorkingCopy(String localWorkingCopy) {
068: this .localWorkingCopy = localWorkingCopy;
069: }
070:
071: /**
072: * This method validates that at least the filename or the local working copy location has been specified.
073: *
074: * @throws net.sourceforge.cruisecontrol.CruiseControlException
075: * Thrown when the repository location and the local working copy location are both null
076: */
077: public void validate() throws CruiseControlException {
078: ValidationHelper
079: .assertTrue(localWorkingCopy != null,
080: "'localWorkingCopy' is a required attribute on the Mercurial bootstrapper task");
081:
082: if (localWorkingCopy != null) {
083: File workingDir = new File(localWorkingCopy);
084: ValidationHelper.assertTrue(workingDir.exists()
085: && workingDir.isDirectory(),
086: "'localWorkingCopy' must be an existing directory. Was"
087: + workingDir.getAbsolutePath());
088: }
089: }
090:
091: /**
092: * Update from the mercurial repository.
093: *
094: * @throws net.sourceforge.cruisecontrol.CruiseControlException
095: */
096: public void bootstrap() throws CruiseControlException {
097: buildPullUpdateCommand().executeAndWait(LOG);
098: }
099:
100: /**
101: * Generates the command line for the mercurial pull update command.
102: *
103: * For example:
104: *
105: * 'hg pull -u'
106: *
107: * @return the command line for the mercurial pull update command
108: * @throws net.sourceforge.cruisecontrol.CruiseControlException
109: * if the working directory is not valid.
110: */
111: Commandline buildPullUpdateCommand() throws CruiseControlException {
112: Commandline command = new Commandline();
113: command.setExecutable("hg");
114:
115: if (localWorkingCopy != null) {
116: command.setWorkingDirectory(localWorkingCopy);
117: }
118:
119: command.createArgument("pull");
120: command.createArgument("-u");
121:
122: LOG.debug("Executing command = " + command);
123:
124: return command;
125: }
126: }
|