diff --git a/src/Plugins/SourceGenerator.Foundations.Windows/EnvironmentType.cs b/src/Plugins/SourceGenerator.Foundations.Windows/EnvironmentType.cs new file mode 100644 index 0000000..8485519 --- /dev/null +++ b/src/Plugins/SourceGenerator.Foundations.Windows/EnvironmentType.cs @@ -0,0 +1,10 @@ +namespace SGF +{ + internal enum EnvironmentType + { + Unknown, + VisualStudio, + VSCode, + Rider + } +} diff --git a/src/Plugins/SourceGenerator.Foundations.Windows/VSCodeEnvironment.cs b/src/Plugins/SourceGenerator.Foundations.Windows/VSCodeEnvironment.cs deleted file mode 100644 index c0622b6..0000000 --- a/src/Plugins/SourceGenerator.Foundations.Windows/VSCodeEnvironment.cs +++ /dev/null @@ -1,24 +0,0 @@ -using Serilog.Core; -using System; -using System.Collections.Generic; - -namespace SGF -{ - /// - /// Represents a enviroment where the user is authoring code in VSCode - /// - internal class VSCodeEnvironment : IDevelopmentEnviroment - { - /// - public bool AttachDebugger(int processId) - { - return false; - } - - /// - public IEnumerable GetLogSinks() - { - return Array.Empty(); - } - } -} diff --git a/src/Plugins/SourceGenerator.Foundations.Windows/VisualStudioEnvironment.cs b/src/Plugins/SourceGenerator.Foundations.Windows/VisualStudioEnvironment.cs deleted file mode 100644 index ed2cda0..0000000 --- a/src/Plugins/SourceGenerator.Foundations.Windows/VisualStudioEnvironment.cs +++ /dev/null @@ -1,26 +0,0 @@ -using Serilog.Core; -using SGF.Interop.VisualStudio; -using System.Collections.Generic; -using System.Diagnostics; - -namespace SGF -{ - /// - /// Represents a enviroment where the user is authoring code in Visual Studio - /// - internal class VisualStudioEnvironment : IDevelopmentEnviroment - { - /// - public bool AttachDebugger(int processId) - { - VisualStudioInterop.AttachDebugger(); - return true; - } - - /// - public IEnumerable GetLogSinks() - { - yield return new VisualStudioLogEventSink(); - } - } -} diff --git a/src/Plugins/SourceGenerator.Foundations.Windows/WindowsDevelopmentEnvironment.cs b/src/Plugins/SourceGenerator.Foundations.Windows/WindowsDevelopmentEnvironment.cs new file mode 100644 index 0000000..56423b2 --- /dev/null +++ b/src/Plugins/SourceGenerator.Foundations.Windows/WindowsDevelopmentEnvironment.cs @@ -0,0 +1,51 @@ +using Serilog.Core; +using SGF.Interop.VisualStudio; +using System; +using System.Collections.Generic; +using System.Diagnostics; + +namespace SGF +{ + + /// + /// Represents a enviroment where the user is authoring code in Visual Studio + /// + internal class WindowsDevelopmentEnvironment : IDevelopmentEnviroment + { + public EnvironmentType Type { get; } + + + public WindowsDevelopmentEnvironment() + { + Type = EnvironmentType.VisualStudio; + + if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("VisualStudioVersion"))) + { + Type = EnvironmentType.VisualStudio; + } + } + + /// + public bool AttachDebugger(int processId) + { + switch (Type) + { + case EnvironmentType.VisualStudio: + VisualStudioInterop.AttachDebugger(); + break; + } + return true; + } + + /// + public IEnumerable GetLogSinks() + { + switch (Type) + { + case EnvironmentType.VisualStudio: + yield return new VisualStudioLogEventSink(); + break; + } + } + } +} diff --git a/src/SourceGenerator.Foundations.Contracts/DevelopmentEnviroment.cs b/src/SourceGenerator.Foundations.Contracts/DevelopmentEnviroment.cs index 50467db..72d4b7f 100644 --- a/src/SourceGenerator.Foundations.Contracts/DevelopmentEnviroment.cs +++ b/src/SourceGenerator.Foundations.Contracts/DevelopmentEnviroment.cs @@ -58,17 +58,21 @@ string assemblyVersion try { + Assembly? environmentAssembly = null; Type? developmentEnvironment = null; if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { AssemblyName windowsAssemblyName = new AssemblyName($"SourceGenerator.Foundations.Windows, Version={assemblyVersion}, Culture=neutral, PublicKeyToken=null"); - Assembly windowsEnvironmentAssembly = Assembly.Load(windowsAssemblyName); + environmentAssembly = Assembly.Load(windowsAssemblyName); + } - if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("VisualStudioVersion"))) - { - developmentEnvironment = windowsEnvironmentAssembly.GetType("SGF.VisualStudioEnvironment"); - } + if(environmentAssembly != null) + { + developmentEnvironment = environmentAssembly + .GetTypes() + .Where(typeof(IDevelopmentEnviroment).IsAssignableFrom) + .FirstOrDefault(); } if (developmentEnvironment != null)