Skip to content

Commit

Permalink
cleanup: upgrade dependencies in samples and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
natemcmaster committed Jan 31, 2021
1 parent 09ce384 commit 65144b1
Show file tree
Hide file tree
Showing 16 changed files with 107 additions and 133 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)src\StrongName.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<NoWarn>$(NoWarn);NU5105</NoWarn>
<WarningsNotAsErrors>$(WarningsNotAsErrors);1591</WarningsNotAsErrors>
<WarningsNotAsErrors>$(WarningsNotAsErrors);1591;RS0037</WarningsNotAsErrors>

<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<GenerateFullPaths Condition="'$(TERM_PROGRAM)' == 'vscode'">true</GenerateFullPaths>
Expand Down
60 changes: 27 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,41 +115,35 @@ This is just one way to implement a host. More examples of how to use plugins ca
```csharp
using McMaster.NETCore.Plugins;

public class Program
var loaders = new List<PluginLoader>();

// create plugin loaders
var pluginsDir = Path.Combine(AppContext.BaseDirectory, "plugins");
foreach (var dir in Directory.GetDirectories(pluginsDir))
{
public static void Main(string[] args)
var dirName = Path.GetFileName(dir);
var pluginDll = Path.Combine(dir, dirName + ".dll");
if (File.Exists(pluginDll))
{
var loaders = new List<PluginLoader>();

// create plugin loaders
var pluginsDir = Path.Combine(AppContext.BaseDirectory, "plugins");
foreach (var dir in Directory.GetDirectories(pluginsDir))
{
var dirName = Path.GetFileName(dir);
var pluginDll = Path.Combine(dir, dirName + ".dll");
if (File.Exists(pluginDll))
{
var loader = PluginLoader.CreateFromAssemblyFile(
pluginDll,
sharedTypes: new [] { typeof(IPlugin) });
loaders.Add(loader);
}
}

// Create an instance of plugin types
foreach (var loader in loaders)
{
foreach (var pluginType in loader
.LoadDefaultAssembly()
.GetTypes()
.Where(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract))
{
// This assumes the implementation of IPlugin has a parameterless constructor
IPlugin plugin = (IPlugin)Activator.CreateInstance(pluginType);

Console.WriteLine($"Created plugin instance '{plugin.GetName()}'.");
}
}
var loader = PluginLoader.CreateFromAssemblyFile(
pluginDll,
sharedTypes: new [] { typeof(IPlugin) });
loaders.Add(loader);
}
}

// Create an instance of plugin types
foreach (var loader in loaders)
{
foreach (var pluginType in loader
.LoadDefaultAssembly()
.GetTypes()
.Where(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract))
{
// This assumes the implementation of IPlugin has a parameterless constructor
IPlugin plugin = (IPlugin)Activator.CreateInstance(pluginType);

Console.WriteLine($"Created plugin instance '{plugin.GetName()}'.");
}
}
```
Expand Down
3 changes: 0 additions & 3 deletions samples/Directory.Build.targets

This file was deleted.

2 changes: 1 addition & 1 deletion samples/hello-world/HostApp/HostApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
60 changes: 26 additions & 34 deletions samples/hello-world/HostApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,37 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using HelloWorld;
using McMaster.NETCore.Plugins;

namespace HelloWorld
var loaders = new List<PluginLoader>();

// create plugin loaders
var pluginsDir = Path.Combine(AppContext.BaseDirectory, "plugins");
foreach (var dir in Directory.GetDirectories(pluginsDir))
{
public class Program
var dirName = Path.GetFileName(dir);
var pluginDll = Path.Combine(dir, dirName + ".dll");
if (File.Exists(pluginDll))
{
public static void Main(string[] args)
{
var loaders = new List<PluginLoader>();

// create plugin loaders
var pluginsDir = Path.Combine(AppContext.BaseDirectory, "plugins");
foreach (var dir in Directory.GetDirectories(pluginsDir))
{
var dirName = Path.GetFileName(dir);
var pluginDll = Path.Combine(dir, dirName + ".dll");
if (File.Exists(pluginDll))
{
var loader = PluginLoader.CreateFromAssemblyFile(
pluginDll,
sharedTypes: new[] { typeof(IPlugin) });
loaders.Add(loader);
}
}
var loader = PluginLoader.CreateFromAssemblyFile(
pluginDll,
sharedTypes: new[] { typeof(IPlugin) });
loaders.Add(loader);
}
}

// Create an instance of plugin types
foreach (var loader in loaders)
{
foreach (var pluginType in loader
.LoadDefaultAssembly()
.GetTypes()
.Where(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract))
{
// This assumes the implementation of IPlugin has a parameterless constructor
var plugin = Activator.CreateInstance(pluginType) as IPlugin;
// Create an instance of plugin types
foreach (var loader in loaders)
{
foreach (var pluginType in loader
.LoadDefaultAssembly()
.GetTypes()
.Where(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract))
{
// This assumes the implementation of IPlugin has a parameterless constructor
var plugin = Activator.CreateInstance(pluginType) as IPlugin;

Console.WriteLine($"Created plugin instance '{plugin?.GetName()}'.");
}
}
}
Console.WriteLine($"Created plugin instance '{plugin?.GetName()}'.");
}
}
2 changes: 1 addition & 1 deletion samples/hot-reload/HotReloadApp/HotReloadApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
67 changes: 29 additions & 38 deletions samples/hot-reload/HotReloadApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,34 @@
using System.Threading.Tasks;
using McMaster.NETCore.Plugins;

