1 package org.apache.maven.scm.provider.vss.commands;
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.vss.repository.VssScmProviderRepository;
26 import org.apache.maven.scm.providers.vss.settings.Settings;
27 import org.apache.maven.scm.providers.vss.settings.io.xpp3.VssXpp3Reader;
28 import org.codehaus.plexus.util.ReaderFactory;
29 import org.codehaus.plexus.util.StringUtils;
30 import org.codehaus.plexus.util.cli.CommandLineException;
31 import org.codehaus.plexus.util.cli.CommandLineUtils;
32 import org.codehaus.plexus.util.cli.Commandline;
33 import org.codehaus.plexus.util.cli.StreamConsumer;
34 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
35
36 import java.io.File;
37 import java.io.FileNotFoundException;
38 import java.io.IOException;
39 import java.util.Iterator;
40
41
42
43
44
45 public final class VssCommandLineUtils
46
47 {
48
49 private VssCommandLineUtils() {
50 }
51
52 private static File scmConfDir = new File( System.getProperty( "user.home" ), ".scm" );
53
54 private static Settings settings;
55
56 public static void addFiles( Commandline cl, ScmFileSet fileSet )
57 {
58 Iterator<File> it = fileSet.getFileList().iterator();
59
60 while ( it.hasNext() )
61 {
62 File file = it.next();
63
64 cl.createArg().setValue( file.getPath().replace( '\\', '/' ) );
65 }
66
67 }
68
69 public static Commandline getBaseVssCommandLine( File workingDirectory, String cmd,
70 VssScmProviderRepository repository )
71 {
72 Commandline cl = new Commandline();
73
74 cl.setExecutable( VssConstants.SS_EXE );
75
76 cl.setWorkingDirectory( workingDirectory.getAbsolutePath() );
77
78 if ( !StringUtils.isEmpty( repository.getUser() ) )
79 {
80 cl.createArg().setValue( "-Y" );
81
82 StringBuilder sb = new StringBuilder( repository.getUser() );
83 if ( !StringUtils.isEmpty( repository.getPassword() ) )
84 {
85 sb.append( "," ).append( repository.getPassword() );
86 }
87 cl.createArg().setValue( sb.toString() );
88 }
89
90 return cl;
91 }
92
93 public static int executeCommandline( Commandline cl, StreamConsumer consumer,
94 CommandLineUtils.StringStreamConsumer stderr, ScmLogger logger )
95 throws ScmException
96 {
97 try
98 {
99 if ( logger.isInfoEnabled() )
100 {
101 logger.info( "Executing: " + cl );
102 logger.info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
103 }
104
105 int exitcode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
106
107 if ( logger.isDebugEnabled() )
108 {
109 logger.debug( "VSS Command Exit_Code: " + exitcode );
110 }
111
112 return exitcode;
113 }
114 catch ( CommandLineException ex )
115 {
116 throw new ScmException( "Error while executing command.", ex );
117 }
118 }
119
120
121 public static final Settings getSettings()
122 {
123 if ( settings == null )
124 {
125 settings = readSettings();
126 }
127 return settings;
128 }
129
130 public static Settings readSettings()
131 {
132 Settings settings = null;
133 File settingsFile = getScmConfFile();
134 if ( settingsFile.exists() )
135 {
136 VssXpp3Reader reader = new VssXpp3Reader();
137 try
138 {
139 settings = reader.read( ReaderFactory.newXmlReader( settingsFile ) );
140 }
141 catch ( FileNotFoundException e )
142 {
143
144 }
145 catch ( IOException e )
146 {
147
148 }
149 catch ( XmlPullParserException e )
150 {
151 String message = settingsFile.getAbsolutePath() + " isn't well formed. SKIPPED." + e.getMessage();
152
153 System.err.println( message );
154 }
155 }
156
157
158 String vssDirectory = System.getProperty( "vssDirectory" );
159 if ( StringUtils.isNotEmpty( vssDirectory ) )
160 {
161 if ( settings == null )
162 {
163 settings = new Settings();
164 }
165 settings.setVssDirectory( vssDirectory );
166 }
167 return settings;
168 }
169
170 protected static final File getScmConfDir()
171 {
172 return scmConfDir;
173 }
174
175 protected static final void setScmConfDir( File directory )
176 {
177 scmConfDir = directory;
178 settings = readSettings();
179 }
180
181 public static final String getSsDir()
182 {
183 String ssDir = "";
184 if ( VssCommandLineUtils.getSettings() != null )
185 {
186 String ssDir2 = VssCommandLineUtils.getSettings().getVssDirectory();
187
188 if ( ssDir2 != null )
189 {
190 ssDir = StringUtils.replace( ssDir2, "\\", "/" );
191
192 if ( !ssDir.endsWith( "/" ) )
193 {
194 ssDir += "/";
195 }
196 }
197 }
198 return ssDir;
199 }
200
201 public static File getScmConfFile()
202 {
203 return new File( scmConfDir, "vss-settings.xml" );
204 }
205 }