From d4bcabe903e2f1dee37c18b5fe76440d8ab96ab7 Mon Sep 17 00:00:00 2001 From: Paul Siersma Date: Fri, 27 Oct 2023 14:01:02 +0200 Subject: [PATCH] Conditionally include Project column and value into Markdown output --- src/Methods.cs | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/src/Methods.cs b/src/Methods.cs index f200855a..d160bd8f 100644 --- a/src/Methods.cs +++ b/src/Methods.cs @@ -1139,11 +1139,15 @@ public void PrintLicenses(List libraries) if (!libraries.Any()) { return; } WriteOutput(Environment.NewLine + "References:", logLevel: LogLevel.Always); - WriteOutput(libraries.ToStringTable(new[] { "Reference", "Version", "License Type", "License" }, false, - a => a.PackageName ?? "---", - a => a.PackageVersion ?? "---", - a => a.LicenseType ?? "---", - a => a.LicenseUrl ?? "---"), logLevel: LogLevel.Always); + var columns = _packageOptions.IncludeProjectFile + ? StandardMdColumns.Append("Project").ToArray() + : StandardMdColumns; + + var valueSelectors = _packageOptions.IncludeProjectFile + ? StandardMdValueSelectors.Append(a => a.Projects ?? "---").ToArray() + : StandardMdValueSelectors; + + WriteOutput(libraries.ToStringTable(columns, false, valueSelectors), logLevel: LogLevel.Always); } public void SaveAsJson(List libraries) @@ -1198,17 +1202,31 @@ public void SaveAsTextFile(List libraries) File.WriteAllText(GetOutputFilename("licenses.txt"), sb.ToString()); } + private static readonly string[] StandardMdColumns = { "Reference", "Version", "License Type", "License" }; + + private static readonly Func[] StandardMdValueSelectors = + { + a => a.PackageName ?? "---", + a => a.PackageVersion ?? "---", + a => a.LicenseType ?? "---", + a => a.LicenseUrl ?? "---" + }; + public void SaveAsMarkdown(List libraries) { - if (libraries is null) { throw new ArgumentNullException(nameof(libraries)); } + if (libraries is null) { throw new ArgumentNullException(nameof(libraries)); }t if (!libraries.Any()) { return; } WriteOutput(Environment.NewLine + "References:", logLevel: LogLevel.Always); - var output = (libraries.ToStringTable(new[] { "Reference", "Version", "License Type", "License" }, true, - a => a.PackageName ?? "---", - a => a.PackageVersion ?? "---", - a => a.LicenseType ?? "---", - a => a.LicenseUrl ?? "---"), logLevel: LogLevel.Always); + var columns = _packageOptions.IncludeProjectFile + ? StandardMdColumns.Append("Project").ToArray() + : StandardMdColumns; + + var valueSelectors = _packageOptions.IncludeProjectFile + ? StandardMdValueSelectors.Append(a => a.Projects ?? "---").ToArray() + : StandardMdValueSelectors; + + var output = (libraries.ToStringTable(columns, true, valueSelectors), logLevel: LogLevel.Always); File.WriteAllText(GetOutputFilename("licenses.md"), output.Item1); }