Skip to content

Commit 4172e31

Browse files
Refactor configuration generation and analysis command
- Removed the `TryGenerateConfig` method from `Program.cs` to streamline the main execution flow. - Integrated configuration generation directly into the `AnalyseCommand`, allowing users to create a default configuration file via the `--generate-config` option. - Updated `AnalyseCommandSettings` to include a new command option for generating configuration files. - Enhanced `IConsoleNotifier` and its implementation to provide feedback when a configuration file is created, improving user experience. - Aimed to simplify the command-line interface and improve usability for new users setting up the analysis environment.
1 parent c06b0d9 commit 4172e31

8 files changed

Lines changed: 43 additions & 24 deletions

File tree

CognitiveCodeAnalysisConsoleApp.Tests/src/Application/AnalyseApplicationServiceTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ public void WriteReportGenerated(string reportType, string fullPath) { }
218218

219219
public void WriteConfigUsed(string configSourceDisplay)
220220
=> ConfigUsedMessages.Add(configSourceDisplay);
221+
222+
public void WriteConfigFileCreated(string fullPath) { }
221223
}
222224

223225
private sealed class RecordingReportGenerationService : IReportGenerationService

CognitiveCodeAnalysisConsoleApp.Tests/src/Commands/AnalyseCommandCliIntegrationTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,7 @@ public void WriteNoSourceFilesFound(string absoluteSourcePath) { }
116116
public void WriteReportGenerated(string reportType, string fullPath) { }
117117

118118
public void WriteConfigUsed(string configSourceDisplay) { }
119+
120+
public void WriteConfigFileCreated(string fullPath) { }
119121
}
120122
}

CognitiveCodeAnalysisConsoleApp.Tests/src/Commands/AnalyseCommandSettingsTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,26 @@ public void ParseLongReportFormatAndOutputOptions_MapsToAnalysisRequest()
6363
Assert.That(request.OutputFile, Is.EqualTo(".\\report.html"));
6464
}
6565

66+
[Test]
67+
public void ParseGenerateConfigWithoutPath_SetsGenerateConfigFlag()
68+
{
69+
Parse("--generate-config");
70+
71+
Assert.That(CliParseProbeCommand.Parsed, Is.Not.Null);
72+
Assert.That(CliParseProbeCommand.Parsed!.GenerateConfig.IsSet, Is.True);
73+
Assert.That(CliParseProbeCommand.Parsed.GenerateConfig.Value, Is.Null);
74+
}
75+
76+
[Test]
77+
public void ParseGenerateConfigWithPath_BindsDirectory()
78+
{
79+
Parse("--generate-config .\\config");
80+
81+
Assert.That(CliParseProbeCommand.Parsed, Is.Not.Null);
82+
Assert.That(CliParseProbeCommand.Parsed!.GenerateConfig.IsSet, Is.True);
83+
Assert.That(CliParseProbeCommand.Parsed.GenerateConfig.Value, Is.EqualTo(".\\config"));
84+
}
85+
6686
private static void Parse(string commandLine)
6787
{
6888
var services = new ServiceCollection();

CognitiveCodeAnalysisConsoleApp/src/Commands/AnalyseCommand.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ CancellationToken cancellationToken
2424
) {
2525
try
2626
{
27+
if (settings.GenerateConfig.IsSet)
28+
{
29+
string directory = string.IsNullOrWhiteSpace(settings.GenerateConfig.Value)
30+
? Directory.GetCurrentDirectory()
31+
: settings.GenerateConfig.Value!;
32+
string written = ConfigFileGenerator.Generate(directory);
33+
consoleNotifier.WriteConfigFileCreated(written);
34+
return Success;
35+
}
36+
2737
var request = AnalyseRequestMapper.FromSettings(settings);
2838
var result = applicationService.Run(request);
2939

CognitiveCodeAnalysisConsoleApp/src/Commands/AnalyseCommandSettings.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ internal sealed class AnalyseCommandSettings : CommandSettings
1818
[CommandOption("-c|--config")]
1919
public string? ConfigFile { get; init; }
2020

21+
[Description("Write a default cognitive-metrics-settings.json to [path] or the current directory, then exit")]
22+
[CommandOption("--generate-config [path]")]
23+
public FlagValue<string?> GenerateConfig { get; init; }
24+
2125
[Description("Report type: ConsoleText, Html, Markdown, Json, Sarif, GithubActions, GitlabCodeQuality, Csv. Defaults to console.")]
2226
[CommandOption("-f|-r|--report-type|--report-format")]
2327
[DefaultValue("ConsoleText")]

CognitiveCodeAnalysisConsoleApp/src/Infrastructure/IConsoleNotifier.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ internal interface IConsoleNotifier
1515
void WriteReportGenerated(string reportType, string fullPath);
1616

1717
void WriteConfigUsed(string configSourceDisplay);
18+
19+
void WriteConfigFileCreated(string fullPath);
1820
}

CognitiveCodeAnalysisConsoleApp/src/Infrastructure/SpectreConsoleNotifier.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@ public void WriteReportGenerated(string reportType, string fullPath)
2222

2323
public void WriteConfigUsed(string configSourceDisplay)
2424
=> AnsiConsole.MarkupLine($"[grey]Config:[/] {Markup.Escape(configSourceDisplay)}");
25+
26+
public void WriteConfigFileCreated(string fullPath)
27+
=> AnsiConsole.MarkupLine($"[green]Config file created:[/] {Markup.Escape(fullPath)}");
2528
}

CognitiveCodeAnalysisConsoleApp/src/Program.cs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
using CognitiveCodeAnalysisConsoleApp.DependencyInjection;
1616
using CognitiveCodeAnalysisConsoleApp.Infrastructure;
1717
using Microsoft.Extensions.DependencyInjection;
18-
using Spectre.Console;
1918
using Spectre.Console.Cli;
2019

2120
namespace CognitiveCodeAnalysisConsoleApp;
@@ -30,12 +29,6 @@ public class Program
3029
{
3130
public static int Main(string[] args)
3231
{
33-
int? generateConfigExitCode = TryGenerateConfig(args);
34-
if (generateConfigExitCode.HasValue)
35-
{
36-
return generateConfigExitCode.Value;
37-
}
38-
3932
var serviceCollection = new ServiceCollection();
4033

4134
CognitiveConfiguration defaultConfig = ConfigurationLoader.Load();
@@ -69,21 +62,4 @@ public static int Main(string[] args)
6962

7063
return new CommandApp<AnalyseCommand>(registrar).Run(args);
7164
}
72-
73-
private static int? TryGenerateConfig(string[] args)
74-
{
75-
int generateConfigIndex = Array.IndexOf(args, "--generate-config");
76-
if (generateConfigIndex < 0)
77-
{
78-
return null;
79-
}
80-
81-
string? nextArg = generateConfigIndex + 1 < args.Length ? args[generateConfigIndex + 1] : null;
82-
string directory = nextArg is not null && !nextArg.StartsWith('-')
83-
? nextArg
84-
: Directory.GetCurrentDirectory();
85-
string written = ConfigFileGenerator.Generate(directory);
86-
AnsiConsole.MarkupLine($"[green]Config file created:[/] {Markup.Escape(written)}");
87-
return 0;
88-
}
8965
}

0 commit comments

Comments
 (0)