/
ReportWriter.cs
57 lines (49 loc) · 1.95 KB
/
ReportWriter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using Microsoft.Extensions.Logging;
namespace Microsoft.CodeAnalysis.Tools
{
internal static class ReportWriter
{
public static void Write(string reportPath, IEnumerable<FormattedFile> formattedFiles, ILogger logger)
{
var reportFilePath = GetReportFilePath(reportPath);
var reportFolderPath = Path.GetDirectoryName(reportFilePath);
// If no folder path is specified to the report path, set it to "." (which is the current working directory)
if (string.Empty.Equals(reportFolderPath, StringComparison.OrdinalIgnoreCase))
{
reportFolderPath = ".";
}
if (reportFolderPath != null && !Directory.Exists(reportFolderPath))
{
Directory.CreateDirectory(reportFolderPath);
}
logger.LogInformation(Resources.Writing_formatting_report_to_0, reportFilePath);
var seralizerOptions = new JsonSerializerOptions
{
WriteIndented = true
};
var formattedFilesJson = JsonSerializer.Serialize(formattedFiles, seralizerOptions);
File.WriteAllText(reportFilePath, formattedFilesJson);
}
private static string GetReportFilePath(string reportPath)
{
var defaultReportName = "format-report.json";
if (reportPath.EndsWith(".json"))
{
return reportPath;
}
else if (reportPath == ".")
{
return Path.Combine(Environment.CurrentDirectory, defaultReportName);
}
else
{
return Path.Combine(reportPath, defaultReportName);
}
}
}
}