Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#34 Configuration for NuGet #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions samples/WebApp/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Extensions.Hosting;
using Weikio.PluginFramework.Samples.Shared;
using Weikio.PluginFramework.Abstractions;
using Weikio.PluginFramework.AspNetCore;
using Weikio.PluginFramework.Catalogs;

namespace WebApp
Expand Down
4 changes: 3 additions & 1 deletion samples/WebAppWithAppSettings/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Weikio.PluginFramework.Catalogs.NuGet;
using Weikio.PluginFramework.Samples.Shared;
using Weikio.PluginFramework.TypeFinding;

Expand All @@ -13,7 +14,8 @@ public void ConfigureServices(IServiceCollection services)
{
TypeFinderOptions.Defaults.TypeFinderCriterias.Add(TypeFinderCriteriaBuilder.Create().Implements<IOperator>().Tag("MathOperator"));
TypeFinderOptions.Defaults.TypeFinderCriterias.Add(TypeFinderCriteriaBuilder.Create().Tag("All"));
services.AddPluginFramework();
services.AddPluginFramework()
.AddNugetConfiguration();

services.AddControllers();
}
Expand Down
1 change: 1 addition & 0 deletions samples/WebAppWithAppSettings/WebAppWithAppSettings.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Weikio.PluginFramework.AspNetCore\Weikio.PluginFramework.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\Weikio.PluginFramework.Catalogs.NuGet\Weikio.PluginFramework.Catalogs.NuGet.csproj" />
<ProjectReference Include="..\Shared\Weikio.PluginFramework.Samples.SharedPlugins\Weikio.PluginFramework.Samples.SharedPlugins.csproj" />
<ProjectReference Include="..\Shared\Weikio.PluginFramework.Samples.Shared\Weikio.PluginFramework.Samples.Shared.csproj" />
<ProjectReference Include="..\WebAppPluginsLibrary\WebAppPluginsLibrary.csproj" />
Expand Down
2 changes: 2 additions & 0 deletions samples/WebAppWithDelegate/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Weikio.PluginFramework.AspNetCore;
using Weikio.PluginFramework.Catalogs;
using Weikio.PluginFramework.Catalogs.Delegates;

Expand Down Expand Up @@ -54,6 +55,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddPluginFramework()
.AddPluginCatalog(new CompositePluginCatalog(actionCatalog, funcCatalog, funcWithExternalServiceCatalog));


services.AddSingleton<ExternalService>();
services.AddControllers();
}
Expand Down
1 change: 1 addition & 0 deletions samples/WebAppWithNuget/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Weikio.PluginFramework.AspNetCore;
using Weikio.PluginFramework.Catalogs;
using Weikio.PluginFramework.Catalogs.NuGet;
using Weikio.PluginFramework.Samples.Shared;
Expand Down
1 change: 1 addition & 0 deletions samples/WebAppWithRoslyn/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Weikio.PluginFramework.AspNetCore;
using Weikio.PluginFramework.Catalogs;
using Weikio.PluginFramework.Catalogs.Roslyn;

