Exemplo n.º 1
0
        private void ExecutePlugin(IEnumerable<string> args)
        {
            if (String.IsNullOrWhiteSpace(pluginPath))
            {
                throw new ArgumentException("Plugin assembly not specified");
            }

            if (!File.Exists(pluginPath))
            {
                throw new IOException("Plugin assembly not found");
            }

            var factory = new Factory<IPlugin>();

            if (verbose)
            {
                Console.WriteLine("scanning plugin path: {0}", pluginPath);
            }

            try
            {
                factory.ScanAssembly(pluginPath);
            }
            catch (Exception e)
            {
                throw new Exception("Error loading plugin", e);
            }

            if (factory.Count == 0)
            {
                throw new Exception("Assembly does not contain any plugins");
            }

            if (verbose)
            {
                Console.WriteLine("found {0} plugin module(s)", factory.Count);
            }

            IPlugin p = null;

            if (!String.IsNullOrWhiteSpace(pluginName))
            {
                try
                {
                    p = factory.Make(pluginName);
                }
                catch (Exception e)
                {
                    throw new Exception("Error creating plugin: " + pluginName, e);
                }

                if (verbose)
                {
                    Console.WriteLine("using plugin module: {0}", pluginName);
                }
            }
            else
            {
                try
                {
                    p = (IPlugin)factory[0].Make();
                }
                catch (Exception e)
                {
                    throw new Exception("Error creating default plugin: " + factory[0].ToString(), e);
                }

                if (verbose)
                {
                    Console.WriteLine("using plugin module: {0}", Reflect.DisplayName(p));
                }
            }

            if (p != null)
            {
                p.Execute(args);
            }
        }