Skip to content

Commit 36667e0

Browse files
committed
Updated Roslyn catalog to support tagging
1 parent 5aef538 commit 36667e0

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

src/Weikio.PluginFramework.Catalogs.Roslyn/RoslynPluginCatalog.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Microsoft.CodeAnalysis.Scripting;
99
using Weikio.PluginFramework.Abstractions;
1010
using Weikio.PluginFramework.Catalogs.Roslyn;
11+
using Weikio.PluginFramework.TypeFinding;
1112

1213
// ReSharper disable once CheckNamespace
1314
namespace Weikio.PluginFramework.Catalogs
@@ -88,9 +89,21 @@ public async Task Initialize()
8889
_assembly = await regularInitializer.CreateAssembly();
8990
}
9091

91-
var options = new AssemblyPluginCatalogOptions { PluginNameOptions = _options.PluginNameOptions };
92+
var assemblyCatalogOptions = new AssemblyPluginCatalogOptions { PluginNameOptions = _options.PluginNameOptions};
9293

93-
_catalog = new AssemblyPluginCatalog(_assembly, options);
94+
if (_options.Tags?.Any() == true)
95+
{
96+
assemblyCatalogOptions.TypeFinderOptions = new TypeFinderOptions() { TypeFinderCriterias = new List<TypeFinderCriteria>()
97+
{
98+
new TypeFinderCriteria()
99+
{
100+
Query = (context, type) => true,
101+
Tags = _options.Tags
102+
}
103+
} };
104+
}
105+
106+
_catalog = new AssemblyPluginCatalog(_assembly, assemblyCatalogOptions);
94107
await _catalog.Initialize();
95108

96109
IsInitialized = true;

src/Weikio.PluginFramework.Catalogs.Roslyn/RoslynPluginCatalogOptions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public Version PluginVersion
2727
set
2828
{
2929
_pluginVersion = value;
30-
30+
3131
PluginNameOptions.PluginVersionGenerator = (options, type) => _pluginVersion;
3232
}
3333
}
@@ -42,5 +42,10 @@ public Version PluginVersion
4242
public List<Assembly> AdditionalReferences { get; set; } = new List<Assembly>();
4343
public List<string> AdditionalNamespaces { get; set; } = new List<string>();
4444
public PluginNameOptions PluginNameOptions { get; set; } = new PluginNameOptions();
45+
46+
/// <summary>
47+
/// Gets or sets the tags assigned to plugin
48+
/// </summary>
49+
public List<string> Tags { get; set; } = new List<string>();
4550
}
4651
}

tests/unit/Weikio.PluginFramework.Catalogs.Roslyn.Tests/RoslynPluginCatalogTests.cs

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.Linq;
45
using System.Threading.Tasks;
@@ -65,13 +66,13 @@ public async Task ScriptAssemblyContainsValidVersion()
6566

6667
// Act
6768
var type = (await TestHelpers.CreateCatalog(code)).Single();
68-
69+
6970
// Assert
7071
var versionInfo = FileVersionInfo.GetVersionInfo(type.Assembly.Location);
7172
var fileVersion = versionInfo.FileVersion;
7273
Assert.NotNull(fileVersion);
7374
}
74-
75+
7576
[Fact]
7677
public async Task ScriptContainsVersion1000ByDefault()
7778
{
@@ -80,13 +81,13 @@ public async Task ScriptContainsVersion1000ByDefault()
8081

8182
// Act
8283
var type = (await TestHelpers.CreateCatalog(code)).Single();
83-
84+
8485
// Assert
8586
var versionInfo = FileVersionInfo.GetVersionInfo(type.Assembly.Location);
8687
var fileVersion = versionInfo.FileVersion;
8788
Assert.Equal("1.0.0.0", fileVersion);
8889
}
89-
90+
9091
[Fact]
9192
public async Task CanHandleRegular()
9293
{
@@ -310,7 +311,7 @@ public void RunThings()
310311
{
311312
PluginNameOptions = new PluginNameOptions() { PluginVersionGenerator = (nameOptions, type) => new Version(2, 0, 0) }
312313
};
313-
314+
314315
var catalog = new RoslynPluginCatalog(code, options);
315316

316317
// Act
@@ -320,5 +321,45 @@ public void RunThings()
320321
// Assert
321322
Assert.Equal(new Version(2, 0, 0), plugin.Version);
322323
}
324+
325+
[Fact]
326+
public async Task CanTagCode()
327+
{
328+
// Arrange
329+
var code = @"public class MyClass
330+
{
331+
public void RunThings()
332+
{
333+
var y = 0;
334+
var a = 1;
335+
336+
a = y + 10;
337+
338+
Debug.WriteLine(y + a);
339+
}
340+
}";
341+
342+
var catalog = new RoslynPluginCatalog(code, new RoslynPluginCatalogOptions() { Tags = new List<string>() { "CustomTag" } });
343+
344+
await catalog.Initialize();
345+
var plugin = catalog.Single();
346+
347+
Assert.Equal("CustomTag", plugin.Tag);
348+
}
349+
350+
[Fact]
351+
public async Task CanTagScript()
352+
{
353+
// Arrange
354+
var code = "Debug.WriteLine(\"Hello world!\");";
355+
356+
var catalog = new RoslynPluginCatalog(code, new RoslynPluginCatalogOptions() { Tags = new List<string>() { "CustomTag" } });
357+
358+
await catalog.Initialize();
359+
360+
var plugin = catalog.Single();
361+
362+
Assert.Equal("CustomTag", plugin.Tag);
363+
}
323364
}
324365
}

0 commit comments

Comments
 (0)