1 | |
package org.apache.maven.scm.provider.perforce.command.edit; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
|
41 | |
|
42 | |
|
43 | 0 | public class PerforceEditCommand |
44 | |
extends AbstractEditCommand |
45 | |
implements PerforceCommand |
46 | |
{ |
47 | |
|
48 | |
protected ScmResult executeEditCommand( ScmProviderRepository repo, ScmFileSet files ) |
49 | |
throws ScmException |
50 | |
{ |
51 | 0 | Commandline cl = createCommandLine( (PerforceScmProviderRepository) repo, files.getBasedir(), files ); |
52 | 0 | PerforceEditConsumer consumer = new PerforceEditConsumer(); |
53 | |
try |
54 | |
{ |
55 | 0 | if ( getLogger().isDebugEnabled() ) |
56 | |
{ |
57 | 0 | getLogger().debug( PerforceScmProvider.clean( "Executing " + cl.toString() ) ); |
58 | |
} |
59 | |
|
60 | 0 | CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer(); |
61 | 0 | int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err ); |
62 | |
|
63 | 0 | if ( exitCode != 0 ) |
64 | |
{ |
65 | 0 | String cmdLine = CommandLineUtils.toString( cl.getCommandline() ); |
66 | |
|
67 | 0 | StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() ); |
68 | 0 | msg.append( '\n' ); |
69 | 0 | msg.append( "Command line was:" + cmdLine ); |
70 | |
|
71 | 0 | throw new CommandLineException( msg.toString() ); |
72 | |
} |
73 | |
} |
74 | 0 | catch ( CommandLineException e ) |
75 | |
{ |
76 | 0 | if ( getLogger().isErrorEnabled() ) |
77 | |
{ |
78 | 0 | getLogger().error( "CommandLineException " + e.getMessage(), e ); |
79 | |
} |
80 | 0 | } |
81 | |
|
82 | 0 | if ( consumer.isSuccess() ) |
83 | |
{ |
84 | 0 | return new EditScmResult( cl.toString(), consumer.getEdits() ); |
85 | |
} |
86 | |
|
87 | 0 | return new EditScmResult( cl.toString(), "Unable to edit file(s)", consumer.getErrorMessage(), false ); |
88 | |
} |
89 | |
|
90 | |
public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory, |
91 | |
ScmFileSet files ) |
92 | |
{ |
93 | 2 | Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory ); |
94 | |
|
95 | 2 | command.createArg().setValue( "edit" ); |
96 | |
|
97 | |
try |
98 | |
{ |
99 | 2 | String candir = workingDirectory.getCanonicalPath(); |
100 | 2 | List<File> fs = files.getFileList(); |
101 | 6 | for ( int i = 0; i < fs.size(); i++ ) |
102 | |
{ |
103 | 4 | File file = new File( workingDirectory, fs.get( i ).getPath() ); |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | 4 | String canfile = file.getCanonicalPath(); |
110 | 4 | if ( canfile.startsWith( candir ) ) |
111 | |
{ |
112 | 4 | canfile = canfile.substring( candir.length() + 1 ); |
113 | |
} |
114 | 4 | command.createArg().setValue( canfile ); |
115 | |
} |
116 | |
} |
117 | 0 | catch ( IOException e ) |
118 | |
{ |
119 | 0 | e.printStackTrace(); |
120 | 2 | } |
121 | 2 | return command; |
122 | |
} |
123 | |
} |