Skip to content

Commit ff09675

Browse files
committed
Update to .NET 10 and stable System.CommandLine
1 parent 73a5458 commit ff09675

File tree

5 files changed

+79
-26
lines changed

5 files changed

+79
-26
lines changed

src/CommandArgumentParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static FileInfo ParseFileInfo(ArgumentResult result)
1111

1212
if (!fileInfo.Exists)
1313
{
14-
result.ErrorMessage = $"File '{fileName}' does not exist.";
14+
result.AddError($"File '{fileName}' does not exist.");
1515
}
1616

1717
return fileInfo;

src/Commands/ConvertCommand.cs

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,34 @@ namespace SonarRulesetTool.Commands;
88

99
internal static class ConvertCommand
1010
{
11-
public static Command Register()
11+
public static Command Create()
1212
{
13-
var ruleSetFileArgument = new Argument<FileInfo>("ruleSetFile", CommandArgumentParser.ParseFileInfo, false,
14-
"Path to the .ruleset file to read severities from");
13+
var ruleSetFileArgument = new Argument<FileInfo>("ruleSetFile")
14+
{
15+
Description = "Path to the .ruleset file to read severities from",
16+
CustomParser = CommandArgumentParser.ParseFileInfo
17+
};
1518

16-
var sonarBuiltinFileArgument = new Argument<FileInfo>("sonarBuiltinFile", CommandArgumentParser.ParseFileInfo, false,
17-
"Path to the .xml file that contains the 'Sonar way' built-in profile");
19+
var sonarBuiltinFileArgument = new Argument<FileInfo>("sonarBuiltinFile")
20+
{
21+
Description = "Path to the .xml file that contains the 'Sonar way' built-in profile",
22+
CustomParser = CommandArgumentParser.ParseFileInfo
23+
};
1824

19-
var outputFileNameOption = new Option<string?>("--outputFile", "Path to the output .xml file to import into SonarCloud afterwards");
20-
var profileNameOption = new Option<string?>("--profileName", "Name of the SonarCloud profile to use in the output file");
21-
var verboseOption = new Option<bool>("--verbose", "Displays the Rule IDs being processed");
25+
var outputFileNameOption = new Option<string?>("--outputFile")
26+
{
27+
Description = "Path to the output .xml file to import into SonarCloud afterwards"
28+
};
29+
30+
var profileNameOption = new Option<string?>("--profileName")
31+
{
32+
Description = "Name of the SonarCloud profile to use in output file (default: SonarRuleSetAutoGenerated)"
33+
};
34+
35+
var verboseOption = new Option<bool>("--verbose")
36+
{
37+
Description = "Displays the Rule IDs being processed (default: false)"
38+
};
2239

2340
var command = new Command("convert", "Converts a .ruleset file containing SonarAnalyzer severities to SonarCloud")
2441
{
@@ -29,7 +46,16 @@ public static Command Register()
2946
verboseOption
3047
};
3148

32-
command.SetHandler(HandleCommand, ruleSetFileArgument, sonarBuiltinFileArgument, outputFileNameOption, profileNameOption, verboseOption);
49+
command.SetAction(parseResult =>
50+
{
51+
FileInfo ruleSetFileInfo = parseResult.GetRequiredValue(ruleSetFileArgument);
52+
FileInfo sonarBuiltinFileInfo = parseResult.GetRequiredValue(sonarBuiltinFileArgument);
53+
string? outputFileName = parseResult.GetValue(outputFileNameOption);
54+
string? profileName = parseResult.GetValue(profileNameOption);
55+
bool isVerbose = parseResult.GetValue(verboseOption);
56+
57+
HandleCommand(ruleSetFileInfo, sonarBuiltinFileInfo, outputFileName, profileName, isVerbose);
58+
});
3359

3460
return command;
3561
}

src/Commands/NormalizeCommand.cs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,30 @@ namespace SonarRulesetTool.Commands;
66

77
internal static class NormalizeCommand
88
{
9-
public static Command Register()
9+
public static Command Create()
1010
{
11-
var inputFileArgument = new Argument<FileInfo>("inputFile", CommandArgumentParser.ParseFileInfo, false,
12-
"Path to the .xml file that contains the SonarCloud profile");
11+
var inputFileArgument = new Argument<FileInfo>("inputFile")
12+
{
13+
Description = "Path to the .xml file that contains the SonarCloud profile",
14+
CustomParser = CommandArgumentParser.ParseFileInfo
15+
};
1316

14-
var outputFileNameOption = new Option<string?>("--outputFile", "Path to the normalized output .xml file");
15-
var sortByKeyOption = new Option<bool>("--sortByKey", () => true, "Sort rules alphabetically by key");
16-
var cleanRuleOption = new Option<bool>("--cleanRule", () => true, "Only preserve 'key' and 'repositoryKey' per rule");
17+
var outputFileNameOption = new Option<string?>("--outputFile")
18+
{
19+
Description = "Path to the normalized output .xml file (by default, adds -normalized to the file name)"
20+
};
21+
22+
var sortByKeyOption = new Option<bool>("--sortByKey")
23+
{
24+
Description = "Sort rules alphabetically by key (default: true)",
25+
DefaultValueFactory = _ => true
26+
};
27+
28+
var cleanRuleOption = new Option<bool>("--cleanRule")
29+
{
30+
Description = "Only preserve 'key' and 'repositoryKey' per rule (default: true)",
31+
DefaultValueFactory = _ => true
32+
};
1733

1834
var command = new Command("normalize", "Normalizes an exported SonarCloud .xml file for easy diffing")
1935
{
@@ -23,15 +39,25 @@ public static Command Register()
2339
cleanRuleOption
2440
};
2541

26-
command.SetHandler(HandleCommand, inputFileArgument, outputFileNameOption, sortByKeyOption, cleanRuleOption);
42+
command.SetAction(parseResult =>
43+
{
44+
FileInfo inputFileInfo = parseResult.GetRequiredValue(inputFileArgument);
45+
string? outputFileName = parseResult.GetValue(outputFileNameOption);
46+
bool sortByKey = parseResult.GetValue(sortByKeyOption);
47+
bool cleanRule = parseResult.GetValue(cleanRuleOption);
48+
49+
HandleCommand(inputFileInfo, outputFileName, sortByKey, cleanRule);
50+
});
2751

2852
return command;
2953
}
3054

3155
private static void HandleCommand(FileInfo inputFileInfo, string? outputFileName, bool sortByKey, bool cleanRule)
3256
{
3357
string parentDirectory = Path.GetDirectoryName(inputFileInfo.FullName)!;
34-
string outputPath = Path.Combine(parentDirectory, outputFileName ?? Path.GetFileNameWithoutExtension(inputFileInfo.Name) + "-normalized.xml");
58+
59+
string outputPath = Path.Combine(parentDirectory,
60+
outputFileName ?? $"{Path.GetFileNameWithoutExtension(inputFileInfo.Name)}-normalized{Path.GetExtension(inputFileInfo.Name)}");
3561

3662
NormalizeFile(inputFileInfo, outputPath, sortByKey, cleanRule);
3763
}

src/Program.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
var rootCommand = new RootCommand("Support tool for SonarAnalyzer and SonarCloud")
55
{
6-
ConvertCommand.Register(),
7-
NormalizeCommand.Register()
6+
ConvertCommand.Create(),
7+
NormalizeCommand.Create()
88
};
99

10-
return await rootCommand.InvokeAsync(args);
10+
ParseResult parseResult = rootCommand.Parse(args);
11+
return await parseResult.InvokeAsync().ConfigureAwait(false);

src/SonarRulesetTool.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<OutputType>Exe</OutputType>
4-
<TargetFramework>net9.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
</PropertyGroup>
@@ -11,9 +11,9 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.12.0" />
15-
<PackageReference Include="SonarAnalyzer.CSharp" Version="10.9.0.115408" GeneratePathProperty="true" IncludeAssets="none" ExcludeAssets="All" PrivateAssets="none" />
16-
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
14+
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.14.0" />
15+
<PackageReference Include="SonarAnalyzer.CSharp" Version="10.15.0.120848" GeneratePathProperty="true" IncludeAssets="none" ExcludeAssets="All" PrivateAssets="none" />
16+
<PackageReference Include="System.CommandLine" Version="2.0.0" />
1717
</ItemGroup>
1818

1919
<ItemGroup>

0 commit comments

Comments
 (0)