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 public class PerforceEditCommand
44 extends AbstractEditCommand
45 implements PerforceCommand
46 {
47
48
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
117
118
119
120
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 }