001package org.apache.maven.scm.provider.vss.commands.add;
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.command.add.AbstractAddCommand;
026import org.apache.maven.scm.command.add.AddScmResult;
027import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
028import org.apache.maven.scm.provider.ScmProviderRepository;
029import org.apache.maven.scm.provider.vss.commands.VssCommandLineUtils;
030import org.apache.maven.scm.provider.vss.commands.VssConstants;
031import org.apache.maven.scm.provider.vss.repository.VssScmProviderRepository;
032import org.codehaus.plexus.util.cli.CommandLineUtils;
033import org.codehaus.plexus.util.cli.Commandline;
034
035/**
036 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
037 *
038 */
039public class VssAddCommand
040    extends AbstractAddCommand
041{
042    /** {@inheritDoc} */
043    protected ScmResult executeAddCommand( ScmProviderRepository repository, ScmFileSet fileSet, String message,
044                                           boolean binary )
045        throws ScmException
046    {
047        VssScmProviderRepository repo = (VssScmProviderRepository) repository;
048
049        if ( fileSet.getFileList().isEmpty() )
050        {
051            throw new ScmException( "You must provide at least one file/directory to add" );
052        }
053
054        Commandline cl = buildCmdLine( repo, fileSet );
055
056        VssAddConsumer consumer = new VssAddConsumer( getLogger() );
057
058        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
059
060        if ( getLogger().isInfoEnabled() )
061        {
062            getLogger().info( "Executing: " + cl );
063            getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
064        }
065
066        int exitCode = VssCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
067
068        if ( exitCode != 0 )
069        {
070            return new ChangeLogScmResult( cl.toString(), "The vss command failed.", stderr.getOutput(), false );
071        }
072
073        return new AddScmResult( cl.toString(), consumer.getAddedFiles() );
074    }
075
076    public Commandline buildCmdLine( VssScmProviderRepository repo, ScmFileSet fileSet )
077        throws ScmException
078    {
079        Commandline command = new Commandline();
080
081        command.setWorkingDirectory( fileSet.getBasedir().getAbsolutePath() );
082
083        try
084        {
085            command.addSystemEnvironment();
086        }
087        catch ( Exception e )
088        {
089            throw new ScmException( "Can't add system environment.", e );
090        }
091
092        command.addEnvironment( "SSDIR", repo.getVssdir() );
093
094        String ssDir = VssCommandLineUtils.getSsDir();
095
096        command.setExecutable( ssDir + VssConstants.SS_EXE );
097
098        command.createArg().setValue( VssConstants.COMMAND_ADD );
099
100        VssCommandLineUtils.addFiles( command, fileSet );
101
102        //        command.createArg().setValue( VssConstants.PROJECT_PREFIX + repo.getProject() );
103
104        //User identification to get access to vss repository
105        if ( repo.getUserPassword() != null )
106        {
107            command.createArg().setValue( VssConstants.FLAG_LOGIN + repo.getUserPassword() );
108        }
109
110        //Ignore: Do not ask for input under any circumstances.
111        command.createArg().setValue( VssConstants.FLAG_AUTORESPONSE_DEF );
112
113        return command;
114    }
115
116    public Commandline buildSetCurrentProjectCmdLine( VssScmProviderRepository repo )
117        throws ScmException
118    {
119        Commandline command = new Commandline();
120
121        try
122        {
123            command.addSystemEnvironment();
124        }
125        catch ( Exception e )
126        {
127            throw new ScmException( "Can't add system environment.", e );
128        }
129
130        command.addEnvironment( "SSDIR", repo.getVssdir() );
131
132        String ssDir = VssCommandLineUtils.getSsDir();
133
134        command.setExecutable( ssDir + VssConstants.SS_EXE );
135
136        command.createArg().setValue( VssConstants.COMMAND_CP );
137
138        command.createArg().setValue( VssConstants.PROJECT_PREFIX + repo.getProject() );
139
140        //User identification to get access to vss repository
141        if ( repo.getUserPassword() != null )
142        {
143            command.createArg().setValue( VssConstants.FLAG_LOGIN + repo.getUserPassword() );
144        }
145
146        //Ignore: Do not ask for input under any circumstances.
147        command.createArg().setValue( VssConstants.FLAG_AUTORESPONSE_DEF );
148
149        return command;
150    }
151
152}