public void TestPropertyNameHelpers()
        {
            Assert.Equal(
                "MicrosoftNETCoreAppRefPackageVersion",
                WriteBuildOutputProps.GetPropertyName("Microsoft.NETCore.App.Ref"));

            Assert.Equal(
                "MicrosoftNETCoreAppRefVersion",
                WriteBuildOutputProps.GetAlternatePropertyName("Microsoft.NETCore.App.Ref"));
        }
Exemple #2
0
        public override bool Execute()
        {
            UsageData data = JObject.Parse(File.ReadAllText(DataFile)).ToObject <UsageData>();

            IEnumerable <RepoOutput> sourceBuildRepoOutputs = GetSourceBuildRepoOutputs();

            // Map package id to the build name that created them in a ProdCon build.
            var prodConPackageOrigin = new Dictionary <string, string>(
                StringComparer.OrdinalIgnoreCase);

            foreach (ITaskItem item in ProdConPackageInfos ?? Enumerable.Empty <ITaskItem>())
            {
                AddProdConPackage(
                    prodConPackageOrigin,
                    item.GetMetadata("PackageId"),
                    item.GetMetadata("OriginBuildName"));
            }

            if (File.Exists(ProdConBuildManifestFile))
            {
                var xml = XElement.Parse(File.ReadAllText(ProdConBuildManifestFile));
                foreach (var x in xml.Descendants("Package"))
                {
                    AddProdConPackage(
                        prodConPackageOrigin,
                        x.Attribute("Id")?.Value,
                        x.Attribute("OriginBuildName")?.Value);
                }
            }

            PreviousReleasePrebuilt[] previousReleasePrebuilts = PreviousReleasePrebuiltPackageInfos
                                                                 ?.Select(item => new PreviousReleasePrebuilt
            {
                Id      = item.GetMetadata("PackageId"),
                Version = item.GetMetadata("PackageVersion"),
                Release = item.GetMetadata("Release")
            })
                                                                 .ToArray();

            var poisonNupkgFilenames = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            if (File.Exists(PoisonedReportFile))
            {
                foreach (string line in File.ReadAllLines(PoisonedReportFile))
                {
                    string[] segments = line.Split('\\');
                    if (segments.Length > 2 &&
                        segments[0].Trim() == "intermediate" &&
                        segments[1].EndsWith(".nupkg"))
                    {
                        poisonNupkgFilenames.Add(Path.GetFileNameWithoutExtension(segments[1]));
                    }
                }
            }

            foreach (Usage usage in data.Usages)
            {
                string[] identity = usage.PackageIdentity.Split('/');
                if (identity.Length != 2)
                {
                    Log.LogWarning(
                        $"Data file contains invalid package identity '{usage.PackageIdentity}'. " +
                        "Expected 2 segments when split by '/'.");
                    continue;
                }

                string id      = identity[0];
                string version = identity[1];

                string pvpIdent = WriteBuildOutputProps.GetPropertyName(id);

                var sourceBuildCreator = new StringBuilder();
                foreach (RepoOutput output in sourceBuildRepoOutputs)
                {
                    foreach (PackageVersionPropsElement p in output.Built)
                    {
                        if (p.Name.Equals(pvpIdent, StringComparison.OrdinalIgnoreCase))
                        {
                            if (sourceBuildCreator.Length != 0)
                            {
                                sourceBuildCreator.Append(" ");
                            }
                            sourceBuildCreator.Append(output.Repo);
                            sourceBuildCreator.Append(" ");
                            sourceBuildCreator.Append(p.Name);
                            sourceBuildCreator.Append("/");
                            sourceBuildCreator.Append(p.Version);
                        }
                    }
                }

                prodConPackageOrigin.TryGetValue(id, out string prodConCreator);

                string previousReleasePrebuilt = previousReleasePrebuilts
                                                 ?.FirstOrDefault(p => p.Matches(id, version))
                                                 ?.Release;

                usage.Annotation = new UsageAnnotation
                {
                    SourceBuildPackageIdCreator   = sourceBuildCreator.ToString(),
                    ProdConPackageIdCreator       = prodConCreator,
                    InEarlierReleaseExactPrebuilt = previousReleasePrebuilt,
                    EndsUpInOutput = poisonNupkgFilenames.Contains($"{id}.{version}")
                };
            }

            Directory.CreateDirectory(OutputDirectory);

            File.WriteAllText(
                Path.Combine(OutputDirectory, "UsageSummary.xml"),
                data.CreateSummaryReport().ToString());

            File.WriteAllText(
                Path.Combine(OutputDirectory, "UsageDetails.xml"),
                data.CreateDetailedReport().ToString());

            return(!Log.HasLoggedErrors);
        }
        public override bool Execute()
        {
            UsageData data = UsageData.Parse(XElement.Parse(File.ReadAllText(DataFile)));

            IEnumerable <RepoOutput> sourceBuildRepoOutputs = GetSourceBuildRepoOutputs();

            // Map package id to the build name that created them in a ProdCon build.
            var prodConPackageOrigin = new Dictionary <string, string>(
                StringComparer.OrdinalIgnoreCase);

            foreach (ITaskItem item in ProdConPackageInfos.NullAsEmpty())
            {
                AddProdConPackage(
                    prodConPackageOrigin,
                    item.GetMetadata("PackageId"),
                    item.GetMetadata("OriginBuildName"));
            }

            if (File.Exists(ProdConBuildManifestFile))
            {
                var xml = XElement.Parse(File.ReadAllText(ProdConBuildManifestFile));
                foreach (var x in xml.Descendants("Package"))
                {
                    AddProdConPackage(
                        prodConPackageOrigin,
                        x.Attribute("Id")?.Value,
                        x.Attribute("OriginBuildName")?.Value);
                }
            }

            var poisonNupkgFilenames = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            if (File.Exists(PoisonedReportFile))
            {
                foreach (string line in File.ReadAllLines(PoisonedReportFile))
                {
                    string[] segments = line.Split('\\');
                    if (segments.Length > 2 &&
                        segments[0].Trim() == "intermediate" &&
                        segments[1].EndsWith(".nupkg"))
                    {
                        poisonNupkgFilenames.Add(Path.GetFileNameWithoutExtension(segments[1]));
                    }
                }
            }

            var report = new XElement("AnnotatedUsages");

            foreach (Usage usage in data.Usages.NullAsEmpty())
            {
                string id      = usage.PackageIdentity.Id;
                string version = usage.PackageIdentity.Version.OriginalVersion;

                string pvpIdent = WriteBuildOutputProps.GetPropertyName(id);

                var sourceBuildCreator = new StringBuilder();
                foreach (RepoOutput output in sourceBuildRepoOutputs)
                {
                    foreach (PackageVersionPropsElement p in output.Built)
                    {
                        if (p.Name.Equals(pvpIdent, StringComparison.OrdinalIgnoreCase))
                        {
                            if (sourceBuildCreator.Length != 0)
                            {
                                sourceBuildCreator.Append(" ");
                            }
                            sourceBuildCreator.Append(output.Repo);
                            sourceBuildCreator.Append(" ");
                            sourceBuildCreator.Append(p.Name);
                            sourceBuildCreator.Append("/");
                            sourceBuildCreator.Append(p.Version);
                        }
                    }
                }

                prodConPackageOrigin.TryGetValue(id, out string prodConCreator);

                var annotated = new AnnotatedUsage
                {
                    Usage = usage,

                    Project = data.ProjectDirectories
                              ?.FirstOrDefault(p => usage.AssetsFile?.StartsWith(p) ?? false),

                    SourceBuildPackageIdCreator = sourceBuildCreator.Length == 0
                        ? null
                        : sourceBuildCreator.ToString(),

                    ProdConPackageIdCreator = prodConCreator,

                    EndsUpInOutput = poisonNupkgFilenames.Contains($"{id}.{version}")
                };

                report.Add(annotated.ToXml());
            }

            Directory.CreateDirectory(OutputDirectory);

            File.WriteAllText(
                Path.Combine(OutputDirectory, "annotated-usage.xml"),
                report.ToString());

            return(!Log.HasLoggedErrors);
        }