private static ICollection <Pair <string, Exception> > LoadAssembliesToMemory(StringCollection assemblyLocations)
        {
            var failedAssemblyLoads = new List <Pair <string, Exception> >();

            foreach (string assemblyLocation in assemblyLocations)
            {
                if (PackageAssemblyHandler.TryGetAlreadyLoadedAssembly(assemblyLocation) != null)
                {
                    continue;
                }

                try
                {
                    var assembly = Assembly.LoadFrom(assemblyLocation);
                    assembly.GetTypes(); // Accessing GetTypes() to iterate classes
                }
                catch (Exception ex)
                {
                    Exception exceptionToLog = ex;

                    var loadException = ex as ReflectionTypeLoadException;
                    if (loadException?.LoaderExceptions != null && loadException.LoaderExceptions.Any())
                    {
                        exceptionToLog = loadException.LoaderExceptions.First();
                    }

                    failedAssemblyLoads.Add(new Pair <string, Exception>(assemblyLocation, exceptionToLog));
                }
            }

            return(failedAssemblyLoads);
        }
Ejemplo n.º 2
0
        private void LoadDataTypesFromDll(string filePath)
        {
            string fileName = Path.GetFileName(filePath);

            if (DllsNotToLoad.Any(fileName.StartsWith))
            {
                return;
            }

            var assembly = PackageAssemblyHandler.TryGetAlreadyLoadedAssembly(filePath);

            if (assembly == null)
            {
                try
                {
                    assembly = Assembly.LoadFrom(filePath);
                }
                catch (Exception)
                {
                    return;
                }
            }

            DataTypeTypesManager.AddNewAssembly(assembly, false);
        }
Ejemplo n.º 3
0
        /// <exclude />
        public override IEnumerable <PackageFragmentValidationResult> Validate()
        {
            var validationResult = new List <PackageFragmentValidationResult>();

            if (this.Configuration.Count(f => f.Name == "Files") > 1)
            {
                validationResult.AddFatal(Texts.FilePackageFragmentInstaller_OnlyOneFilesElement,
                                          this.ConfigurationParent);
                return(validationResult);
            }

            XElement filesElement = this.Configuration.SingleOrDefault(f => f.Name == "Files");

            _filesToCopy = new List <FileToCopy>();

            if (filesElement != null)
            {
                foreach (XElement fileElement in filesElement.Elements("File"))
                {
                    XAttribute sourceFilenameAttribute = fileElement.Attribute("sourceFilename");
                    XAttribute targetFilenameAttribute = fileElement.Attribute("targetFilename");

                    if (sourceFilenameAttribute == null)
                    {
                        validationResult.AddFatal(
                            Texts.FilePackageFragmentInstaller_MissingAttribute("sourceFilename"), fileElement);
                        continue;
                    }

                    if (targetFilenameAttribute == null)
                    {
                        validationResult.AddFatal(
                            Texts.FilePackageFragmentInstaller_MissingAttribute("targetFilename"), fileElement);
                        continue;
                    }

                    XAttribute allowOverwriteAttribute     = fileElement.Attribute("allowOverwrite");
                    XAttribute assemblyLoadAttribute       = fileElement.Attribute("assemblyLoad");
                    XAttribute onlyUpdateAttribute         = fileElement.Attribute("onlyUpdate");
                    XAttribute addAssemblyBindingAttribute = fileElement.Attribute("addAssemblyBinding");


                    bool allowOverwrite = false;
                    if (!ParseBoolAttribute(allowOverwriteAttribute, validationResult, ref allowOverwrite))
                    {
                        continue;
                    }

                    bool loadAssembly = false;
                    if (!ParseBoolAttribute(assemblyLoadAttribute, validationResult, ref loadAssembly))
                    {
                        continue;
                    }

                    bool onlyUpdate = false;
                    if (!ParseBoolAttribute(onlyUpdateAttribute, validationResult, ref onlyUpdate))
                    {
                        continue;
                    }

                    bool addAssemblyBinding = false;
                    if (!ParseBoolAttribute(addAssemblyBindingAttribute, validationResult, ref addAssemblyBinding))
                    {
                        continue;
                    }

                    string sourceFilename = sourceFilenameAttribute.Value;
                    if (!this.InstallerContext.ZipFileSystem.ContainsFile(sourceFilename))
                    {
                        validationResult.AddFatal(Texts.FilePackageFragmentInstaller_MissingFile(sourceFilename),
                                                  sourceFilenameAttribute);
                        continue;
                    }

                    if (loadAssembly && onlyUpdate)
                    {
                        validationResult.AddFatal(
                            Texts.FilePackageFragmentInstaller_OnlyUpdateNotAllowedWithLoadAssemlby, onlyUpdateAttribute);
                        continue;
                    }

                    string targetFilename = PathUtil.Resolve(targetFilenameAttribute.Value);
                    if (C1File.Exists(targetFilename))
                    {
                        if (!allowOverwrite && !onlyUpdate)
                        {
                            validationResult.AddFatal(Texts.FilePackageFragmentInstaller_FileExists(targetFilename),
                                                      targetFilenameAttribute);
                            continue;
                        }

                        if (((C1File.GetAttributes(targetFilename) & FileAttributes.ReadOnly) > 0) && !allowOverwrite)
                        {
                            validationResult.AddFatal(Texts.FilePackageFragmentInstaller_FileReadOnly(targetFilename),
                                                      targetFilenameAttribute);
                            continue;
                        }
                    }
                    else if (onlyUpdate)
                    {
                        Log.LogVerbose(LogTitle, "Skipping updating of the file '{0}' because it does not exist",
                                       targetFilename);
                        continue; // Target file does not, so skip this
                    }

                    var fileToCopy = new FileToCopy
                    {
                        SourceFilename         = sourceFilename,
                        TargetRelativeFilePath = targetFilenameAttribute.Value,
                        TargetFilePath         = targetFilename,
                        Overwrite          = allowOverwrite || onlyUpdate,
                        AddAssemblyBinding = addAssemblyBinding
                    };

                    _filesToCopy.Add(fileToCopy);

                    if (loadAssembly)
                    {
                        string tempFilename = Path.Combine(this.InstallerContext.TempDirectory,
                                                           Path.GetFileName(targetFilename));

                        this.InstallerContext.ZipFileSystem.WriteFileToDisk(sourceFilename, tempFilename);

                        PackageAssemblyHandler.AddAssembly(tempFilename);
                    }
                }
            }


            if (validationResult.Count > 0)
            {
                _filesToCopy = null;
            }

            return(validationResult);
        }
