Skip to content

Commit

Permalink
merge the code from 'dynamic load branch' to 'master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Lamond Lu committed Oct 20, 2019
2 parents 65e9530 + 9919d7a commit 9f3be16
Show file tree
Hide file tree
Showing 35 changed files with 4,501 additions and 68 deletions.
8 changes: 2 additions & 6 deletions DemoPlugin1/DemoPlugin1.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<OutputType>Library</OutputType>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath></OutputPath>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.Extensions" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.ViewFeatures" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DemoReferenceLibrary\DemoReferenceLibrary.csproj" />
<ProjectReference Include="..\Mystique.Core\Mystique.Core.csproj" />
Expand Down
16 changes: 16 additions & 0 deletions DemoPlugin2/Controllers/Plugin1Controller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using DemoReferenceLibrary;
using Microsoft.AspNetCore.Mvc;

namespace DemoPlugin2.Controllers
{
[Area("DemoPlugin2")]
public class Plugin1Controller : Controller
{
public IActionResult HelloWorld()
{
var content = new Demo().SayHello();
ViewBag.Content = content;
return View();
}
}
}
24 changes: 24 additions & 0 deletions DemoPlugin2/DemoPlugin2.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<OutputType>Library</OutputType>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<OutputPath></OutputPath>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\DemoReferenceLibrary\DemoReferenceLibrary.csproj" />
<ProjectReference Include="..\Mystique.Core\Mystique.Core.csproj" />
</ItemGroup>


<ItemGroup>
<None Update="Views\**">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
37 changes: 37 additions & 0 deletions DemoPlugin2/Migrations/Migration.1.0.0.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Mystique.Core.Contracts;
using Mystique.Core.DomainModel;
using Mystique.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Text;

namespace DemoPlugin2.Migrations
{
public class Migration_1_0_0 : BaseMigration
{
private static Mystique.Core.DomainModel.Version _version = new Mystique.Core.DomainModel.Version("1.0.0");
private static string _upScripts = @"CREATE TABLE [dbo].[Test3](
TestId[uniqueidentifier] NOT NULL,
);";
private static string _downScripts = @"DROP TABLE [dbo].[Test3]";

public Migration_1_0_0(DbHelper dbHelper) : base(dbHelper, _version)
{

}

public override void MigrationDown(Guid pluginId)
{
SQL(_downScripts);

base.RemoveMigrationScripts(pluginId);
}

public override void MigrationUp(Guid pluginId)
{
SQL(_upScripts);

base.WriteMigrationScripts(pluginId, _upScripts, _downScripts);
}
}
}
5 changes: 5 additions & 0 deletions DemoPlugin2/Views/Plugin1/HelloWorld.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@{

}

<h1>@ViewBag.Content</h1>
3 changes: 3 additions & 0 deletions DemoPlugin2/Views/_ViewStart.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
2 changes: 1 addition & 1 deletion DemoReferenceLibrary/Demo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Demo
{
public string SayHello()
{
return "Hello World. Version 1";
return "Hello World. Version 2";
}
}
}
2 changes: 2 additions & 0 deletions DemoReferenceLibrary/DemoReferenceLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyVersion>1.0.1.0</AssemblyVersion>
<FileVersion>1.0.1.0</FileVersion>
</PropertyGroup>

