Skip to content

Commit 740a0f5

Browse files
Add code analysis to detect missing using statements and finding fixes (#15)
Add code analysis scanning in build and fixes found warnings
1 parent 600c6a3 commit 740a0f5

17 files changed

+139
-108
lines changed

src/.editorconfig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,10 @@
22

33
# CS1591: Missing XML comment for publicly visible type or member
44
dotnet_diagnostic.CS1591.severity = suggestion
5+
6+
# Organize usings
7+
dotnet_sort_system_directives_first = true
8+
dotnet_separate_import_directive_groups = true
9+
10+
# Missing usings should be reported as error (IDE0005)
11+
dotnet_diagnostic.IDE0005.severity = error

src/AggregateConfigBuildTask.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ EndProject
1212
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{29D0AE56-C184-4741-824F-521198552928}"
1313
ProjectSection(SolutionItems) = preProject
1414
.editorconfig = .editorconfig
15+
Directory.Build.props = Directory.Build.props
1516
Directory.Packages.props = Directory.Packages.props
1617
EndProjectSection
1718
EndProject

src/Contracts/Contracts.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<GenerateDocumentationFile>true</GenerateDocumentationFile>
5+
<NoWarn>CA1027</NoWarn>
66
</PropertyGroup>
77

88
</Project>

src/Contracts/InputOutputEnums.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
namespace AggregateConfigBuildTask.Contracts
22
{
3-
public enum OutputTypeEnum
3+
public enum OutputType
44
{
5-
Json,
6-
Arm,
5+
Json = 0,
6+
Arm = 1,
77
ArmParameter = Arm,
8-
Yml,
8+
Yml = 2,
99
Yaml = Yml
1010
}
1111

12-
public enum InputTypeEnum
12+
public enum InputType
1313
{
14-
Json,
15-
Arm,
14+
Json = 0,
15+
Arm = 1,
1616
ArmParameter = Arm,
17-
Yml,
17+
Yml = 2,
1818
Yaml = Yml
1919
}
2020
}

src/Directory.Build.props

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project>
2+
3+
<PropertyGroup>
4+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
5+
<Deterministic>true</Deterministic>
6+
<EnableNETAnalyzers>true</EnableNETAnalyzers>
7+
<AnalysisLevel>latest</AnalysisLevel>
8+
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
9+
<CodeAnalysisTreatWarningsAsErrors>false</CodeAnalysisTreatWarningsAsErrors>
10+
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
11+
</PropertyGroup>
12+
13+
</Project>

src/Directory.Packages.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
</ItemGroup>
2626
<!-- Global packages -->
2727
<ItemGroup>
28+
<GlobalPackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0" />
2829
<GlobalPackageReference Include="ReferenceTrimmer" Version="3.3.6" />
30+
<GlobalPackageReference Include="Roslynator.Analyzers" Version="4.12.4" />
2931
</ItemGroup>
3032
</Project>

src/Task/AggregateConfig.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class AggregateConfig : Task
2626
[Required]
2727
public string OutputType { get; set; }
2828

29-
public bool AddSourceProperty { get; set; } = false;
29+
public bool AddSourceProperty { get; set; }
3030

3131
public string[] AdditionalProperties { get; set; }
3232

@@ -48,18 +48,18 @@ public override bool Execute()
4848

4949
OutputFile = Path.GetFullPath(OutputFile);
5050

51-
if (!Enum.TryParse(OutputType, true, out OutputTypeEnum outputType) ||
52-
!Enum.IsDefined(typeof(OutputTypeEnum), outputType))
51+
if (!Enum.TryParse(OutputType, true, out OutputType outputType) ||
52+
!Enum.IsDefined(typeof(OutputType), outputType))
5353
{
54-
Log.LogError("Invalid OutputType: {0}. Available options: {1}", OutputType, string.Join(", ", Enum.GetNames(typeof(OutputTypeEnum))));
54+
Log.LogError("Invalid OutputType: {0}. Available options: {1}", OutputType, string.Join(", ", Enum.GetNames(typeof(OutputType))));
5555
return false;
5656
}
5757

58-
InputTypeEnum inputType = InputTypeEnum.Yaml;
58+
InputType inputType = Contracts.InputType.Yaml;
5959
if (!string.IsNullOrEmpty(InputType) &&
60-
(!Enum.TryParse(InputType, true, out inputType) || !Enum.IsDefined(typeof(InputTypeEnum), inputType)))
60+
(!Enum.TryParse(InputType, true, out inputType) || !Enum.IsDefined(typeof(InputType), inputType)))
6161
{
62-
Log.LogError("Invalid InputType: {0}. Available options: {1}", InputType, string.Join(", ", Enum.GetNames(typeof(InputTypeEnum))));
62+
Log.LogError("Invalid InputType: {0}. Available options: {1}", InputType, string.Join(", ", Enum.GetNames(typeof(InputType))));
6363
return false;
6464
}
6565

@@ -103,7 +103,7 @@ private void EmitHeader()
103103
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?
104104
.InformationalVersion;
105105

106-
Log.LogMessage($"AggregateConfig Version: {informationalVersion}");
106+
Log.LogMessage(MessageImportance.High, $"AggregateConfig Version: {informationalVersion}");
107107
}
108108
}
109109
}

src/Task/AggregateConfigBuildTask.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<GenerateDocumentationFile>true</GenerateDocumentationFile>
65
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
7-
<Deterministic>true</Deterministic>
86
<PublishRepositoryUrl>true</PublishRepositoryUrl>
97
<EmbedUntrackedSources>true</EmbedUntrackedSources>
108
<SourceLinkCreate>true</SourceLinkCreate>
11-
<NoWarn>NU5100</NoWarn>
9+
<NoWarn>NU5100,CA1031,CA1819</NoWarn>
1210
</PropertyGroup>
1311

