View Javadoc

1   package org.apache.maven.scm.provider.perforce.command.add;
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 java.io.File;
23  
24  import org.apache.maven.scm.ScmException;
25  import org.apache.maven.scm.ScmFileSet;
26  import org.apache.maven.scm.ScmResult;
27  import org.apache.maven.scm.command.add.AbstractAddCommand;
28  import org.apache.maven.scm.command.add.AddScmResult;
29  import org.apache.maven.scm.provider.ScmProviderRepository;
30  import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
31  import org.apache.maven.scm.provider.perforce.command.PerforceCommand;
32  import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
33  import org.codehaus.plexus.util.cli.CommandLineException;
34  import org.codehaus.plexus.util.cli.CommandLineUtils;
35  import org.codehaus.plexus.util.cli.Commandline;
36  
37  /**
38   * @author Mike Perham
39   *
40   */
41  public class PerforceAddCommand
42      extends AbstractAddCommand
43      implements PerforceCommand
44  {
45      /** {@inheritDoc} */
46      protected ScmResult executeAddCommand( ScmProviderRepository repo, ScmFileSet files, String message,
47                                             boolean binary )
48          throws ScmException
49      {
50          Commandline cl = createCommandLine( (PerforceScmProviderRepository) repo, files.getBasedir(), files );
51          PerforceAddConsumer consumer = new PerforceAddConsumer();
52          try
53          {
54              CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
55              int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err );
56  
57              if ( exitCode != 0 )
58              {
59                  String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
60  
61                  StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
62                  msg.append( '\n' );
63                  msg.append( "Command line was:" + cmdLine );
64  
65                  throw new CommandLineException( msg.toString() );
66              }
67          }
68          catch ( CommandLineException e )
69          {
70              if ( getLogger().isErrorEnabled() )
71              {
72                  getLogger().error( "CommandLineException " + e.getMessage(), e );
73              }
74          }
75  
76          return new AddScmResult( cl.toString(), consumer.getAdditions() );
77      }
78  
79      public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory,
80                                                   ScmFileSet files )
81      {
82          Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
83          command.createArg().setValue( "add" );
84  
85          for ( File file : files.getFileList() )
86          {
87              command.createArg().setValue( file.getName() );
88          }
89          return command;
90      }
91  }