Example #1
0
        public override bool Execute()
        {
            // Parse input
            var metapackageArtifacts = PackageArtifacts.Where(p => p.GetMetadata(MetapackageReferenceType) == "true");
            var externalArtifacts    = ExternalDependencies.Where(p => p.GetMetadata(MetapackageReferenceType) == "true");
            var buildArtifacts       = BuildArtifacts.Select(ArtifactInfo.Parse)
                                       .OfType <ArtifactInfo.Package>()
                                       .Where(p => !p.IsSymbolsArtifact);

            var xmlDoc = new XmlDocument();

            xmlDoc.Load(ReferencePackagePath);

            // Project
            var projectElement = xmlDoc.FirstChild;

            // Items
            var itemGroupElement = xmlDoc.CreateElement("ItemGroup");

            Log.LogMessage(MessageImportance.High, $"{MetapackageReferenceType} will include the following packages");

            foreach (var package in metapackageArtifacts)
            {
                var    packageName = package.ItemSpec;
                string packageVersion;
                try
                {
                    packageVersion = buildArtifacts
                                     .Single(p => string.Equals(p.PackageInfo.Id, packageName, StringComparison.OrdinalIgnoreCase))
                                     .PackageInfo.Version.ToString();
                }
                catch (InvalidOperationException)
                {
                    Log.LogError($"Missing Package: {packageName} from build artifacts");
                    throw;
                }

                var packageVersionValue = LockToExactVersions ? $"[{packageVersion}]" : packageVersion;

                Log.LogMessage(MessageImportance.High, $" - Package: {packageName} Version: {packageVersionValue}");

                var packageReferenceElement = xmlDoc.CreateElement("PackageReference");
                packageReferenceElement.SetAttribute("Include", packageName);
                packageReferenceElement.SetAttribute("Version", packageVersionValue);
                packageReferenceElement.SetAttribute("PrivateAssets", "None");

                itemGroupElement.AppendChild(packageReferenceElement);
            }

            foreach (var package in externalArtifacts)
            {
                var packageName         = package.ItemSpec;
                var packageVersion      = package.GetMetadata("Version");
                var packageVersionValue = LockToExactVersions ? $"[{packageVersion}]" : packageVersion;

                Log.LogMessage(MessageImportance.High, $" - Package: {packageName} Version: {packageVersionValue}");

                var packageReferenceElement = xmlDoc.CreateElement("PackageReference");
                packageReferenceElement.SetAttribute("Include", packageName);
                packageReferenceElement.SetAttribute("Version", packageVersionValue);
                packageReferenceElement.SetAttribute("PrivateAssets", "None");

                itemGroupElement.AppendChild(packageReferenceElement);
            }

            projectElement.AppendChild(itemGroupElement);

            // Save updated file
            xmlDoc.AppendChild(projectElement);
            xmlDoc.Save(ReferencePackagePath);

            return(true);
        }
        public override bool Execute()
        {
            // Parse input
            var externalArchiveArtifacts = ExternalDependencies.Where(p => p.GetMetadata("LZMA") == "true" && p.GetMetadata("PackageType") == "Dependency");
            var externalArchiveTools     = ExternalDependencies.Where(p => p.GetMetadata("LZMA") == "true" && p.GetMetadata("PackageType") == "DotnetCliTool");
            var archiveArtifacts         = PackageArtifacts.Where(p => p.GetMetadata("LZMA") == "true" && p.GetMetadata("PackageType") == "Dependency");
            var archiveTools             = PackageArtifacts.Where(p => p.GetMetadata("LZMA") == "true" && p.GetMetadata("PackageType") == "DotnetCliTool");
            var buildArtifacts           = BuildArtifacts.Select(ArtifactInfo.Parse)
                                           .OfType <ArtifactInfo.Package>()
                                           .Where(p => !p.IsSymbolsArtifact);

            var xmlDoc = new XmlDocument();

            xmlDoc.Load(ReferencePackagePath);

            // Project
            var projectElement = xmlDoc.FirstChild;

            // Items
            var itemGroupElement = xmlDoc.CreateElement("ItemGroup");

            Log.LogMessage(MessageImportance.High, $"Archive will include the following packages");

            foreach (var package in archiveArtifacts)
            {
                var packageName = package.ItemSpec;

                string packageVersion;

                if (string.Equals(packageName, "Microsoft.AspNetCore.All", StringComparison.OrdinalIgnoreCase) ||
                    string.Equals(packageName, "Microsoft.AspNetCore.App", StringComparison.OrdinalIgnoreCase))
                {
                    packageVersion = MetaPackageVersion;
                }
                else
                {
                    var packageVersionCandidates = buildArtifacts.Where(p => string.Equals(p.PackageInfo.Id, packageName, StringComparison.OrdinalIgnoreCase));

                    if (!packageVersionCandidates.Any())
                    {
                        Log.LogError($"Missing Package: {packageName} from artifacts archive.");
                        return(false);
                    }
                    else if (packageVersionCandidates.Count() > 1)
                    {
                        Log.LogError($"Duplicate Packages: {packageName} from artifacts archive.");
                        return(false);
                    }

                    packageVersion = packageVersionCandidates.Single().PackageInfo.Version.ToString();
                }
                Log.LogMessage(MessageImportance.High, $" - Package: {packageName} Version: {packageVersion}");

                var packageReferenceElement = xmlDoc.CreateElement("PackageReference");
                packageReferenceElement.SetAttribute("Include", packageName);
                packageReferenceElement.SetAttribute("Version", packageVersion);

                itemGroupElement.AppendChild(packageReferenceElement);
            }

            foreach (var package in externalArchiveArtifacts)
            {
                var packageName    = package.ItemSpec;
                var packageVersion = package.GetMetadata("Version");
                Log.LogMessage(MessageImportance.High, $" - Package: {packageName} Version: {packageVersion}");

                var packageReferenceElement = xmlDoc.CreateElement("PackageReference");
                packageReferenceElement.SetAttribute("Include", packageName);
                packageReferenceElement.SetAttribute("Version", packageVersion);

                itemGroupElement.AppendChild(packageReferenceElement);
            }

            foreach (var package in archiveTools)
            {
                var packageName = package.ItemSpec;

                var packageVersionCandidates = buildArtifacts.Where(p => string.Equals(p.PackageInfo.Id, packageName, StringComparison.OrdinalIgnoreCase));

                if (!packageVersionCandidates.Any())
                {
                    Log.LogError($"Missing Package: {packageName} from tools archive.");
                    return(false);
                }
                else if (packageVersionCandidates.Count() > 1)
                {
                    Log.LogError($"Duplicate Packages: {packageName} from tools archive.");
                    return(false);
                }

                var packageVersion = packageVersionCandidates.Single().PackageInfo.Version.ToString();

                Log.LogMessage(MessageImportance.High, $" - Tool: {packageName} Version: {packageVersion}");

                var packageReferenceElement = xmlDoc.CreateElement("DotNetCliToolReference");
                packageReferenceElement.SetAttribute("Include", packageName);
                packageReferenceElement.SetAttribute("Version", packageVersion);

                itemGroupElement.AppendChild(packageReferenceElement);
            }

            foreach (var package in externalArchiveTools)
            {
                var packageName    = package.ItemSpec;
                var packageVersion = package.GetMetadata("Version");
                Log.LogMessage(MessageImportance.High, $" - Tool: {packageName} Version: {packageVersion}");

                var packageReferenceElement = xmlDoc.CreateElement("DotNetCliToolReference");
                packageReferenceElement.SetAttribute("Include", packageName);
                packageReferenceElement.SetAttribute("Version", packageVersion);

                itemGroupElement.AppendChild(packageReferenceElement);
            }
            projectElement.AppendChild(itemGroupElement);

            // Save updated file
            xmlDoc.AppendChild(projectElement);
            xmlDoc.Save(ReferencePackagePath);

            return(true);
        }