001    package org.apache.maven.scm.provider.tfs.command;
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    
022    import java.io.File;
023    import java.util.ArrayList;
024    import java.util.List;
025    
026    import org.apache.maven.scm.ScmException;
027    import org.apache.maven.scm.ScmFile;
028    import org.apache.maven.scm.ScmFileSet;
029    import org.apache.maven.scm.ScmFileStatus;
030    import org.apache.maven.scm.ScmResult;
031    import org.apache.maven.scm.ScmTagParameters;
032    import org.apache.maven.scm.command.tag.AbstractTagCommand;
033    import org.apache.maven.scm.command.tag.TagScmResult;
034    import org.apache.maven.scm.provider.ScmProviderRepository;
035    import org.apache.maven.scm.provider.tfs.TfsScmProviderRepository;
036    import org.apache.maven.scm.provider.tfs.command.consumer.ErrorStreamConsumer;
037    import org.codehaus.plexus.util.cli.CommandLineUtils.StringStreamConsumer;
038    
039    /**
040     * @author Olivier Lamy
041     *
042     */
043    public class TfsTagCommand
044        extends AbstractTagCommand
045    {
046    
047        protected ScmResult executeTagCommand( ScmProviderRepository r, ScmFileSet f, String tag, String message )
048            throws ScmException
049        {
050            return executeTagCommand( r, f, tag, new ScmTagParameters( message ) );
051        }
052    
053        protected ScmResult executeTagCommand( ScmProviderRepository r, ScmFileSet f, String tag,
054                                               ScmTagParameters scmTagParameters )
055            throws ScmException
056        {
057            TfsCommand command = createCommand( r, f, tag, scmTagParameters );
058    
059            StringStreamConsumer out = new StringStreamConsumer();
060            ErrorStreamConsumer err = new ErrorStreamConsumer();
061    
062            int status = command.execute( out, err );
063            if ( status != 0 || err.hasBeenFed() )
064            {
065                return new TagScmResult( command.getCommandString(), "Error code for TFS label command - " + status,
066                                         err.getOutput(), false );
067            }
068            List<ScmFile> files = new ArrayList<ScmFile>(f.getFileList().size());
069            for (File file : f.getFileList() )
070            {
071                files.add( new ScmFile( file.getPath(), ScmFileStatus.TAGGED ) );
072            }
073            return new TagScmResult( command.getCommandString(), files );
074    
075        }
076    
077        public TfsCommand createCommand( ScmProviderRepository r, ScmFileSet f, String tag,
078                                            ScmTagParameters scmTagParameters )
079        {
080            TfsScmProviderRepository tfsRepo = (TfsScmProviderRepository) r;
081            String url = tfsRepo.getServerPath();
082    
083            TfsCommand command = new TfsCommand( "label", r, f, getLogger() );
084            command.addArgument( tag );
085            command.addArgument( url );
086            command.addArgument( "-recursive" );
087            command.addArgument( "-child:replace" );
088            String message = scmTagParameters.getMessage();
089            if ( message != null && !message.equals( "" ) )
090            {
091                command.addArgument( "-comment:" + message );
092            }
093            return command;
094        }
095    
096    }