Example #1
0
        /// <summary>
        /// Add a file to the repository.
        /// </summary>
        public override void AddPackage(IPackage package)
        {
            string fileName = _pathResolver.GetPackageFileName(package);

            if (_fileSystem.FileExists(fileName) && !AllowOverrideExistingPackageOnPush)
            {
                throw new InvalidOperationException(String.Format(NuGetResources.Error_PackageAlreadyExists, package));
            }

            lock (_lockObj)
            {
                using (Stream stream = package.GetStream())
                {
                    _fileSystem.AddFile(fileName, stream);
                }

                //InvalidatePackages();

                ParallelOptions opts = new ParallelOptions();
                opts.MaxDegreeOfParallelism = 4;

                ConcurrentDictionary <string, Tuple <IPackage, DerivedPackageData> > absoluteLatest =
                    new ConcurrentDictionary <string, Tuple <IPackage, DerivedPackageData> >();
                ConcurrentDictionary <string, Tuple <IPackage, DerivedPackageData> > latest =
                    new ConcurrentDictionary <string, Tuple <IPackage, DerivedPackageData> >();

                // get settings
                bool checkFrameworks = EnableFrameworkFiltering;
                // we need to save the current context because it's stored in TLS and we're computing hashes on different threads.
                var context = HttpContext.Current;

                // load and cache all packages.
                // Note that we can't pass GetPackageFiles() to Parallel.ForEach() because
                // the file could be added/deleted from _fileSystem, and if this happens,
                // we'll get error "Collection was modified; enumeration operation may not execute."
                // So we have to materialize the IEnumerable into a list first.

                var path = _pathResolver.GetInstallPath(package) + ".nupkg";
                Debug.Assert(package != null, "Unable to open " + path);
                if (package == null)
                {
                    return;
                }
                AddPackageToCache(PackageCache, absoluteLatest, latest, checkFrameworks, context, path, package);

                // Set additional attributes after visiting all packages
                foreach (var entry in absoluteLatest.Values)
                {
                    entry.Item2.IsAbsoluteLatestVersion = true;
                }

                foreach (var entry in latest.Values)
                {
                    entry.Item2.IsLatestVersion = true;
                }
            }
        }
        /// <summary>
        /// Add a file to the repository.
        /// </summary>
        public override void AddPackage(IPackage package)
        {
            string fileName = _pathResolver.GetPackageFileName(package);

            if (_fileSystem.FileExists(fileName) && !AllowOverrideExistingPackageOnPush)
            {
                throw new InvalidOperationException(String.Format(NuGetResources.Error_PackageAlreadyExists, package));
            }

            lock (_lockObj)
            {
                using (Stream stream = package.GetStream())
                {
                    _fileSystem.AddFile(fileName, stream);
                }

                InvalidatePackages();
            }
        }
        public void MarkPackageDirectoryForDeletion(IPackage package, Func <string, IPackage> createZipPackageFromPath)
        {
            IFileSystem          repositoryFileSystem = _repositoryFileSystemFactory();
            IPackagePathResolver pathResolver         = _packagePathResolverFactory();
            string packageDirectoryName = pathResolver.GetPackageDirectory(package);

            try
            {
                if (repositoryFileSystem.DirectoryExists(packageDirectoryName))
                {
                    string   packageFilePath = Path.Combine(packageDirectoryName, pathResolver.GetPackageFileName(package));
                    IPackage zipPackage      = createZipPackageFromPath(repositoryFileSystem.GetFullPath(packageFilePath));

                    IEnumerable <IPackageFile>         origPackageFiles      = zipPackage.GetFiles();
                    IDictionary <string, IPackageFile> origPackageFileLookup = origPackageFiles.ToDictionary(f => Path.Combine(packageDirectoryName, f.Path), StringComparer.OrdinalIgnoreCase);

                    foreach (var filePath in repositoryFileSystem.GetFiles(path: packageDirectoryName, filter: "*.*", recursive: true))
                    {
                        // Assume the package file and the package manifest are unmodified.
                        if (!filePath.Equals(packageFilePath, StringComparison.OrdinalIgnoreCase) &&
                            !Path.GetExtension(filePath).Equals(Constants.ManifestExtension, StringComparison.OrdinalIgnoreCase))
                        {
                            // Don't mark directory for deletion if file has been added.
                            IPackageFile origFile;
                            if (!origPackageFileLookup.TryGetValue(filePath, out origFile))
                            {
                                return;
                            }

                            // Don't mark directory for deletion if file has been modified.
                            using (Stream origStream = origFile.GetStream(),
                                   fileStream = repositoryFileSystem.OpenFile(filePath))
                            {
                                if (!origStream.ContentEquals(fileStream))
                                {
                                    return;
                                }
                            }
                        }
                    }

                    // NOTE: The repository should always be a PhysicalFileSystem, except during testing, so the
                    // .deleteme marker file doesn't get checked into version control
                    repositoryFileSystem.AddFile(packageDirectoryName + DeletionMarkerSuffix, Stream.Null);
                }
            }
            catch (Exception e)
            {
                repositoryFileSystem.Logger.Log(MessageLevel.Warning, String.Format(Resources.VsResources.Warning_FailedToMarkPackageDirectoryForDeletion, packageDirectoryName, e.Message));
            }
        }
Example #4
0
        public static JObject CreatePackage(IPackage version, string repoRoot, IPackagePathResolver pathResolver)
        {
            var value = new JObject();

            AddProp(value, Properties.Type, new JArray(
                        Types.PackageIdentity.ToString(),
                        Types.PackageDescription.ToString(),
                        Types.PackageDependencies.ToString(),
                        Types.PackageLicensing.ToString()));

            AddProp(value, Properties.PackageId, version.Id);
            AddProp(value, Properties.Version, version.Version.ToString());
            AddProp(value, Properties.Summary, version.Summary);
            AddProp(value, Properties.Description, version.Description);
            AddProp(value, Properties.Authors, String.Join(", ", version.Authors));
            AddProp(value, Properties.Owners, String.Join(", ", version.Owners));
            AddProp(value, Properties.IconUrl, version.IconUrl);
            AddProp(value, Properties.LicenseUrl, version.LicenseUrl);
            AddProp(value, Properties.ProjectUrl, version.ProjectUrl);
            AddProp(value, Properties.Tags, version.Tags == null ? null : version.Tags.Split(' '));
            AddProp(value, Properties.DownloadCount, version.DownloadCount);
            AddProp(value, Properties.Published, version.Published.HasValue ? version.Published.Value.ToString("O", CultureInfo.InvariantCulture) : null);
            AddProp(value, Properties.RequireLicenseAcceptance, version.RequireLicenseAcceptance);
            AddProp(value, Properties.DependencyGroups, version.DependencySets.Select(set => CreateDependencyGroup(set)));

            if (version.MinClientVersion != null)
            {
                AddProp(value, Properties.MinimumClientVersion, version.MinClientVersion.ToString());
            }

            var dsPackage = version as DataServicePackage;

            if (dsPackage != null)
            {
                AddProp(value, Properties.PackageContent, dsPackage.DownloadUrl);
            }
            else if (pathResolver != null)
            {
                AddProp(
                    value,
                    Properties.PackageContent,
                    Path.Combine(repoRoot, pathResolver.GetPackageFileName(version)));
            }

            return(value);
        }