</Project>
29 changes: 17 additions & 12 deletions Mystique.Core.Mvc/Infrastructure/MystiqueStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,12 @@ public static void MystiqueSetup(this IServiceCollection services, IConfiguratio
services.AddScoped<IPluginManager, PluginManager>();
services.AddScoped<IUnitOfWork, UnitOfWork>();
services.AddSingleton<IActionDescriptorChangeProvider>(MystiqueActionDescriptorChangeProvider.Instance);
services.AddSingleton<IReferenceContainer, DefaultReferenceContainer>();
services.AddSingleton<IReferenceLoader, DefaultReferenceLoader>();
services.AddSingleton<IDependanceLoader, DefaultDependanceLoader>();
services.AddSingleton(MystiqueActionDescriptorChangeProvider.Instance);

var mvcBuilder = services.AddMvc().AddRazorRuntimeCompilation(o =>
{
foreach (var item in _presets)
{
o.AdditionalReferencePaths.Add(item);
}

AdditionalReferencePathHolder.AdditionalReferencePaths = o.AdditionalReferencePaths;
});
var mvcBuilder = services.AddMvc();

var provider = services.BuildServiceProvider();
using (var scope = provider.CreateScope())
Expand All @@ -48,21 +43,21 @@ public static void MystiqueSetup(this IServiceCollection services, IConfiguratio

var unitOfWork = scope.ServiceProvider.GetService<IUnitOfWork>();
var allEnabledPlugins = unitOfWork.PluginRepository.GetAllEnabledPlugins();
var loader = scope.ServiceProvider.GetService<IReferenceLoader>();

foreach (var plugin in allEnabledPlugins)
{
var context = new CollectibleAssemblyLoadContext();
var moduleName = plugin.Name;
var filePath = $"{AppDomain.CurrentDomain.BaseDirectory}Modules\\{moduleName}\\{moduleName}.dll";
var jsonPath = $"{AppDomain.CurrentDomain.BaseDirectory}Modules\\{moduleName}\\{moduleName}.deps.json";
var referenceFolderPath = $"{AppDomain.CurrentDomain.BaseDirectory}Modules\\{moduleName}";

_presets.Add(filePath);
using (var fs = new FileStream(filePath, FileMode.Open))
{
var assembly = context.LoadFromStream(fs);

DefaultReferenceLoader loader = new DefaultReferenceLoader(referenceFolderPath, $"{moduleName}.dll");
loader.LoadStreamsIntoContext(context);
loader.LoadStreamsIntoContext(context, referenceFolderPath, assembly, jsonPath);

var controllerAssemblyPart = new MystiqueAssemblyPart(assembly);
mvcBuilder.PartManager.ApplicationParts.Add(controllerAssemblyPart);
Expand All @@ -71,6 +66,16 @@ public static void MystiqueSetup(this IServiceCollection services, IConfiguratio
}
}

mvcBuilder.AddRazorRuntimeCompilation(o =>
{
foreach (var item in _presets)
{
o.AdditionalReferencePaths.Add(item);
}

AdditionalReferencePathHolder.AdditionalReferencePaths = o.AdditionalReferencePaths;
});

services.Configure<RazorViewEngineOptions>(o =>
{
o.AreaViewLocationFormats.Add("/Modules/{2}/Views/{1}/{0}" + RazorViewEngine.ViewExtension);
Expand Down
11 changes: 5 additions & 6 deletions Mystique.Core.Mvc/MvcModuleSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ namespace Mystique.Core.Mvc
public class MvcModuleSetup : IMvcModuleSetup
{
private ApplicationPartManager _partManager;
private IReferenceLoader _referenceLoader = null;

public MvcModuleSetup(ApplicationPartManager partManager)
public MvcModuleSetup(ApplicationPartManager partManager, IReferenceLoader referenceLoader)
{
_partManager = partManager;
_referenceLoader = referenceLoader;
}

public void EnableModule(string moduleName)
Expand All @@ -27,15 +29,12 @@ public void EnableModule(string moduleName)
var context = new CollectibleAssemblyLoadContext();

var filePath = $"{AppDomain.CurrentDomain.BaseDirectory}Modules\\{moduleName}\\{moduleName}.dll";
var jsonPath = $"{AppDomain.CurrentDomain.BaseDirectory}Modules\\{moduleName}\\{moduleName}.deps.json";
var referenceFolderPath = $"{AppDomain.CurrentDomain.BaseDirectory}Modules\\{moduleName}";
using (var fs = new FileStream(filePath, FileMode.Open))
{
var assembly = context.LoadFromStream(fs);


DefaultReferenceLoader loader = new DefaultReferenceLoader(referenceFolderPath, $"{moduleName}.dll");
loader.LoadStreamsIntoContext(context);

_referenceLoader.LoadStreamsIntoContext(context, referenceFolderPath, assembly, jsonPath);

var controllerAssemblyPart = new MystiqueAssemblyPart(assembly);

Expand Down
Loading

0 comments on commit 9f3be16

Please sign in to comment.