-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6fb9e65
commit 7aae3c0
Showing
21 changed files
with
619 additions
and
585 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
167 changes: 85 additions & 82 deletions
167
DemoCoreWebControllers/Controllers/CodeGenController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,85 @@ | ||
#if DEBUG //This controller is not needed in production release, since the client API should be generated during development of the Web Api. | ||
using Fonlow.CodeDom.Web; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.ApiExplorer; | ||
using System.Net; | ||
|
||
namespace Fonlow.WebApiClientGen | ||
{ | ||
[ApiExplorerSettings(IgnoreApi = true)] | ||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class CodeGenController : ControllerBase | ||
{ | ||
private readonly IApiDescriptionGroupCollectionProvider apiExplorer; | ||
private readonly string webRootPath; | ||
|
||
/// <summary> | ||
/// For injecting some environment config by the run time. | ||
/// </summary> | ||
/// <param name="apiExplorer"></param> | ||
/// <param name="hostingEnvironment"></param> | ||
public CodeGenController(IApiDescriptionGroupCollectionProvider apiExplorer, IWebHostEnvironment hostingEnvironment) | ||
{ | ||
this.apiExplorer = apiExplorer; | ||
this.webRootPath = hostingEnvironment.WebRootPath; | ||
} | ||
|
||
/// <summary> | ||
/// Trigger the API to generate WebApiClientAuto.cs for an established client API project. | ||
/// </summary> | ||
/// <param name="settings"></param> | ||
/// <returns>OK if OK</returns> | ||
[HttpPost] | ||
public IActionResult TriggerCodeGen([FromBody] CodeGenSettings settings) | ||
{ | ||
if (settings == null) | ||
return BadRequest("No settings"); | ||
|
||
if (settings.ClientApiOutputs == null) | ||
return BadRequest("No settings/ClientApiOutputs"); | ||
|
||
Fonlow.Web.Meta.WebApiDescription[] webApiDescriptions; | ||
try | ||
{ | ||
ApiDescription[] descriptions = ApiExplorerHelper.GetApiDescriptions(apiExplorer); | ||
webApiDescriptions = descriptions.Select(d => Fonlow.Web.Meta.MetaTransform.GetWebApiDescription(d)).OrderBy(d => d.ActionDescriptor.ActionName).ToArray(); | ||
|
||
} | ||
catch (Fonlow.Web.Meta.CodeGenException e) | ||
{ | ||
System.Diagnostics.Trace.TraceWarning(e.Message); | ||
return StatusCode((int)HttpStatusCode.InternalServerError, e.Message); | ||
} | ||
catch (System.InvalidOperationException e) | ||
{ | ||
System.Diagnostics.Trace.TraceWarning(e.Message); | ||
return StatusCode((int)HttpStatusCode.InternalServerError, e.Message); | ||
} | ||
|
||
if (!settings.ClientApiOutputs.CamelCase.HasValue) | ||
{ | ||
settings.ClientApiOutputs.CamelCase = true; | ||
} | ||
|
||
try | ||
{ | ||
CodeGen.GenerateClientAPIs(this.webRootPath, settings, webApiDescriptions); | ||
} | ||
catch (Fonlow.Web.Meta.CodeGenException e) | ||
{ | ||
string msg = e.Message + (string.IsNullOrEmpty(e.Description) ? string.Empty : (" : " + e.Description)); | ||
System.Diagnostics.Trace.TraceError(msg); | ||
return BadRequest(msg); | ||
} | ||
|
||
return Ok("Done"); | ||
} | ||
} | ||
|
||
} | ||
#endif | ||
#if DEBUG //This controller is not needed in production release, since the client API should be generated during development of the Web Api. | ||
using Fonlow.CodeDom.Web; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.ApiExplorer; | ||
using System.Net; | ||
|
||
namespace Fonlow.WebApiClientGen | ||
{ | ||
/// <summary> | ||
/// For CodeGen triggered by a client call. | ||
/// </summary> | ||
[ApiExplorerSettings(IgnoreApi = true)] | ||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class CodeGenController : ControllerBase | ||
{ | ||
private readonly IApiDescriptionGroupCollectionProvider apiExplorer; | ||
private readonly string webRootPath; | ||
|
||
/// <summary> | ||
/// For injecting some environment config by the run time. | ||
/// </summary> | ||
/// <param name="apiExplorer"></param> | ||
/// <param name="hostingEnvironment"></param> | ||
public CodeGenController(IApiDescriptionGroupCollectionProvider apiExplorer, IWebHostEnvironment hostingEnvironment) | ||
{ | ||
this.apiExplorer = apiExplorer; | ||
this.webRootPath = hostingEnvironment.WebRootPath; | ||
} | ||
|
||
/// <summary> | ||
/// Trigger the API to generate WebApiClientAuto.cs for an established client API project. | ||
/// </summary> | ||
/// <param name="settings"></param> | ||
/// <returns>OK if OK</returns> | ||
[HttpPost] | ||
public IActionResult TriggerCodeGen([FromBody] CodeGenSettings settings) | ||
{ | ||
if (settings == null) | ||
return BadRequest("No settings"); | ||
|
||
if (settings.ClientApiOutputs == null) | ||
return BadRequest("No settings/ClientApiOutputs"); | ||
|
||
Fonlow.Web.Meta.WebApiDescription[] webApiDescriptions; | ||
try | ||
{ | ||
ApiDescription[] descriptions = ApiExplorerHelper.GetApiDescriptions(apiExplorer); | ||
webApiDescriptions = descriptions.Select(d => Fonlow.Web.Meta.MetaTransform.GetWebApiDescription(d)).OrderBy(d => d.ActionDescriptor.ActionName).ToArray(); | ||
|
||
} | ||
catch (Fonlow.Web.Meta.CodeGenException e) | ||
{ | ||
Console.Error.WriteLine(e.Message); | ||
return StatusCode((int)HttpStatusCode.InternalServerError, e.Message); | ||
} | ||
catch (System.InvalidOperationException e) | ||
{ | ||
Console.Error.WriteLine(e.Message); | ||
return StatusCode((int)HttpStatusCode.InternalServerError, e.Message); | ||
} | ||
|
||
if (!settings.ClientApiOutputs.CamelCase.HasValue) | ||
{ | ||
settings.ClientApiOutputs.CamelCase = true; | ||
} | ||
|
||
try | ||
{ | ||
CodeGen.GenerateClientAPIs(this.webRootPath, settings, webApiDescriptions); | ||
} | ||
catch (Fonlow.Web.Meta.CodeGenException e) | ||
{ | ||
string msg = e.Message + (string.IsNullOrEmpty(e.Description) ? string.Empty : (" : " + e.Description)); | ||
Console.Error.WriteLine(msg); | ||
return BadRequest(msg); | ||
} | ||
|
||
return Ok("Done"); | ||
} | ||
} | ||
|
||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.