Skip to content

Commit

Permalink
Update workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
pomianowski committed Mar 19, 2024
1 parent 47ede38 commit eef77b1
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 39 deletions.
33 changes: 33 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
release:
- base-branch: 'main'

PR:
- base-branch: [ 'main', 'development' ]

github_actions:
- changed-files:
- any-glob-to-any-file: '.github/workflows/**'

documentation:
- changed-files:
- any-glob-to-any-file: [ 'docs/**', 'documentation/**' ]

dotnet:
- changed-files:
- any-glob-to-any-file: '**/*.cs'

update:
- changed-files:
- any-glob-to-any-file: 'src/Directory.Build.props'

NuGet:
- changed-files:
- any-glob-to-any-file: [ 'src/OpenApi.Client/**', 'src/Directory.Packages.props' ]

dependencies:
- changed-files:
- any-glob-to-any-file: [ 'src/Directory.Packages.props' ]

source-generators:
- changed-files:
- any-glob-to-any-file: [ 'src/OpenApi.Client.SourceGenerators/**' ]
35 changes: 35 additions & 0 deletions .github/workflows/openapi-client-cd-nuget.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: openapi-client-cd-nuget

on:
push:
branches: [main]

workflow_dispatch:

jobs:
deploy:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- uses: microsoft/[email protected]
with:
msbuild-architecture: x64
- uses: nuget/setup-nuget@v2
with:
nuget-api-key: ${{ secrets.NUGET_API_KEY }}
- name: Setup .NET Core SDK 8.x
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.x

- name: Install dependencies
run: dotnet restore

- name: Build
run: dotnet build src\OpenApi.Client\OpenApi.Client.csproj --configuration Release --no-restore

- name: Publish the package to NuGet.org
run: nuget push **\*.nupkg -NonInteractive -SkipDuplicate -Source 'https://api.nuget.org/v3/index.json'

- name: Publish the symbols to NuGet.org
run: nuget push **\*.snupkg -NonInteractive -SkipDuplicate -Source 'https://api.nuget.org/v3/index.json'
15 changes: 15 additions & 0 deletions .github/workflows/openapi-client-labeler.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: openapi-client-labeler

on:
- pull_request_target

jobs:
triage:
permissions:
contents: read
pull-requests: write
runs-on: ubuntu-latest
steps:
- uses: actions/labeler@v5
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
31 changes: 31 additions & 0 deletions .github/workflows/openapi-client-pr-validator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: openapi-client-pr-validator

on:
pull_request:
branches: [development]
push:
branches: [development]

workflow_dispatch:

jobs:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- uses: microsoft/[email protected]
with:
msbuild-architecture: x64
- uses: nuget/setup-nuget@v2
with:
nuget-api-key: ${{ secrets.NUGET_API_KEY }}
- name: Setup .NET Core SDK 8.x
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.x

- name: Install dependencies
run: dotnet restore

