View Javadoc
1   package org.apache.maven.scm.provider.perforce.command.edit;
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.edit.AbstractEditCommand;
26  import org.apache.maven.scm.command.edit.EditScmResult;
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.io.IOException;
37  import java.util.List;
38  
39  /**
40   * @author Mike Perham
41   *
42   */
43  public class PerforceEditCommand
44      extends AbstractEditCommand
45      implements PerforceCommand
46  {
47      /**
48       * {@inheritDoc}
49       */
50      @Override
51      protected ScmResult executeEditCommand( ScmProviderRepository repo, ScmFileSet files )
52          throws ScmException
53      {
54          Commandline cl = createCommandLine( (PerforceScmProviderRepository) repo, files.getBasedir(), files );
55          PerforceEditConsumer consumer = new PerforceEditConsumer();
56          try
57          {
58              if ( getLogger().isDebugEnabled() )
59              {
60                  getLogger().debug( PerforceScmProvider.clean( "Executing " + cl.toString() ) );
61              }
62  
63              CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
64              int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err );
65  
66              if ( exitCode != 0 )
67              {
68                  String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
69  
70                  StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
71                  msg.append( '\n' );
72                  msg.append( "Command line was:" + cmdLine );
73  
74                  throw new CommandLineException( msg.toString() );
75              }
76          }
77          catch ( CommandLineException e )
78          {
79              if ( getLogger().isErrorEnabled() )
80              {
81                  getLogger().error( "CommandLineException " + e.getMessage(), e );
82              }
83          }
84  
85          if ( consumer.isSuccess() )
86          {
87              return new EditScmResult( cl.toString(), consumer.getEdits() );
88          }
89  
90          return new EditScmResult( cl.toString(), "Unable to edit file(s)", consumer.getErrorMessage(), false );
91      }
92  
93      public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory,
94                                                   ScmFileSet files )
95          throws ScmException
96      {
97          Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
98  
99          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 }