View Javadoc
1   package org.apache.maven.scm.provider.perforce.command.remove;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.ScmException;
23  import org.apache.maven.scm.ScmFileSet;
24  import org.apache.maven.scm.ScmResult;
25  import org.apache.maven.scm.command.remove.AbstractRemoveCommand;
26  import org.apache.maven.scm.command.remove.RemoveScmResult;
27  import org.apache.maven.scm.provider.ScmProviderRepository;
28  import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
29  import org.apache.maven.scm.provider.perforce.command.PerforceCommand;
30  import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
31  import org.codehaus.plexus.util.cli.CommandLineException;
32  import org.codehaus.plexus.util.cli.CommandLineUtils;
33  import org.codehaus.plexus.util.cli.Commandline;
34  
35  import java.io.File;
36  import java.util.List;
37  
38  /**
39   * @author Mike Perham
40   * @author Olivier Lamy
41   *
42   */
43  public class PerforceRemoveCommand
44      extends AbstractRemoveCommand
45      implements PerforceCommand
46  {
47      /**
48       * {@inheritDoc}
49       */
50      protected ScmResult executeRemoveCommand( ScmProviderRepository repo, ScmFileSet files, String message )
51          throws ScmException
52      {
53          Commandline cl = createCommandLine( (PerforceScmProviderRepository) repo, files.getBasedir(), files );
54          PerforceRemoveConsumer consumer = new PerforceRemoveConsumer();
55          try
56          {
57              CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
58              int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err );
59  
60              if ( exitCode != 0 )
61              {
62                  String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
63  
64                  StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
65                  msg.append( '\n' );
66                  msg.append( "Command line was:" + cmdLine );
67  
68                  throw new CommandLineException( msg.toString() );
69              }
70          }
71          catch ( CommandLineException e )
72          {
73              throw new ScmException( "CommandLineException " + e.getMessage(), e );
74  
75          }
76  
77          return new RemoveScmResult( cl.toString(), consumer.getRemovals() );
78      }
79  
80      public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory,
81                                                   ScmFileSet files )
82      {
83          Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
84          command.createArg().setValue( "delete" );
85  
86          List<File> fs = files.getFileList();
87          for ( int i = 0; i < fs.size(); i++ )
88          {
89              File file = (File) fs.get( i );
90              command.createArg().setValue( file.getName() );
91          }
92          return command;
93      }
94  }