Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/Refitter/GenerateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se

Analytics.LogFeatureUsage(settings, refitGeneratorSettings);

// Generate .refitter settings file if not using existing settings file
if (string.IsNullOrWhiteSpace(settings.SettingsFilePath))
{
await WriteRefitterSettingsFile(settings, refitGeneratorSettings);
}

if (refitGeneratorSettings.IncludePathMatches.Length > 0 &&
generator.OpenApiDocument.Paths.Count == 0)
{
Expand Down Expand Up @@ -831,4 +837,59 @@ private static void TryWriteLine(
Console.ForegroundColor = originalColor;
}
}

private static async Task WriteRefitterSettingsFile(Settings settings, RefitGeneratorSettings refitGeneratorSettings)
{
var settingsFilePath = DetermineSettingsFilePath(settings);
var settingsDirectory = Path.GetDirectoryName(settingsFilePath);

if (!string.IsNullOrWhiteSpace(settingsDirectory) && !Directory.Exists(settingsDirectory))
Directory.CreateDirectory(settingsDirectory);

var json = Serializer.Serialize(refitGeneratorSettings);
await File.WriteAllTextAsync(settingsFilePath, json);

Comment on lines +846 to +851

Copilot AI Dec 25, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The File.WriteAllTextAsync and Directory.CreateDirectory operations lack error handling. If the file system operation fails (e.g., due to permissions, disk space, or path issues), the exception will propagate to the outer catch handler, which may provide a confusing error message since the main code generation has already completed successfully. Consider wrapping these operations in a try-catch block to provide a more specific error message about the settings file creation failure.

Suggested change
if (!string.IsNullOrWhiteSpace(settingsDirectory) && !Directory.Exists(settingsDirectory))
Directory.CreateDirectory(settingsDirectory);
var json = Serializer.Serialize(refitGeneratorSettings);
await File.WriteAllTextAsync(settingsFilePath, json);
var json = Serializer.Serialize(refitGeneratorSettings);
try
{
if (!string.IsNullOrWhiteSpace(settingsDirectory) && !Directory.Exists(settingsDirectory))
{
Directory.CreateDirectory(settingsDirectory);
}
await File.WriteAllTextAsync(settingsFilePath, json);
}
catch (Exception ex) when (ex is IOException ||
ex is UnauthorizedAccessException ||
ex is System.Security.SecurityException ||
ex is NotSupportedException)
{
throw new InvalidOperationException(
$"Failed to write Refitter settings file to '{settingsFilePath}'. See inner exception for details.",
ex);
}

Copilot uses AI. Check for mistakes.
if (settings.SimpleOutput)
{
Console.WriteLine($"Settings file written to: {settingsFilePath}");
Console.WriteLine();
}
else
{
var fileName = Path.GetFileName(settingsFilePath);
var directory = Path.GetDirectoryName(settingsFilePath) ?? "";

var panel = new Panel(
$"[bold white]📄 File:[/] [cyan]{fileName}[/]\n" +
$"[bold white]📂 Directory:[/] [dim]{directory}[/]"
)
.BorderColor(Color.Green)
.RoundedBorder()
.Header("[bold green]💾 Settings File Generated[/]")
.HeaderAlignment(Justify.Center);

AnsiConsole.Write(panel);
AnsiConsole.WriteLine();
}
}
Comment on lines +841 to +874

Copilot AI Dec 25, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new WriteRefitterSettingsFile functionality lacks test coverage. Based on the repository's comprehensive test patterns (as seen in the Examples directory and existing tests), this new feature should include unit tests to verify: 1) Settings file generation with default path, 2) Settings file generation with custom output paths, 3) Settings file generation when GenerateMultipleFiles is true, 4) Settings file generation when ContractsOutputPath is set, 5) Proper serialization of RefitGeneratorSettings to JSON. This is especially important since the feature is automatically invoked during code generation.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback


private static string DetermineSettingsFilePath(Settings settings)
{
// If output path is specified and is a directory, put .refitter file there
if (!string.IsNullOrWhiteSpace(settings.OutputPath) &&
settings.OutputPath != Settings.DefaultOutputPath)
{
var outputDir = settings.GenerateMultipleFiles || !string.IsNullOrWhiteSpace(settings.ContractsOutputPath)
? settings.OutputPath
: Path.GetDirectoryName(settings.OutputPath);

if (!string.IsNullOrWhiteSpace(outputDir))
{
return Path.Combine(outputDir, ".refitter");
}
}

// Default: put .refitter file in current directory
return ".refitter";
}
Comment thread
christianhelle marked this conversation as resolved.
}
Loading