1 | |
package org.apache.maven.scm.provider.tfs.command; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.util.Iterator; |
24 | |
|
25 | |
import org.apache.maven.scm.ScmException; |
26 | |
import org.apache.maven.scm.ScmFile; |
27 | |
import org.apache.maven.scm.ScmFileSet; |
28 | |
import org.apache.maven.scm.log.ScmLogger; |
29 | |
import org.apache.maven.scm.provider.ScmProviderRepository; |
30 | |
import org.apache.maven.scm.provider.tfs.command.consumer.ErrorStreamConsumer; |
31 | |
import org.apache.maven.scm.provider.tfs.command.consumer.FileListConsumer; |
32 | |
import org.codehaus.plexus.util.cli.CommandLineException; |
33 | |
import org.codehaus.plexus.util.cli.CommandLineUtils; |
34 | |
import org.codehaus.plexus.util.cli.Commandline; |
35 | |
import org.codehaus.plexus.util.cli.StreamConsumer; |
36 | |
import org.codehaus.plexus.util.cli.CommandLineUtils.StringStreamConsumer; |
37 | |
|
38 | |
public class TfsCommand |
39 | |
{ |
40 | |
|
41 | |
private ScmLogger logger; |
42 | |
|
43 | |
private Commandline command; |
44 | |
|
45 | |
public TfsCommand( String cmd, ScmProviderRepository r, ScmFileSet f, ScmLogger logger ) |
46 | 11 | { |
47 | 11 | command = new Commandline(); |
48 | 11 | command.setExecutable( "tf" ); |
49 | 11 | if ( f != null ) |
50 | |
{ |
51 | 11 | command.setWorkingDirectory( f.getBasedir().getAbsolutePath() ); |
52 | |
} |
53 | |
|
54 | 11 | command.createArg().setValue( cmd ); |
55 | |
|
56 | 11 | if ( r.getUser() != null ) |
57 | |
{ |
58 | 11 | command.createArg().setValue( "-login:" + r.getUser() + "," + r.getPassword() ); |
59 | |
} |
60 | 11 | this.logger = logger; |
61 | 11 | } |
62 | |
|
63 | |
public void addArgument( ScmFileSet f ) |
64 | |
{ |
65 | 5 | info( "files: " + f.getBasedir().getAbsolutePath() ); |
66 | 5 | Iterator<File> iter = f.getFileList().iterator(); |
67 | 10 | while ( iter.hasNext() ) |
68 | |
{ |
69 | 5 | command.createArg().setValue( ( (File) iter.next() ).getPath() ); |
70 | |
} |
71 | 5 | } |
72 | |
|
73 | |
public void addArgument( String s ) |
74 | |
{ |
75 | 23 | command.createArg().setValue( s ); |
76 | 23 | } |
77 | |
|
78 | |
public int execute( StreamConsumer out, ErrorStreamConsumer err ) |
79 | |
throws ScmException |
80 | |
{ |
81 | 0 | info( "Command line - " + getCommandString() ); |
82 | |
int status; |
83 | |
try |
84 | |
{ |
85 | 0 | status = CommandLineUtils.executeCommandLine( command, out, err ); |
86 | |
} |
87 | 0 | catch ( CommandLineException e ) |
88 | |
{ |
89 | 0 | throw new ScmException( "Error while executing TFS command line - " + getCommandString(), e ); |
90 | 0 | } |
91 | 0 | info( "err - " + err.getOutput() ); |
92 | 0 | if ( out instanceof StringStreamConsumer ) |
93 | |
{ |
94 | 0 | StringStreamConsumer sc = (StringStreamConsumer) out; |
95 | 0 | debug( sc.getOutput() ); |
96 | |
} |
97 | 0 | if ( out instanceof FileListConsumer ) |
98 | |
{ |
99 | 0 | FileListConsumer f = (FileListConsumer) out; |
100 | 0 | for ( Iterator<ScmFile> i = f.getFiles().iterator(); i.hasNext(); ) |
101 | |
{ |
102 | 0 | ScmFile file = i.next(); |
103 | 0 | debug( file.getPath() ); |
104 | 0 | } |
105 | |
} |
106 | |
|
107 | 0 | return status; |
108 | |
} |
109 | |
|
110 | |
public String getCommandString() |
111 | |
{ |
112 | 0 | return command.toString(); |
113 | |
} |
114 | |
|
115 | |
public Commandline getCommandline() { |
116 | 11 | return command; |
117 | |
} |
118 | |
|
119 | |
private void info( String message ) |
120 | |
{ |
121 | 5 | if ( logger != null ) |
122 | 0 | logger.info( message ); |
123 | 5 | } |
124 | |
|
125 | |
private void debug( String message ) |
126 | |
{ |
127 | 0 | if ( logger != null ) |
128 | 0 | logger.debug( message ); |
129 | 0 | } |
130 | |
|
131 | |
} |