- name: Build
run: dotnet build src\OpenApi.Client\OpenApi.Client.csproj --configuration Release --no-restore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ namespace OpenApi.Client.SourceGenerators.Genertion;
internal sealed partial class ClientGenerator
{
public const string ClassHeader = """
%A partial class %C : I%C
[global::System.CodeDom.Compiler.GeneratedCode("OpenApiClient", "%VERSION%")]
%ACCESS% partial class %CLASS% : I%CLASS%
{
private global::System.Net.HttpStatusCode? lastStatusCode;
Expand All @@ -20,7 +21,7 @@ internal sealed partial class ClientGenerator
/// <summary>Options for the serializer used to create API objects.</summary>
protected readonly global::System.Text.Json.JsonSerializerOptions JsonSettings;
public %C(global::System.Net.Http.HttpClient httpClient)
public %CLASS%(global::System.Net.Http.HttpClient httpClient)
: this(httpClient, new global::System.Text.Json.JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
Expand All @@ -32,11 +33,11 @@ internal sealed partial class ClientGenerator
{
}
protected %C() : this(default!, default!)
protected %CLASS%() : this(default!, default!)
{
}
protected %C(global::System.Net.Http.HttpClient httpClient, global::System.Text.Json.JsonSerializerOptions jsonSettings)
protected %CLASS%(global::System.Net.Http.HttpClient httpClient, global::System.Text.Json.JsonSerializerOptions jsonSettings)
{
HttpClient = httpClient;
JsonSettings = jsonSettings;
Expand Down Expand Up @@ -160,7 +161,7 @@ public static void AppendClass(StringBuilder builder, OpenApiContract contract)
}

builder.Append(
" public virtual async global::System.Threading.Tasks.Task<%CResult"
" public virtual async global::System.Threading.Tasks.Task<%CLASS%Result"
);

if (path.ResponseType?.Length > 0)
Expand Down Expand Up @@ -210,10 +211,10 @@ public static void AppendClass(StringBuilder builder, OpenApiContract contract)
}
catch (global::System.Exception e)
{
return new %CResult(new %CResultError[]{new %CResultError(e.Message)}, lastStatusCode);
return new %CLASS%Result(new %CLASS%ResultError[]{new %CLASS%ResultError(e.Message)}, lastStatusCode);
}
return new %CResult(lastStatusCode);
return new %CLASS%Result(lastStatusCode);
}
"""
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ namespace OpenApi.Client.SourceGenerators.Genertion;
internal sealed partial class ClientGenerator
{
public const string InterfaceHeader = """
/// <summary>An interface for the <c>%T</c> Open API Client.</summary>
/// <summary>An interface for the <c>%TITLE%</c> Open API Client.</summary>
/// <remarks>Generated with Open API Client Source Generator. See: <see href="https://github.com/lepoco/openapi.client"/></remarks>
%A interface I%C
[global::System.CodeDom.Compiler.GeneratedCode("OpenApiClient", "%VERSION%")]
%ACCESS% interface I%CLASS%
{
/// <summary>Gets the last status code from the HTTP request.</summary>
global::System.Net.HttpStatusCode? GetLastStatusCode();
Expand Down Expand Up @@ -43,7 +44,7 @@ public static void AppendInterface(StringBuilder builder, OpenApiContract contra
builder.AppendLine("</summary>");
}

builder.Append(" global::System.Threading.Tasks.Task<%CResult");
builder.Append(" global::System.Threading.Tasks.Task<%CLASS%Result");

if (path.ResponseType?.Length > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ internal sealed partial class ClientGenerator
{
public const string ResultTemplate = """
/// <summary>Represents a result of the API call.</summary>
%A struct %CResultError
[global::System.CodeDom.Compiler.GeneratedCode("OpenApiClient", "%VERSION%")]
%ACCESS% struct %CLASS%ResultError
{
/// <summary>Initializes a new instance of the error struct.</summary>
public %CResultError(string message)
public %CLASS%ResultError(string message)
{
Message = message;
}
Expand All @@ -22,28 +23,29 @@ internal sealed partial class ClientGenerator
}
/// <summary>Represents a result of the API call.</summary>
%A class %CResult
[global::System.CodeDom.Compiler.GeneratedCode("OpenApiClient", "%VERSION%")]
%ACCESS% class %CLASS%Result
{
/// <summary>Initializes a new instance of the class.</summary>
/// <param name="errors">The errors of the API call.</param>
/// <param name="statusCode">The status code of the API call.</param>
public %CResult(%CResultError[] errors, global::System.Net.HttpStatusCode? statusCode)
public %CLASS%Result(%CLASS%ResultError[] errors, global::System.Net.HttpStatusCode? statusCode)
{
Errors = errors;
StatusCode = statusCode;
}
/// <summary>Initializes a new instance of the result class.</summary>
/// <param name="statusCode">The status code of the API call.</param>
public %CResult(global::System.Net.HttpStatusCode? statusCode) : this(new %CResultError[0], statusCode)
public %CLASS%Result(global::System.Net.HttpStatusCode? statusCode) : this(new %CLASS%ResultError[0], statusCode)
{
}
/// <summary>Gets the status code of the API call.</summary>
public global::System.Net.HttpStatusCode? StatusCode { get; }
/// <summary>Gets the errors of the API call.</summary>
public %CResultError[] Errors { get; }
public %CLASS%ResultError[] Errors { get; }
/// <summary>Gets a value indicating whether the API call has errors.</summary>
public bool HasErrors
Expand All @@ -56,14 +58,15 @@ public bool HasErrors
}
/// <summary>Represents a result from the API.</summary>
%A sealed class %CResult<TResult> : %CResult where TResult : class
[global::System.CodeDom.Compiler.GeneratedCode("OpenApiClient", "%VERSION%")]
%ACCESS% sealed class %CLASS%Result<TResult> : %CLASS%Result where TResult : class
{
private readonly TResult? _result;
/// <summary>Initializes a new instance of the result class.</summary>
/// <param name="result">Result of the API call.</param>
/// <param name="statusCode">The status code of the API call.</param>
public %CResult(TResult result, global::System.Net.HttpStatusCode? statusCode) : base(statusCode)
public %CLASS%Result(TResult result, global::System.Net.HttpStatusCode? statusCode) : base(statusCode)
{
if (result == null)
{
Expand All @@ -76,7 +79,7 @@ public bool HasErrors
/// <summary>Initializes a new instance of the result class.</summary>
/// <param name="errors">The errors of the API call.</param>
/// <param name="statusCode">The errors of the API call.</param>
public %CResult(%CResultError[] errors, global::System.Net.HttpStatusCode? statusCode) : base(errors, statusCode)
public %CLASS%Result(%CLASS%ResultError[] errors, global::System.Net.HttpStatusCode? statusCode) : base(errors, statusCode)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static void AppendTypes(StringBuilder builder, OpenApiContract contract)
builder.AppendLine("</summary>");
}

builder.AppendLine(" [global::System.CodeDom.Compiler.GeneratedCode(\"OpenApiClient\", \"%VERSION%\")]");
builder.Append(" public sealed class ");
builder.AppendLine(type.Name);
builder.AppendLine(" {");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ namespace OpenApi.Client.SourceGenerators.Genertion;

internal sealed partial class ClientGenerator(OpenApiContract contract)
{
public static readonly string[] Placeholders = ["%C", "%T", "%A"];
public const string GeneratorVersion = "1.0.0";

public static readonly string[] Placeholders = ["%CLASS%", "%TITLE%", "%ACCESS%", "%VERSION%"];

public const string DocumentPrefix = """
// <auto-generated/>
Expand Down Expand Up @@ -43,9 +45,10 @@ public GenerationResult<string> Generate()

builder.AppendLine("}");

builder.Replace("%C", contract.ClassName);
builder.Replace("%T", RemoveUnsafeWords(contract.Title));
builder.Replace("%A", contract.Access);
builder.Replace("%CLASS%", contract.ClassName);
builder.Replace("%TITLE%", RemoveUnsafeWords(contract.Title));
builder.Replace("%ACCESS%", contract.Access);
builder.Replace("%VERSION%", GeneratorVersion);

return builder.ToString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,31 @@ internal static class OpenApiClientGenerationHelper
#nullable enable
namespace OpenApi.Client
{
/// <summary>
/// Library which API should use at runtime.
/// </summary>
/// <summary>Library which API client should use at runtime.</summary>
internal enum OpenApiClientSerialization
{
SystemTextJson,
NewtonsoftJson
}
/// <summary>An attribute that indicates that a given class should have an Open API Client generated for it.</summary>
/// <summary>Indicates that a given class should have an Open API Client generated for it.</summary>
/// <remarks>Generated with Open API Client Source Generator. See: <see href="https://github.com/lepoco/openapi.client"/></remarks>
[global::System.AttributeUsage(global::System.AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
internal sealed class OpenApiClientAttribute : global::System.Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="OpenApiClientAttribute"/> class.
/// </summary>
/// <summary>Initializes a new instance of the <see cref="OpenApiClientAttribute"/> class.</summary>
/// <param name="specification">The specification resource name for the Open API Client.</param>
/// <remarks>
/// The specification is the name of the resource which is a yaml or json open api code.
/// </remarks>
/// <remarks>The specification is the name of the resource which is a yaml or json open api code.</remarks>
public OpenApiClientAttribute(string specification)
{
Specification = specification;
Serialization = OpenApiClientSerialization.SystemTextJson;
}
/// <summary>
/// Initializes a new instance of the <see cref="OpenApiClientAttribute"/> class.
/// </summary>
/// <summary>Initializes a new instance of the <see cref="OpenApiClientAttribute"/> class.</summary>
/// <param name="specification">The specification resource name for the Open API Client.</param>
/// <param name="serializationTool">A flag indicating whether to use the Service Collection for the Open API Client.</param>
/// <remarks>
/// The specification is the name of the resource which is a yaml or json open api code.
/// </remarks>
/// <remarks>The specification is the name of the resource which is a yaml or json open api code.</remarks>
public OpenApiClientAttribute(string specification, OpenApiClientSerialization serializationTool)
{
Specification = specification;
Expand Down

0 comments on commit eef77b1

Please sign in to comment.