diff --git a/pvs2codequality/Converter/XMLConverter.cs b/pvs2codequality/Converter/XMLConverter.cs index 8f831e2..f1858ae 100644 --- a/pvs2codequality/Converter/XMLConverter.cs +++ b/pvs2codequality/Converter/XMLConverter.cs @@ -10,7 +10,8 @@ internal static class XMLConverter { public static (int status, string? result, int linesFound) ParseFullDocument( string inputXML, - string? trimFolderName = null + string? trimFolderName = null, + string? reportFilenamePrefix = null ) { var xDoc = new XmlDocument(); @@ -46,24 +47,34 @@ public static (int status, string? result, int linesFound) ParseFullDocument( .Where(x => x.Name == "PVS-Studio_Analysis_Log") .ToArray(); - var result = ParseAllLogNodes(logRecords, trimFolderName); + var result = ParseAllLogNodes( + logRecords, + trimFolderName, + reportFilenamePrefix + ); return (0, System.Text.Json.JsonSerializer.Serialize(result), result.Count); } public static ICollection ParseAllLogNodes( ICollection logRecords, - string trimFolderName + string trimFolderName, + string? reportFilenamePrefix ) { return logRecords - .SelectMany(t => ParseAllLogNodes(t, trimFolderName)) + .SelectMany(t => ParseAllLogNodes( + logRecord: t, + trimFolderName: trimFolderName, + reportFilenamePrefix: reportFilenamePrefix + )) .ToArray(); } public static ICollection ParseAllLogNodes( XmlElement logRecord, - string trimFolderName + string trimFolderName, + string? reportFilenamePrefix ) { var fields = new[] @@ -75,16 +86,24 @@ string trimFolderName { if (t.InnerText != null) { - return (fieldName, value: t.InnerText); + return (fieldName, value: (string?) t.InnerText); } } - return (fieldName, null); + return (fieldName, value: (string?) null); }) .ToDictionary( t => t.fieldName, t => t.value ); + if (reportFilenamePrefix != null) + { + reportFilenamePrefix = reportFilenamePrefix.TrimEnd('/', '\\'); + if (reportFilenamePrefix == string.Empty) + { + reportFilenamePrefix = null; + } + } var additionalPositionNodes = logRecord .GetElementsByTagName("Positions") @@ -96,19 +115,23 @@ string trimFolderName var filename = t.InnerText; return lines .Split(',') - .Select(line => (filename, lineNumber: int.Parse(line.Trim()))); + .Select(line => (filename: (string?) filename, lineNumber: int.Parse(line.Trim()))); }) .SelectMany(t => t) .ToList(); - additionalPositionNodes.Add((values["File"], int.Parse(values["Line"]))); + additionalPositionNodes.Add((values["File"], int.Parse(values["Line"] ?? "0"))); var results = additionalPositionNodes .ToHashSet() .Select(t => { var localFile = ""; - if (t.filename != "") + if (!string.IsNullOrEmpty(t.filename)) { localFile = t.filename.Substring(trimFolderName.Length).TrimStart('/', '\\'); + if (reportFilenamePrefix != null) + { + localFile = reportFilenamePrefix + "/" + localFile; + } } // ReSharper disable once UseObjectOrCollectionInitializer diff --git a/pvs2codequality/Options.cs b/pvs2codequality/Options.cs index 70833e2..81d2e94 100644 --- a/pvs2codequality/Options.cs +++ b/pvs2codequality/Options.cs @@ -1,7 +1,11 @@ -using CommandLine; +using System.Diagnostics.CodeAnalysis; +using CommandLine; namespace Pvs2codequality { + [SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Global")] + [SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")] +#pragma warning disable 8618 public class Options { [Option('i', "input", Required = true, HelpText = "Input plog file")] @@ -9,5 +13,12 @@ public class Options [Option('o', "output", Required = false, HelpText = "Output json file")] public string? OutputFile { get; set; } + + [Option("report-filename-prefix", Required = false, Default = null, + HelpText = "Prefixes for filename in output report. If null or empty, then no prefixes attach")] + public string? ReportFilenamePrefix { get; set; } + + // TODO TrimFolderName } +#pragma warning restore 8618 } \ No newline at end of file diff --git a/pvs2codequality/Program.cs b/pvs2codequality/Program.cs index 4166d61..c37fcd1 100644 --- a/pvs2codequality/Program.cs +++ b/pvs2codequality/Program.cs @@ -38,7 +38,11 @@ public static int RunOptionsAndReturnExitCode(Options options) } var inputXML = File.ReadAllText(options.InputFile); - var outputJson = XMLConverter.ParseFullDocument(inputXML); + var outputJson = XMLConverter.ParseFullDocument( + inputXML, + trimFolderName: null, + reportFilenamePrefix: options.ReportFilenamePrefix + ); File.WriteAllText(outputFilename, outputJson.result!); Console.WriteLine( "File {0} created. {1} lines found", diff --git a/pvs2codequality/pvs2codequality.csproj b/pvs2codequality/pvs2codequality.csproj index e9c5b63..440bb3f 100644 --- a/pvs2codequality/pvs2codequality.csproj +++ b/pvs2codequality/pvs2codequality.csproj @@ -8,12 +8,12 @@ Pvs2codequality Pvs2codequality Nokita Kaze - Pvs2codequality + Nokita Kaze Pvs2codequality false - 0.1.0 + 0.1.1 0.1.0 - 0.1.0 +