Ejemplo n.º 1
0
        private void Downloader_DownloadCompleted(object sender, Net.DownloadCompletedEventArgs e)
        {
            var e2    = (AssemblyPackageDownloadCompletedEventArgs)e;
            var state = (DownloaderState)e.UserState;

            if (state == null)
            {
                return;
            }
            if (e2 == null)
            {
                if (state != null)
                {
                    state.Release();
                }
                return;
            }
            var       packages  = new List <AssemblyPackageCacheEntry>();
            Exception exception = null;

            try
            {
                var buffer = new byte[1024];
                var read   = 0;
                if (e.Error == null && e2.PackagesResult != null)
                {
                    try
                    {
                        foreach (var i in e2.PackagesResult)
                        {
                            var packageFile = new System.IO.FileInfo(GetAssemblyPackageLocalFileName(i.Uid));
                            if (packageFile.Exists)
                            {
                                packageFile.Delete();
                            }
                            var inStream = i.Stream;
                            using (var outStream = packageFile.Create())
                                while ((read = inStream.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    outStream.Write(buffer, 0, read);
                                }
                            packageFile.LastAccessTime = i.LastWriteTime;
                            var pkg = new AssemblyPackageResult(_assemblyResolverManager, i.Uid, packageFile.FullName).CreatePackage();
                            if (pkg.Count > 0)
                            {
                                packages.Add(new AssemblyPackageCacheEntry(pkg, null, packageFile.FullName));
                            }
                            else
                            {
                                pkg.Dispose();
                                packageFile.Delete();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        exception = ex;
                    }
                }
                else if (e.Error != null)
                {
                    exception = e.Error;
                }
                if (_packages != null)
                {
                    lock (_objLock)
                        _packages.AddRange(packages);
                }
            }
            catch (Exception ex)
            {
                exception = ex;
            }
            finally
            {
                state.DownloadEntries = packages;
                state.Exception       = exception;
                state.Release();
            }
        }
Ejemplo n.º 2
0
        private AssemblyPackageContainer GetAssemblyPackageFromLocal(IEnumerable <AssemblyPart> assemblyParts)
        {
            var           assemblyNames = new List <string>();
            List <string> files         = new List <string>();

            foreach (var dir in _assemblyFilesDirectories)
            {
                try
                {
                    foreach (var file in System.IO.Directory.GetFiles(dir).Select(f => System.IO.Path.GetFileName(f)).OrderBy(f => f))
                    {
                        var index = files.BinarySearch(file, StringComparer.OrdinalIgnoreCase);
                        if (index < 0)
                        {
                            files.Insert(~index, file);
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw new DetailsException(ResourceMessageFormatter.Create(() => Properties.Resources.AssemblyRepository_GetFilesFromRepositoryDirectoryError, dir), ex);
                }
            }
            var parts2 = assemblyParts.ToArray();
            IComparer <string> comparer = StringComparer.Create(System.Globalization.CultureInfo.InvariantCulture, true);

            foreach (var part in parts2)
            {
                var index = files.BinarySearch(part.Source, comparer);
                if (index >= 0)
                {
                    assemblyNames.Add(files[index]);
                }
            }
            var analyzer      = new AssemblyAnalyzer();
            var assemblyPaths = new List <string>();
            var names         = new List <string>();

            if (_assemblyInfoRepository != null)
            {
                AssemblyInfo info      = null;
                Exception    exception = null;
                var          infos     = new Queue <AssemblyInfo>();
                var          pending   = new Queue <string>(assemblyNames);
                while (pending.Count > 0)
                {
                    var assemblyName = pending.Dequeue();
                    if (names.FindIndex(f => StringComparer.InvariantCultureIgnoreCase.Equals(f, assemblyName)) < 0)
                    {
                        if (!_assemblyInfoRepository.TryGet(System.IO.Path.GetFileNameWithoutExtension(assemblyName), out info, out exception))
                        {
                            continue;
                        }
                        foreach (var i in info.References)
                        {
                            pending.Enqueue(i + ".dll");
                        }
                        names.Add(assemblyName);
                        foreach (var dir in _assemblyFilesDirectories)
                        {
                            if (System.IO.File.Exists(System.IO.Path.Combine(dir, assemblyName)))
                            {
                                assemblyPaths.Add(System.IO.Path.Combine(dir, assemblyName));
                            }
                        }
                    }
                }
            }
            else
            {
                foreach (var assemblyName in assemblyNames)
                {
                    string assemblyPath = null;
                    foreach (var dir in _assemblyFilesDirectories)
                    {
                        assemblyPath = System.IO.Path.Combine(dir, assemblyName);
                        if (System.IO.File.Exists(assemblyPath))
                        {
                            break;
                        }
                    }
                    AsmData data = null;
                    try
                    {
                        data = analyzer.AnalyzeRootAssembly(assemblyPath);
                    }
                    catch (Exception ex)
                    {
                        throw new DetailsException(ResourceMessageFormatter.Create(() => Properties.Resources.AssemblyRepository_AnalyzeAssemblyError, assemblyName), ex);
                    }
                    var queue = new Queue <AsmData>();
                    queue.Enqueue(data);
                    while (queue.Count > 0)
                    {
                        data = queue.Dequeue();
                        if (string.IsNullOrEmpty(data.Path))
                        {
                            continue;
                        }
                        var fileName = System.IO.Path.GetFileName(data.Path);
                        var index    = names.FindIndex(f => StringComparer.InvariantCultureIgnoreCase.Equals(f, fileName));
                        if (index < 0)
                        {
                            names.Add(fileName);
                            assemblyPaths.Add(data.Path);
                        }
                        foreach (var i in data.References)
                        {
                            if (!string.IsNullOrEmpty(i.Path) && names.FindIndex(f => f == System.IO.Path.GetFileName(i.Path)) < 0)
                            {
                                queue.Enqueue(i);
                            }
                        }
                    }
                }
            }
            names.Reverse();
            var languages = new IO.Xap.LanguageInfo[] {
                new IO.Xap.LanguageInfo(new string[] {
                    ".dll"
                }, names.ToArray(), "")
            };
            var configuration = new IO.Xap.XapConfiguration(new IO.Xap.AppManifestTemplate(), languages, null);

            if (assemblyPaths.Count > 0 && UseDirectoryAssemblyPackages)
            {
                return(new AssemblyPackageContainer(new DirectoryAssemblyPackage(_assemblyResolverManager, assemblyPaths)));
            }
            else if (assemblyPaths.Count > 0)
            {
                assemblyPaths.Reverse();
                var entries = assemblyPaths.Select(f => {
                    var fileInfo = new System.IO.FileInfo(f);
                    return(new IO.Xap.XapBuilder.XapEntry(fileInfo.Name, new Lazy <System.IO.Stream>(() => fileInfo.OpenRead()), fileInfo.LastWriteTime));
                }).ToArray();
                var pkgUid = Guid.NewGuid();
                try
                {
                    var fileName = GetAssemblyPackageLocalFileName(pkgUid);
                    if (System.IO.File.Exists(fileName))
                    {
                        System.IO.File.Delete(fileName);
                    }
                    IO.Xap.XapBuilder.XapToDisk(configuration, entries, fileName);
                    var pkg = new AssemblyPackageResult(_assemblyResolverManager, pkgUid, fileName).CreatePackage();
                    lock (_objLock)
                        _packages.Add(new AssemblyPackageCacheEntry(pkg, null, fileName));
                    return(new AssemblyPackageContainer(pkg));
                }
                finally
                {
                    foreach (var i in entries)
                    {
                        i.Dispose();
                    }
                }
            }
            else
            {
                lock (_objLock)
                    _packages.Add(new AssemblyPackageCacheEntry(null, parts2, null));
                return(null);
            }
        }
Ejemplo n.º 3
0
 private void DoStart()
 {
     try
     {
         var exceptions = new List <Exception>();
         try
         {
             var repositoryFolder      = GetRepositoryFolder();
             var repositoryDirectories = System.IO.Directory.GetDirectories(repositoryFolder);
             var files = System.IO.Directory.GetFiles(repositoryFolder, "*.xap");
             var packageDirectories = files.Select(f => System.IO.Path.Combine(repositoryFolder, System.IO.Path.GetFileNameWithoutExtension(f))).ToArray();
             foreach (var invalidDirectory in repositoryDirectories.Except(packageDirectories, StringComparer.InvariantCultureIgnoreCase))
             {
                 try
                 {
                     System.IO.Directory.Delete(invalidDirectory, true);
                 }
                 catch
                 {
                     if (System.IO.Directory.Exists(invalidDirectory))
                     {
                         foreach (var file in System.IO.Directory.GetFiles(invalidDirectory))
                         {
                             try
                             {
                                 System.IO.File.Delete(file);
                             }
                             catch
                             {
                             }
                         }
                     }
                 }
             }
             var packages = new List <AssemblyPackageCacheEntry>(files.Length);
             for (var i = 0; i < files.Length; i++)
             {
                 var file = files[i];
                 try
                 {
                     var uid = Guid.Parse(System.IO.Path.GetFileNameWithoutExtension(file));
                     var pkg = new AssemblyPackageResult(_assemblyResolverManager, uid, file).CreatePackage();
                     packages.Add(new AssemblyPackageCacheEntry(pkg, null, file));
                 }
                 catch (Exception ex)
                 {
                     exceptions.Add(ex);
                 }
             }
             if (_validator != null)
             {
                 var validateResult = _validator.Validate(packages.Select(f => f.Package).ToArray());
                 lock (_objLock)
                 {
                     for (var i = 0; i < packages.Count; i++)
                     {
                         if (validateResult[i])
                         {
                             _packages.Add(packages[i]);
                         }
                         else
                         {
                             try
                             {
                                 packages[i].Dispose();
                                 var packageDirectory = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(files[i]), System.IO.Path.GetFileNameWithoutExtension(files[i]));
                                 if (System.IO.Directory.Exists(packageDirectory))
                                 {
                                     try
                                     {
                                         System.IO.Directory.Delete(packageDirectory, true);
                                     }
                                     catch
                                     {
                                     }
                                 }
                                 System.IO.File.Delete(files[i]);
                             }
                             catch
                             {
                             }
                         }
                     }
                 }
             }
             else
             {
                 lock (_objLock)
                 {
                     foreach (var i in packages)
                     {
                         _packages.Add(i);
                     }
                 }
             }
             _maintenanceInstances = new List <IAssemblyRepositoryMaintenance>(Microsoft.Practices.ServiceLocation.ServiceLocator.Current.GetAllInstances <IAssemblyRepositoryMaintenance>());
         }
         catch (System.Threading.ThreadAbortException)
         {
             return;
         }
         catch (Exception ex)
         {
             exceptions.Add(ex);
         }
         finally
         {
             OnStartedInternal(new AssemblyRepositoryStartedArgs(exceptions.ToArray()));
         }
         if (_maintenanceInstances != null)
         {
             MaintenanceRobot();
         }
     }
     catch (System.Threading.ThreadAbortException)
     {
     }
 }