Skip to content

Commit

Permalink
+ ReportFilenamePrefix
Browse files Browse the repository at this point in the history
* Code style fixes
  • Loading branch information
nokitakaze committed Nov 3, 2020
1 parent 927da49 commit 41744b4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 15 deletions.
43 changes: 33 additions & 10 deletions pvs2codequality/Converter/XMLConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<CodeQualityLogRecord> ParseAllLogNodes(
ICollection<XmlElement> 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<CodeQualityLogRecord> ParseAllLogNodes(
XmlElement logRecord,
string trimFolderName
string trimFolderName,
string? reportFilenamePrefix
)
{
var fields = new[]
Expand All @@ -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")
Expand All @@ -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
Expand Down
13 changes: 12 additions & 1 deletion pvs2codequality/Options.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
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")]
public string InputFile { get; set; }

[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
}
6 changes: 5 additions & 1 deletion pvs2codequality/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions pvs2codequality/pvs2codequality.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
<RootNamespace>Pvs2codequality</RootNamespace>
<PackageId>Pvs2codequality</PackageId>
<Authors>Nokita Kaze</Authors>
<Company>Pvs2codequality</Company>
<Company>Nokita Kaze</Company>
<Product>Pvs2codequality</Product>
<IsPackable>false</IsPackable>
<AssemblyVersion>0.1.0</AssemblyVersion>
<AssemblyVersion>0.1.1</AssemblyVersion>
<PackageVersion>0.1.0</PackageVersion>
<FileVersion>0.1.0</FileVersion>
<FileVersion></FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 41744b4

Please sign in to comment.