Expand Down
12 changes: 9 additions & 3 deletions src/PluginFramework.sln
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WinFormsApp", "..\samples\W
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WinFormsPluginsLibrary", "..\samples\WinFormsPluginsLibrary\WinFormsPluginsLibrary.csproj", "{E92B0C5D-FFAC-4AB9-B069-869AEED565B0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Weikio.PluginFramework.Configuration", "Weikio.PluginFramework.Configuration\Weikio.PluginFramework.Configuration.csproj", "{882BB58E-D256-4BBF-8C5F-80C4FA39B775}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Weikio.PluginFramework.Configuration", "Weikio.PluginFramework.Configuration\Weikio.PluginFramework.Configuration.csproj", "{882BB58E-D256-4BBF-8C5F-80C4FA39B775}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebAppWithAppSettings", "..\samples\WebAppWithAppSettings\WebAppWithAppSettings.csproj", "{A5FAE1A5-74A0-4A83-9520-DCABF6610ADE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebAppWithAppSettings", "..\samples\WebAppWithAppSettings\WebAppWithAppSettings.csproj", "{A5FAE1A5-74A0-4A83-9520-DCABF6610ADE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebAppPluginsLibrary", "..\samples\WebAppPluginsLibrary\WebAppPluginsLibrary.csproj", "{38CCE0F7-F998-4766-A14A-44C047E8D0AA}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebAppPluginsLibrary", "..\samples\WebAppPluginsLibrary\WebAppPluginsLibrary.csproj", "{38CCE0F7-F998-4766-A14A-44C047E8D0AA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Weikio.PluginFramework.Abstractions.DependencyInjection", "Weikio.PluginFramework.Abstractions.DependencyInjection\Weikio.PluginFramework.Abstractions.DependencyInjection.csproj", "{64FD5284-87A2-4A6D-8CCD-324CDD944A11}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -185,6 +187,10 @@ Global
{38CCE0F7-F998-4766-A14A-44C047E8D0AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{38CCE0F7-F998-4766-A14A-44C047E8D0AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{38CCE0F7-F998-4766-A14A-44C047E8D0AA}.Release|Any CPU.Build.0 = Release|Any CPU
{64FD5284-87A2-4A6D-8CCD-324CDD944A11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{64FD5284-87A2-4A6D-8CCD-324CDD944A11}.Debug|Any CPU.Build.0 = Debug|Any CPU
{64FD5284-87A2-4A6D-8CCD-324CDD944A11}.Release|Any CPU.ActiveCfg = Release|Any CPU
{64FD5284-87A2-4A6D-8CCD-324CDD944A11}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.DependencyInjection;

namespace Weikio.PluginFramework.Abstractions.DependencyInjection
{
public interface IPluginFrameworkBuilder
{
IServiceCollection Services { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.1.5" />
</ItemGroup>

</Project>
18 changes: 18 additions & 0 deletions src/Weikio.PluginFramework.AspNetCore/PluginFrameworkBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Weikio.PluginFramework.Abstractions.DependencyInjection;

namespace Weikio.PluginFramework.AspNetCore
{
public class PluginFrameworkBuilder : IPluginFrameworkBuilder
{
public PluginFrameworkBuilder(IServiceCollection services)
{
Services = services;
}

public IServiceCollection Services { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Weikio.PluginFramework.Abstractions;
using Weikio.PluginFramework.Abstractions.DependencyInjection;

namespace Weikio.PluginFramework.AspNetCore
{
public static class PluginFrameworkBuilderExtensions
{
public static IPluginFrameworkBuilder AddPluginCatalog(this IPluginFrameworkBuilder builder, IPluginCatalog pluginCatalog)
{
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IPluginCatalog), pluginCatalog));

return builder;
}

public static IPluginFrameworkBuilder AddPluginType<T>(this IPluginFrameworkBuilder builder, ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
where T : class
{
var serviceDescriptorEnumerable = new ServiceDescriptor(typeof(IEnumerable<T>), sp =>
{
var pluginProvider = sp.GetService<PluginProvider>();
var result = pluginProvider.GetTypes<T>();

return result.AsEnumerable();
}, serviceLifetime);

var serviceDescriptorSingle = new ServiceDescriptor(typeof(T), sp =>
{
var pluginProvider = sp.GetService<PluginProvider>();
var result = pluginProvider.GetTypes<T>();

return result.FirstOrDefault();
}, serviceLifetime);

builder.Services.Add(serviceDescriptorEnumerable);
builder.Services.Add(serviceDescriptorSingle);

return builder;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using Weikio.PluginFramework.Abstractions;
using Weikio.PluginFramework.Abstractions.DependencyInjection;
using Weikio.PluginFramework.AspNetCore;
using Weikio.PluginFramework.Catalogs;
using Weikio.PluginFramework.Configuration;
Expand All @@ -21,8 +22,10 @@ namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddPluginFramework(this IServiceCollection services, Action<PluginFrameworkOptions> configure = null)
public static IPluginFrameworkBuilder AddPluginFramework(this IServiceCollection services, Action<PluginFrameworkOptions> configure = null)
{
var frameworkBuilder = new PluginFrameworkBuilder(services);

if (configure != null)
{
services.Configure(configure);
Expand Down Expand Up @@ -56,7 +59,7 @@ public static IServiceCollection AddPluginFramework(this IServiceCollection serv

if (string.IsNullOrWhiteSpace(aspNetCoreControllerAssemblyLocation))
{
return services;
return frameworkBuilder;
}

var aspNetCoreLocation = Path.GetDirectoryName(aspNetCoreControllerAssemblyLocation);
Expand All @@ -71,11 +74,13 @@ public static IServiceCollection AddPluginFramework(this IServiceCollection serv
PluginLoadContextOptions.Defaults.AdditionalRuntimePaths.Add(aspNetCoreLocation);
}

return services;
return frameworkBuilder;
}

public static IServiceCollection AddPluginFramework<TType>(this IServiceCollection services, string dllPath = "") where TType : class
public static IPluginFrameworkBuilder AddPluginFramework<TType>(this IServiceCollection services, string dllPath = "") where TType : class
{
var frameworkBuilder = new PluginFrameworkBuilder(services);

services.AddPluginFramework();

if (string.IsNullOrWhiteSpace(dllPath))
Expand All @@ -97,11 +102,11 @@ public static IServiceCollection AddPluginFramework<TType>(this IServiceCollecti
.Build();

var catalog = new FolderPluginCatalog(dllPath, typeFinderCriteria);
services.AddPluginCatalog(catalog);
frameworkBuilder.AddPluginCatalog(catalog);

services.AddPluginType<TType>();
frameworkBuilder.AddPluginType<TType>();

return services;
return frameworkBuilder;
}

/// <summary>
Expand Down Expand Up @@ -170,37 +175,5 @@ private static IServiceCollection AddConfiguration(this IServiceCollection servi

return services;
}

public static IServiceCollection AddPluginCatalog(this IServiceCollection services, IPluginCatalog pluginCatalog)
{
services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IPluginCatalog), pluginCatalog));

return services;
}

public static IServiceCollection AddPluginType<T>(this IServiceCollection services, ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
where T : class
{
var serviceDescriptorEnumerable = new ServiceDescriptor(typeof(IEnumerable<T>), sp =>
{
var pluginProvider = sp.GetService<PluginProvider>();
var result = pluginProvider.GetTypes<T>();

return result.AsEnumerable();
}, serviceLifetime);

var serviceDescriptorSingle = new ServiceDescriptor(typeof(T), sp =>
{
var pluginProvider = sp.GetService<PluginProvider>();
var result = pluginProvider.GetTypes<T>();

return result.FirstOrDefault();
}, serviceLifetime);

services.Add(serviceDescriptorEnumerable);
services.Add(serviceDescriptorSingle);

return services;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Weikio.PluginFramework.Abstractions.DependencyInjection\Weikio.PluginFramework.Abstractions.DependencyInjection.csproj" />
<ProjectReference Include="..\Weikio.PluginFramework.Abstractions\Weikio.PluginFramework.Abstractions.csproj" />
<ProjectReference Include="..\Weikio.PluginFramework.Configuration\Weikio.PluginFramework.Configuration.csproj" />
<ProjectReference Include="..\Weikio.PluginFramework\Weikio.PluginFramework.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.Configuration;
using Weikio.NugetDownloader;
using Weikio.PluginFramework.Abstractions;
using Weikio.PluginFramework.Configuration.Converters;
using Weikio.PluginFramework.TypeFinding;

namespace Weikio.PluginFramework.Catalogs.NuGet
{
public class NugetFeedCatalogConfigurationConverter : IConfigurationToCatalogConverter
{
public bool CanConvert(string type)
{
return string.Equals(type, "NugetFeed", StringComparison.InvariantCultureIgnoreCase);
}

public IPluginCatalog Convert(IConfigurationSection configuration)
{
var feedName = configuration.GetValue<string>("Name")
?? throw new ArgumentException("Plugin Framework's NugetFeedCatalog requires a NuGet package name.");

var feedUrl = configuration.GetValue<string>("Feed")
?? throw new ArgumentException("Plugin Framework's NugetFeedCatalog requires a NuGet feed url.");

var searchTerm = configuration.GetValue<string>("SearchTerm")
?? throw new ArgumentException("Plugin Framework's NugetFeedCatalog requires a NuGet search term");

var options = new NugetFeedOptions();
configuration.Bind("Options", options);

return new NugetFeedPluginCatalog(new NuGetFeed(feedName, feedUrl)
{
Username = options.UserName,
Password = options.Password
}, searchTerm, options.IncludePreRelease, options.MaxPackages ?? 128);
}
}

class NugetFeedOptions
{
public string UserName { get; set; }

public string Password { get; set; }

public bool IncludePreRelease { get; set; }

public int? MaxPackages { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Extensions.Configuration;
using Weikio.PluginFramework.Abstractions;
using Weikio.PluginFramework.Configuration.Converters;

namespace Weikio.PluginFramework.Catalogs.NuGet
{
public class NugetPackageCatalogConfigurationConverter : IConfigurationToCatalogConverter
{
public bool CanConvert(string type)
{
return string.Equals(type, "NugetPackage", StringComparison.InvariantCultureIgnoreCase);
}

public IPluginCatalog Convert(IConfigurationSection configuration)
{
var packageName = configuration.GetValue<string>("Name")
?? throw new ArgumentException("Plugin Framework's NugetPackageCatalog requires a NuGet package name.");

var packageVersion = configuration.GetValue<string>("Version")
?? throw new ArgumentException("Plugin Framework's NugetPackageCatalog requires a NuGet package version.");

return new NugetPackagePluginCatalog(packageName, packageVersion, true);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using Weikio.PluginFramework.Abstractions.DependencyInjection;
using Weikio.PluginFramework.Configuration.Converters;

namespace Weikio.PluginFramework.Catalogs.NuGet
{
public static class PluginFrameworkBuilderExtensions
{
public static IPluginFrameworkBuilder AddNugetConfiguration(this IPluginFrameworkBuilder builder)
{
builder.Services.AddTransient(typeof(IConfigurationToCatalogConverter), typeof(NugetFeedCatalogConfigurationConverter));
builder.Services.AddTransient(typeof(IConfigurationToCatalogConverter), typeof(NugetPackageCatalogConfigurationConverter));

return builder;
}
}
}
Loading