Beispiel #1
0
        public LocalPackageIndexBuilderResult RemovePackage(string packageName, bool force = false)
        {
            _logger.WriteInformation("Started package removing {0}.", packageName);
            var stopWatch = Stopwatch.StartNew();

            var errors = _index.RemovePackage(packageName);

            stopWatch.Stop();
            _logger.WriteInformation("Finished package removing.");

            return(new LocalPackageIndexBuilderResult {
                Success = errors == null || !errors.Any(), TimeElapsed = stopWatch.Elapsed
            });
        }
        public Task <LocalPackageIndexBuilderResult> BuildAsync(bool shouldClean = false,
                                                                bool newOnly     = false,
                                                                CancellationToken cancellationToken = default(CancellationToken))
        {
            // Fire and forget. While index is building, it will be locked from
            // other write attempts. In meanwhile readers would just not be able
            // to find any types, but will be still operatable (when an instance of
            // a reader is created it can return data from the snapshot before next
            // write happened).

            return(Task.Run(() =>
            {
                bool success = true;
                _logger.WriteInformation("Started building index.");
                var stopWatch = Stopwatch.StartNew();

                try
                {
                    if (shouldClean)
                    {
                        // if we cleaned index , we should add all packages, not only new
                        Clean();
                        newOnly = false;
                    }

                    if (newOnly)
                    {
                        _logger.WriteVerbose("Indexing only new packages...");
                    }
                    else
                    {
                        _logger.WriteVerbose("Indexing all existing packages...");
                    }

                    var existentPackages = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
                    if (!newOnly && !shouldClean)
                    {
                        var indexedPackages = _index.GetPackages();
                        // remove packages from index that don't exist on disk anymore
                        foreach (var indexedPackage in indexedPackages)
                        {
                            if (File.Exists(indexedPackage.Path) && ShouldInclude(indexedPackage.Name))
                            {
                                existentPackages.Add(indexedPackage.Path);
                            }
                            else
                            {
                                _logger.WriteVerbose(string.Format("Package {0} does not exist on disk, removing ...", indexedPackage.Name));
                                _index.RemovePackage(indexedPackage.Name);
                            }
                        }
                    }

                    var packages = _discoverer.DiscoverPackages(_packageSources,
                                                                existentPackages,
                                                                newOnly,
                                                                _index.LastWriteTime,
                                                                cancellationToken,
                                                                ShouldInclude);

                    _logger.WriteVerbose("Found packages to be added to the index.");

                    _index.WarmUp();

                    int counter = 0;
                    foreach (var package in packages)
                    {
                        if (cancellationToken != null && cancellationToken.IsCancellationRequested)
                        {
                            return new LocalPackageIndexBuilderResult {
                                Success = false, TimeElapsed = stopWatch.Elapsed
                            };
                        }

                        var errors = _index.AddPackage(package, force: false);
                        success &= (errors == null || !errors.Any());
                        counter++;
                    }

                    _logger.WriteVerbose("Added {0} packages.", counter);

                    _index.CoolDown();
                }
                catch (Exception e)
                {
                    Debug.Write(e.ToString());
                    success = false;
                }

                stopWatch.Stop();
                _logger.WriteInformation("Finished building index.");


                return new LocalPackageIndexBuilderResult
                {
                    Success = success,
                    TimeElapsed = stopWatch.Elapsed
                };
            }, PackageIndexFactory.LocalIndexCancellationTokenSource.Token));
        }