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
39
40
41 public class TfsCommand
42 {
43
44 private ScmLogger logger;
45
46 private Commandline command;
47
48 public TfsCommand( String cmd, ScmProviderRepository r, ScmFileSet f, ScmLogger logger )
49 {
50 command = new Commandline();
51 command.setExecutable( "tf" );
52 if ( f != null )
53 {
54 command.setWorkingDirectory( f.getBasedir().getAbsolutePath() );
55 }
56
57 command.createArg().setValue( cmd );
58
59 if ( r.getUser() != null )
60 {
61 command.createArg().setValue( "-login:" + r.getUser() + "," + r.getPassword() );
62 }
63 this.logger = logger;
64 }
65
66 public void addArgument( ScmFileSet f )
67 {
68 info( "files: " + f.getBasedir().getAbsolutePath() );
69 Iterator<File> iter = f.getFileList().iterator();
70 while ( iter.hasNext() )
71 {
72 command.createArg().setValue( ( (File) iter.next() ).getPath() );
73 }
74 }
75
76 public void addArgument( String s )
77 {
78 command.createArg().setValue( s );
79 }
80
81 public int execute( StreamConsumer out, ErrorStreamConsumer err )
82 throws ScmException
83 {
84 info( "Command line - " + getCommandString() );
85 int status;
86 try
87 {
88 status = CommandLineUtils.executeCommandLine( command, out, err );
89 }
90 catch ( CommandLineException e )
91 {
92 throw new ScmException( "Error while executing TFS command line - " + getCommandString(), e );
93 }
94 info( "err - " + err.getOutput() );
95 if ( out instanceof StringStreamConsumer )
96 {
97 StringStreamConsumer sc = (StringStreamConsumer) out;
98 debug( sc.getOutput() );
99 }
100 if ( out instanceof FileListConsumer )
101 {
102 FileListConsumer f = (FileListConsumer) out;
103 for ( Iterator<ScmFile> i = f.getFiles().iterator(); i.hasNext(); )
104 {
105 ScmFile file = i.next();
106 debug( file.getPath() );
107 }
108 }
109
110 return status;
111 }
112
113 public String getCommandString()
114 {
115 return command.toString();
116 }
117
118 public Commandline getCommandline()
119 {
120 return command;
121 }
122
123 private void info( String message )
124 {
125 if ( logger != null )
126 {
127 logger.info( message );
128 }
129 }
130
131 private void debug( String message )
132 {
133 if ( logger != null )
134 {
135 logger.debug( message );
136 }
137 }
138
139 }