Skip to content

Commit

Permalink
Updated Roslyn catalog to support tagging
Browse files Browse the repository at this point in the history
  • Loading branch information
mikoskinen committed Dec 30, 2020
1 parent 5aef538 commit 36667e0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
17 changes: 15 additions & 2 deletions src/Weikio.PluginFramework.Catalogs.Roslyn/RoslynPluginCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<TypeFinderCriteria>()
{
new TypeFinderCriteria()
{
Query = (context, type) => true,
Tags = _options.Tags
}
} };
}

_catalog = new AssemblyPluginCatalog(_assembly, assemblyCatalogOptions);
await _catalog.Initialize();

IsInitialized = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public Version PluginVersion
set
{
_pluginVersion = value;

PluginNameOptions.PluginVersionGenerator = (options, type) => _pluginVersion;
}
}
Expand All @@ -42,5 +42,10 @@ public Version PluginVersion
public List<Assembly> AdditionalReferences { get; set; } = new List<Assembly>();
public List<string> AdditionalNamespaces { get; set; } = new List<string>();
public PluginNameOptions PluginNameOptions { get; set; } = new PluginNameOptions();

/// <summary>
/// Gets or sets the tags assigned to plugin
/// </summary>
public List<string> Tags { get; set; } = new List<string>();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -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()
{
Expand All @@ -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()
{
Expand Down Expand Up @@ -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
Expand All @@ -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<string>() { "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<string>() { "CustomTag" } });

await catalog.Initialize();

var plugin = catalog.Single();

Assert.Equal("CustomTag", plugin.Tag);
}
}
}

0 comments on commit 36667e0

Please sign in to comment.