From 98460ceaf0d898f6b6656ae7be1e98c55e9f96ae Mon Sep 17 00:00:00 2001 From: heqianwang <158102624+heqianwang@users.noreply.github.com> Date: Wed, 8 Jan 2025 15:37:15 -0500 Subject: [PATCH] [IDPHD-35] Add Do not edit comment on spec (#99) * [IDPHD-35] Add Do not edit comment on spec * Extract into own method --- src/Workleap.OpenApi.MSBuild/SwaggerManager.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Workleap.OpenApi.MSBuild/SwaggerManager.cs b/src/Workleap.OpenApi.MSBuild/SwaggerManager.cs index b9665ed..7c911ae 100644 --- a/src/Workleap.OpenApi.MSBuild/SwaggerManager.cs +++ b/src/Workleap.OpenApi.MSBuild/SwaggerManager.cs @@ -6,6 +6,7 @@ namespace Workleap.OpenApi.MSBuild; internal sealed class SwaggerManager : ISwaggerManager { private const string SwaggerVersion = "6.5.0"; + private const string DoNotEditComment = "# DO NOT EDIT. This is a generated file\n"; private const int MaxRetryCount = 3; private readonly IProcessWrapper _processWrapper; private readonly ILoggerWrapper _loggerWrapper; @@ -100,6 +101,22 @@ public async Task GenerateOpenApiSpecAsync(string swaggerExePath, string break; } + this.PrependComment(outputOpenApiSpecPath, DoNotEditComment); + return outputOpenApiSpecPath; } + + private void PrependComment(string outputOpenApiSpecPath, string comment) + { + try + { + // Add the comment at the top of the generated YAML file + var yamlContent = File.ReadAllText(outputOpenApiSpecPath); + File.WriteAllText(outputOpenApiSpecPath, comment + yamlContent); + } + catch (Exception) + { + this._loggerWrapper.LogWarning($"Failed to add comment to generated spec for {outputOpenApiSpecPath}."); + } + } } \ No newline at end of file