namespace HostApp
var pluginPath = args[0];
var loader = PluginLoader.CreateFromAssemblyFile(pluginPath,
config => config.EnableHotReload = true);

loader.Reloaded += ShowPluginInfo;

var cts = new CancellationTokenSource();
Console.CancelKeyPress += (_, __) => cts.Cancel();

// Show info on first load
InvokePlugin(loader);

await Task.Delay(-1, cts.Token);

static void ShowPluginInfo(object sender, PluginReloadedEventArgs eventArgs)
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("HotReloadApp: ");
Console.ResetColor();
Console.WriteLine("plugin was reloaded");
InvokePlugin(eventArgs.Loader);
}

static void InvokePlugin(PluginLoader loader)
{
internal class Program
{
public static async Task Main(string[] args)
{
var pluginPath = args[0];
var loader = PluginLoader.CreateFromAssemblyFile(pluginPath,
config => config.EnableHotReload = true);

loader.Reloaded += ShowPluginInfo;

var cts = new CancellationTokenSource();
Console.CancelKeyPress += (_, __) => cts.Cancel();

// Show info on first load
InvokePlugin(loader);

await Task.Delay(-1, cts.Token);
}

private static void ShowPluginInfo(object sender, PluginReloadedEventArgs eventArgs)
{
Console.ForegroundColor = ConsoleColor.Blue;
Console.Write("HotReloadApp: ");
Console.ResetColor();
Console.WriteLine("plugin was reloaded");
InvokePlugin(eventArgs.Loader);
}

private static void InvokePlugin(PluginLoader loader)
{
var assembly = loader.LoadDefaultAssembly();
assembly
.GetType("TimestampedPlugin.InfoDisplayer", throwOnError: true)
!.GetMethod("Print")
!.Invoke(null, null);
}
}
var assembly = loader.LoadDefaultAssembly();
assembly
.GetType("TimestampedPlugin.InfoDisplayer", throwOnError: true)
!.GetMethod("Print")
!.Invoke(null, null);
}
4 changes: 2 additions & 2 deletions samples/hot-reload/TimestampedPlugin/TimestampedPlugin.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand All @@ -12,7 +12,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Data.Sqlite" Version="3.1.0" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="5.0.0" />
</ItemGroup>

</Project>
6 changes: 3 additions & 3 deletions src/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
</ItemGroup>

<ItemGroup Condition="'$(DisablePublicApiAnalyzer)' != 'true'">
<PackageReference Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="2.9.8" PrivateAssets="All" />
<PackageReference Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.2" PrivateAssets="All" />
<AdditionalFiles Include="PublicAPI.Shipped.txt" />
<AdditionalFiles Include="PublicAPI.Unshipped.txt" />
</ItemGroup>

<Target Name="UpdateCiSettings" Condition="'$(CI)' == 'true'">
<Message Importance="High" Text="##vso[build.updatebuildnumber]$(PackageVersion)" />
<Target Name="UpdateCiSettings" Condition="'$(CI)' == 'true' AND '$(IsPackable)' == 'true'">
<Message Importance="High" Text="::set-output name=package_version::$(PackageVersion)" />
</Target>

