From 36667e0ac155b30b8ac2d3dec4fc590c5f56515e Mon Sep 17 00:00:00 2001 From: Mikael Koskinen Date: Wed, 30 Dec 2020 13:12:19 +0200 Subject: [PATCH] Updated Roslyn catalog to support tagging --- .../RoslynPluginCatalog.cs | 17 ++++++- .../RoslynPluginCatalogOptions.cs | 7 ++- .../RoslynPluginCatalogTests.cs | 51 +++++++++++++++++-- 3 files changed, 67 insertions(+), 8 deletions(-) diff --git a/src/Weikio.PluginFramework.Catalogs.Roslyn/RoslynPluginCatalog.cs b/src/Weikio.PluginFramework.Catalogs.Roslyn/RoslynPluginCatalog.cs index a08dce7..cff96c9 100644 --- a/src/Weikio.PluginFramework.Catalogs.Roslyn/RoslynPluginCatalog.cs +++ b/src/Weikio.PluginFramework.Catalogs.Roslyn/RoslynPluginCatalog.cs @@ -8,6 +8,7 @@ using Microsoft.CodeAnalysis.Scripting; using Weikio.PluginFramework.Abstractions; using Weikio.PluginFramework.Catalogs.Roslyn; +using Weikio.PluginFramework.TypeFinding; // ReSharper disable once CheckNamespace namespace Weikio.PluginFramework.Catalogs @@ -88,9 +89,21 @@ public async Task Initialize() _assembly = await regularInitializer.CreateAssembly(); } - var options = new AssemblyPluginCatalogOptions { PluginNameOptions = _options.PluginNameOptions }; + var assemblyCatalogOptions = new AssemblyPluginCatalogOptions { PluginNameOptions = _options.PluginNameOptions}; - _catalog = new AssemblyPluginCatalog(_assembly, options); + if (_options.Tags?.Any() == true) + { + assemblyCatalogOptions.TypeFinderOptions = new TypeFinderOptions() { TypeFinderCriterias = new List() + { + new TypeFinderCriteria() + { + Query = (context, type) => true, + Tags = _options.Tags + } + } }; + } + + _catalog = new AssemblyPluginCatalog(_assembly, assemblyCatalogOptions); await _catalog.Initialize(); IsInitialized = true; diff --git a/src/Weikio.PluginFramework.Catalogs.Roslyn/RoslynPluginCatalogOptions.cs b/src/Weikio.PluginFramework.Catalogs.Roslyn/RoslynPluginCatalogOptions.cs index 0f99529..0b60e38 100644 --- a/src/Weikio.PluginFramework.Catalogs.Roslyn/RoslynPluginCatalogOptions.cs +++ b/src/Weikio.PluginFramework.Catalogs.Roslyn/RoslynPluginCatalogOptions.cs @@ -27,7 +27,7 @@ public Version PluginVersion set { _pluginVersion = value; - + PluginNameOptions.PluginVersionGenerator = (options, type) => _pluginVersion; } } @@ -42,5 +42,10 @@ public Version PluginVersion public List AdditionalReferences { get; set; } = new List(); public List AdditionalNamespaces { get; set; } = new List(); public PluginNameOptions PluginNameOptions { get; set; } = new PluginNameOptions(); + + /// + /// Gets or sets the tags assigned to plugin + /// + public List Tags { get; set; } = new List(); } } diff --git a/tests/unit/Weikio.PluginFramework.Catalogs.Roslyn.Tests/RoslynPluginCatalogTests.cs b/tests/unit/Weikio.PluginFramework.Catalogs.Roslyn.Tests/RoslynPluginCatalogTests.cs index 64d2ead..432bdf5 100644 --- a/tests/unit/Weikio.PluginFramework.Catalogs.Roslyn.Tests/RoslynPluginCatalogTests.cs +++ b/tests/unit/Weikio.PluginFramework.Catalogs.Roslyn.Tests/RoslynPluginCatalogTests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; @@ -65,13 +66,13 @@ public async Task ScriptAssemblyContainsValidVersion() // Act var type = (await TestHelpers.CreateCatalog(code)).Single(); - + // Assert var versionInfo = FileVersionInfo.GetVersionInfo(type.Assembly.Location); var fileVersion = versionInfo.FileVersion; Assert.NotNull(fileVersion); } - + [Fact] public async Task ScriptContainsVersion1000ByDefault() { @@ -80,13 +81,13 @@ public async Task ScriptContainsVersion1000ByDefault() // Act var type = (await TestHelpers.CreateCatalog(code)).Single(); - + // Assert var versionInfo = FileVersionInfo.GetVersionInfo(type.Assembly.Location); var fileVersion = versionInfo.FileVersion; Assert.Equal("1.0.0.0", fileVersion); } - + [Fact] public async Task CanHandleRegular() { @@ -310,7 +311,7 @@ public void RunThings() { PluginNameOptions = new PluginNameOptions() { PluginVersionGenerator = (nameOptions, type) => new Version(2, 0, 0) } }; - + var catalog = new RoslynPluginCatalog(code, options); // Act @@ -320,5 +321,45 @@ public void RunThings() // Assert Assert.Equal(new Version(2, 0, 0), plugin.Version); } + + [Fact] + public async Task CanTagCode() + { + // Arrange + var code = @"public class MyClass + { + public void RunThings() + { + var y = 0; + var a = 1; + + a = y + 10; + + Debug.WriteLine(y + a); + } + }"; + + var catalog = new RoslynPluginCatalog(code, new RoslynPluginCatalogOptions() { Tags = new List() { "CustomTag" } }); + + await catalog.Initialize(); + var plugin = catalog.Single(); + + Assert.Equal("CustomTag", plugin.Tag); + } + + [Fact] + public async Task CanTagScript() + { + // Arrange + var code = "Debug.WriteLine(\"Hello world!\");"; + + var catalog = new RoslynPluginCatalog(code, new RoslynPluginCatalogOptions() { Tags = new List() { "CustomTag" } }); + + await catalog.Initialize(); + + var plugin = catalog.Single(); + + Assert.Equal("CustomTag", plugin.Tag); + } } }