001package org.apache.maven.scm.provider.perforce.command.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 java.io.File;
023
024import org.apache.maven.scm.ScmException;
025import org.apache.maven.scm.ScmFileSet;
026import org.apache.maven.scm.ScmResult;
027import org.apache.maven.scm.command.add.AbstractAddCommand;
028import org.apache.maven.scm.command.add.AddScmResult;
029import org.apache.maven.scm.provider.ScmProviderRepository;
030import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
031import org.apache.maven.scm.provider.perforce.command.PerforceCommand;
032import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
033import org.codehaus.plexus.util.cli.CommandLineException;
034import org.codehaus.plexus.util.cli.CommandLineUtils;
035import org.codehaus.plexus.util.cli.Commandline;
036
037/**
038 * @author Mike Perham
039 *
040 */
041public class PerforceAddCommand
042    extends AbstractAddCommand
043    implements PerforceCommand
044{
045    /** {@inheritDoc} */
046    protected ScmResult executeAddCommand( ScmProviderRepository repo, ScmFileSet files, String message,
047                                           boolean binary )
048        throws ScmException
049    {
050        Commandline cl = createCommandLine( (PerforceScmProviderRepository) repo, files.getBasedir(), files );
051        PerforceAddConsumer consumer = new PerforceAddConsumer();
052        try
053        {
054            CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
055            int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err );
056
057            if ( exitCode != 0 )
058            {
059                String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
060
061                StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
062                msg.append( '\n' );
063                msg.append( "Command line was:" + cmdLine );
064
065                throw new CommandLineException( msg.toString() );
066            }
067        }
068        catch ( CommandLineException e )
069        {
070            if ( getLogger().isErrorEnabled() )
071            {
072                getLogger().error( "CommandLineException " + e.getMessage(), e );
073            }
074        }
075
076        return new AddScmResult( cl.toString(), consumer.getAdditions() );
077    }
078
079    public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory,
080                                                 ScmFileSet files )
081    {
082        Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
083        command.createArg().setValue( "add" );
084
085        for ( File file : files.getFileList() )
086        {
087            command.createArg().setValue( file.getName() );
088        }
089        return command;
090    }
091}