Skip to content
Snippets Groups Projects
Commit 49e47f50 authored by Benedikt Heinrichs's avatar Benedikt Heinrichs Committed by Petar Hristov
Browse files

New: Create an Api Client Script

parent b9df816c
Branches
No related tags found
1 merge request!1New: Create a Api Client Script
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto
###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp
###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary
###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary
###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Database connection for t4 class generation from database
**/*.generated.cs
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
# Visual Studio 2015 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUNIT
*.VisualState.xml
TestResult.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# DNX
project.lock.json
project.fragment.lock.json
artifacts/
*_i.c
*_p.c
*_i.h
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
Resharper
# JustCode is a .NET coding add-in
.JustCode
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# TODO: Comment the next line if you want to checkin your web deploy settings
# but database connection strings (with potential passwords) will be unencrypted
#*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/packages/*
# except build/, which is used as an MSBuild target.
!**/packages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config
# NuGet v3's project.json files produces more ignoreable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
node_modules/
orleans.codegen.cs
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
# SQL Server files
*.mdf
*.ldf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# JetBrains Rider
.idea/
*.sln.iml
# CodeRush
.cr/
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
#cake
tools/*
!tools/packages.config
dist/
# Dotnet Tool Manifest
.config/*
include:
- project: coscine/tools/gitlab-ci-templates
file:
- /dotnet.yml
stages:
- build
- test
variables:
DOTNET_MAIN_PROJECT_FOLDER: archiveMigrationParser
build-branch:
extends: .build-branch
test:
extends: .test
mode: ContinuousDeployment
next-version: 1.0.0
major-version-bump-message: 'Breaking:'
minor-version-bump-message: '(Update|New):'
patch-version-bump-message: 'Fix:'
no-bump-message: '.*'
commit-message-incrementing: Enabled
\ No newline at end of file
LICENSE 0 → 100644
MIT License
Copyright (c) 2020 RWTH Aachen University
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
MIT License
Copyright (c) 2020 RWTH Aachen University
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# CodeGen
This project aims to be able to generate Code from our Swagger / OpenAPI definitions.
\ No newline at end of file
This project aims to be able to generate Code from our Swagger / OpenAPI definitions.
## Build
* .NET 5 SDK has to be installed
* `dotnet build src`
## Usage
* Execute the built CodeGen executable
* You should find the output client definitions in the specified output folder (default. `Output`)
## Implementation
An abstract `CodeGenerator` has been implemented fully as a `CoscineCodeGenerator`.
The idea is that with this abstraction someone can easily adapt and generate the code they want to produce.
<?xml version="1.0" encoding="utf-8"?>
<!--
Taken from: https://github.com/nunit/nunit-transforms/tree/master/nunit3-junit
-->
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/test-run">
<testsuites tests="{@testcasecount}" failures="{@failed}" disabled="{@skipped}" time="{@duration}">
<xsl:apply-templates/>
</testsuites>
</xsl:template>
<xsl:template match="test-suite">
<xsl:if test="test-case">
<testsuite tests="{@testcasecount}" time="{@duration}" errors="{@testcasecount - @passed - @skipped - @failed}" failures="{@failed}" skipped="{@skipped}" timestamp="{@start-time}">
<xsl:attribute name="name">
<xsl:for-each select="ancestor-or-self::test-suite/@name">
<xsl:value-of select="concat(., '.')"/>
</xsl:for-each>
</xsl:attribute>
<xsl:apply-templates select="test-case"/>
</testsuite>
<xsl:apply-templates select="test-suite"/>
</xsl:if>
<xsl:if test="not(test-case)">
<xsl:apply-templates/>
</xsl:if>
</xsl:template>
<xsl:template match="test-case">
<testcase name="{@name}" assertions="{@asserts}" time="{@duration}" status="{@result}" classname="{@classname}">
<xsl:if test="@runstate = 'Skipped' or @runstate = 'Ignored'">
<skipped/>
</xsl:if>
<xsl:apply-templates/>
</testcase>
</xsl:template>
<xsl:template match="command-line"/>
<xsl:template match="settings"/>
<xsl:template match="output">
<system-out>
<xsl:value-of select="."/>
</system-out>
</xsl:template>
<xsl:template match="stack-trace">
</xsl:template>
<xsl:template match="test-case/failure">
<failure message="{./message}">
<xsl:value-of select="./stack-trace"/>
</failure>
</xsl:template>
<xsl:template match="test-suite/failure"/>
<xsl:template match="test-case/reason">
<skipped message="{./message}"/>
</xsl:template>
<xsl:template match="test-case/assertions">
</xsl:template>
<xsl:template match="test-suite/reason"/>
<xsl:template match="properties"/>
</xsl:stylesheet>
\ No newline at end of file

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31205.134
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Coscine.CodeGen", "CodeGen\CodeGen.csproj", "{1E91603C-DC28-4056-A00B-B8209A5FB431}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1E91603C-DC28-4056-A00B-B8209A5FB431}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1E91603C-DC28-4056-A00B-B8209A5FB431}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1E91603C-DC28-4056-A00B-B8209A5FB431}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1E91603C-DC28-4056-A00B-B8209A5FB431}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F61FDE69-B069-4DE7-9F68-FFEEA9BAF137}
EndGlobalSection
EndGlobal
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<AssemblyName>Coscine.CodeGen</AssemblyName>
<RootNamespace>Coscine.CodeGen</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Coscine.Configuration" Version="1.6.0" />
</ItemGroup>
</Project>
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Coscine.CodeGen.CodeGenerator
{
public abstract class CodeGenerator
{
#region Functionality
public async Task GenerateCode()
{
var jarFileName = await GetClientGenerator();
var keys = await GetApiNames();
var outputPath = await GetOutputPath();
if (!Directory.Exists(outputPath))
{
Directory.CreateDirectory(outputPath);
}
await RetrieveDefinitions(outputPath, jarFileName, keys);
await WriteDefinitions(outputPath);
}
private async Task<string> RetrieveDefinitions(string outputPath, string jarFileName, IEnumerable<string> keys)
{
var domainName = System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().DomainName;
var hostName = Dns.GetHostName();
foreach (var key in keys)
{
Console.WriteLine(key);
var swaggerUrl = await GetSwaggerUrl(domainName, hostName, key);
var command = GetGenerationCommand(outputPath, jarFileName, key, swaggerUrl);
await ExecuteCommand(command);
}
return outputPath;
}
private async Task WriteDefinitions(string outputPath)
{
var combinationFile = outputPath + "/apis.ts";
var indexFile = outputPath + "/index.ts";
var combinationFileImports = new List<string>();
var combinationFileExports = new List<string>();
var concreteApiNames = new List<string>();
var first = true;
var apiRegex = new Regex("(?<= )(.*?)(?= extends BaseAPI)");
foreach (var directory in Directory.GetDirectories(outputPath))
{
var apiName = directory[(directory.LastIndexOf(".") + 1)..];
var directoryName = directory[(directory.LastIndexOf("\\") + 1)..];
apiName = apiName.Replace("Resources", "Resource");
apiName = apiName.Replace("Notices", "Notice");
var apiContent = File.ReadAllText($"./{outputPath}/{directoryName}/api.ts");
var apiImplementations = apiRegex.Matches(apiContent);
var customBasePath = await GetCustomBasePath(directoryName);
foreach (var apiImplementation in apiImplementations)
{
var concreteApiName = apiImplementation.ToString().Replace("class ", "");
concreteApiNames.Add(concreteApiName);
combinationFileImports.Add($"import {{ {concreteApiName}Factory }} from './{directoryName}/api';");
if (first)
{
first = false;
combinationFileImports.Add($"import {{ Configuration }} from './{directoryName}/configuration';");
}
combinationFileExports.Add($"{concreteApiName}: {concreteApiName}Factory(new Configuration({{ 'accessToken': accessToken }}), '{customBasePath}', axios)");
}
}
combinationFileExports.Sort();
concreteApiNames.Sort();
await WriteCombinationFile(combinationFile, combinationFileImports, combinationFileExports);
await WriteIndexFile(indexFile, concreteApiNames);
}
private async Task WriteCombinationFile(string combinationFile, IEnumerable<string> combinationFileImports, IEnumerable<string> combinationFileExports)
{
var combinationFileText = "import { AxiosInstance } from 'axios';\n\n";
combinationFileText += string.Join('\n', combinationFileImports);
combinationFileText += "\n\n";
combinationFileText = await GetCustomCodeForCombinationFile(combinationFileText);
combinationFileText += @"function implementations(axios?: AxiosInstance) {
return {
";
combinationFileText += string.Join(",\n ", combinationFileExports);
combinationFileText += "\n };\n};\n\nexport default implementations;\n";
await File.WriteAllTextAsync(combinationFile, combinationFileText);
}
private async Task WriteIndexFile(string indexFile, IEnumerable<string> concreteApiNames)
{
var indexFileText = @"import implementations from './apis';
import instance from './axios-basic';
const apis = implementations(instance);
";
foreach (var concreteApiName in concreteApiNames)
{
indexFileText += $"export const {concreteApiName} = apis.{concreteApiName};\n";
}
indexFileText += "\nexport default apis;\n";
await File.WriteAllTextAsync(indexFile, indexFileText);
}
internal Task ExecuteCommand(string command)
{
var startInfo = new ProcessStartInfo
{
FileName = @"C:\Windows\System32\cmd.exe",
Arguments = "/c " + command,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
};
using (var process = new Process
{
StartInfo = startInfo
})
{
process.Start();
var outputTask = process.StandardOutput.ReadToEndAsync();
var errorTask = process.StandardError.ReadToEndAsync();
Task.WaitAll(outputTask, errorTask);
Console.WriteLine(outputTask.Result);
Console.WriteLine(errorTask.Result);
return process.WaitForExitAsync();
}
}
#endregion
#region Abstract Methods
public abstract Task<string> GetClientGenerator();
public abstract Task<IEnumerable<string>> GetApiNames();
internal abstract Task<string> GetOutputPath();
internal abstract Task<string> GetSwaggerUrl(string domainName, string hostName, string key);
internal abstract string GetGenerationCommand(string outputPath, string jarFileName, string key, string swaggerUrl);
internal abstract Task<string> GetCustomBasePath(string directoryName);
internal abstract Task<string> GetCustomCodeForCombinationFile(string combinationFileText);
#endregion
}
}
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);
}
}
}
using Coscine.Configuration;
using Coscine.CodeGen.CodeGenerator;
namespace Coscine.CodeGen
{
public class Program
{
public static void Main(string[] args)
{
new CoscineCodeGenerator(new ConsulConfiguration()).GenerateCode().GetAwaiter().GetResult();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment