Skip to content

Commit

Permalink
Stop binding assembly versions at all, and add version info
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamTheCoder committed Apr 21, 2020
1 parent 1308d42 commit b9ddf9e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 68 deletions.
1 change: 0 additions & 1 deletion Vsix/CodeConversion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ internal class CodeConversion
private readonly IAsyncServiceProvider _serviceProvider;
private readonly JoinableTaskFactory _joinableTaskFactory;
private readonly VisualStudioWorkspace _visualStudioWorkspace;
public static readonly string ConverterTitle = "Code converter";
private static readonly string Intro = Environment.NewLine + Environment.NewLine + new string(Enumerable.Repeat('-', 80).ToArray()) + Environment.NewLine;
private readonly OutputWindow _outputWindow;
private readonly Cancellation _packageCancellation;
Expand Down
60 changes: 0 additions & 60 deletions Vsix/CodeConverterPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,58 +84,17 @@ public sealed class CodeConverterPackage : AsyncPackage

internal Cancellation PackageCancellation { get; } = new Cancellation();

private readonly AssemblyName _thisAssemblyName;
private readonly HashSet<string> _ourAssemblyNames;
/// <summary>
/// Initializes a new instance of package class.
/// </summary>
public CodeConverterPackage()
{
var thisAssembly = GetType().Assembly;
_thisAssemblyName = thisAssembly.GetName();
_ourAssemblyNames = new HashSet<string>(new[] { _thisAssemblyName }.Concat(thisAssembly.GetReferencedAssemblies().Where(a => a.Name.StartsWith("ICSharpCode"))).Select(a => a.FullName));
// Inside this method you can place any initialization code that does not require
// any Visual Studio service because at this point the package object is created but
// not sited yet inside Visual Studio environment. The place to do all the other
// initialization is the Initialize method.
}

// System.Threading.Tasks.Dataflow 4.5.24.0 shipped with VS2017 15.9.21+28307.1064 but we want to target 4.6.0.0
private Assembly LoadWithoutVersionForOurDependencies(object sender, ResolveEventArgs args)
{
var requestedAssemblyName = new AssemblyName(args.Name);
if (requestedAssemblyName.Version != null && IsThisExtensionRequestingAssembly()) {
return LoadAnyVersionOfAssembly(requestedAssemblyName);
}
return null;

}

private static Assembly LoadAnyVersionOfAssembly(AssemblyName assemblyName)
{
try {
return Assembly.Load(new AssemblyName(assemblyName.Name){CultureName = assemblyName.CultureName});
} catch (FileNotFoundException e) when (e.FileName.Contains("Microsoft.VisualStudio.LanguageServices") && ProbablyRequiresVsUpgrade) {
MessageBox.Show(
"Code Converter cannot find `Microsoft.VisualStudio.LanguageServices`. Please upgrade Visual Studio to version 15.9.3 or above.\r\n\r\n" +
"If after upgrading you still see this error, attach your activity log %AppData%\\Microsoft\\VisualStudio\\<version>\\ActivityLog.xml to a GitHub issue at https://github.com/icsharpcode/CodeConverter \r\n\r\n" +
"You can press Ctrl + C to copy this message",
"Upgrade Visual Studio", MessageBoxButton.OK);
return null;
}
}

private bool IsThisExtensionRequestingAssembly()
{
return GetPossibleRequestingAssemblies().Any(requesting => _ourAssemblyNames.Contains(requesting.FullName));
}

private IEnumerable<AssemblyName> GetPossibleRequestingAssemblies()
{
return new StackTrace().GetFrames().Select(f => f.GetMethod().DeclaringType?.Assembly)
.Select(a => a.GetName()).SkipWhile(a => Equals(a, _thisAssemblyName));
}

/// <summary>
/// Initialization of the package; this method is called right after the package is sited, so this is the place
/// where you can put all the initialization code that rely on services provided by VisualStudio.
Expand All @@ -155,25 +114,6 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
await base.InitializeAsync(cancellationToken, progress);
}

public static bool ProbablyRequiresVsUpgrade {
get {
var version = FullVsVersion;
return version == null || version < new Version(15, 9, 3, 0);
}
}

private static Version FullVsVersion {
get {
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "msenv.dll");

if (File.Exists(path)) {
var fvi = FileVersionInfo.GetVersionInfo(path);
return new Version(fvi.ProductMajorPart, fvi.ProductMinorPart, fvi.ProductBuildPart,
fvi.ProductPrivatePart);
} else return null;
}
}

