Exemple #1
0
        /// <summary>
        /// Generates a table in markdown that lists the API version supported by
        /// various packages at all levels of NETStandard.
        /// </summary>
        /// <returns></returns>
        public override bool Execute()
        {
            if (PackageReports == null || PackageReports.Length == 0)
            {
                Log.LogError("PackageReports argument must be specified");
                return(false);
            }

            if (TargetMoniker == null || TargetMoniker.Length == 0)
            {
                Log.LogError("TargetMoniker argument must be specified");
                return(false);
            }

            NuGetFramework fx = NuGetFramework.Parse(TargetMoniker);

            string targetString = String.IsNullOrEmpty(TargetRuntime) ? fx.ToString() : $"{fx}/{TargetRuntime}";

            List <ITaskItem> compileAssets = new List <ITaskItem>();
            List <ITaskItem> runtimeAssets = new List <ITaskItem>();

            foreach (var reportPath in PackageReports)
            {
                var report = ValidationReport.Load(reportPath);

                Target target = null;
                if (report.Targets.TryGetValue(targetString, out target))
                {
                    compileAssets.AddRange(target.CompileAssets.Select(c => ItemFromApplicableAsset(c, report.Id, report.Version)));
                    runtimeAssets.AddRange(target.RuntimeAssets.Select(r => ItemFromApplicableAsset(r, report.Id, report.Version)));
                }
                else
                {
                    Log.LogMessage($"No assets found for '{report.Id}' applicable to '{targetString}'.");
                }
            }

            CompileAssets = compileAssets.ToArray();
            RuntimeAssets = runtimeAssets.ToArray();

            return(!Log.HasLoggedErrors);
        }
Exemple #2
0
        /// <summary>
        /// Generates a table in markdown that lists the API version supported by
        /// various packages at all levels of NETStandard.
        /// </summary>
        /// <returns></returns>
        public override bool Execute()
        {
            if (Reports == null || Reports.Length == 0)
            {
                Log.LogError("Reports argument must be specified");
                return(false);
            }

            if (String.IsNullOrEmpty(DocFilePath))
            {
                Log.LogError("DocFilePath argument must be specified");
                return(false);
            }

            string docDir = Path.GetDirectoryName(DocFilePath);

            if (!Directory.Exists(docDir))
            {
                Directory.CreateDirectory(docDir);
            }

            SortedSet <Version> knownNetStandardVersions = new SortedSet <Version>();
            List <SupportRow>   rows = new List <SupportRow>(Reports.Length);

            foreach (var reportPath in Reports.Select(r => r.GetMetadata("FullPath")))
            {
                SupportRow row = new SupportRow();
                row.Name             = Path.GetFileNameWithoutExtension(reportPath);
                row.SuportedVersions = new SortedSet <NETStandardApiVersion>();

                var report = ValidationReport.Load(reportPath);

                foreach (var supportedFramework in report.SupportedFrameworks)
                {
                    var fx = NuGetFramework.Parse(supportedFramework.Key);

                    if (fx.Framework == FrameworkConstants.FrameworkIdentifiers.NetStandard)
                    {
                        row.SuportedVersions.Add(new NETStandardApiVersion(fx.Version, new Version(supportedFramework.Value.ToString())));
                        knownNetStandardVersions.Add(fx.Version);
                    }
                }
                rows.Add(row);
            }

            StringBuilder table = new StringBuilder();

            table.AppendLine($"| Contract | {String.Join(" | ", knownNetStandardVersions.Select(v => v.ToString(2)))} |");
            table.AppendLine($"| -------- | {String.Join(" | ", Enumerable.Repeat("---", knownNetStandardVersions.Count))}");

            foreach (var row in rows.OrderBy(r => r.Name))
            {
                if (row.SuportedVersions.Count == 0)
                {
                    Log.LogMessage($"Skipping {row.Name} since it has no supported NETStandard versions");
                    continue;
                }

                table.Append($"| {row.Name} |");

                foreach (var netStandardVersion in knownNetStandardVersions)
                {
                    var apiVersion = row.SuportedVersions.LastOrDefault(a => a.NETStandardVersion <= netStandardVersion);

                    table.Append(" ");
                    if (apiVersion != null)
                    {
                        table.Append(apiVersion.APIVersion.ToString(3));
                    }
                    table.Append(" |");
                }
                table.AppendLine();
            }

            if (!InsertIntoFile)
            {
                File.WriteAllText(DocFilePath, table.ToString());
            }
            else
            {
                if (!File.Exists(DocFilePath))
                {
                    Log.LogError($"InsertIntoFile was specified as true but {DocFilePath} did not exist.");
                    return(false);
                }

                string originalText = File.ReadAllText(DocFilePath);
                int    startIndex   = originalText.IndexOf(startMarker);

                if (startIndex < 0)
                {
                    Log.LogError($"InsertIntoFile was specified as true but could not locate insertion start text \"{startMarker}\".");
                    return(false);
                }
                startIndex += startMarker.Length;
                // skip any white-space / new line
                while (startIndex < originalText.Length && Char.IsWhiteSpace(originalText[startIndex]))
                {
                    startIndex++;
                }

                int endIndex = originalText.IndexOf(endMarker, startIndex);

                if (endIndex < 0)
                {
                    Log.LogError($"InsertIntoFile was specified as true but could not locate insertion end text \"{endMarker}\".");
                    return(false);
                }
                var docText = new StringBuilder(originalText);
                docText.Remove(startIndex, endIndex - startIndex);
                docText.Insert(startIndex, table.ToString());

                File.WriteAllText(DocFilePath, docText.ToString(), Encoding.UTF8);
            }


            return(!Log.HasLoggedErrors);
        }