Select Git revision
-
Asghar, Muhammad Hassan authoredAsghar, Muhammad Hassan authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
CoscineCodeGenerator.cs 3.04 KiB
using Coscine.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
namespace Coscine.CodeGen.CodeGenerator
{
public class CoscineCodeGenerator : CodeGenerator
{
private readonly IConfiguration _configuration;
public CoscineCodeGenerator(IConfiguration configuration)
{
this._configuration = configuration;
}
public async override Task<string> GetClientGenerator()
{
var jarDownloadLink = await _configuration.GetStringAsync("coscine/local/codegen/jarlink",
"https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/5.2.1/openapi-generator-cli-5.2.1.jar"
);
var webClient = new WebClient();
var jarFileName = await _configuration.GetStringAsync("coscine/local/codegen/jarpath", "codegen.jar");
await webClient.DownloadFileTaskAsync(new Uri(jarDownloadLink), jarFileName);
return jarFileName;
}
public async override Task<IEnumerable<string>> GetApiNames()
{
var apiPrefix = "coscine/apis/";
var keys = await _configuration.KeysAsync(apiPrefix);
return keys.Select((entry) => entry.Split('/')[2]).Distinct();
}
internal async override Task<string> GetOutputPath()
{
return await _configuration.GetStringAsync("coscine/local/codegen/outputpath", "Output");
}
internal override Task<string> GetSwaggerUrl(string domainName, string hostName, string key)
{
return Task.FromResult($"https://{hostName}.{domainName}/coscine/api/{key}/swagger/v1/swagger.json");
}
internal override string GetGenerationCommand(string outputPath, string jarFileName, string key, string swaggerUrl)
{
return $"java \"-Dio.swagger.parser.util.RemoteUrl.trustAll=true\" \"-Dio.swagger.v3.parser.util.RemoteUrl.trustAll=true\" -jar \"{jarFileName}\" generate -i \"{swaggerUrl}\" -g typescript-axios -o \"{outputPath}/{key}\" --skip-validate-spec";
}
internal override Task<string> GetCustomBasePath(string directoryName)
{
return Task.FromResult($"https://' + getHostName() + '/coscine/api/{directoryName}");
}
internal override Task<string> GetCustomCodeForCombinationFile(string combinationFileText)
{
combinationFileText += "let accessToken = '';";
combinationFileText += @"
if (typeof coscine !== 'undefined') {
accessToken = coscine.authorization.bearer;
}
const getHostName = () => {
let hostName = typeof window !== 'undefined' ? window.location.hostname : 'coscine.rwth-aachen.de';
if (hostName.indexOf(':') !== -1) {
if (hostName.indexOf('https://') !== -1) {
hostName = hostName.replace('https://', '');
}
hostName = hostName.substr(0, hostName.indexOf(':'));
}
return hostName;
};
";
return Task.FromResult(combinationFileText);
}
}
}