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
38
39
40
41
42 public class PerforceEditCommand
43 extends AbstractEditCommand
44 implements PerforceCommand
45 {
46
47
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
116
117
118
119
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 }