001package org.apache.maven.scm.provider.perforce.command.edit;
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.edit.AbstractEditCommand;
026import org.apache.maven.scm.command.edit.EditScmResult;
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.io.IOException;
037import java.util.List;
038
039/**
040 * @author Mike Perham
041 *
042 */
043public class PerforceEditCommand
044    extends AbstractEditCommand
045    implements PerforceCommand
046{
047    /**
048     * {@inheritDoc}
049     */
050    @Override
051    protected ScmResult executeEditCommand( ScmProviderRepository repo, ScmFileSet files )
052        throws ScmException
053    {
054        Commandline cl = createCommandLine( (PerforceScmProviderRepository) repo, files.getBasedir(), files );
055        PerforceEditConsumer consumer = new PerforceEditConsumer();
056        try
057        {
058            if ( getLogger().isDebugEnabled() )
059            {
060                getLogger().debug( PerforceScmProvider.clean( "Executing " + cl.toString() ) );
061            }
062
063            CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
064            int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err );
065
066            if ( exitCode != 0 )
067            {
068                String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
069
070                StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
071                msg.append( '\n' );
072                msg.append( "Command line was:" + cmdLine );
073
074                throw new CommandLineException( msg.toString() );
075            }
076        }
077        catch ( CommandLineException e )
078        {
079            if ( getLogger().isErrorEnabled() )
080            {
081                getLogger().error( "CommandLineException " + e.getMessage(), e );
082            }
083        }
084
085        if ( consumer.isSuccess() )
086        {
087            return new EditScmResult( cl.toString(), consumer.getEdits() );
088        }
089
090        return new EditScmResult( cl.toString(), "Unable to edit file(s)", consumer.getErrorMessage(), false );
091    }
092
093    public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory,
094                                                 ScmFileSet files )
095        throws ScmException
096    {
097        Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
098
099        command.createArg().setValue( "edit" );
100
101        try
102        {
103            String candir = workingDirectory.getCanonicalPath();
104
105            for ( File f : files.getFileList() )
106            {
107                File file = null;
108                if ( f.isAbsolute() )
109                {
110                    file = new File( f.getPath() );
111                }
112                else
113                {
114                    file = new File( workingDirectory, f.getPath() );
115                }
116                // I want to use relative paths to add files to make testing
117                // simpler.
118                // Otherwise the absolute path will be different on everyone's
119                // machine
120                // and testing will be a little more painful.
121                String canfile = file.getCanonicalPath();
122                if ( canfile.startsWith( candir ) )
123                {
124                    canfile = canfile.substring( candir.length() + 1 );
125                }
126                command.createArg().setValue( canfile );
127            }
128        }
129        catch ( IOException e )
130        {
131            throw new ScmException( e.getMessage(), e );
132        }
133        return command;
134    }
135}