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