Skip to content

Commit

Permalink
[IDP-684] Add oasfiff download (#8)
Browse files Browse the repository at this point in the history
* Add oasfiff download

* Moved file check to HttpClientWrapper

* Moved RuntimeInformation to a helper class for reusability

---------

Co-authored-by: Rushikesh Vyas <[email protected]>
  • Loading branch information
Zav and Rushikesh Vyas authored Nov 6, 2023
1 parent e6488cb commit f145924
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 44 deletions.
7 changes: 6 additions & 1 deletion src/Workleap.OpenApi.MSBuild/HttpClientWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ internal sealed class HttpClientWrapper : IHttpClientWrapper, IDisposable

public async Task DownloadFileToDestinationAsync(string url, string destination, CancellationToken cancellationToken)
{
if (File.Exists(destination))
{
return;
}

try
{
using var responseStream = await this._httpClient.GetStreamAsync(url);
Expand All @@ -19,7 +24,7 @@ public async Task DownloadFileToDestinationAsync(string url, string destination,
}
else
{
throw new OpenApiTaskFailedException("Spectral could not be downloaded.");
throw new OpenApiTaskFailedException($"{url} could not be downloaded.");
}
}
else
Expand Down
6 changes: 6 additions & 0 deletions src/Workleap.OpenApi.MSBuild/IOasdiffManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Workleap.OpenApi.MSBuild;

internal interface IOasdiffManager
{
Task InstallOasdiffAsync(CancellationToken cancellationToken);
}
49 changes: 49 additions & 0 deletions src/Workleap.OpenApi.MSBuild/OasdiffManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace Workleap.OpenApi.MSBuild;

internal sealed class OasdiffManager : IOasdiffManager
{
private const string OasdiffVersion = "1.9.2";

private readonly IHttpClientWrapper _httpClientWrapper;
private readonly IProcessWrapper _processWrapper;
private readonly string _toolDirectory;

public OasdiffManager(IProcessWrapper processWrapper, string openApiToolsDirectoryPath, IHttpClientWrapper httpClientWrapper)
{
this._httpClientWrapper = httpClientWrapper;
this._processWrapper = processWrapper;
this._toolDirectory = Path.Combine(openApiToolsDirectoryPath, "oasdiff", OasdiffVersion);
}

public async Task InstallOasdiffAsync(CancellationToken cancellationToken)
{
Directory.CreateDirectory(this._toolDirectory);

var oasdiffFileName = GetOasdiffFileName();
var url = $"https://github.com/Tufin/oasdiff/releases/download/v{OasdiffVersion}/{oasdiffFileName}";

await this._httpClientWrapper.DownloadFileToDestinationAsync(url, Path.Combine(this._toolDirectory, oasdiffFileName), cancellationToken);
await this.DecompressDownloadedFileAsync(oasdiffFileName, cancellationToken);
}

private async Task DecompressDownloadedFileAsync(string oasdiffFileName, CancellationToken cancellationToken)
{
var pathToCompressedFile = Path.Combine(this._toolDirectory, oasdiffFileName);
await this._processWrapper.RunProcessAsync("tar", new[] { "-xzf", $"{pathToCompressedFile}", "-C", $"{this._toolDirectory}" }, cancellationToken);
}

private static string GetOasdiffFileName()
{
var osType = RuntimeInformationHelper.GetOperatingSystem();
var architecture = RuntimeInformationHelper.GetArchitecture("amd");

var fileName = $"oasdiff_{OasdiffVersion}_{osType}_{architecture}.tar.gz";

if (osType == "macos")
{
fileName = $"oasdiff_{OasdiffVersion}_darwin_all.tar.gz";
}

return fileName;
}
}
34 changes: 34 additions & 0 deletions src/Workleap.OpenApi.MSBuild/RuntimeInformationHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Runtime.InteropServices;

namespace Workleap.OpenApi.MSBuild;

internal static class RuntimeInformationHelper
{
internal static string GetOperatingSystem()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return "linux";
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
return "macos";
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return "windows";
}

throw new OpenApiTaskFailedException("Unknown operating system encountered");
}

internal static string GetArchitecture(string? prefix = default)
=> RuntimeInformation.OSArchitecture switch
{
Architecture.X64 => string.IsNullOrEmpty(prefix) ? "x64" : $"{prefix}64",
Architecture.Arm64 => "arm64",
_ => throw new OpenApiTaskFailedException("Unknown processor architecture encountered"),
};
}
48 changes: 5 additions & 43 deletions src/Workleap.OpenApi.MSBuild/SpectralManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ public async Task InstallSpectralAsync(CancellationToken cancellationToken)
this.ExecutablePath = GetSpectralFileName();
var url = $"https://github.com/stoplightio/spectral/releases/download/v{SpectralVersion}/{this.ExecutablePath}";
var destination = Path.Combine(this._spectralDirectory, this.ExecutablePath);

