Example #1
0
 private void _createAssemblyIfNeeded()
 {
     if (_pluginAssembly == null)
     {
         _pluginAssembly = DistribIOC.Kernel.Get<IPluginAssemblyFactory>().CreatePluginAssemblyFromPath(_assemblyPath);
     }
 }
 public IPluginInstance CreatePluginInstance(IPluginDescriptor descriptor, IPluginAssembly parentAssembly)
 {
     return _ioc.Get<IPluginInstance>(new[]
     {
         new IOCConstructorArgument("descriptor", descriptor),
         new IOCConstructorArgument("pluginAssembly", parentAssembly),
     });
 }
Example #3
0
        /// <summary>
        ///     Finds and returns the PluginAssembly in the specified list of type PluginAssembly whose FQN matches the specified FQN.
        /// </summary>
        /// <param name="fqn">The FQN of the desired PluginAssembly.</param>
        /// <returns>
        ///     The PluginAssembly instance whose FQN matches the specified FQN, or the default PluginAssembly if not found.
        /// </returns>
        public IPluginAssembly FindPluginAssembly(string fqn)
        {
            logger.EnterMethod(xLogger.Params(fqn, new xLogger.ExcludedParam()));

            IPluginAssembly retVal = PluginAssemblies.Where(p => p.FQN == fqn).FirstOrDefault();

            logger.ExitMethod(retVal);
            return(retVal);
        }
