Esempio n. 1
0
        public void Load(Package package, ModuleCollection modules, IWriterCollection writers)
        {
            if (PackageLogic.Instance.LoadedPackages.Contains(package))
            {
                throw new Exception(string.Format("The package {0} is already loaded", package.Name));
            }

            Dictionary <string, System.Reflection.Assembly> asmDic = new Dictionary <string, System.Reflection.Assembly>();

            bool isErrors = false;

            if (package.Modules.Count > 0)
            {
                foreach (var module in package.Modules)
                {
                    Type moduleType = null;
                    System.Reflection.Assembly asm = null;

                    try
                    {
                        if (!asmDic.ContainsKey(module.Assembly))
                        {
                            asmDic.Add(module.Assembly, Util.LoadAssembly(module.Assembly));
                        }
                        asm = asmDic[module.Assembly];

                        moduleType = asm.GetType(module.ModuleType);
                        if (!modules.Install(moduleType, writers) && isErrors == false)
                        {
                            isErrors = true;
                        }
                    }
                    catch (Exception ex)
                    {
                        writers.Error.WriteLine(string.Format("An error occured while installing module '{0}' from assembly '{1}' :: {2}\r\n{3}"
                                                              , moduleType, asm.CodeBase.Replace("file:///", string.Empty), ex.Message, ex.StackTrace));

                        if (moduleType != null && modules.Any(m => m.GetType() == moduleType))
                        {
                            modules.Uninstall(moduleType, writers);
                        }

                        writers.Error.WriteLine("The module has been uninstalled.");

                        isErrors = true;
                    }
                }
            }
            else
            {
                writers.Warning.WriteLine("The package has been loaded but no modules were installed - the package does not define any modules.");
            }

            _loadedPackageNames.Add(package.Name);

            if (isErrors)
            {
                writers.Warning.WriteLine("The package has been loaded with errors.");
            }
            else
            {
                writers.Standard.WriteLine("Package loaded");
            }
        }