Skip to content

Commit

Permalink
Add output path configuration options for RefitGenerator
Browse files Browse the repository at this point in the history
The code was modified to allow users more control over output path configuration in RefitGenerator. Now, users can define the output folder and filename either directly with --output or via 'outputFolder' and 'outputFilename' settings in the file. A validation error will be returned if both routes are attempted simultaneously. This change offers greater flexibility and security to avoid path-related problems. Default values have also been shifted from being hard coded, to being set as constants within Settings and RefitGeneratorSettings classes.
  • Loading branch information
christianhelle committed Oct 12, 2023
1 parent 04ca9f5 commit c5a2e57
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/Refitter.Core/Settings/RefitGeneratorSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Refitter.Core;
[ExcludeFromCodeCoverage]
public class RefitGeneratorSettings
{
public const string DefaultOutputFolder = "./Generated";

/// <summary>
/// Gets or sets the path to the Open API.
/// </summary>
Expand Down Expand Up @@ -110,7 +112,7 @@ public class RefitGeneratorSettings
/// <summary>
/// Gets or sets the relative path to a folder in which the output files are generated. (default: ./Generated)
/// </summary>
public string OutputFolder { get; set; } = "./Generated";
public string OutputFolder { get; set; } = DefaultOutputFolder;

/// <summary>
/// Gets or sets the filename of the generated code.
Expand Down
29 changes: 26 additions & 3 deletions src/Refitter/GenerateCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics;
using System.Text.Json;

using Microsoft.OpenApi.Models;

Expand Down Expand Up @@ -36,12 +35,21 @@ public override ValidationResult Validate(CommandContext context, Settings setti
var json = File.ReadAllText(settings.SettingsFilePath);
var refitGeneratorSettings = Serializer.Deserialize<RefitGeneratorSettings>(json);
settings.OpenApiPath = refitGeneratorSettings.OpenApiPath;

if (string.IsNullOrWhiteSpace(refitGeneratorSettings.OpenApiPath))
return ValidationResult.Error(
"The 'openApiPath' in settings file is required when " +
"URL or file path to OpenAPI Specification file " +
"is not specified in command line argument");

if (!string.IsNullOrWhiteSpace(settings.OutputPath) &&
settings.OutputPath != Settings.DefaultOutputPath &&
(!string.IsNullOrWhiteSpace(refitGeneratorSettings.OutputFolder) ||
!string.IsNullOrWhiteSpace(refitGeneratorSettings.OutputFilename)))
return ValidationResult.Error(
"You should either specify an output path directly from --output " +
"or use specify it in 'outputFolder' and 'outputFilename' from the settings file, " +
"not both");
}

if (!string.IsNullOrWhiteSpace(settings.OperationNameTemplate) &&
Expand Down Expand Up @@ -112,7 +120,7 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se
Directory.CreateDirectory(directory);
}

var outputPath = settings.OutputPath ?? "Output.cs";
var outputPath = GetOutputPath(settings, refitGeneratorSettings);
AnsiConsole.MarkupLine($"[green]Output: {Path.GetFullPath(outputPath)}[/]");
await File.WriteAllTextAsync(outputPath, code);
await Analytics.LogFeatureUsage(settings);
Expand All @@ -134,6 +142,21 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se
}
}

private static string GetOutputPath(Settings settings, RefitGeneratorSettings refitGeneratorSettings)
{
var outputPath = settings.OutputPath != Settings.DefaultOutputPath && !string.IsNullOrWhiteSpace(settings.OutputPath)
? settings.OutputPath
: refitGeneratorSettings.OutputFilename ?? "Output.cs";

if (!string.IsNullOrWhiteSpace(refitGeneratorSettings.OutputFolder) &&
refitGeneratorSettings.OutputFolder != RefitGeneratorSettings.DefaultOutputFolder)
{
outputPath = Path.Combine(refitGeneratorSettings.OutputFolder, outputPath);
}

return outputPath;
}

private static async Task ValidateOpenApiSpec(Settings settings)
{
var validationResult = await OpenApiValidator.Validate(settings.OpenApiPath!);
Expand Down
4 changes: 3 additions & 1 deletion src/Refitter/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace Refitter;

public sealed class Settings : CommandSettings
{
public const string DefaultOutputPath = "Output.cs";

[Description("URL or file path to OpenAPI Specification file")]
[CommandArgument(0, "[URL or input file]")]
[DefaultValue(null)]
Expand All @@ -22,7 +24,7 @@ public sealed class Settings : CommandSettings

[Description("Path to Output file")]
[CommandOption("-o|--output")]
[DefaultValue("Output.cs")]
[DefaultValue(DefaultOutputPath)]
public string? OutputPath { get; set; }

[Description("Don't add <auto-generated> header to output file")]
Expand Down

0 comments on commit c5a2e57

Please sign in to comment.