Exemplo n.º 1
0
        /// <summary>
        /// Getting packages from local folders that contain packages.
        /// </summary>
        public IEnumerable <string> GetPackages(bool newOnly)
        {
            _logger.WriteVerbose("Checking packages at: {0}", string.Join(";", _packageSources));

            foreach (var source in _packageSources)
            {
                var nupkgFiles = Directory.GetFiles(source, "*.nupkg", SearchOption.AllDirectories);
                foreach (var nupkgFile in nupkgFiles)
                {
                    if (newOnly && File.GetLastWriteTime(nupkgFile) <= _index.LastWriteTime)
                    {
                        continue;
                    }

                    yield return(nupkgFile);
                }
            }
        }
        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));
        }