示例#1
0
        /// <summary>
        /// Runs an <see cref="UpdateOperation"/> on the dependencies contained in a <see cref="XmlDocument"/> loaded from a .nuspec file.
        /// </summary>
        /// <param name="document"></param>
        /// <param name="operation"></param>
        /// <returns></returns>
        public static IEnumerable <UpdateOperation> UpdateDependencies(
            this XmlDocument document,
            UpdateOperation operation
            )
        {
            var operations = new List <UpdateOperation>();

            foreach (var node in document.SelectElements("dependency", $"[@id='{operation.PackageId}']"))
            {
                var versionNodeValue = node.GetAttribute("version");

                // only nodes with explicit version, skip expansion.
                if (!versionNodeValue.Contains("{", System.StringComparison.OrdinalIgnoreCase))
                {
                    var currentOperation = operation.WithPreviousVersion(versionNodeValue);

                    if (currentOperation.ShouldProceed())
                    {
                        node.SetAttribute("version", currentOperation.UpdatedVersion.ToString());
                    }

                    operations.Add(currentOperation);
                }
            }

            return(operations);
        }
示例#2
0
        /// <summary>
        /// Runs an <see cref="UpdateOperation"/> on the PackageReferences contained in a <see cref="XmlDocument"/>.
        /// </summary>
        /// <param name="document"></param>
        /// <param name="operation"></param>
        /// <returns></returns>
        public static IEnumerable <UpdateOperation> UpdatePackageReferences(
            this XmlDocument document,
            UpdateOperation operation
            )
        {
            var operations = new List <UpdateOperation>();

            var packageId = operation.PackageId;

            var packageReferences   = document.SelectElements("PackageReference", $"[@Include='{packageId}' or @Update='{packageId}']");
            var dotnetCliReferences = document.SelectElements("DotNetCliToolReference", $"[@Include='{packageId}' or @Update='{packageId}']");

            foreach (var packageReference in packageReferences.Concat(dotnetCliReferences))
            {
                var packageVersion = packageReference.GetAttributeOrChild("Version");

                if (packageVersion.HasValue())
                {
                    var currentOperation = operation.WithPreviousVersion(packageVersion);

                    if (currentOperation.ShouldProceed())
                    {
                        packageReference.SetAttributeOrChild("Version", currentOperation.UpdatedVersion.ToString());
                    }

                    operations.Add(currentOperation);
                }
            }

            return(operations);
        }