001    package org.apache.maven.scm.provider.vss.commands.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    
022    import org.apache.maven.scm.ScmException;
023    import org.apache.maven.scm.ScmFileSet;
024    import org.apache.maven.scm.ScmResult;
025    import org.apache.maven.scm.ScmTagParameters;
026    import org.apache.maven.scm.command.tag.AbstractTagCommand;
027    import org.apache.maven.scm.command.tag.TagScmResult;
028    import org.apache.maven.scm.provider.ScmProviderRepository;
029    import org.apache.maven.scm.provider.vss.commands.VssCommandLineUtils;
030    import org.apache.maven.scm.provider.vss.commands.VssConstants;
031    import org.apache.maven.scm.provider.vss.repository.VssScmProviderRepository;
032    import org.codehaus.plexus.util.cli.CommandLineUtils;
033    import org.codehaus.plexus.util.cli.Commandline;
034    
035    /**
036     * @author <a href="mailto:matpimenta@gmail.com">Mateus Pimenta</a>
037     * @since 1.3
038     */
039    public class VssTagCommand
040        extends AbstractTagCommand
041    {
042    
043        protected ScmResult executeTagCommand( ScmProviderRepository repository, ScmFileSet fileSet, String tagName,
044                                               ScmTagParameters scmTagParameters )
045            throws ScmException
046        {
047            return executeTagCommand( repository, fileSet, tagName, scmTagParameters == null ? "" : scmTagParameters
048                .getMessage() );
049        }
050    
051    
052        /**
053         * @see org.apache.maven.scm.command.tag.AbstractTagCommand#executeTagCommand(org.apache.maven.scm.provider.ScmProviderRepository, org.apache.maven.scm.ScmFileSet, java.lang.String, java.lang.String)
054         */
055        protected ScmResult executeTagCommand( ScmProviderRepository repository, ScmFileSet fileSet, String tagName,
056                                               String message )
057            throws ScmException
058        {
059            if ( getLogger().isDebugEnabled() )
060            {
061                getLogger().debug( "executing tag command..." );
062            }
063    
064            VssScmProviderRepository repo = (VssScmProviderRepository) repository;
065    
066            Commandline cl = buildCmdLine( repo, fileSet, tagName, message );
067    
068            VssTagConsumer consumer = new VssTagConsumer( repo, getLogger() );
069    
070            //      TODO handle deleted files from VSS
071            CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
072    
073            int exitCode;
074    
075            if ( getLogger().isDebugEnabled() )
076            {
077                getLogger().debug( "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>" + cl.toString() );
078            }
079    
080            exitCode = VssCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
081    
082            if ( exitCode != 0 )
083            {
084                String error = stderr.getOutput();
085    
086                if ( getLogger().isDebugEnabled() )
087                {
088                    getLogger().debug( "VSS returns error: [" + error + "] return code: [" + exitCode + "]" );
089                }
090                if ( error.indexOf( "A writable copy of" ) < 0 )
091                {
092                    return new TagScmResult( cl.toString(), "The vss command failed.", error, false );
093                }
094                // print out the writable copy for manual handling
095                if ( getLogger().isWarnEnabled() )
096                {
097                    getLogger().warn( error );
098                }
099            }
100    
101            return new TagScmResult( cl.toString(), consumer.getUpdatedFiles() );
102        }
103    
104        public Commandline buildCmdLine( VssScmProviderRepository repo, ScmFileSet fileSet, String tagName, String message )
105            throws ScmException
106        {
107    
108            Commandline command = new Commandline();
109    
110            command.setWorkingDirectory( fileSet.getBasedir().getAbsolutePath() );
111    
112            try
113            {
114                command.addSystemEnvironment();
115            }
116            catch ( Exception e )
117            {
118                throw new ScmException( "Can't add system environment.", e );
119            }
120    
121            command.addEnvironment( "SSDIR", repo.getVssdir() );
122    
123            String ssDir = VssCommandLineUtils.getSsDir();
124    
125            command.setExecutable( ssDir + VssConstants.SS_EXE );
126    
127            command.createArg().setValue( VssConstants.COMMAND_LABEL );
128    
129            command.createArg().setValue( VssConstants.PROJECT_PREFIX + repo.getProject() );
130    
131            // User identification to get access to vss repository
132            if ( repo.getUserPassword() != null )
133            {
134                command.createArg().setValue( VssConstants.FLAG_LOGIN + repo.getUserPassword() );
135            }
136    
137            // Ignore: Do not ask for input under any circumstances.
138            command.createArg().setValue( VssConstants.FLAG_AUTORESPONSE_DEF );
139    
140            command.createArg().setValue( VssConstants.FLAG_LABEL + tagName );
141    
142            return command;
143        }
144    
145    }