1 package org.apache.maven.scm.provider.starteam.command;
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.log.ScmLogger;
25 import org.apache.maven.scm.provider.starteam.repository.StarteamScmProviderRepository;
26 import org.apache.maven.scm.provider.starteam.util.StarteamUtil;
27 import org.apache.maven.scm.providers.starteam.settings.Settings;
28 import org.codehaus.plexus.util.cli.CommandLineException;
29 import org.codehaus.plexus.util.cli.CommandLineUtils;
30 import org.codehaus.plexus.util.cli.Commandline;
31 import org.codehaus.plexus.util.cli.StreamConsumer;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.util.List;
36
37
38
39
40
41
42
43 public final class StarteamCommandLineUtils
44 {
45
46 private StarteamCommandLineUtils()
47 {
48 }
49
50 private static Settings settings = StarteamUtil.getSettings();
51
52 public static Commandline createStarteamBaseCommandLine( String action, StarteamScmProviderRepository repo )
53 {
54 Commandline cl = new Commandline();
55
56 cl.createArg().setValue( "stcmd" );
57
58 cl.createArg().setValue( action );
59
60 cl.createArg().setValue( "-x" );
61
62 cl.createArg().setValue( "-nologo" );
63
64 cl.createArg().setValue( "-stop" );
65
66 return cl;
67 }
68
69 private static Commandline addCommandlineArguments( Commandline cl, List<String> args )
70 {
71 if ( args == null )
72 {
73 return cl;
74 }
75 for ( String arg : args )
76 {
77 cl.createArg().setValue( arg );
78 }
79 return cl;
80 }
81
82 public static Commandline createStarteamCommandLine( String action, List<String> args, ScmFileSet scmFileSet,
83 StarteamScmProviderRepository repo )
84 {
85 Commandline cl = StarteamCommandLineUtils.createStarteamBaseCommandLine( action, repo );
86
87
88 if ( scmFileSet.getFileList().size() == 0 )
89 {
90
91 cl.createArg().setValue( "-p" );
92 cl.createArg().setValue( repo.getFullUrl() );
93 cl.createArg().setValue( "-fp" );
94 cl.createArg().setValue( scmFileSet.getBasedir().getAbsolutePath().replace( '\\', '/' ) );
95
96 cl.createArg().setValue( "-is" );
97
98 addCompressionOption( cl );
99
100 addCommandlineArguments( cl, args );
101
102 return cl;
103 }
104
105
106 File fileInFileSet = (File) scmFileSet.getFileList().get( 0 );
107 File subFile = new File( scmFileSet.getBasedir(), fileInFileSet.getPath() );
108
109
110
111
112
113 File workingDirectory = subFile;
114 String scmUrl = repo.getFullUrl() + "/" + fileInFileSet.getPath().replace( '\\', '/' );
115 if ( !subFile.isDirectory() )
116 {
117 workingDirectory = subFile.getParentFile();
118 if ( fileInFileSet.getParent() != null )
119 {
120 scmUrl = repo.getFullUrl() + "/" + fileInFileSet.getParent().replace( '\\', '/' );
121 }
122 else
123 {
124
125 scmUrl = repo.getFullUrl();
126 }
127 }
128
129 cl.createArg().setValue( "-p" );
130 cl.createArg().setValue( scmUrl );
131
132 cl.createArg().setValue( "-fp" );
133 cl.createArg().setValue( workingDirectory.getPath().replace( '\\', '/' ) );
134
135 cl.setWorkingDirectory( workingDirectory.getPath() );
136
137 if ( subFile.isDirectory() )
138 {
139 cl.createArg().setValue( "-is" );
140 }
141
142 StarteamCommandLineUtils.addCompressionOption( cl );
143
144 addCommandlineArguments( cl, args );
145
146 if ( !subFile.isDirectory() )
147 {
148 cl.createArg().setValue( subFile.getName() );
149 }
150
151 return cl;
152 }
153
154 public static void addCompressionOption( Commandline cl )
155 {
156 if ( settings.isCompressionEnable() )
157 {
158 cl.createArg().setValue( "-cmp" );
159 }
160 }
161
162 public static void addEOLOption( List<String> args )
163 {
164 if ( settings.getEol() != null )
165 {
166 args.add( "-eol" );
167 args.add( settings.getEol() );
168 }
169 }
170
171 public static String toJavaPath( String path )
172 {
173 return path.replace( '\\', '/' );
174 }
175
176
177
178
179
180
181
182
183 public static String displayCommandlineWithoutPassword( Commandline cl )
184 throws ScmException
185 {
186 String retStr = "";
187
188 String fullStr = cl.toString();
189
190
191
192 int usernamePos = fullStr.indexOf( "-p " ) + 3;
193
194 if ( usernamePos == 2 )
195 {
196
197
198
199 throw new ScmException( "Invalid command line" );
200 }
201
202 retStr = fullStr.substring( 0, usernamePos );
203
204 int passwordStartPos = fullStr.indexOf( ':' );
205
206 if ( passwordStartPos == -1 )
207 {
208 throw new ScmException( "Invalid command line" );
209 }
210
211 int passwordEndPos = fullStr.indexOf( '@' );
212
213 if ( passwordEndPos == -1 )
214 {
215 throw new ScmException( "Invalid command line" );
216 }
217
218 retStr += fullStr.substring( usernamePos, passwordStartPos );
219
220 retStr += fullStr.substring( passwordEndPos );
221
222 return retStr;
223
224 }
225
226 public static int executeCommandline( Commandline cl, StreamConsumer consumer,
227 CommandLineUtils.StringStreamConsumer stderr, ScmLogger logger )
228 throws ScmException
229 {
230 if ( logger.isInfoEnabled() )
231 {
232 logger.info( "Command line: " + displayCommandlineWithoutPassword( cl ) );
233 }
234
235 try
236 {
237 return CommandLineUtils.executeCommandLine( cl, consumer, stderr );
238 }
239 catch ( CommandLineException ex )
240 {
241 throw new ScmException( "Error while executing command.", ex );
242 }
243 }
244
245
246
247
248
249
250
251
252
253 public static String getRelativeChildDirectory( String parent, String child )
254 {
255
256 try
257 {
258 String childPath = new File( child ).getCanonicalFile().getPath().replace( '\\', '/' );
259
260 String parentPath = new File( parent ).getCanonicalFile().getPath().replace( '\\', '/' );
261
262 if ( !childPath.startsWith( parentPath ) )
263 {
264 throw new IllegalStateException();
265 }
266
267 String retDir = "." + childPath.substring( parentPath.length() );
268
269 return retDir;
270
271 }
272 catch ( IOException e )
273 {
274 throw new IllegalStateException(
275 "Unable to convert to canonical path of either " + parent + " or " + child );
276 }
277 }
278
279 }