if (!File.Exists(destination))
{
await this._httpClientWrapper.DownloadFileToDestinationAsync(url, destination, cancellationToken);
}

await this._httpClientWrapper.DownloadFileToDestinationAsync(url, destination, cancellationToken);
}

public async Task RunSpectralAsync(IEnumerable<string> swaggerDocumentPaths, string rulesetUrl, CancellationToken cancellationToken)
Expand All @@ -55,8 +52,8 @@ public async Task RunSpectralAsync(IEnumerable<string> swaggerDocumentPaths, str

private static string GetSpectralFileName()
{
var osType = GetOperatingSystem();
var architecture = GetArchitecture();
var osType = RuntimeInformationHelper.GetOperatingSystem();
var architecture = RuntimeInformationHelper.GetArchitecture();

if (osType == "linux")
{
Expand All @@ -69,49 +66,14 @@ private static string GetSpectralFileName()

var fileName = $"spectral-{osType}-{architecture}";

if (osType == "win")
if (osType == "windows")
{
fileName = "spectral.exe";
}

return fileName;
}

private static string GetOperatingSystem()
{
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()
{
if (RuntimeInformation.OSArchitecture == Architecture.X64)
{
return "x64";
}

if (RuntimeInformation.OSArchitecture == Architecture.Arm64)
{
return "arm64";
}

throw new OpenApiTaskFailedException("Unknown processor architecture encountered");
}

private async Task GenerateSpectralReport(string spectralExecutePath, string swaggerDocumentPath, string rulesetUrl, string htmlReportPath, CancellationToken cancellationToken)
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
Expand Down
3 changes: 3 additions & 0 deletions src/Workleap.OpenApi.MSBuild/ValidateOpenApiTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ protected override async Task<bool> ExecuteAsync(CancellationToken cancellationT
var loggerWrapper = new LoggerWrapper(this.Log);
var processWrapper = new ProcessWrapper(this.OpenApiToolsDirectoryPath);
var swaggerManager = new SwaggerManager(processWrapper, loggerWrapper, this.OpenApiToolsDirectoryPath, this.OpenApiWebApiAssemblyPath);

using var httpClientWrapper = new HttpClientWrapper();

var spectralManager = new SpectralManager(loggerWrapper, this.OpenApiToolsDirectoryPath, httpClientWrapper, processWrapper);
var oasdiffManager = new OasdiffManager(processWrapper, this.OpenApiToolsDirectoryPath, httpClientWrapper);

this.Log.LogMessage(MessageImportance.Low, "{0} = '{1}'", nameof(this.OpenApiWebApiAssemblyPath), this.OpenApiWebApiAssemblyPath);
this.Log.LogMessage(MessageImportance.Low, "{0} = '{1}'", nameof(this.OpenApiToolsDirectoryPath), this.OpenApiToolsDirectoryPath);
Expand All @@ -52,6 +54,7 @@ protected override async Task<bool> ExecuteAsync(CancellationToken cancellationT

await swaggerManager.InstallSwaggerCliAsync(cancellationToken);
await spectralManager.InstallSpectralAsync(cancellationToken);
await oasdiffManager.InstallOasdiffAsync(cancellationToken);

var swaggerDocPaths = await swaggerManager.RunSwaggerAsync(this.OpenApiSwaggerDocumentNames, cancellationToken);
await spectralManager.RunSpectralAsync(swaggerDocPaths, this.OpenApiSpectralRulesetUrl, cancellationToken);
Expand Down

0 comments on commit f145924

Please sign in to comment.