</Project>
6 changes: 3 additions & 3 deletions src/Plugins/McMaster.NETCore.Plugins.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ See https://github.com/natemcmaster/DotNetCorePlugins/blob/master/README.md for
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="3.1.0" />
<PackageReference Include="System.Text.Json" Version="4.7.0" Condition="'$(TargetFramework)' == 'netcoreapp2.0'" />
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="3.1.6" />
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="5.0.0" />
<PackageReference Include="System.Text.Json" Version="5.0.1" Condition="'$(TargetFramework)' == 'netcoreapp2.0'" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions test/Plugins.Tests/BasicAssemblyLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace McMaster.NETCore.Plugins.Tests
{
public class BasicAssemblyLoaderTests
{
#if NETCOREAPP3_1
#if !NETCOREAPP2_1
[Fact]
public void PluginLoaderCanUnload()
{
Expand Down Expand Up @@ -147,7 +147,7 @@ private IFruit GetPlátano()
{
var path = TestResources.GetTestProjectAssembly("Plátano");
var loader = PluginLoader.CreateFromAssemblyFile(path,
#if NETCOREAPP3_1
#if !NETCOREAPP2_1
isUnloadable: true,
#endif
sharedTypes: new[] { typeof(IFruit) });
Expand Down
10 changes: 5 additions & 5 deletions test/Plugins.Tests/McMaster.NETCore.Plugins.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;netcoreapp2.1</TargetFrameworks>
<TargetFrameworks>net5.0;netcoreapp3.1;netcoreapp2.1</TargetFrameworks>
<DefaultItemExcludes>$(DefaultItemExcludes);TestResults\**</DefaultItemExcludes>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="McMaster.Extensions.Xunit" Version="0.1.0" />
<PackageReference Include="coverlet.collector" Version="3.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<PackageReference Include="coverlet.collector" Version="3.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
</ItemGroup>

<ItemGroup>
Expand All @@ -31,7 +31,7 @@
<TestProject Include="..\TestProjects\XunitSample\XunitSample.csproj" />
<TestProject Include="..\TestProjects\SqlClientApp\SqlClientApp.csproj" />
<TestProject Include="..\TestProjects\TransitivePlugin\TransitivePlugin.csproj" />
<TestProject Include="..\TestProjects\NativeDependency\NativeDependency.csproj" Condition=" '$(TargetFramework)' == 'netcoreapp3.1' " />
<TestProject Include="..\TestProjects\NativeDependency\NativeDependency.csproj" Condition=" '$(TargetFramework)' != 'netcoreapp2.1' " />
<PublishedTestProject Include="..\TestProjects\PowerShellPlugin\PowerShellPlugin.csproj" />
<MultitargetTestProject Include="..\TestProjects\WithOwnPlugins\WithOwnPlugins.csproj" />
</ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions test/Plugins.Tests/ShadowCopyTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Nate McMaster.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

#if NETCOREAPP3_1
#if !NETCOREAPP2_1

using Xunit;

Expand All @@ -17,11 +17,11 @@ public void DoesNotThrowWhenLoadingSameNativeDependecyMoreThanOnce()
using var loader = PluginLoader
.CreateFromAssemblyFile(samplePath, config => config.EnableHotReload = true);

var nativeDependecyLoadMethod = loader.LoadDefaultAssembly()
var nativeDependencyLoadMethod = loader.LoadDefaultAssembly()
?.GetType("NativeDependency.NativeDependencyLoader")
?.GetMethod("Load");

var exception = Record.Exception(() => nativeDependecyLoadMethod?.Invoke(null, null));
var exception = Record.Exception(() => nativeDependencyLoadMethod?.Invoke(null, null));

Assert.Null(exception);
}
Expand Down
4 changes: 2 additions & 2 deletions test/TestProjects/NativeDependency/NativeDependency.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Data.SQLite" Version="1.0.113.1" />
<PackageReference Include="System.Data.SQLite" Version="1.0.113.7" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion test/TestProjects/WithOwnPlugins/WithOwnPlugins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public bool TryLoadPluginsInCustomContext(AssemblyLoadContext? callingContext)
throw new ArgumentException("The context of the caller is the context of this assembly. This invalidates the test.");
}

#if NETCOREAPP3_1
#if !NETCOREAPP2_1
/*
Ensure the source calling context does not have our plugin's interfaces loaded.
This guarantees that the Assembly cannot possibly unify with the default load context.
Expand Down
2 changes: 1 addition & 1 deletion test/TestProjects/WithOwnPlugins/WithOwnPlugins.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;netcoreapp2.1</TargetFrameworks>
<TargetFrameworks>net5.0;netcoreapp3.1;netcoreapp2.1</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 65144b1

Please sign in to comment.