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  
38  /**
39   * @author Mike Perham
40   *
41   */
42  public class PerforceEditCommand
43      extends AbstractEditCommand
44      implements PerforceCommand
45  {
46      /**
47       * {@inheritDoc}
48       */
49      @Override
50      protected ScmResult executeEditCommand( ScmProviderRepository repo, ScmFileSet files )
51          throws ScmException
52      {
53          Commandline cl = createCommandLine( (PerforceScmProviderRepository) repo, files.getBasedir(), files );
54          PerforceEditConsumer consumer = new PerforceEditConsumer();
55          try
56          {
57              if ( getLogger().isDebugEnabled() )
58              {
59                  getLogger().debug( PerforceScmProvider.clean( "Executing " + cl.toString() ) );
60              }
61  
62              CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
63              int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err );
64  
65              if ( exitCode != 0 )
66              {
67                  String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
68  
69                  StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
70                  msg.append( '\n' );
71                  msg.append( "Command line was:" + cmdLine );
72  
73                  throw new CommandLineException( msg.toString() );
74              }
75          }
76          catch ( CommandLineException e )
77          {
78              if ( getLogger().isErrorEnabled() )
79              {
80                  getLogger().error( "CommandLineException " + e.getMessage(), e );
81              }
82          }
83  
84          if ( consumer.isSuccess() )
85          {
86              return new EditScmResult( cl.toString(), consumer.getEdits() );
87          }
88  
89          return new EditScmResult( cl.toString(), "Unable to edit file(s)", consumer.getErrorMessage(), false );
90      }
91  
92      public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory,
93                                                   ScmFileSet files )
94          throws ScmException
95      {
96          Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
97  
98          command.createArg().setValue( "edit" );
99  
100         try
101         {
102             String candir = workingDirectory.getCanonicalPath();
103 
104             for ( File f : files.getFileList() )
105             {
106                 File file = null;
107                 if ( f.isAbsolute() )
108                 {
109                     file = new File( f.getPath() );
110                 }
111                 else
112                 {
113                     file = new File( workingDirectory, f.getPath() );
114                 }
115                 // I want to use relative paths to add files to make testing
116                 // simpler.
117                 // Otherwise the absolute path will be different on everyone's
118                 // machine
119                 // and testing will be a little more painful.
120                 String canfile = file.getCanonicalPath();
121                 if ( canfile.startsWith( candir ) )
122                 {
123                     canfile = canfile.substring( candir.length() + 1 );
124                 }
125                 command.createArg().setValue( canfile );
126             }
127         }
128         catch ( IOException e )
129         {
130             throw new ScmException( e.getMessage(), e );
131         }
132         return command;
133     }
134 }