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 public 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 throw new ApplicationException("Not Implemented. Workaround: use /login");
118 return ""; //PromptForLogin();
121 private void GetUserCredentials()
123 if (! String
.IsNullOrEmpty(username
)) return;
125 string login
= GetLogin();
126 string userinfo
= "";
128 int comma
= login
.IndexOf(",");
131 userinfo
= login
.Substring(0, comma
);
132 password
= login
.Substring(comma
+1);
134 else userinfo
= login
;
136 int slash
= userinfo
.IndexOf('\\');
137 if (-1 == slash
) username
= userinfo
;
140 domain
= userinfo
.Substring(0, slash
);
141 username
= userinfo
.Substring(slash
+1);
145 public VersionControlServer VersionControlServer
149 if (null != _versionControl
) return _versionControl
;
150 GetUserCredentials();
152 if (String
.IsNullOrEmpty(password
))
154 Console
.Write("Password: ");
155 password
= Console
.ReadLine();
158 _tfs
= new TeamFoundationServer(ServerUrl
, new NetworkCredential(username
, password
, domain
));
159 _versionControl
= (VersionControlServer
) _tfs
.GetService(typeof(VersionControlServer
));
161 _versionControl
.Conflict
+= ConflictEventHandler
;
162 _versionControl
.NonFatalError
+= ExceptionEventHandler
;
164 // save credentials if passed
165 bool saveSetting
= Settings
.Current
.GetAsBool("Credentials.Save");
166 if (saveSetting
&& !String
.IsNullOrEmpty(Options
.Login
))
167 TfsKeyring
.SetCredentials(ServerUrl
, domain
, username
, password
);
169 return _versionControl
;
173 public Driver(string[] args
)
176 Options
.ProcessArgs(args
);
179 static void ConflictEventHandler(Object sender
, ConflictEventArgs e
)
181 Console
.Error
.WriteLine(e
.Message
);
184 static void ExceptionEventHandler(Object sender
, ExceptionEventArgs e
)
186 // sometimes e.Failure is null, not sure why yet
187 if (e
.Failure
!= null)
188 Console
.Error
.WriteLine(e
.Failure
.Message
);
191 private bool IsOption(string arg
)
193 if (String
.IsNullOrEmpty(arg
)) return false;
194 return (arg
[0] == '-' || arg
[0] == '/');
197 public void ProcessAllCommands()
199 List
<string> cmdArgs
= new List
<string>();
201 for (int i
=0; i
< Arguments
.Length
; i
++)
203 // unknown options are not sub commands
204 string scmd
= Arguments
[i
].ToLower();
211 // pull in the rest of the args for this subcommand
213 for (j
=i
+1; j
< Arguments
.Length
; j
++)
215 string arg
= Arguments
[j
];
216 if (arg
== "%") break;
220 ProcessCommand(scmd
, cmdArgs
.ToArray());
226 public void ProcessCommand(string cmd
, string[] cmdArgs
)
228 Type commandType
= CommandRegistry
.GetCommandType(cmd
);
229 if (commandType
== null)
231 Console
.WriteLine("Unknown command: " + cmd
);
235 Command command
= (Command
) Activator
.CreateInstance(commandType
, new object[]{ this, cmdArgs }
);
239 // ignoring certificate errors
240 public bool CheckValidationResult (ServicePoint sp
,
241 X509Certificate certificate
, WebRequest request
, int error
)
246 public static void Main(string[] args
)
248 Driver driver
= new Driver(args
);
249 ServicePointManager
.CertificatePolicy
= driver
;
251 if (args
.Length
== 0) {
252 HelpCommand cmd
= new HelpCommand(driver
, args
);
257 // basic auth doesn't seem to work well on mono when POSTing
258 // large data sets via a webservice.
259 AuthenticationManager
.Unregister("Basic");
263 driver
.ProcessAllCommands();
265 catch (TeamFoundationServerException e
)
267 Console
.Error
.WriteLine(e
.Message
);
268 //Console.WriteLine(driver.Domain + "\\" + driver.Username);