001package org.apache.maven.scm.provider.starteam.command.tag;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.scm.ScmException;
023import org.apache.maven.scm.ScmFileSet;
024import org.apache.maven.scm.ScmResult;
025import org.apache.maven.scm.ScmTagParameters;
026import org.apache.maven.scm.command.tag.AbstractTagCommand;
027import org.apache.maven.scm.command.tag.TagScmResult;
028import org.apache.maven.scm.provider.ScmProviderRepository;
029import org.apache.maven.scm.provider.starteam.command.StarteamCommand;
030import org.apache.maven.scm.provider.starteam.command.StarteamCommandLineUtils;
031import org.apache.maven.scm.provider.starteam.repository.StarteamScmProviderRepository;
032import org.codehaus.plexus.util.cli.CommandLineUtils;
033import org.codehaus.plexus.util.cli.Commandline;
034
035import java.io.File;
036
037/**
038 * @author <a href="mailto:dantran@gmail.com">Dan T. Tran</a>
039 * @author Olivier Lamy
040 *
041 */
042public class StarteamTagCommand
043    extends AbstractTagCommand
044    implements StarteamCommand
045{
046    // ----------------------------------------------------------------------
047    // AbstractTagCommand Implementation
048    // ----------------------------------------------------------------------
049
050    protected ScmResult executeTagCommand( ScmProviderRepository repo, ScmFileSet fileSet, String tag, String message )
051        throws ScmException
052    {
053        return executeTagCommand( repo, fileSet, tag, new ScmTagParameters( message ) );
054    }
055    
056    /** {@inheritDoc} */
057    protected ScmResult executeTagCommand( ScmProviderRepository repo, ScmFileSet fileSet, String tag,
058                                           ScmTagParameters scmTagParameters )
059        throws ScmException
060    {
061        if ( fileSet.getFileList().isEmpty() )
062        {
063            throw new ScmException( "This provider doesn't support tagging subsets of a directory" );
064        }
065
066        if ( tag == null || tag.trim().length() == 0 )
067        {
068            throw new ScmException( "tag must be specified" );
069        }
070
071        if ( getLogger().isInfoEnabled() )
072        {
073            getLogger().info( "Working directory: " + fileSet.getBasedir().getAbsolutePath() );
074        }
075
076        StarteamScmProviderRepository repository = (StarteamScmProviderRepository) repo;
077
078        StarteamTagConsumer consumer = new StarteamTagConsumer( getLogger() );
079
080        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
081
082        Commandline cl = createCommandLine( repository, fileSet.getBasedir(), tag );
083
084        int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
085
086        if ( exitCode != 0 )
087        {
088            return new TagScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(), false );
089        }
090
091        return new TagScmResult( cl.toString(), consumer.getTaggedFiles() );
092    }
093
094    // ----------------------------------------------------------------------
095    //
096    // ----------------------------------------------------------------------
097
098    public static Commandline createCommandLine( StarteamScmProviderRepository repo, File workingDirectory, String tag )
099        throws ScmException
100    {
101        Commandline cl = StarteamCommandLineUtils.createStarteamBaseCommandLine( "label", repo );
102
103        cl.createArg().setValue( "-p" );
104
105        cl.createArg().setValue( repo.getFullUrl() );
106
107        cl.createArg().setValue( "-nl" );
108
109        cl.createArg().setValue( tag );
110
111        cl.createArg().setValue( "-b" );
112
113        return cl;
114    }
115}