Example #4
0
        /// <summary>
        ///     Iterates over the specified List of type <see cref="PluginManagerConfigurationPluginInstance"/>, retrieves the
        ///     matching PluginAssembly from the supplied List of type PluginAssembly and instantiates each instance, passing the
        ///     instance name and an instance of xLogger with the Fully Qualified Name of the instance.
        /// </summary>
        /// <remarks>
        ///     The <see cref="InstantiatePlugin{T}(IApplicationManager, string, xLogger)"/> method is invoked via reflection so
        ///     that the type parameter for the method can be specified dynamically.
        /// </remarks>
        /// <returns>A Result containing the result of the operation and a Dictionary containing the instantiated Plugins.</returns>
        private Result <List <IPluginInstance> > InstantiatePlugins()
        {
            logger.EnterMethod();
            logger.Info("Creating Plugin Instances...");

            Result <List <IPluginInstance> > retVal = new Result <List <IPluginInstance> >();

            retVal.ReturnValue = new List <IPluginInstance>();

            IApplicationManager applicationManager = Dependency <IApplicationManager>();

            // iterate over the configured plugin instances from the configuration
            foreach (PluginManagerConfigurationPluginInstance instance in Configuration.Instances)
            {
                logger.SubSubHeading(LogLevel.Debug, "Instance: " + instance.InstanceName);
                logger.Info("Creating instance '" + instance.InstanceName + "' of Type '" + instance.AssemblyName + "'...");

                // locate the PluginAssembly matching the instance
                IPluginAssembly assembly = FindPluginAssembly(instance.AssemblyName);
                if (assembly == default(PluginAssembly))
                {
                    retVal.AddWarning("Plugin assembly '" + instance.AssemblyName + "' not found in the list of loaded assemblies.");
                }
                else
                {
                    // create an instance of xLogger for the new instance
                    xLogger instanceLogger = (xLogger)LogManager.GetLogger(assembly.FQN + "." + instance.InstanceName, typeof(xLogger));

                    // invoke the CreatePluginInstance method
                    MethodInfo method = this.GetType().GetMethod("InstantiatePlugin").MakeGenericMethod(assembly.Type);
                    Result <IPluginInstance> invokeResult = (Result <IPluginInstance>)method.Invoke(this, new object[] { applicationManager, instance.InstanceName, instanceLogger });

                    // if the invocation succeeded, add the result to the Instances Dictionary
                    if (invokeResult.ResultCode == ResultCode.Success)
                    {
                        retVal.ReturnValue.Add(invokeResult.ReturnValue);
                        logger.Info("Instantiated " + assembly.PluginType.ToString() + " plugin '" + instance.InstanceName + "'.");
                    }

                    invokeResult.LogResult(logger, "InstantiatePlugin");
                    retVal.Incorporate(invokeResult);
                }
            }

            retVal.LogResult(logger);
            logger.ExitMethod(retVal);
            return(retVal);
        }
        protected override void DoUninit()
        {
            try
            {
                lock (_lock)
                {
                    if (_processInstance != null)
                    {
                        _processInstance.UninitProcess();
                    }

                    if (_pluginAssembly != null && _pluginAssembly.IsInitialised)
                    {
                        _pluginAssembly.Unitialise();
                    }

                    _pluginAssembly = null;
                    _pluginInstance = null;
                    _processInstance = null;
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Failed to do uninit", ex);
            }
        }
        protected override void DoInit()
        {
            try
            {
                lock (_lock)
                {
                    Assembly.LoadFrom(_pluginDescriptor.AssemblyPath);

                    AppDomain.CurrentDomain.AssemblyResolve += (s, e) =>
                        {
                            return AppDomain.CurrentDomain.GetAssemblies()
                                .DefaultIfEmpty(null)
                                .SingleOrDefault(asm => asm.FullName == e.Name);
                        };

                    try
                    {
                        _pluginAssembly = _pluginAssemblyFactory.CreatePluginAssemblyFromPath(_pluginDescriptor.AssemblyPath);
                        var initRes = _pluginAssembly.Initialise();

                        if (!initRes.HasUsablePlugins)
                        {
                            throw new ApplicationException("The plugin assembly contains no usable plugins");
                        }

                        if (initRes.UsablePlugins.DefaultIfEmpty(null)
                            .SingleOrDefault(p => p.Match(_pluginDescriptor)) == null)
                        {
                            throw new ApplicationException("A matching plugin descriptor couldn't be found in the plugin assembly");
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new ApplicationException("Failed to initialise plugin assembly and locate plugin", ex);
                    }

                    try
                    {
                        _pluginInstance = _pluginAssembly.CreatePluginInstance(_pluginDescriptor);
                        _pluginInstance.Initialise();
                    }
                    catch (Exception ex)
                    {
                        throw new ApplicationException("Failed to create and initialise the plugin instance", ex);
                    }

                    try
                    {
                        _processInstance = _pluginInstance.GetUnderlyingInstance<IProcess>();
                        _processInstance.InitProcess();

                        if (_processInstance.JobDefinitions == null || _processInstance.JobDefinitions.Count == 0)
                        {
                            throw new ApplicationException("Process doesn't have any job definitions");
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new ApplicationException("Failed to create and initialise the process", ex);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Failed to perform init", ex);
            }
        }
Example #7
0
        static void Main(string[] args)
        {
            try
            {
                string[] pluginPaths = new string[]
                {
                    @"D:\Work\Digger\src\FindReplace\bin\Debug\netcoreapp3.0\FindReplace.Plugin.dll",
                    @"D:\Work\Digger\src\Analysis\bin\Debug\netcoreapp3.0\Analysis.Plugin.dll"
                };

                var plugins = pluginPaths.SelectMany(pluginPath => { return(PluginLoader.CreateInstance(pluginPath)); }).ToList();

                if (args.Length == 0)
                {
                    Prompt(ConsoleColor.White, "Digger CLI by Asif Raja", ConsoleColor.DarkCyan, " (https://github.com/asifraja)", true);
                    Console.WriteLine();
                    Prompt("Usage:", true);
                    foreach (IPluginAssembly command in plugins)
                    {
                        Prompt(ConsoleColor.White, "  digger");
                        Prompt($" {command.Verb}");
                        Prompt(ConsoleColor.DarkGreen, " [options]");
                        Prompt(ConsoleColor.DarkCyan, $" ## {command.Description}", true);
                    }
                    Prompt(ConsoleColor.White, "  digger");
                    Prompt(ConsoleColor.DarkGreen, " [path-to-options-file] |");
                    Prompt(ConsoleColor.DarkGreen, " [options]");
                    Prompt(ConsoleColor.DarkCyan, " ## provide options in a file", true);
                    Console.WriteLine();
                    Prompt(ConsoleColor.DarkGreen, "options:", true);
                    Prompt("  --help");
                    Prompt(ConsoleColor.DarkCyan, " ## List all available options", true);
                    Prompt("  --version", true);
                    Console.WriteLine();
                    Prompt(ConsoleColor.DarkGreen, "path-to-options-file:", true);
                    Prompt("  [<path to file containg option flags>]");
                }
                else
                {
                    var commandName = args[0].ToLower().Trim();
                    Prompt(ConsoleColor.White, $"Command : {commandName}");
                    Console.WriteLine();
                    IPluginAssembly command = plugins.FirstOrDefault(c => c.Verb == commandName);
                    if (command == null)
                    {
                        Console.WriteLine($"{commandName} is an unknown command.");
                        return;
                    }
                    var fi = command.Execute(args);
                    if (fi != null)
                    {
                        foreach (var f in fi.OrderBy(o => o.Order))
                        {
                            Prompt(ConsoleColor.Magenta, $"{f.Files.Count()} files in {f.Path}", true);
                            foreach (var str in f.SeekStrings)
                            {
                                Prompt(ConsoleColor.DarkMagenta, $"  {str.Value} instances of {str.Key}", true);
                                foreach (var fl in f.Files.Where(f => f.FoundLines.Count() > 0 && f.FoundLines.Any(x => x.SeekString == str.Key)).OrderBy(f => f.Path))
                                {
                                    Prompt(ConsoleColor.DarkGreen, $"    {fl.Path}", true);
                                    foreach (var l in fl.FoundLines.OrderBy(f => f.SeekedString).ThenBy(f => f.Filename).ThenBy(f => f.LineNo))
                                    {
                                        Prompt(ConsoleColor.Green, $"     {l.LineNo} {l.Line.Trim()}", true);
                                    }
                                }
                            }
                            Console.WriteLine();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.WriteLine();
            Console.WriteLine();
            Console.Write("Press any key to exist...");
            Console.ReadKey();
        }
Example #8
0
        public void Init()
        {
            if (_pluginAssembly == null)
            {
                _pluginAssembly = _pluginAssemblyFactory.CreatePluginAssemblyFromPath(_path);
            }

            _initResult = _pluginAssembly.Initialise();
        }