5 // Joel Reed (joelwreed@gmail.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System
.Reflection
;
31 using System
.Collections
.Generic
;
32 using System
.Collections
.Specialized
;
36 using System
.Security
.Cryptography
.X509Certificates
;
37 using Mono
.GetOptions
;
38 using Microsoft
.TeamFoundation
;
39 using Microsoft
.TeamFoundation
.Client
;
40 using Microsoft
.TeamFoundation
.VersionControl
.Client
;
41 using Microsoft
.TeamFoundation
.Server
;
43 [assembly
: AssemblyTitle ("tf.exe")]
44 [assembly
: AssemblyVersion ("0.5.1")]
45 [assembly
: AssemblyDescription ("Team Foundation Source Control Tool")]
46 [assembly
: AssemblyCopyright ("(c) Joel W. Reed")]
48 [assembly
: Mono
.UsageComplement ("")]
50 [assembly
: Mono
.About("Team Foundation Source Control Tool")]
51 [assembly
: Mono
.Author ("Joel W. Reed")]
53 partial class Driver
: ICertificatePolicy
55 private TeamFoundationServer _tfs
;
56 private VersionControlServer _versionControl
;
57 private DriverOptions Options
= new DriverOptions();
58 private string[] Arguments
;
60 private string domain
;
61 private string username
;
62 private string password
;
63 private string serverUrl
;
73 public string Username
81 public string ServerUrl
84 if (!String
.IsNullOrEmpty(serverUrl
)) return serverUrl
;
86 if (!String
.IsNullOrEmpty(Options
.Server
))
88 if (Options
.Server
.StartsWith("http://") || Options
.Server
.StartsWith("https://"))
89 return Options
.Server
;
91 return String
.Format("http://{0}:8080/", Options
.Server
);
94 WorkspaceInfo info
= Workstation
.Current
.GetLocalWorkspaceInfo(Environment
.CurrentDirectory
);
97 Console
.WriteLine("Unable to determine the team foundation server");
98 Console
.WriteLine(" hint: try adding /server:<ip|name>");
102 serverUrl
= info
.ServerUri
.ToString();
107 private string GetLogin()
109 if (!String
.IsNullOrEmpty(Options
.Login
))
110 return Options
.Login
;
113 string login
= TfsKeyring
.GetCredentials(ServerUrl
);
114 if (!String
.IsNullOrEmpty(login
)) return login
;
117 return PromptForLogin();
120 private void GetUserCredentials()
122 if (! String
.IsNullOrEmpty(username
)) return;
124 string login
= GetLogin();
125 string userinfo
= "";
127 int comma
= login
.IndexOf(",");
130 userinfo
= login
.Substring(0, comma
);
131 password
= login
.Substring(comma
+1);
133 else userinfo
= login
;
135 int slash
= userinfo
.IndexOf('\\');
136 if (-1 == slash
) username
= userinfo
;
139 domain
= userinfo
.Substring(0, slash
);
140 username
= userinfo
.Substring(slash
+1);
144 public VersionControlServer VersionControlServer
148 if (null != _versionControl
) return _versionControl
;
149 GetUserCredentials();
151 if (String
.IsNullOrEmpty(password
))
153 Console
.Write("Password: ");
154 password
= Console
.ReadLine();
157 _tfs
= new TeamFoundationServer(ServerUrl
, new NetworkCredential(username
, password
, domain
));
158 _versionControl
= (VersionControlServer
) _tfs
.GetService(typeof(VersionControlServer
));
160 _versionControl
.Conflict
+= ConflictEventHandler
;
161 _versionControl
.NonFatalError
+= ExceptionEventHandler
;
163 // save credentials if passed
164 bool saveSetting
= Settings
.Current
.GetAsBool("Credentials.Save");
165 if (saveSetting
&& !String
.IsNullOrEmpty(Options
.Login
))
166 TfsKeyring
.SetCredentials(ServerUrl
, domain
, username
, password
);
168 return _versionControl
;
172 public Driver(string[] args
)
175 Options
.ProcessArgs(args
);
178 static void ConflictEventHandler(Object sender
, ConflictEventArgs e
)
180 Console
.Error
.WriteLine(e
.Message
);
183 static void ExceptionEventHandler(Object sender
, ExceptionEventArgs e
)
185 // sometimes e.Failure is null, not sure why yet
186 if (e
.Failure
!= null)
187 Console
.Error
.WriteLine(e
.Failure
.Message
);
190 private bool IsOption(string arg
)
192 if (String
.IsNullOrEmpty(arg
)) return false;
193 return (arg
[0] == '-' || arg
[0] == '/');
196 public void ProcessAllCommands()
198 List
<string> cmdArgs
= new List
<string>();
200 for (int i
=0; i
< Arguments
.Length
; i
++)
202 // unknown options are not sub commands
203 string scmd
= Arguments
[i
].ToLower();
210 // pull in the rest of the args for this subcommand
212 for (j
=i
+1; j
< Arguments
.Length
; j
++)
214 string arg
= Arguments
[j
];
215 if (arg
== "%") break;
219 ProcessCommand(scmd
, cmdArgs
.ToArray());
225 public void ProcessCommand(string cmd
, string[] cmdArgs
)
227 Type commandType
= CommandRegistry
.GetCommandType(cmd
);
228 if (commandType
== null)
230 Console
.WriteLine("Unknown command: " + cmd
);
234 Command command
= (Command
) Activator
.CreateInstance(commandType
, new object[]{ this, cmdArgs }
);
238 // ignoring certificate errors
239 public bool CheckValidationResult (ServicePoint sp
,
240 X509Certificate certificate
, WebRequest request
, int error
)
245 public static void Main(string[] args
)
247 Driver driver
= new Driver(args
);
248 ServicePointManager
.CertificatePolicy
= driver
;
250 if (args
.Length
== 0) {
251 HelpCommand cmd
= new HelpCommand(driver
, args
);
256 // basic auth doesn't seem to work well on mono when POSTing
257 // large data sets via a webservice.
258 AuthenticationManager
.Unregister("Basic");
262 driver
.ProcessAllCommands();
264 catch (TeamFoundationServerException e
)
266 Console
.Error
.WriteLine(e
.Message
);
267 //Console.WriteLine(driver.Domain + "\\" + driver.Username);