コード例 #1
0
        public Dictionary <string, NuGetPackage> GetInstalledPackages(IProject project, XNode xNode)
        {
            var(prefix, namespaceManager, _) = xNode.Document.GetNamespaceManager();

            var packageReferenceElements = xNode.XPathSelectElements($"/{prefix}:Project/{prefix}:ItemGroup/{prefix}:PackageReference", namespaceManager);

            return(packageReferenceElements.ToDictionary(
                       element => element.Attribute("Include")?.Value,
                       element =>
            {
                var version = element.XPathSelectElement($"{prefix}:Version", namespaceManager)?.Value;

                var privateAssetsElement = element.XPathSelectElement($"{prefix}:PrivateAssets", namespaceManager);
                var privateAssets = privateAssetsElement != null
                        ? privateAssetsElement.Value
                                    .Split(';')
                                    .Select(y => y.Trim())
                        : new string[0];

                var includeAssetsElement = element.XPathSelectElement($"{prefix}:IncludeAssets", namespaceManager);
                var includeAssets = includeAssetsElement != null
                        ? includeAssetsElement.Value
                                    .Split(';')
                                    .Select(y => y.Trim())
                        : new string[0];

                return NuGetPackage.Create(version, includeAssets, privateAssets);
            }));
        }
コード例 #2
0
        public Dictionary <string, NuGetPackage> GetInstalledPackages(IProject project, XNode xNode)
        {
            var packageReferenceElements = xNode.XPathSelectElements("Project/ItemGroup/PackageReference");

            return(packageReferenceElements.ToDictionary(
                       element => element.Attribute("Include")?.Value,
                       element =>
            {
                var version = element.Attribute("Version")?.Value;

                var privateAssetsElement = element.XPathSelectElement("PrivateAssets");
                var privateAssets = privateAssetsElement != null
                        ? privateAssetsElement.Value
                                    .Split(';')
                                    .Select(y => y.Trim())
                        : new string[0];

                var includeAssetsElement = element.XPathSelectElement("IncludeAssets");
                var includeAssets = includeAssetsElement != null
                        ? includeAssetsElement.Value
                                    .Split(';')
                                    .Select(y => y.Trim())
                        : new string[0];

                return NuGetPackage.Create(version, includeAssets, privateAssets);
            }));
        }
コード例 #3
0
        public Dictionary <string, NuGetPackage> GetInstalledPackages(string projectPath, XNode xNode)
        {
            var projectFileDirectory = Path.GetDirectoryName(projectPath);

            if (projectFileDirectory == null)
            {
                throw new Exception("projectFileDirectory is null.");
            }

            var packagesDotConfigPath = Path.Combine(projectFileDirectory, "packages.config");

            if (!File.Exists(packagesDotConfigPath))
            {
                throw new Exception($"Could not find `{packagesDotConfigPath}`");
            }

            var d            = XDocument.Load(packagesDotConfigPath);
            var rootElements = d.Elements().ToArray();

            if (rootElements.Length != 1 || rootElements.Single().Name != "packages")
            {
                throw new Exception($"Error reading '{packagesDotConfigPath}', expected single root element called 'packages'");
            }

            var subElements = rootElements.Single().Elements().ToArray();

            if (subElements.Any(x => x.Name != "package"))
            {
                throw new Exception($"Error reading '{packagesDotConfigPath}', expected all child elements to be called 'package'");
            }

            var nugetPackages = subElements
                                .Select(x => new
            {
                Id      = x.Attribute("id")?.Value,
                Version = x.Attribute("version")?.Value
            })
                                .ToArray();

            if (nugetPackages.Any(x =>
                                  string.IsNullOrWhiteSpace(x.Id) ||
                                  string.IsNullOrWhiteSpace(x.Version) ||
                                  !NuGetVersion.TryParse(x.Version, out _)))
            {
                throw new Exception($"Error reading '{packagesDotConfigPath}', expected all child elements to have both an 'id' and a valid Semantic Version 2.0 'version' value.");
            }

            return(nugetPackages.ToDictionary(x => x.Id, x => NuGetPackage.Create(x.Version, new string[0], new string[0])));
        }
