Skip to content

Commit

Permalink
v0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Beffyman committed Jun 30, 2018
1 parent 72070e9 commit 68fbfd7
Show file tree
Hide file tree
Showing 17 changed files with 842 additions and 222 deletions.
12 changes: 12 additions & 0 deletions Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ if([System.String]::IsNullOrEmpty($version)){
}

$artifacts = "$scriptBin/artifacts";
$testGenerator = Resolve-Path "$scriptBin/test/AspNetCore.Client.Test.Generator";

Remove-Item $artifacts -Recurse -ErrorAction Ignore
New-Item -Force -ItemType directory -Path $artifacts
$outputDir = Resolve-Path $artifacts;

Expand All @@ -26,6 +28,16 @@ Write-Host "Building Version $version";
Write-Host ">> dotnet build -c Release -v m;"
dotnet build -c Release -v m;

#Run the test project generators
Push-Location -Path $testGenerator -StackName "Run";
Write-Host ">> dotnet run -c Release -v m;"
dotnet run -c Release -v m;
Pop-Location -StackName "Run";

#Build again, making sure that our clients that were just regenerated via the previous command build
Write-Host ">> dotnet build -c Release -v m;"
dotnet build -c Release -v m;

Write-Host ">> dotnet test -c Release -v m;"
dotnet test -c Release -v m;

Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Package that contains required classes/attributes used by the generator package.

On Build generator that will generate a Clients.cs file based on the ClientGeneratorSettings.json file the generator creates.


#### ClientGeneratorSettings.json Reference

- Locked
Expand All @@ -30,8 +31,13 @@ On Build generator that will generate a Clients.cs file based on the ClientGener
- Whether or not the generate the clients so they are compatible with Blazor views
- Differences include
- Newtonsoft.Json.JsonConvert.DeserializeObject => JsonUtil.Deserialize
- Namespace include changes.
- Default namespaces are different.
- Requires a reference to [Microsoft.AspNetCore.Blazor](https://www.nuget.org/packages/Microsoft.AspNetCore.Blazor/) inside the client project
- IncludeHttpOverride
- You will be required to provide the service registration for `IHttpOverride`.
- Allows for overrides to a separate service that will check to see if a HttpResponseMessage can be provided by it instead so it doesn't need to make the http call.
- Useful if you want to intercept a request before it goes out depending on internal flags, etc.
- Can be used with mocking
- AllowedNamespaces
- Namespaces allowed to be pulled from Controllers that the generator is pulling the data from.
- ex) You have `using MyApp.Contracts;` inside the Controller, if MyApp.Contracts is inside the allowed namespaces, it would be copied into the Clients.cs
Expand Down
10 changes: 1 addition & 9 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
version: '0.1.{build}'
version: '0.2.{build}'
configuration: Release
image: Visual Studio 2017

max_jobs: 1
skip_non_tags: true
clone_depth: 1

branches:
only:
- master

nuget:
account_feed: true
project_feed: true
Expand Down
36 changes: 30 additions & 6 deletions src/AspNetCore.Client.Generator/ClientWriter.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using AspNetCore.Client.Generator.Data;
using AspNetCore.Client.Core;
using AspNetCore.Client.Generator.Data;
using Flurl.Http;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace AspNetCore.Client.Generator
{
Expand Down Expand Up @@ -49,21 +54,38 @@ public static IServiceCollection InstallClients(this IServiceCollection services
";
}

public static string GetIncludePreHttpCheck()
{
if (!Settings.Instance.IncludeHttpOverride)
{
return string.Empty;
}


return $@"
public interface {Constants.HttpOverride}
{{
{Helpers.GetTaskType()}<{nameof(HttpResponseMessage)}> {Constants.HttpOverrideGetMethod}({nameof(String)} {Constants.UrlVariable}, {nameof(CancellationToken)} {Constants.CancellationTokenParameter} = default({nameof(CancellationToken)}));
{nameof(Task)} {Constants.HttpOverrideOnNonOverridedResponse}({nameof(String)} {Constants.UrlVariable}, {nameof(HttpResponseMessage)} {Constants.ResponseVariable}, {nameof(CancellationToken)} {Constants.CancellationTokenParameter} = default({nameof(CancellationToken)}));
}}
";
}

private static string GetServiceClients()
{
return $@"
public class {Settings.Instance.ClientInterfaceName}
{{
public readonly FlurlClient ClientWrapper;
public readonly {nameof(FlurlClient)} {Constants.FlurlClientVariable};
public {Settings.Instance.ClientInterfaceName}(HttpClient client)
public {Settings.Instance.ClientInterfaceName}({nameof(HttpClient)} client)
{{
ClientWrapper = new FlurlClient(client);
{Constants.FlurlClientVariable} = new {nameof(FlurlClient)}(client);
}}
}}
public interface I{Settings.Instance.ClientInterfaceName} : IClient {{ }}";
public interface I{Settings.Instance.ClientInterfaceName} : {nameof(IClient)} {{ }}";
}

public static void WriteClientsFile(IList<ParsedFile> parsedFiles)
Expand All @@ -83,7 +105,8 @@ public static void WriteClientsFile(IList<ParsedFile> parsedFiles)
"using AspNetCore.Client.Core;",
"using AspNetCore.Client.Core.Authorization;",
"using AspNetCore.Client.Core.Exceptions;",
"using Microsoft.Extensions.DependencyInjection;"
"using Microsoft.Extensions.DependencyInjection;",
"using System.Threading;"
};