1412
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">

src/Task/FileHandlers/ArmParametersFileHandler.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@ namespace AggregateConfigBuildTask.FileHandlers
88
{
99
public class ArmParametersFileHandler : IOutputWriter, IInputReader
1010
{
11-
readonly IFileSystem fileSystem;
11+
private readonly IFileSystem fileSystem;
12+
13+
private readonly JsonSerializerOptions jsonOptions = new JsonSerializerOptions { WriteIndented = true };
1214

1315
internal ArmParametersFileHandler(IFileSystem fileSystem)
1416
{
1517
this.fileSystem = fileSystem;
1618
}
1719

1820
/// <inheritdoc/>
19-
public ValueTask<JsonElement> ReadInput(string inputPath)
21+
public async ValueTask<JsonElement> ReadInput(string inputPath)
2022
{
2123
using (var stream = fileSystem.OpenRead(inputPath))
2224
{
23-
using (var jsonDoc = JsonDocument.Parse(stream))
25+
using (var jsonDoc = await JsonDocument.ParseAsync(stream).ConfigureAwait(false))
2426
{
2527
if (jsonDoc.RootElement.TryGetProperty("parameters", out JsonElement parameters))
2628
{
@@ -41,10 +43,10 @@ public ValueTask<JsonElement> ReadInput(string inputPath)
4143
}
4244

4345
var modifiedJson = modifiedParameters.ToJsonString();
44-
return new ValueTask<JsonElement>(Task.FromResult(JsonSerializer.Deserialize<JsonElement>(modifiedJson)));
46+
return JsonSerializer.Deserialize<JsonElement>(modifiedJson);
4547
}
4648

47-
return new ValueTask<JsonElement>(Task.FromResult(jsonDoc.RootElement.Clone()));
49+
return jsonDoc.RootElement.Clone();
4850
}
4951
}
5052
}
@@ -74,8 +76,6 @@ public void WriteOutput(JsonElement? mergedData, string outputPath)
7476
["contentVersion"] = "1.0.0.0",
7577
["parameters"] = parameters
7678
};
77-
78-
var jsonOptions = new JsonSerializerOptions { WriteIndented = true };
7979
var jsonContent = JsonSerializer.Serialize(armTemplate, jsonOptions);
8080
fileSystem.WriteAllText(outputPath, jsonContent);
8181
}
@@ -90,7 +90,7 @@ public void WriteOutput(JsonElement? mergedData, string outputPath)
9090
/// </summary>
9191
/// <param name="value">The JsonElement value to evaluate.</param>
9292
/// <returns>A string representing the ARM template parameter type.</returns>
93-
private string GetParameterType(JsonElement value)
93+
private static string GetParameterType(JsonElement value)
9494
{
9595
switch (value.ValueKind)
9696
{
@@ -115,7 +115,7 @@ private string GetParameterType(JsonElement value)
115115
}
116116
}
117117

118-
private JsonNode ConvertElementToNode(JsonElement element)
118+
private static JsonNode ConvertElementToNode(JsonElement element)
119119
{
120120
// Use GetRawText to get the JSON string representation of the JsonElement
121121
var jsonString = element.GetRawText();

src/Task/FileHandlers/FileHandlerFactory.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,45 @@ namespace AggregateConfigBuildTask.FileHandlers
66
{
77
public static class FileHandlerFactory
88
{
9-
internal static IOutputWriter GetOutputWriter(IFileSystem fileSystem, OutputTypeEnum format)
9+
internal static IOutputWriter GetOutputWriter(IFileSystem fileSystem, OutputType format)
1010
{
1111
switch (format)
1212
{
13-
case OutputTypeEnum.Json:
13+
case OutputType.Json:
1414
return new JsonFileHandler(fileSystem);
15-
case OutputTypeEnum.Yaml:
15+
case OutputType.Yaml:
1616
return new YamlFileHandler(fileSystem);
17-
case OutputTypeEnum.Arm:
17+
case OutputType.Arm:
1818
return new ArmParametersFileHandler(fileSystem);
1919
default:
2020
throw new ArgumentException("Unsupported format");
2121
}
2222
}
2323

24-
internal static IInputReader GetInputReader(IFileSystem fileSystem, InputTypeEnum format)
24+
internal static IInputReader GetInputReader(IFileSystem fileSystem, InputType format)
2525
{
2626
switch (format)
2727
{
28-
case InputTypeEnum.Yaml:
28+
case InputType.Yaml:
2929
return new YamlFileHandler(fileSystem);
30-
case InputTypeEnum.Json:
30+
case InputType.Json:
3131
return new JsonFileHandler(fileSystem);
32-
case InputTypeEnum.Arm:
32+
case InputType.Arm:
3333
return new ArmParametersFileHandler(fileSystem);
3434
default:
3535
throw new ArgumentException("Unsupported input format");
3636
}
3737
}
3838

39-
internal static List<string> GetExpectedFileExtensions(InputTypeEnum inputType)
39+
internal static List<string> GetExpectedFileExtensions(InputType inputType)
4040
{
4141
switch (inputType)
4242
{
43-
case InputTypeEnum.Json:
43+
case InputType.Json:
4444
return new List<string> { ".json" };
45-
case InputTypeEnum.Yaml:
45+
case InputType.Yaml:
4646
return new List<string> { ".yml", ".yaml" };
47-
case InputTypeEnum.Arm:
47+
case InputType.Arm:
4848
return new List<string> { ".json" };
4949
default:
5050
throw new ArgumentException("Unsupported input type");

0 commit comments

Comments
 (0)