コード例 #4
0
        internal static NuGetProject DetermineProjectNugetPackageInfo(IVisualStudioProjectTemplate project)
        {
            var projectContent = project.LoadContent();
            var document       = XDocument.Parse(projectContent);

            var projectType = document.ResolveProjectScheme();

            if (!NuGetProjectSchemeProcessors.TryGetValue(projectType, out var processor))
            {
                throw new ArgumentOutOfRangeException(nameof(projectType), $"No scheme registered for type {projectType}.");
            }

            //if (!_projectRegistry.ContainsKey(project.Model.Id))
            //{
            //    Logging.Log.Warning($"Project file not found for project [{project}]");
            //    continue;
            //}
            var installedPackages = processor.GetInstalledPackages(project.FilePath, document);

            var highestVersionsInProject = new Dictionary <string, VersionRange>();

            foreach (var installedPackage in installedPackages)
            {
                var packageId = installedPackage.Key;

                if (!highestVersionsInProject.TryGetValue(packageId, out var highestVersion) ||
                    highestVersion.MinVersion < installedPackage.Value.Version.MinVersion)
                {
                    highestVersionsInProject[packageId] = installedPackage.Value.Version;
                }
            }

            //var consolidatedPackages = installedPackages.ToDictionary(x => x.Key, x => x.Value.Clone());
            var requestedPackages = new Dictionary <string, NuGetPackage>();

            foreach (var package in project.RequestedNugetPackages())
            {
                if (!VersionRange.TryParse(package.Version, out var semanticVersion))
                {
                    throw new Exception(
                              $"Could not parse '{package.Version}' from Intent metadata for package '{package.Name}' in project '{project.Name}' as a valid Semantic Version 2.0 'version' value.");
                }

                if (!highestVersionsInProject.TryGetValue(package.Name, out var highestVersion) ||
                    highestVersion.MinVersion < semanticVersion.MinVersion)
                {
                    highestVersionsInProject[package.Name] = highestVersion = semanticVersion;
                }

                //if (consolidatedPackages.TryGetValue(package.Name, out var consolidatedPackage))
                //{
                //    consolidatedPackage.Update(highestVersion, package);
                //}
                //else
                //{
                //    consolidatedPackages.Add(package.Name, NuGetPackage.Create(package, highestVersion));
                //}

                if (requestedPackages.TryGetValue(package.Name, out var requestedPackage))
                {
                    requestedPackage.Update(highestVersion, package);
                }
                else
                {
                    requestedPackages.Add(package.Name, NuGetPackage.Create(package, highestVersion));
                }
            }

            return(new NuGetProject
            {
                Content = projectContent,
                RequestedPackages = requestedPackages,
                InstalledPackages = installedPackages,
                HighestVersions = highestVersionsInProject,
                Name = project.Name,
                FilePath = project.FilePath,
                Processor = processor
            });
        }
コード例 #5
0
        /// <summary>
        /// Internal so available to unit tests
        /// </summary>
        internal static (IReadOnlyCollection <NuGetProject> Projects, Dictionary <string, SemanticVersion> HighestVersions) DeterminePackages(IEnumerable <IProject> applicationProjects, Func <IProject, string> loadDelegate)
        {
            var projects        = new List <NuGetProject>();
            var highestVersions = new Dictionary <string, SemanticVersion>();

            foreach (var project in applicationProjects.OrderBy(x => x.Name))
            {
                string projectContent = null;
                var    document       = project.ProjectFile() != null
                    ? XDocument.Parse(projectContent = loadDelegate(project))
                    : null;

                var projectType = ResolveNuGetScheme(document);
                if (!NuGetProjectSchemeProcessors.TryGetValue(projectType, out var processor))
                {
                    throw new ArgumentOutOfRangeException(nameof(projectType), $"No scheme registered for type {projectType}.");
                }

                var installedPackages = processor.GetInstalledPackages(project, document);

                foreach (var installedPackage in installedPackages)
                {
                    var packageId = installedPackage.Key;

                    if (!highestVersions.TryGetValue(packageId, out var highestVersion) ||
                        highestVersion < installedPackage.Value.Version)
                    {
                        highestVersions[packageId] = installedPackage.Value.Version;
                    }
                }

                var consolidatedPackages = installedPackages.ToDictionary(x => x.Key, x => x.Value.Clone());
                var requestedPackages    = new Dictionary <string, NuGetPackage>();

                foreach (var package in project.NugetPackages())
                {
                    if (!SemanticVersion.TryParse(package.Version, out var semanticVersion))
                    {
                        throw new Exception($"Could not parse '{package.Version}' from Intent metadata for package '{package.Name}' in project '{project.Name}' as a valid Semantic Version 2.0 'version' value.");
                    }

                    if (!highestVersions.TryGetValue(package.Name, out var highestVersion) ||
                        highestVersion < semanticVersion)
                    {
                        highestVersions[package.Name] = highestVersion = semanticVersion;
                    }

                    if (consolidatedPackages.TryGetValue(package.Name, out var consolidatedPackage))
                    {
                        consolidatedPackage.Update(highestVersion, package);
                    }
                    else
                    {
                        consolidatedPackages.Add(package.Name, NuGetPackage.Create(package, highestVersion));
                    }

                    if (requestedPackages.TryGetValue(package.Name, out var requestedPackage))
                    {
                        requestedPackage.Update(highestVersion, package);
                    }
                    else
                    {
                        requestedPackages.Add(package.Name, NuGetPackage.Create(package, highestVersion));
                    }
                }

                projects.Add(new NuGetProject
                {
                    Content           = projectContent,
                    RequestedPackages = requestedPackages,
                    InstalledPackages = installedPackages,
                    Name      = project.Name,
                    FilePath  = project.ProjectFile(),
                    Processor = processor
                });
            }

            return(projects, highestVersions);
        }