internal OleMenuCommandWithBlockingStatus CreateCommand(Func<CancellationToken, Task> callbackAsync, CommandID menuCommandId)
{
return new OleMenuCommandWithBlockingStatus(JoinableTaskFactory, PackageCancellation, callbackAsync, menuCommandId);
Expand Down
4 changes: 2 additions & 2 deletions Vsix/ConvertCSToVBCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private async Task SolutionOrProjectMenuItemCallbackAsync(CancellationToken canc
var projects = VisualStudioInteraction.GetSelectedProjectsAsync(ProjectExtension);
await _codeConversion.ConvertProjectsAsync<CSToVBConversion>(await projects, cancellationToken);
} catch (Exception ex) {
await VisualStudioInteraction.ShowExceptionAsync(ServiceProvider, CodeConversion.ConverterTitle, ex);
await VisualStudioInteraction.ShowExceptionAsync(ex);
}
}

Expand All @@ -179,7 +179,7 @@ private async Task ConvertDocumentAsync(string documentPath, Span selected, Canc
try {
await _codeConversion.ConvertDocumentAsync<CSToVBConversion>(documentPath, selected, cancellationToken);
} catch (Exception ex) {
await VisualStudioInteraction.ShowExceptionAsync(ServiceProvider, CodeConversion.ConverterTitle, ex);
await VisualStudioInteraction.ShowExceptionAsync(ex);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Vsix/ConvertVBToCSCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private async Task SolutionOrProjectMenuItemCallbackAsync(CancellationToken canc
var projects = VisualStudioInteraction.GetSelectedProjectsAsync(ProjectExtension);
await _codeConversion.ConvertProjectsAsync<VBToCSConversion>(await projects, cancellationToken);
} catch (Exception ex) {
await VisualStudioInteraction.ShowExceptionAsync(ServiceProvider, CodeConversion.ConverterTitle, ex);
await VisualStudioInteraction.ShowExceptionAsync(ex);
}
}

Expand All @@ -179,7 +179,7 @@ private async Task ConvertDocumentAsync(string documentPath, Span selected, Canc
try {
await _codeConversion.ConvertDocumentAsync<VBToCSConversion>(documentPath, selected, cancellationToken);
} catch (Exception ex) {
await VisualStudioInteraction.ShowExceptionAsync(ServiceProvider, CodeConversion.ConverterTitle, ex);
await VisualStudioInteraction.ShowExceptionAsync(ex);
}
}
}
Expand Down
34 changes: 31 additions & 3 deletions Vsix/VisualStudioInteraction.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using EnvDTE;
using EnvDTE80;
using ICSharpCode.CodeConverter.Shared;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Editor;
using Microsoft.VisualStudio.Shell;
Expand Down Expand Up @@ -35,6 +38,24 @@ internal static class VisualStudioInteraction
internal static DTE2 Dte => m_Dte ?? (m_Dte = Package.GetGlobalService(typeof(DTE)) as DTE2);

private static CancellationToken CancelAllToken;
private static readonly Version m_LowestSupportedVersion = new Version(15, 7, 0, 0);
private static readonly Version m_FullVsVersion = GetFullVsVersion();
private static readonly string m_Title = "Code converter " + new AssemblyName(typeof(CodeConversion).Assembly.FullName).Version.ToString(3) + " - Visual Studio " + m_FullVsVersion;

private static Version GetFullVsVersion()
{
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "msenv.dll");

if (File.Exists(path)) {
var fvi = FileVersionInfo.GetVersionInfo(path);
return new Version(fvi.ProductMajorPart, fvi.ProductMinorPart, fvi.ProductBuildPart,
fvi.ProductPrivatePart);
} else {
return null;
}
}


internal static void Initialize(Cancellation packageCancellation)
{
CancelAllToken = packageCancellation.CancelAll;
Expand Down Expand Up @@ -86,12 +107,19 @@ public static async Task<ITextDocument> GetTextDocumentAsync(this IWpfTextViewHo
return textDocument;
}

public static async Task ShowExceptionAsync(IAsyncServiceProvider serviceProvider, string title, Exception ex)
public static async Task ShowExceptionAsync(Exception ex)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(CancelAllToken);
if (!CancelAllToken.IsCancellationRequested) {
MessageBox.Show($"An error has occured during conversion - press Ctrl+C to copy the details: {ex}",
title, MessageBoxButton.OK, MessageBoxImage.Error);
var versionMessageSuffix = "";
if (m_FullVsVersion < m_LowestSupportedVersion) {
versionMessageSuffix = $"{Environment.NewLine}This extension only supports VS {m_LowestSupportedVersion}+, you are currently using {m_FullVsVersion}";
}
if (m_FullVsVersion.Major < 16) {
versionMessageSuffix = $"{Environment.NewLine}Support for VS2017 (15.*) is likely to end this year. You're using: {m_FullVsVersion}";
}
MessageBox.Show($"An error has occured during conversion - press Ctrl+C to copy the details: {ex}{versionMessageSuffix}",
m_Title, MessageBoxButton.OK, MessageBoxImage.Error);
}
}

Expand Down

0 comments on commit b9ddf9e

Please sign in to comment.