Ejemplo n.º 4
0
        /// <exclude />
        public override IEnumerable <PackageFragmentValidationResult> Validate()
        {
            var validationResult = new List <PackageFragmentValidationResult>();

            if (this.Configuration.Count(f => f.Name == "Files") > 1)
            {
                validationResult.AddFatal(Texts.FilePackageFragmentInstaller_OnlyOneFilesElement, this.ConfigurationParent);
                return(validationResult);
            }

            if (this.Configuration.Count(f => f.Name == "Directories") > 1)
            {
                validationResult.AddFatal(Texts.FilePackageFragmentInstaller_OnlyOneDirectoriesElement, this.ConfigurationParent);
                return(validationResult);
            }

            XElement filesElement       = this.Configuration.SingleOrDefault(f => f.Name == "Files");
            XElement directoriesElement = this.Configuration.SingleOrDefault(f => f.Name == "Directories");

            _filesToCopy         = new List <FileToCopy>();
            _directoriesToDelete = new List <string>();

            if (filesElement != null)
            {
                foreach (XElement fileElement in filesElement.Elements("File"))
                {
                    XAttribute sourceFilenameAttribute = fileElement.Attribute("sourceFilename");
                    XAttribute targetFilenameAttribute = fileElement.Attribute("targetFilename");

                    if (sourceFilenameAttribute == null)
                    {
                        validationResult.AddFatal(Texts.FilePackageFragmentInstaller_MissingAttribute("sourceFilename"), fileElement);
                        continue;
                    }

                    if (targetFilenameAttribute == null)
                    {
                        validationResult.AddFatal(Texts.FilePackageFragmentInstaller_MissingAttribute("targetFilename"), fileElement);
                        continue;
                    }

                    XAttribute allowOverwriteAttribute        = fileElement.Attribute("allowOverwrite");
                    XAttribute assemblyLoadAttribute          = fileElement.Attribute("assemblyLoad");
                    XAttribute deleteTargetDirectoryAttribute = fileElement.Attribute("deleteTargetDirectory");
                    XAttribute onlyUpdateAttribute            = fileElement.Attribute("onlyUpdate");
                    XAttribute onlyAddAttribute = fileElement.Attribute("onlyAdd");

                    if (deleteTargetDirectoryAttribute != null)
                    {
                        validationResult.AddFatal(Texts.FilePackageFragmentInstaller_DeleteTargetDirectoryNotAllowed, fileElement);
                        continue;
                    }

                    bool allowOverwrite = false;
                    if (!ParseBoolAttribute(allowOverwriteAttribute, validationResult, ref allowOverwrite))
                    {
                        continue;
                    }

                    bool loadAssembly = false;
                    if (!ParseBoolAttribute(assemblyLoadAttribute, validationResult, ref loadAssembly))
                    {
                        continue;
                    }

                    bool onlyUpdate = false;
                    if (!ParseBoolAttribute(onlyUpdateAttribute, validationResult, ref onlyUpdate))
                    {
                        continue;
                    }

                    bool onlyAdd = false;
                    if (!ParseBoolAttribute(onlyAddAttribute, validationResult, ref onlyAdd))
                    {
                        continue;
                    }

                    string sourceFilename = sourceFilenameAttribute.Value;
                    if (!this.InstallerContext.ZipFileSystem.ContainsFile(sourceFilename))
                    {
                        validationResult.AddFatal(Texts.FilePackageFragmentInstaller_MissingFile(sourceFilename), sourceFilenameAttribute);
                        continue;
                    }

                    if (loadAssembly && onlyUpdate)
                    {
                        validationResult.AddFatal(Texts.FilePackageFragmentInstaller_OnlyUpdateNotAllowedWithLoadAssemlby, onlyUpdateAttribute);
                        continue;
                    }

                    if (onlyAdd && onlyUpdate)
                    {
                        validationResult.AddFatal(Texts.FilePackageFragmentInstaller_OnlyUpdateAndOnlyAddNotAllowed, onlyUpdateAttribute);
                        continue;
                    }

                    if (onlyAdd && allowOverwrite)
                    {
                        validationResult.AddFatal(Texts.FilePackageFragmentInstaller_OnlyAddAndAllowOverwriteNotAllowed, onlyAddAttribute);
                        continue;
                    }

                    string targetFilename = PathUtil.Resolve(targetFilenameAttribute.Value);
                    if (C1File.Exists(targetFilename))
                    {
                        if (onlyAdd)
                        {
                            Log.LogVerbose(LogTitle, "Skipping adding of the file '{0}' because it already exist and is marked 'onlyAdd'", targetFilename);
                            continue; // Target file does not, so skip this
                        }

                        if (!allowOverwrite && !onlyUpdate)
                        {
                            validationResult.AddFatal(Texts.FilePackageFragmentInstaller_FileExists(targetFilename), targetFilenameAttribute);
                            continue;
                        }

                        if (((C1File.GetAttributes(targetFilename) & FileAttributes.ReadOnly) > 0) && !allowOverwrite)
                        {
                            validationResult.AddFatal(Texts.FilePackageFragmentInstaller_FileReadOnly(targetFilename), targetFilenameAttribute);
                            continue;
                        }
                    }
                    else if (onlyUpdate)
                    {
                        Log.LogVerbose(LogTitle, "Skipping updating of the file '{0}' because it does not exist", targetFilename);
                        continue; // Target file does not, so skip this
                    }

                    var fileToCopy = new FileToCopy
                    {
                        SourceFilename         = sourceFilename,
                        TargetRelativeFilePath = targetFilenameAttribute.Value,
                        TargetFilePath         = targetFilename,
                        Overwrite = allowOverwrite || onlyUpdate
                    };

                    _filesToCopy.Add(fileToCopy);

                    if (loadAssembly)
                    {
                        string tempFilename = Path.Combine(this.InstallerContext.TempDirectory, Path.GetFileName(targetFilename));

                        this.InstallerContext.ZipFileSystem.WriteFileToDisk(sourceFilename, tempFilename);

                        PackageAssemblyHandler.AddAssembly(tempFilename);
                    }
                }
            }

            if (directoriesElement != null)
            {
                foreach (XElement directoryElement in directoriesElement.Elements("Directory"))
                {
                    XAttribute sourceDirectoryAttribute = directoryElement.Attribute("sourceDirectory");
                    XAttribute targetDirectoryAttribute = directoryElement.Attribute("targetDirectory");

                    if (sourceDirectoryAttribute == null)
                    {
                        validationResult.AddFatal(Texts.FilePackageFragmentInstaller_MissingAttribute("sourceDirectory"), directoryElement);
                        continue;
                    }

                    if (targetDirectoryAttribute == null)
                    {
                        validationResult.AddFatal(Texts.FilePackageFragmentInstaller_MissingAttribute("targetDirectory"), directoryElement);
                        continue;
                    }


                    XAttribute allowOverwriteAttribute        = directoryElement.Attribute("allowOverwrite");
                    XAttribute assemblyLoadAttribute          = directoryElement.Attribute("assemblyLoad");
                    XAttribute deleteTargetDirectoryAttribute = directoryElement.Attribute("deleteTargetDirectory");
                    XAttribute onlyUpdateAttribute            = directoryElement.Attribute("onlyUpdate");

                    if (assemblyLoadAttribute != null)
                    {
                        validationResult.AddFatal(Texts.FilePackageFragmentInstaller_AssemblyLoadNotAllowed, directoryElement);
                        continue;
                    }

                    if (onlyUpdateAttribute != null)
                    {
                        validationResult.AddFatal(Texts.FilePackageFragmentInstaller_OnlyUpdateNotAllowed, directoryElement);
                        continue;
                    }


                    bool allowOverwrite = false;
                    if (!ParseBoolAttribute(allowOverwriteAttribute, validationResult, ref allowOverwrite))
                    {
                        continue;
                    }

                    bool deleteTargetDirectory = false;
                    if (!ParseBoolAttribute(deleteTargetDirectoryAttribute, validationResult, ref deleteTargetDirectory))
                    {
                        continue;
                    }

                    string sourceDirectory = sourceDirectoryAttribute.Value;
                    if (!this.InstallerContext.ZipFileSystem.ContainsDirectory(sourceDirectory))
                    {
                        validationResult.AddFatal(Texts.FilePackageFragmentInstaller_MissingDirectory(sourceDirectory), sourceDirectoryAttribute);
                        continue;
                    }

                    string targetDirectory = PathUtil.Resolve(targetDirectoryAttribute.Value);

                    if (deleteTargetDirectory)
                    {
                        if (C1Directory.Exists(targetDirectory))
                        {
                            _directoriesToDelete.Add(targetDirectory);
                        }
                    }

                    foreach (string sourceFilename in this.InstallerContext.ZipFileSystem.GetFilenames(sourceDirectory))
                    {
                        string resolvedSourceFilename = sourceFilename.Remove(0, sourceDirectory.Length);
                        if (resolvedSourceFilename.StartsWith("/"))
                        {
                            resolvedSourceFilename = resolvedSourceFilename.Remove(0, 1);
                        }

                        string targetFilename = Path.Combine(targetDirectory, resolvedSourceFilename);

                        if (C1File.Exists(targetFilename) && !deleteTargetDirectory && !allowOverwrite)
                        {
                            validationResult.AddFatal(Texts.FilePackageFragmentInstaller_FileExists(targetFilename), targetDirectoryAttribute);
                            continue;
                        }

                        var fileToCopy = new FileToCopy
                        {
                            SourceFilename         = sourceFilename,
                            TargetRelativeFilePath = Path.Combine(targetDirectoryAttribute.Value, resolvedSourceFilename),
                            TargetFilePath         = targetFilename,
                            Overwrite = allowOverwrite
                        };
                        _filesToCopy.Add(fileToCopy);
                    }
                }
            }

            if (validationResult.Count > 0)
            {
                _filesToCopy         = null;
                _directoriesToDelete = null;
            }

            return(validationResult);
        }