-
Notifications
You must be signed in to change notification settings - Fork 0
Updated MS Task with swagger/swashbuckle install #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
90452c3
Updated MS Task with swagger/swashbuckle install
a27de8f
WIP, split by concerns, DI not done yet,unit tests not done yet
Zav ec247e6
Rename exception
Zav 3133fea
Change log format
Zav cc2788b
Install spectral
Zav a12b59d
Fix parameter
Zav 9dbc3f7
Fix parameter
Zav e16fae7
Pair review changes
Zav 9122cbf
Addressed few of the PR comments
b9850f2
PR corrections
Zav a88d529
Merge conflict
Zav 3df41f0
Add missing files
Zav abe63a6
Removed http client wrapper constructor
Zav File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ bld/ | |
[Oo]bj/ | ||
[Ll]og/ | ||
[Ll]ogs/ | ||
[Tt]ool/ | ||
|
||
# Visual Studio 2015/2017 cache/options directory | ||
.vs/ | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Net; | ||
|
||
namespace Workleap.OpenApi.MSBuild; | ||
|
||
internal sealed class HttpClientWrapper : IHttpClientWrapper, IDisposable | ||
{ | ||
private readonly HttpClient _httpClient = new(); | ||
|
||
public async Task DownloadFileToDestinationAsync(string url, string destination, CancellationToken cancellationToken) | ||
{ | ||
using var responseStream = await this._httpClient.GetStreamAsync(url); | ||
|
||
if (responseStream == null) | ||
{ | ||
using var retryResponseStream = await this._httpClient.GetStreamAsync(url); | ||
if (retryResponseStream != null) | ||
{ | ||
await SaveFileFromResponseAsync(destination, retryResponseStream, cancellationToken); | ||
} | ||
else | ||
{ | ||
throw new OpenApiTaskFailedException("Spectral could not be downloaded."); | ||
} | ||
} | ||
else | ||
{ | ||
await SaveFileFromResponseAsync(destination, responseStream, cancellationToken); | ||
} | ||
} | ||
|
||
private static async Task SaveFileFromResponseAsync(string destination, Stream responseStream, CancellationToken cancellationToken) | ||
{ | ||
using var fileTarget = new FileStream(destination, FileMode.Create, FileAccess.Write, FileShare.None); | ||
|
||
await responseStream.CopyToAsync(fileTarget, 81920, cancellationToken); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
this._httpClient.Dispose(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Workleap.OpenApi.MSBuild; | ||
|
||
internal interface IHttpClientWrapper | ||
{ | ||
Task DownloadFileToDestinationAsync(string url, string destination, CancellationToken cancellationToken); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Microsoft.Build.Utilities; | ||
|
||
namespace Workleap.OpenApi.MSBuild; | ||
|
||
public interface ILoggerWrapper | ||
{ | ||
void LogWarning(string message, params object[] messageArgs); | ||
|
||
void LogMessage(string message, params object[] messageArgs); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Workleap.OpenApi.MSBuild; | ||
|
||
public interface IProcessWrapper | ||
{ | ||
public Task<int> RunProcessAsync(string filename, string[] arguments, CancellationToken cancellationToken); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Workleap.OpenApi.MSBuild; | ||
|
||
public interface ISpectralManager | ||
{ | ||
public Task InstallSpectralAsync(CancellationToken cancellationToken); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Workleap.OpenApi.MSBuild; | ||
|
||
public interface ISwaggerManager | ||
{ | ||
Task RunSwaggerAsync(string[] openApiSwaggerDocumentNames, CancellationToken cancellationToken); | ||
|
||
Task InstallSwaggerCliAsync(CancellationToken cancellationToken); | ||
|
||
Task GenerateOpenApiSpecAsync(string swaggerExePath, string outputOpenApiSpecPath, string documentName, CancellationToken cancellationToken); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Microsoft.Build.Utilities; | ||
|
||
namespace Workleap.OpenApi.MSBuild; | ||
|
||
internal sealed class LoggerWrapper : ILoggerWrapper | ||
{ | ||
public LoggerWrapper(TaskLoggingHelper helper) | ||
{ | ||
this.Helper = helper; | ||
} | ||
|
||
public TaskLoggingHelper Helper { get; set; } | ||
|
||
public void LogWarning(string message, params object[] messageArgs) | ||
{ | ||
this.Helper.LogWarning(message, messageArgs); | ||
} | ||
|
||
public void LogMessage(string message, params object[] messageArgs) | ||
{ | ||
this.Helper.LogMessage(message, messageArgs); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Workleap.OpenApi.MSBuild; | ||
|
||
public class OpenApiTaskFailedException : Exception | ||
{ | ||
public OpenApiTaskFailedException(string message) : base(message) | ||
{ | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using CliWrap; | ||
using CliWrap.Buffered; | ||
|
||
namespace Workleap.OpenApi.MSBuild; | ||
|
||
internal sealed class ProcessWrapper : IProcessWrapper | ||
{ | ||
private readonly string _workingDirectory; | ||
|
||
public ProcessWrapper(string workingDirectory) | ||
{ | ||
this._workingDirectory = workingDirectory; | ||
} | ||
|
||
public async Task<int> RunProcessAsync(string filename, string[] arguments, CancellationToken cancellationToken) | ||
Zav marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
var result = await Cli.Wrap(filename) | ||
.WithWorkingDirectory(this._workingDirectory) | ||
.WithValidation(CommandResultValidation.None) | ||
.WithArguments(arguments) | ||
.WithStandardOutputPipe(PipeTarget.ToStream(Console.OpenStandardOutput())) | ||
.WithStandardErrorPipe(PipeTarget.ToStream(Console.OpenStandardError())) | ||
.ExecuteBufferedAsync(cancellationToken); | ||
|
||
return result.ExitCode; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using System.Runtime.InteropServices; | ||
using Microsoft.Build.Framework; | ||
|
||
namespace Workleap.OpenApi.MSBuild; | ||
|
||
internal sealed class SpectralManager : ISpectralManager | ||
{ | ||
private const string SpectralVersion = "6.11.0"; | ||
|
||
private readonly ILoggerWrapper _loggerWrapper; | ||
private readonly IHttpClientWrapper _httpClientWrapper; | ||
private readonly string _toolDirectory; | ||
|
||
public SpectralManager(LoggerWrapper loggerWrapper, string openApiToolsDirectoryPath, IHttpClientWrapper httpClientWrapper) | ||
{ | ||
this._loggerWrapper = loggerWrapper; | ||
this._httpClientWrapper = httpClientWrapper; | ||
this._toolDirectory = Path.Combine(openApiToolsDirectoryPath, "spectral", SpectralVersion); | ||
} | ||
|
||
public async Task InstallSpectralAsync(CancellationToken cancellationToken) | ||
{ | ||
Directory.CreateDirectory(this._toolDirectory); | ||
|
||
var executableFileName = GetSpectralFileName(); | ||
var url = $"https://github.com/stoplightio/spectral/releases/download/v{SpectralVersion}/{executableFileName}"; | ||
|
||
await this._httpClientWrapper.DownloadFileToDestinationAsync(url, Path.Combine(this._toolDirectory, executableFileName), cancellationToken); | ||
} | ||
|
||
private static string GetSpectralFileName() | ||
{ | ||
var osType = GetOperatingSystem(); | ||
var architecture = GetArchitecture(); | ||
|
||
if (osType == "linux") | ||
{ | ||
var distro = File.Exists("/etc/os-release") ? File.ReadAllText("/etc/os-release") : string.Empty; | ||
Zav marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (distro.Contains("Alpine Linux")) | ||
{ | ||
osType = "alpine"; | ||
} | ||
} | ||
|
||
var fileName = $"spectral-{osType}-{architecture}"; | ||
|
||
if (osType == "win") | ||
{ | ||
fileName = "spectral.exe"; | ||
} | ||
|
||
return fileName; | ||
} | ||
|
||
private static string GetOperatingSystem() | ||
ruvyas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) | ||
{ | ||
return "linux"; | ||
} | ||
|
||
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) | ||
{ | ||
return "macos"; | ||
} | ||
|
||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
return "win"; | ||
} | ||
|
||
throw new OpenApiTaskFailedException("Unknown operating system encountered"); | ||
} | ||
|
||
private static string GetArchitecture() | ||
ruvyas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (RuntimeInformation.OSArchitecture == Architecture.X64) | ||
{ | ||
return "x64"; | ||
} | ||
|
||
if (RuntimeInformation.OSArchitecture == Architecture.Arm64) | ||
{ | ||
return "arm64"; | ||
} | ||
|
||
throw new OpenApiTaskFailedException("Unknown processor architecture encountered"); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Workleap.OpenApi.MSBuild; | ||
|
||
internal sealed class SwaggerManager : ISwaggerManager | ||
{ | ||
private const string SwaggerVersion = "6.5.0"; | ||
private readonly IProcessWrapper _processWrapper; | ||
private readonly ILoggerWrapper _loggerWrapper; | ||
private readonly string _openApiWebApiAssemblyPath; | ||
private readonly string _swaggerDirectory; | ||
|
||
public SwaggerManager(IProcessWrapper processWrapper, ILoggerWrapper loggerWrapper, string openApiToolsDirectoryPath, string openApiWebApiAssemblyPath) | ||
{ | ||
this._processWrapper = processWrapper; | ||
this._loggerWrapper = loggerWrapper; | ||
this._openApiWebApiAssemblyPath = openApiWebApiAssemblyPath; | ||
this._swaggerDirectory = Path.Combine(openApiToolsDirectoryPath, "swagger", SwaggerVersion); | ||
} | ||
|
||
public async Task RunSwaggerAsync(string[] openApiSwaggerDocumentNames, CancellationToken cancellationToken) | ||
{ | ||
var swaggerExePath = Path.Combine(this._swaggerDirectory, RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "swagger.exe" : "swagger"); | ||
|
||
foreach (var documentName in openApiSwaggerDocumentNames) | ||
{ | ||
var outputOpenApiSpecName = $"openapi-{documentName.ToLowerInvariant()}.yaml"; | ||
|
||
var outputOpenApiSpecPath = Path.Combine(this._swaggerDirectory, outputOpenApiSpecName); | ||
Zav marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
await this.GenerateOpenApiSpecAsync(swaggerExePath, outputOpenApiSpecPath, documentName, cancellationToken); | ||
} | ||
} | ||
|
||
public async Task InstallSwaggerCliAsync(CancellationToken cancellationToken) | ||
Zav marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
var retryCount = 0; | ||
while (retryCount < 2) | ||
{ | ||
var exitCode = await this._processWrapper.RunProcessAsync("dotnet", new[] { "tool", "update", "Swashbuckle.AspNetCore.Cli", "--tool-path", this._swaggerDirectory, "--version", SwaggerVersion }, cancellationToken); | ||
|
||
if (exitCode != 0 && retryCount != 1) | ||
{ | ||
this._loggerWrapper.LogWarning("Swashbuckle download failed. Retrying once more..."); | ||
retryCount++; | ||
continue; | ||
} | ||
|
||
if (retryCount == 1 && exitCode != 0) | ||
{ | ||
throw new OpenApiTaskFailedException("Swashbuckle CLI could not be installed."); | ||
} | ||
|
||
break; | ||
} | ||
} | ||
|
||
public async Task GenerateOpenApiSpecAsync(string swaggerExePath, string outputOpenApiSpecPath, string documentName, CancellationToken cancellationToken) | ||
{ | ||
var exitCode = await this._processWrapper.RunProcessAsync(swaggerExePath, new[] { "tofile", "--output", outputOpenApiSpecPath, "--yaml", this._openApiWebApiAssemblyPath, documentName }, cancellationToken); | ||
|
||
if (exitCode != 0) | ||
{ | ||
throw new OpenApiTaskFailedException("OpenApi file could not be created."); | ||
Zav marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.