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

PT-12095: Use moduleType specified in the manifest #2652

Merged
merged 4 commits into from
May 8, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 10 additions & 6 deletions src/VirtoCommerce.Platform.Core/Modularity/ManifestModuleInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public virtual ManifestModuleInfo LoadFromManifest(ModuleManifest manifest)
throw new ArgumentNullException(nameof(manifest));
}

ModuleName = manifest.Id;
ModuleName = manifest.Id;

if (manifest.Dependencies != null)
{
Expand All @@ -69,10 +69,12 @@ public virtual ManifestModuleInfo LoadFromManifest(ModuleManifest manifest)
PlatformVersion = SemanticVersion.Parse(manifest.PlatformVersion);
ReleaseNotes = manifest.ReleaseNotes;
Ref = manifest.PackageUrl;

if (manifest.Dependencies != null)
{
Dependencies.AddRange(manifest.Dependencies.Select(x => new ModuleIdentity(x.Id, SemanticVersion.Parse(x.Version))));
}

if (manifest.Incompatibilities != null)
{
Incompatibilities.AddRange(manifest.Incompatibilities.Select(x => new ModuleIdentity(x.Id, SemanticVersion.Parse(x.Version))));
Expand All @@ -89,14 +91,16 @@ public virtual ManifestModuleInfo LoadFromManifest(ModuleManifest manifest)
Copyright = manifest.Copyright;
Tags = manifest.Tags;
Identity = new ModuleIdentity(Id, Version);
ModuleType = manifest.ModuleType;

if (manifest.Groups != null)
{
Groups.AddRange(manifest.Groups);
}

if(manifest.Apps!=null)
if (manifest.Apps != null)
{
this.Apps.AddRange(manifest.Apps.Select(x => new ManifestAppInfo(x)));
Apps.AddRange(manifest.Apps.Select(x => new ManifestAppInfo(x)));
}

return this;
Expand All @@ -105,12 +109,12 @@ public virtual ManifestModuleInfo LoadFromManifest(ModuleManifest manifest)

public virtual ManifestModuleInfo LoadFromExternalManifest(ExternalModuleManifest manifest, ExternalModuleManifestVersion version)
{
if(manifest == null)
if (manifest == null)
{
throw new ArgumentNullException(nameof(manifest));
}

ModuleName = manifest.Id;
ModuleName = manifest.Id;
if (version.Dependencies != null)
{
foreach (var dependency in version.Dependencies)
Expand Down Expand Up @@ -142,7 +146,7 @@ public virtual ManifestModuleInfo LoadFromExternalManifest(ExternalModuleManifes
LicenseUrl = manifest.LicenseUrl;
ProjectUrl = manifest.ProjectUrl;
IconUrl = manifest.IconUrl;
RequireLicenseAcceptance = manifest.RequireLicenseAcceptance;
RequireLicenseAcceptance = manifest.RequireLicenseAcceptance;
Copyright = manifest.Copyright;
Tags = manifest.Tags;
Identity = new ModuleIdentity(Id, Version);
Expand Down
42 changes: 34 additions & 8 deletions src/VirtoCommerce.Platform.Modules/ModuleInitializer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -144,20 +145,45 @@ public virtual void HandleModuleInitializationError(ModuleInfo moduleInfo, Excep
protected virtual IModule CreateModule(ModuleInfo moduleInfo)
{
if (moduleInfo == null)
throw new ArgumentNullException("moduleInfo");

IModule result = null;
var moduleInitializerType = moduleInfo.Assembly.GetTypes().FirstOrDefault(x => typeof(IModule).IsAssignableFrom(x));
if (moduleInitializerType != null && moduleInitializerType != typeof(IModule))
{
result = (IModule)Activator.CreateInstance(moduleInitializerType);
throw new ArgumentNullException(nameof(moduleInfo));
}
if (result == null)

if (!TryResolveModuleTypeFromAssembly(moduleInfo.Assembly, moduleInfo.ModuleType, out var moduleInitializerType))
{
throw new ModuleInitializeException($"Unable to retrieve the module type {moduleInitializerType} from the loaded assemblies.You may need to specify a more fully - qualified type name");
throw new ModuleInitializeException($"Unable to resolve IModule {moduleInfo.ModuleType} from the assembly {moduleInfo.Assembly.FullName}.");
}

var result = (IModule)Activator.CreateInstance(moduleInitializerType);
result.ModuleInfo = moduleInfo as ManifestModuleInfo;

return result;
}

protected virtual bool TryResolveModuleTypeFromAssembly(Assembly moduleAssembly, string moduleType, out Type moduleInitializerType)
{
if (moduleAssembly == null)
{
throw new ArgumentNullException(nameof(moduleAssembly));
}

var moduleInitializerTypes = moduleAssembly.GetTypes().Where(x => typeof(IModule).IsAssignableFrom(x)).ToArray();
if (!moduleInitializerTypes.Any())
{
moduleInitializerType = null;
return false;
}

if (moduleInitializerTypes.Length == 1)
{
moduleInitializerType = moduleInitializerTypes.Single();
return true;
}
else
{
moduleInitializerType = moduleInitializerTypes.FirstOrDefault(x => x.AssemblyQualifiedName.StartsWith(moduleType));
return moduleInitializerType != null;
}
}
}
}