Bring back all the TFS common code in TfsHelper.Common.cs (DRY!)

This commit is contained in:
Philippe Miossec
2014-07-10 22:17:07 +02:00
parent 344c5a2a3e
commit eabd86771d
5 changed files with 101 additions and 229 deletions
-103
View File
@@ -2,14 +2,6 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.Win32;
using Sep.Git.Tfs.Core;
using Sep.Git.Tfs.Core.TfsInterop;
using Sep.Git.Tfs.Util;
using Sep.Git.Tfs.VsCommon;
using StructureMap;
@@ -17,62 +9,11 @@ namespace Sep.Git.Tfs.Vs2010
{
public class TfsHelper : TfsHelperBase
{
private readonly TfsApiBridge _bridge;
public TfsHelper(TextWriter stdout, TfsApiBridge bridge, IContainer container) : base(stdout, bridge, container)
{
_bridge = bridge;
}
public override string TfsClientLibraryVersion
{
get { return typeof(TfsTeamProjectCollection).Assembly.GetName().Version + " (MS)"; }
}
public override void EnsureAuthenticated()
{
if (string.IsNullOrEmpty(Url))
{
_server = null;
}
else
{
Uri uri;
if (!Uri.IsWellFormedUriString(Url, UriKind.Absolute))
{
// maybe it is not an Uri but instance name
var servers = RegisteredTfsConnections.GetConfigurationServers();
var registered = servers.FirstOrDefault(s => String.Compare(s.Name, Url, StringComparison.OrdinalIgnoreCase) == 0);
if (registered == null)
throw new GitTfsException("Given tfs name is not correct URI and not found as a registered TFS instance");
uri = registered.Uri;
}
else
{
uri = new Uri(Url);
}
_server = HasCredentials ?
new TfsTeamProjectCollection(uri, GetCredential(), new UICredentialsProvider()) :
new TfsTeamProjectCollection(uri, new UICredentialsProvider());
_server.EnsureAuthenticated();
}
}
protected override T GetService<T>()
{
if (_server == null) EnsureAuthenticated();
return (T)_server.GetService(typeof(T));
}
protected override string GetAuthenticatedUser()
{
return VersionControl.AuthorizedUser;
}
private string vsInstallDir;
protected override string GetVsInstallDir()
{
if (vsInstallDir == null)
@@ -82,49 +23,5 @@ namespace Sep.Git.Tfs.Vs2010
}
return vsInstallDir;
}
private static string TryGetRegString(string path, string name)
{
try
{
Trace.WriteLine("Trying to get " + path + "|" + name);
var key = Registry.LocalMachine.OpenSubKey(path);
if(key != null)
{
return key.GetValue(name) as string;
}
}
catch(Exception e)
{
Trace.WriteLine("Unable to get registry value " + path + "|" + name + ": " + e);
}
return null;
}
}
public class ItemDownloadStrategy : IItemDownloadStrategy
{
private readonly TfsApiBridge _bridge;
public ItemDownloadStrategy(TfsApiBridge bridge)
{
_bridge = bridge;
}
public TemporaryFile DownloadFile(IItem item)
{
var temp = new TemporaryFile();
try
{
_bridge.Unwrap<Item>(item).DownloadFile(temp);
return temp;
}
catch (Exception)
{
Trace.WriteLine(String.Format("Something went wrong downloading \"{0}\" in changeset {1}", item.ServerItem, item.ChangesetId));
temp.Dispose();
throw;
}
}
}
}
-1
View File
@@ -11,7 +11,6 @@ namespace Sep.Git.Tfs.Vs2012
}
private string vsInstallDir;
protected override string GetVsInstallDir()
{
if (vsInstallDir == null)
+3 -4
View File
@@ -11,15 +11,14 @@ namespace Sep.Git.Tfs.Vs2013
}
private string vsInstallDir;
protected override string GetVsInstallDir()
{
if (vsInstallDir == null)
{
vsInstallDir = TryGetRegString(@"Software\Microsoft\VisualStudio\12.0", "InstallDir")
?? TryGetRegString(@"Software\WOW6432Node\Microsoft\VisualStudio\12.0", "InstallDir")
?? TryGetUserRegString(@"Software\Microsoft\WDExpress\12.0_Config", "InstallDir")
?? TryGetUserRegString(@"Software\WOW6432Node\Microsoft\WDExpress\12.0_Config", "InstallDir");
?? TryGetRegString(@"Software\WOW6432Node\Microsoft\VisualStudio\12.0", "InstallDir")
?? TryGetUserRegString(@"Software\Microsoft\WDExpress\12.0_Config", "InstallDir")
?? TryGetUserRegString(@"Software\WOW6432Node\Microsoft\WDExpress\12.0_Config", "InstallDir");
}
return vsInstallDir;
}
+97 -5
View File
@@ -10,6 +10,7 @@ using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.Win32;
using SEP.Extensions;
using Sep.Git.Tfs.Commands;
using Sep.Git.Tfs.Core;
@@ -34,7 +35,6 @@ namespace Sep.Git.Tfs.VsCommon
_stdout = stdout;
_bridge = bridge;
_container = container;
_bridge = bridge;
if (!_resolverInstalled)
{
AppDomain.CurrentDomain.AssemblyResolve += LoadFromVsFolder;
@@ -44,7 +44,7 @@ namespace Sep.Git.Tfs.VsCommon
[SetterProperty]
public Janitor Janitor { get; set; }
public abstract string TfsClientLibraryVersion { get; }
public string TfsClientLibraryVersion { get { return typeof(TfsTeamProjectCollection).Assembly.GetName().Version + " (MS)"; } }
public string Url { get; set; }
@@ -57,7 +57,38 @@ namespace Sep.Git.Tfs.VsCommon
get { return !String.IsNullOrEmpty(Username); }
}
public abstract void EnsureAuthenticated();
public void EnsureAuthenticated()
{
if (string.IsNullOrEmpty(Url))
{
_server = null;
}
else
{
Uri uri;
if (!Uri.IsWellFormedUriString(Url, UriKind.Absolute))
{
// maybe it is not an Uri but instance name
var servers = RegisteredTfsConnections.GetConfigurationServers();
var registered = servers.FirstOrDefault(s => String.Compare(s.Name, Url, StringComparison.OrdinalIgnoreCase) == 0);
if (registered == null)
throw new GitTfsException("Given tfs name is not correct URI and not found as a registered TFS instance");
uri = registered.Uri;
}
else
{
uri = new Uri(Url);
}
// TODO: Use TfsTeamProjectCollection constructor that takes a TfsClientCredentials object
_server = HasCredentials ?
new TfsTeamProjectCollection(uri, GetCredential(), new UICredentialsProvider()) :
new TfsTeamProjectCollection(uri, new UICredentialsProvider());
_server.EnsureAuthenticated();
}
}
private string[] _legacyUrls;
@@ -73,7 +104,11 @@ namespace Sep.Git.Tfs.VsCommon
return new NetworkCredential(Username, Password);
}
protected abstract T GetService<T>();
protected T GetService<T>()
{
if (_server == null) EnsureAuthenticated();
return (T)_server.GetService(typeof(T));
}
private VersionControlServer _versionControl;
protected VersionControlServer VersionControl
@@ -687,7 +722,10 @@ namespace Sep.Git.Tfs.VsCommon
return matchingShelvesets != null && matchingShelvesets.Length > 0;
}
protected abstract string GetAuthenticatedUser();
protected string GetAuthenticatedUser()
{
return VersionControl.AuthorizedUser;
}
public bool CanShowCheckinDialog { get { return true; } }
@@ -1170,5 +1208,59 @@ namespace Sep.Git.Tfs.VsCommon
Assembly assembly = Assembly.LoadFrom(assemblyPath);
return assembly;
}
protected string TryGetUserRegString(string path, string name)
{
return TryGetRegString(Registry.CurrentUser, path, name);
}
protected string TryGetRegString(string path, string name)
{
return TryGetRegString(Registry.LocalMachine, path, name);
}
protected string TryGetRegString(RegistryKey registryKey, string path, string name)
{
try
{
Trace.WriteLine("Trying to get " + registryKey.Name + "\\" + path + "|" + name);
var key = registryKey.OpenSubKey(path);
if (key != null)
{
return key.GetValue(name) as string;
}
}
catch (Exception e)
{
Trace.WriteLine("Unable to get registry value " + registryKey.Name + "\\" + path + "|" + name + ": " + e);
}
return null;
}
}
public class ItemDownloadStrategy : IItemDownloadStrategy
{
private readonly TfsApiBridge _bridge;
public ItemDownloadStrategy(TfsApiBridge bridge)
{
_bridge = bridge;
}
public TemporaryFile DownloadFile(IItem item)
{
var temp = new TemporaryFile();
try
{
_bridge.Unwrap<Item>(item).DownloadFile(temp);
return temp;
}
catch (Exception)
{
Trace.WriteLine(String.Format("Something went wrong downloading \"{0}\" in changeset {1}", item.ServerItem, item.ChangesetId));
temp.Dispose();
throw;
}
}
}
}
+1 -116
View File
@@ -1,133 +1,18 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.Win32;
using Sep.Git.Tfs.Core;
using Sep.Git.Tfs.Core.TfsInterop;
using Sep.Git.Tfs.Util;
using StructureMap;
namespace Sep.Git.Tfs.VsCommon
{
public abstract class TfsHelperVs2012Base : TfsHelperBase
{
private readonly TfsApiBridge _bridge;
public TfsHelperVs2012Base(TextWriter stdout, TfsApiBridge bridge, IContainer container) : base(stdout, bridge, container)
protected TfsHelperVs2012Base(TextWriter stdout, TfsApiBridge bridge, IContainer container) : base(stdout, bridge, container)
{
_bridge = bridge;
}
public override string TfsClientLibraryVersion
{
get { return typeof(TfsTeamProjectCollection).Assembly.GetName().Version + " (MS)"; }
}
public override void EnsureAuthenticated()
{
if (string.IsNullOrEmpty(Url))
{
_server = null;
}
else
{
Uri uri;
if (!Uri.IsWellFormedUriString(Url, UriKind.Absolute))
{
// maybe it is not an Uri but instance name
var servers = RegisteredTfsConnections.GetConfigurationServers();
var registered = servers.FirstOrDefault(s => String.Compare(s.Name, Url, StringComparison.OrdinalIgnoreCase) == 0);
if (registered == null)
throw new GitTfsException("Given tfs name is not correct URI and not found as a registered TFS instance");
uri = registered.Uri;
}
else
{
uri = new Uri(Url);
}
// TODO: Use TfsTeamProjectCollection constructor that takes a TfsClientCredentials object
_server = HasCredentials ?
new TfsTeamProjectCollection(uri, GetCredential(), new UICredentialsProvider()) :
new TfsTeamProjectCollection(uri, new UICredentialsProvider());
_server.EnsureAuthenticated();
}
}
protected override T GetService<T>()
{
if (_server == null) EnsureAuthenticated();
return (T) _server.GetService(typeof (T));
}
protected override string GetAuthenticatedUser()
{
return VersionControl.AuthorizedUser;
}
protected override bool HasWorkItems(Changeset changeset)
{
return Retry.Do(() => changeset.AssociatedWorkItems.Length > 0);
}
protected string TryGetUserRegString(string path, string name)
{
return TryGetRegString(Registry.CurrentUser, path, name);
}
protected string TryGetRegString(string path, string name)
{
return TryGetRegString(Registry.LocalMachine, path, name);
}
protected string TryGetRegString(RegistryKey registryKey, string path, string name)
{
try
{
Trace.WriteLine("Trying to get " + registryKey.Name + "\\" + path + "|" + name);
var key = registryKey.OpenSubKey(path);
if(key != null)
{
return key.GetValue(name) as string;
}
}
catch(Exception e)
{
Trace.WriteLine("Unable to get registry value " + registryKey.Name + "\\" + path + "|" + name + ": " + e);
}
return null;
}
}
public class ItemDownloadStrategy : IItemDownloadStrategy
{
private readonly TfsApiBridge _bridge;
public ItemDownloadStrategy(TfsApiBridge bridge)
{
_bridge = bridge;
}
public TemporaryFile DownloadFile(IItem item)
{
var temp = new TemporaryFile();
try
{
_bridge.Unwrap<Item>(item).DownloadFile(temp);
return temp;
}
catch (Exception)
{
Trace.WriteLine(String.Format("Something went wrong downloading \"{0}\" in changeset {1}", item.ServerItem, item.ChangesetId));
temp.Dispose();
throw;
}
}
}
}