Ejemplo n.º 1
0
 /// <summary>
 /// Uninstall plugin
 /// </summary>
 public virtual async Task Uninstall()
 {
     await PluginExtensions.MarkPluginAsUninstalled(PluginInfo.SystemName);
 }
Ejemplo n.º 2
0
        public static void Load(IMvcCoreBuilder mvcCoreBuilder, AppConfig config)
        {
            lock (_synLock)
            {
                if (mvcCoreBuilder == null)
                {
                    throw new ArgumentNullException(nameof(mvcCoreBuilder));
                }

                _config = config ?? throw new ArgumentNullException(nameof(config));

                _pluginFolder = new DirectoryInfo(CommonPath.PluginsPath);
                _copyFolder   = new DirectoryInfo(CommonPath.PluginsCopyPath);

                var referencedPlugins = new List <PluginInfo>();
                try
                {
                    var installedPluginSystemNames = PluginExtensions.ParseInstalledPluginsFile(CommonPath.InstalledPluginsFilePath);

                    Log.Information("Creating shadow copy folder and querying for dlls");
                    Directory.CreateDirectory(_pluginFolder.FullName);
                    Directory.CreateDirectory(_copyFolder.FullName);
                    var binFiles = _copyFolder.GetFiles("*", SearchOption.AllDirectories);
                    if (config.ClearPluginShadowDirectoryOnStartup)
                    {
                        //clear out shadow plugins
                        foreach (var f in binFiles)
                        {
                            Log.Information($"Deleting {f.Name}");
                            try
                            {
                                var fileName = Path.GetFileName(f.FullName);
                                if (fileName.Equals("index.htm", StringComparison.OrdinalIgnoreCase))
                                {
                                    continue;
                                }

                                File.Delete(f.FullName);
                            }
                            catch (Exception exc)
                            {
                                Log.Error(exc, "PluginManager");
                            }
                        }
                    }

                    //load description files
                    foreach (var plugin in GetPluginInfo())
                    {
                        if (plugin.SupportedVersion != GrandVersion.SupportedPluginVersion)
                        {
                            Log.Information($"Incompatible plugin {plugin.SystemName}");
                            //set as not installed
                            referencedPlugins.Add(plugin);
                            continue;
                        }

                        //some validation
                        if (string.IsNullOrWhiteSpace(plugin.SystemName))
                        {
                            throw new Exception(string.Format("The plugin '{0}' has no system name.", plugin.SystemName));
                        }
                        if (referencedPlugins.Contains(plugin))
                        {
                            throw new Exception(string.Format("The plugin with '{0}' system name is already defined", plugin.SystemName));
                        }

                        //set 'Installed' property
                        plugin.Installed = installedPluginSystemNames
                                           .FirstOrDefault(x => x.Equals(plugin.SystemName, StringComparison.OrdinalIgnoreCase)) != null;

                        try
                        {
                            if (!config.PluginShadowCopy)
                            {
                                //remove deps.json files
                                var depsFiles = plugin.OriginalAssemblyFile.Directory.GetFiles("*.deps.json", SearchOption.TopDirectoryOnly);
                                foreach (var f in depsFiles)
                                {
                                    try
                                    {
                                        File.Delete(f.FullName);
                                    }
                                    catch (Exception exc)
                                    {
                                        Log.Error(exc, "PluginManager");
                                    }
                                }
                            }

                            //main plugin file
                            AddApplicationPart(mvcCoreBuilder, plugin.ReferencedAssembly, plugin.SystemName, plugin.PluginFileName);

                            //register interface for IPlugin
                            RegisterPluginInterface(mvcCoreBuilder, plugin.ReferencedAssembly);

                            //init plugin type
                            foreach (var t in plugin.ReferencedAssembly.GetTypes())
                            {
                                if (typeof(IPlugin).IsAssignableFrom(t))
                                {
                                    if (!t.GetTypeInfo().IsInterface)
                                    {
                                        if (t.GetTypeInfo().IsClass&& !t.GetTypeInfo().IsAbstract)
                                        {
                                            plugin.PluginType = t;
                                            break;
                                        }
                                    }
                                }
                            }

                            referencedPlugins.Add(plugin);
                        }
                        catch (ReflectionTypeLoadException ex)
                        {
                            var msg = string.Format("Plugin '{0}'. ", plugin.FriendlyName);
                            foreach (var e in ex.LoaderExceptions)
                            {
                                msg += e.Message + Environment.NewLine;
                            }

                            var fail = new Exception(msg, ex);
                            throw fail;
                        }
                        catch (Exception ex)
                        {
                            var msg = string.Format("Plugin '{0}'. {1}", plugin.FriendlyName, ex.Message);

                            var fail = new Exception(msg, ex);
                            throw fail;
                        }
                    }
                }
                catch (Exception ex)
                {
                    var msg = string.Empty;
                    for (var e = ex; e != null; e = e.InnerException)
                    {
                        msg += e.Message + Environment.NewLine;
                    }

                    var fail = new Exception(msg, ex);
                    throw fail;
                }

                ReferencedPlugins = referencedPlugins;
            }
        }