if (Settings.Instance.BlazorClients)
Expand Down Expand Up @@ -133,6 +156,7 @@ namespace {Settings.Instance.Namespace}
{{
{GetInstaller(parsedFiles)}
{GetIncludePreHttpCheck()}
{GetServiceClients()}
{string.Join(Environment.NewLine, blocks)}
}}
Expand Down
36 changes: 36 additions & 0 deletions src/AspNetCore.Client.Generator/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace AspNetCore.Client.Generator
{
public static class Constants
{
public const string Route = "Route";
public const string Authorize = "Authorize";
public const string Obsolete = "Obsolete";
public const string Http = "Http";
public const string Attribute = "Attribute";
public const string AllowAnonymous = "AllowAnonymous";
public const string ProducesResponseType = "ProducesResponseType";

public const string ClientInterfaceName = "Client";
public const string FlurlClientVariable = "ClientWrapper";

public const string HttpOverride = "IHttpOverride";
public const string HttpOverrideField = "HttpOverride";
public const string HttpOverrideGetMethod = "GetResponseAsync";
public const string HttpOverrideOnNonOverridedResponse = "OnNonOverridedResponseAsync";

public const string IActionResult = "IActionResult";

public const string ControllerRouteReserved = "controller";
public const string ActionRouteReserved = "action";

public const string CancellationTokenParameter = "cancellationToken";

public const string ResponseVariable = "response";
public const string UrlVariable = "url";
public const string AuthParameter = "auth";
}
}
50 changes: 43 additions & 7 deletions src/AspNetCore.Client.Generator/Data/ClassDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class ClassDefinition
}


var routeAttribute = attributes.SingleOrDefault(x => x.Name.ToFullString().StartsWith("Route"));
var routeAttribute = attributes.SingleOrDefault(x => x.Name.ToFullString().StartsWith(Constants.Route));
if (routeAttribute != null)//Fetch route from RouteAttribute
{
Route = routeAttribute.ArgumentList.Arguments.ToFullString().Replace("\"", "");
Expand All @@ -74,10 +74,10 @@ public class ClassDefinition
.ToList();

//Authorize Attribute
Options.Authorize = attributes.SingleOrDefault(x => x.Name.ToFullString().StartsWith("Authorize")) != null;
Options.Authorize = attributes.SingleOrDefault(x => x.Name.ToFullString().StartsWith(Constants.Authorize)) != null;

//Obsolete Attribute
var obsoleteAttribute = attributes.SingleOrDefault(x => x.Name.ToFullString().StartsWith("Obsolete"));
var obsoleteAttribute = attributes.SingleOrDefault(x => x.Name.ToFullString().StartsWith(Constants.Obsolete));
if (obsoleteAttribute != null)
{
Options.Obsolete = obsoleteAttribute.ArgumentList.Arguments.ToFullString().Replace("\"", "").Trim();
Expand All @@ -91,6 +91,42 @@ public class ClassDefinition
public string GetText()
{

var fields = new List<string>();

fields.Add($@" public readonly {Settings.Instance.ClientInterfaceName} {Constants.ClientInterfaceName};");
if (Settings.Instance.IncludeHttpOverride)
{
fields.Add($@" public readonly {Constants.HttpOverride} {Constants.HttpOverrideField};");
}

var classFields = string.Join(Environment.NewLine, fields);


var parameters = new List<string>();

parameters.Add($@"{Settings.Instance.ClientInterfaceName} client");

if (Settings.Instance.IncludeHttpOverride)
{
parameters.Add($@"{Constants.HttpOverride} httpOverride");
}


string @params = string.Join(", ", parameters);




var initializers = new List<string>();

initializers.Add($" {Constants.ClientInterfaceName} = client;");
if (Settings.Instance.IncludeHttpOverride)
{
initializers.Add($" {Constants.HttpOverrideField} = httpOverride;");
}

string init = string.Join(Environment.NewLine, initializers);

var str =
$@"
{GetObsolete()}
Expand All @@ -102,11 +138,11 @@ public interface I{ClientName} : I{Settings.Instance.ClientInterfaceName}
{GetObsolete()}
public class {ClientName} : I{ClientName}
{{
public readonly {Settings.Instance.ClientInterfaceName} Client;
{classFields}
public {ClientName}({Settings.Instance.ClientInterfaceName} client)
public {ClientName}({@params})
{{
Client = client;
{init}
}}
{string.Join($"{Environment.NewLine}", Methods.Where(x => !x.IsNotEndpoint).Select(x => x.GetImplementationText()))}
Expand All @@ -122,7 +158,7 @@ private string GetObsolete()
{
if (Options.Obsolete != null)
{
return $@" [Obsolete(""{Options.Obsolete}"")]";
return $@" [{Constants.Obsolete}(""{Options.Obsolete}"")]";
}
else
{
Expand Down
Loading

0 comments on commit 68fbfd7

Please sign in to comment.