Browse Source

Generate proxy command refactor

pull/3138/head
Arkat Erol 7 years ago
parent
commit
c7e575c689
  1. 408
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/GenerateProxyCommand.cs

408
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/GenerateProxyCommand.cs

@ -18,6 +18,7 @@ using Volo.Abp.DependencyInjection;
using Microsoft.CSharp; using Microsoft.CSharp;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using static System.String;
namespace Volo.Abp.Cli.Commands namespace Volo.Abp.Cli.Commands
{ {
@ -36,246 +37,259 @@ namespace Volo.Abp.Cli.Commands
public async Task ExecuteAsync(CommandLineArgs commandLineArgs) public async Task ExecuteAsync(CommandLineArgs commandLineArgs)
{ {
try var apiUrl = commandLineArgs.Options.GetOrNull(Options.ApiUrl.Short, Options.ApiUrl.Long);
{ var uiFramework = GetUiFramework(commandLineArgs);
var apiUrl = commandLineArgs.Options.GetOrNull(Options.ApiUrl.Short, Options.ApiUrl.Long);
var uiFramework = commandLineArgs.Options.GetOrNull(Options.UiFramework.Short, Options.UiFramework.Long);
//WebClient client = new WebClient();
//string json = client.DownloadString(apiUrl);
StreamReader sr = File.OpenText("api-definition.json");
string json = sr.ReadToEnd();
Logger.LogInformation("Downloading api definition..."); //WebClient client = new WebClient();
Logger.LogInformation("Api Url: " + apiUrl); //string json = client.DownloadString(apiUrl);
var sr = File.OpenText("api-definition.json");
var json = sr.ReadToEnd();
JObject data = JObject.Parse(json); Logger.LogInformation("Downloading api definition...");
Logger.LogInformation("Api Url: " + apiUrl);
var serviceIndexList = new List<string>(); var data = JObject.Parse(json);
var modelIndexList = new List<string>();
var moduleList = new Dictionary<string, string>(); Logger.LogInformation("Modules are combining");
var moduleList = GetCombinedModules(data);
Logger.LogInformation("Modules and types are creating");
foreach (var module in data["modules"]) var serviceIndexList = new List<string>();
{ var modelIndexList = new List<string>();
string rootPath = ((string)module.First["rootPath"]).ToLower();
foreach (var module in moduleList)
{
var moduleValue = JObject.Parse(module.Value);
if (moduleList.Any(p => p.Key == rootPath)) var rootPath = module.Key;
{ var controllerName = "";
var value = moduleList[rootPath]; var controllerServiceName = "";
moduleList[rootPath] = value.TrimEnd('}') + "," + module.First["controllers"].ToString().TrimStart('{'); Logger.LogInformation($"{rootPath} directory is creating");
}
else {
moduleList.Add(rootPath, module.First["controllers"].ToString());
}
}
Directory.CreateDirectory($"src/app/{rootPath}/shared/models");
Directory.CreateDirectory($"src/app/{rootPath}/shared/services");
foreach (var module in moduleList) var serviceFileText = new StringBuilder();
{
JObject moduleValue = JObject.Parse(module.Value);
string rootPath = module.Key; serviceFileText.AppendLine("[firstTypeList]");
var controllerName = ""; serviceFileText.AppendLine("import { Injectable } from '@angular/core';");
var controllerServiceName = ""; serviceFileText.AppendLine("import { Observable } from 'rxjs';");
serviceFileText.AppendLine("[secondTypeList]");
serviceFileText.AppendLine("");
serviceFileText.AppendLine("@Injectable()");
serviceFileText.AppendLine("export class [controllerName]Service {");
serviceFileText.AppendLine(" constructor(private restService: RestService) {}");
serviceFileText.AppendLine("");
Directory.CreateDirectory(string.Format("src/app/{0}/shared/models", rootPath)); var firstTypeList = new List<string>();
Directory.CreateDirectory(string.Format("src/app/{0}/shared/services", rootPath)); var secondTypeList = new List<string>();
StringBuilder serviceFileText = new StringBuilder(); foreach (var controller in moduleValue.Root.ToList().Select(item => item.First))
{
controllerName = (string)controller["controllerName"];
controllerServiceName = controllerName.PascalToKebabCase() + ".service.ts";
serviceFileText.AppendLine("firstTypeList"); foreach (var actionItem in controller["actions"])
serviceFileText.AppendLine("import { Injectable } from '@angular/core';"); {
serviceFileText.AppendLine("import { Observable } from 'rxjs';"); var action = actionItem.First;
serviceFileText.AppendLine("secondTypeList"); var actionName = (string)action["uniqueName"];
serviceFileText.AppendLine("");
serviceFileText.AppendLine("@Injectable()");
serviceFileText.AppendLine(string.Format("export class {0}Service ", "[ControllerName]") + "{");
serviceFileText.AppendLine(" constructor(private restService: RestService) {}");
serviceFileText.AppendLine("");
var firstTypeList = new List<string>(); actionName = (char.ToLower(actionName[0]) + actionName.Substring(1)).Replace("Async", "").Replace("Controller", "");
var secondTypeList = new List<string>();
foreach (var item in moduleValue.Root.ToList()) var returnValueType = (string)action["returnValue"]["type"];
{
var controller = item.First;
controllerName = (string)controller["controllerName"]; var parameters = action["parameters"];
controllerServiceName = controllerName.PascalToKebabCase() + ".service.ts"; var parametersText = new StringBuilder();
var parametersIndex = 0;
var bodyExtra = "";
var modelBindingExtra = "";
var modelBindingExtraList = new List<string>();
foreach (var actionItem in controller["actions"]) foreach (var parameter in parameters)
{ {
var action = actionItem.First; parametersIndex++;
var actionName = (string)action["uniqueName"];
actionName = (char.ToLower(actionName[0]) + actionName.Substring(1)).Replace("Async", "").Replace("Controller", "");
var returnValueType = (string)action["returnValue"]["type"]; if (parametersIndex > 1)
{
parametersText.Append(", ");
}
var parameters = action["parameters"]; var bindingSourceId = (string)parameter["bindingSourceId"];
StringBuilder parametersText = new StringBuilder(); bindingSourceId = char.ToLower(bindingSourceId[0]) + bindingSourceId.Substring(1);
var parametersIndex = 0;
var bodyExtra = "";
var modelBindingExtra = "";
var modelBindingExtraList = new List<string>();
foreach (var parameter in parameters) if (bindingSourceId == "body")
{ {
parametersIndex++; bodyExtra = ", body";
var typeArray = ((string)parameter["type"]).Split(".");
var type = typeArray[typeArray.Length - 1];
if (parametersIndex > 1) parametersText.Append(bindingSourceId + ": " + type);
parametersText.Append(", "); secondTypeList.Add(type);
}
else if (bindingSourceId == "path")
{
parametersText.Append((string)parameter["name"] + ": " + (string)parameter["typeSimple"]);
}
else if (bindingSourceId == "modelBinding")
{
var typeSimple = "";
var type = "";
var bindingSourceId = (string)parameter["bindingSourceId"]; var parameterNameOnMethod = (string)parameter["nameOnMethod"];
bindingSourceId = char.ToLower(bindingSourceId[0]) + bindingSourceId.Substring(1);
if (bindingSourceId == "body") var parametersOnMethod = action["parametersOnMethod"];
foreach (var parameterOnMethod in parametersOnMethod)
{ {
bodyExtra = ", body"; var parametersOnMethodName = (string)parameterOnMethod["name"];
var typeArray = ((string)parameter["type"]).Split("."); if (parametersOnMethodName == parameterNameOnMethod)
var type = typeArray[typeArray.Length - 1]; {
typeSimple = (string)parameterOnMethod["typeSimple"];
parametersText.Append(bindingSourceId + ": " + type); var typeArray = ((string)parameterOnMethod["type"]).Split(".");
secondTypeList.Add(type); type = typeArray[typeArray.Length - 1];
}
} }
else if (bindingSourceId == "path")
if (typeSimple == "string" || typeSimple == "boolean" || typeSimple == "number")
{ {
parametersText.Append((string)parameter["name"] + ": " + (string)parameter["typeSimple"]); parametersText.Append((string)parameter["name"] + ": " + (string)parameter["typeSimple"]);
modelBindingExtraList.Add((string)parameter["name"]);
} }
else if (bindingSourceId == "modelBinding") else
{ {
var typeSimple = ""; parametersText.Append($"params = {{}} as {type}");
var type = ""; modelBindingExtra = ", params";
secondTypeList.Add(type);
var parameterNameOnMethod = (string)parameter["nameOnMethod"]; break;
var parametersOnMethod = action["parametersOnMethod"];
foreach (var parameterOnMethod in parametersOnMethod)
{
var parametersOnMethodName = (string)parameterOnMethod["name"];
if (parametersOnMethodName == parameterNameOnMethod)
{
typeSimple = (string)parameterOnMethod["typeSimple"];
var typeArray = ((string)parameterOnMethod["type"]).Split(".");
type = typeArray[typeArray.Length - 1];
}
}
if (typeSimple == "string" || typeSimple == "boolean" || typeSimple == "number")
{
parametersText.Append((string)parameter["name"] + ": " + (string)parameter["typeSimple"]);
modelBindingExtraList.Add((string)parameter["name"]);
}
else
{
parametersText.Append(string.Format("params = {{}} as {0}", type));
modelBindingExtra = ", params";
secondTypeList.Add(type);
break;
}
} }
} }
}
if (returnValueType != null) if (returnValueType != null)
{
if (returnValueType.IndexOf('<') > -1)
{ {
if (returnValueType.IndexOf('<') > -1) var firstTypeArray = returnValueType.Split("<")[0].Split(".");
{ var firstType = firstTypeArray[firstTypeArray.Length - 1];
var firstTypeArray = returnValueType.Split("<")[0].Split(".");
var firstType = firstTypeArray[firstTypeArray.Length - 1];
var secondTypeArray = returnValueType.Split("<")[1].Split(".");
var secondType = secondTypeArray[secondTypeArray.Length - 1].TrimEnd('>');
serviceFileText.AppendLine(string.Format(" {0}({1}): Observable<{2}<{3}>> {{", actionName, parametersText.ToString(), firstType, secondType)); var secondTypeArray = returnValueType.Split("<")[1].Split(".");
var secondType = secondTypeArray[secondTypeArray.Length - 1].TrimEnd('>');
firstTypeList.Add(firstType); serviceFileText.AppendLine(
secondTypeList.Add(secondType); $" {actionName}({parametersText}): Observable<{firstType}<{secondType}>> {{");
}
else
{
var typeArray = returnValueType.Split(".");
var type = typeArray[typeArray.Length - 1].TrimEnd('>');
if (type == "Void") firstTypeList.Add(firstType);
type = "void"; secondTypeList.Add(secondType);
}
else
{
var typeArray = returnValueType.Split(".");
var type = typeArray[typeArray.Length - 1].TrimEnd('>');
serviceFileText.AppendLine(string.Format(" {0}({1}): Observable<{2}> {{", actionName, parametersText.ToString(), type)); if (type == "Void")
secondTypeList.Add(type); {
type = "void";
} }
var modelIndex = CreateType(data, returnValueType, rootPath); serviceFileText.AppendLine(
$" {actionName}({parametersText}): Observable<{type}> {{");
if (!string.IsNullOrWhiteSpace(modelIndex))
modelIndexList.Add(modelIndex);
secondTypeList.Add(type);
} }
if (modelBindingExtraList != null && modelBindingExtraList.Count > 0) var modelIndex = CreateType(data, returnValueType, rootPath);
if (!IsNullOrWhiteSpace(modelIndex))
{ {
modelBindingExtra = ", params: { " + String.Join(", ", modelBindingExtraList.ToArray()) + " }"; modelIndexList.Add(modelIndex);
} }
}
var url = ((string)action["url"]).Replace("/{", "/${"); if (modelBindingExtraList != null && modelBindingExtraList.Count > 0)
var httpMethod = (string)action["httpMethod"]; {
serviceFileText.AppendLine(string.Format(" return this.restService.request({{ url: '/{0}', method: '{1}'{2}{3} }});", url, httpMethod, bodyExtra, modelBindingExtra)); modelBindingExtra = ", params: { " + Join(", ", modelBindingExtraList.ToArray()) + " }";
}
serviceFileText.AppendLine(" }");
}
serviceIndexList.Add(controllerServiceName.Replace(".ts", ""));
}
if (firstTypeList != null && firstTypeList.Count > 0) var url = ((string)action["url"]).Replace("/{", "/${");
{ var httpMethod = (string)action["httpMethod"];
var firstTypeListDistinct = ", " + String.Join(", ", firstTypeList.Where(p => p != "void").Distinct().ToArray()); serviceFileText.AppendLine(
serviceFileText.Replace("firstTypeList", string.Format("import {{ RestService {0}}} from '@abp/ng.core';", firstTypeListDistinct)); $" return this.restService.request({{ url: '/{url}', method: '{httpMethod}'{bodyExtra}{modelBindingExtra} }});");
}
else
{
serviceFileText.Replace("firstTypeList", "");
}
if (secondTypeList != null && secondTypeList.Count > 0) serviceFileText.AppendLine(" }");
{
var secondTypeListDistinct = String.Join(", ", secondTypeList.Where(p => p != "void").Distinct().ToArray());
serviceFileText.Replace("secondTypeList", string.Format("import {{{0}}} from '../models';", secondTypeListDistinct));
}
else
{
serviceFileText.Replace("secondTypeList", "");
} }
serviceFileText.AppendLine("}"); serviceIndexList.Add(controllerServiceName.Replace(".ts", ""));
}
if (firstTypeList != null && firstTypeList.Count > 0)
{
var firstTypeListDistinct = ", " + Join(", ", firstTypeList.Where(p => p != "void").Distinct().ToArray());
serviceFileText.Replace("[firstTypeList]",
$"import {{ RestService {firstTypeListDistinct}}} from '@abp/ng.core';");
}
else
{
serviceFileText.Replace("[firstTypeList]", "");
}
serviceFileText.Replace("[ControllerName]", controllerName); if (secondTypeList != null && secondTypeList.Count > 0)
System.IO.File.WriteAllText(string.Format("src/app/{0}/shared/services/{1}", rootPath, controllerServiceName), serviceFileText.ToString()); {
var secondTypeListDistinct = Join(", ", secondTypeList.Where(p => p != "void").Distinct().ToArray());
serviceFileText.Replace("[secondTypeList]",
$"import {{{secondTypeListDistinct}}} from '../models';");
} }
else
{
serviceFileText.Replace("[secondTypeList]", "");
}
//StringBuilder serviceIndexFileText = new StringBuilder(); serviceFileText.AppendLine("}");
//foreach (var serviceIndexItem in serviceIndexList) serviceFileText.Replace("[controllerName]", controllerName);
//{ File.WriteAllText($"src/app/{rootPath}/shared/services/{controllerServiceName}", serviceFileText.ToString());
// serviceIndexFileText.AppendLine(string.Format("export * from './{0}';", serviceIndexItem));
//}
//System.IO.File.WriteAllText(string.Format("src/app/{0}/shared/services/index.ts", rootPath), serviceIndexFileText.ToString()); var serviceIndexFileText = new StringBuilder();
//StringBuilder modelIndexFileText = new StringBuilder(); foreach (var serviceIndexItem in serviceIndexList)
{
serviceIndexFileText.AppendLine($"export * from './{serviceIndexItem}';");
}
File.WriteAllText($"src/app/{rootPath}/shared/services/index.ts", serviceIndexFileText.ToString());
//foreach (var modelIndexItem in modelIndexList) var modelIndexFileText = new StringBuilder();
//{
// modelIndexFileText.AppendLine(string.Format("export * from './{0}';", modelIndexItem)); foreach (var modelIndexItem in modelIndexList)
//} {
modelIndexFileText.AppendLine($"export * from './{modelIndexItem}';");
}
//System.IO.File.WriteAllText(string.Format("src/app/{0}/shared/models/index.ts", rootPath), modelIndexFileText.ToString()); File.WriteAllText($"src/app/{rootPath}/shared/models/index.ts", modelIndexFileText.ToString());
} }
catch (Exception ex) }
private Dictionary<string, string> GetCombinedModules(JToken data)
{
var moduleList = new Dictionary<string, string>();
foreach (var module in data["modules"])
{ {
throw; var rootPath = ((string)module.First["rootPath"]).ToLower();
if (moduleList.Any(p => p.Key == rootPath))
{
var value = moduleList[rootPath];
moduleList[rootPath] = value.TrimEnd('}') + "," + module.First["controllers"].ToString().TrimStart('{');
}
else
{
moduleList.Add(rootPath, module.First["controllers"].ToString());
}
} }
return moduleList;
} }
private string CreateType(JObject data, string returnValueType, string rootPath) private string CreateType(JObject data, string returnValueType, string rootPath)
@ -290,59 +304,63 @@ namespace Volo.Abp.Cli.Commands
|| returnValueType.Contains("IStringValueType") || returnValueType.Contains("IStringValueType")
|| returnValueType.Contains("IValueValidator") || returnValueType.Contains("IValueValidator")
) )
{
return null; return null;
}
var typeNameSplit = returnValueType.Split("."); var typeNameSplit = returnValueType.Split(".");
var typeName = typeNameSplit[typeNameSplit.Length - 1]; var typeName = typeNameSplit[typeNameSplit.Length - 1];
var typeModelName = typeName.Replace("<", "").Replace(">", "").PascalToKebabCase() + ".ts"; var typeModelName = typeName.Replace("<", "").Replace(">", "").PascalToKebabCase() + ".ts";
var path = string.Format("src/app/{0}/shared/models/{1}", rootPath, typeModelName); var path = $"src/app/{rootPath}/shared/models/{typeModelName}";
if (File.Exists(path)) if (File.Exists(path))
{
return null; return null;
}
var modelFileText = new StringBuilder();
StringBuilder modelFileText = new StringBuilder();
var baseType = (string)type["baseType"]; var baseType = (string)type["baseType"];
var extends = ""; var extends = "";
if (!string.IsNullOrWhiteSpace(baseType) && baseType != "System.Enum") if (!IsNullOrWhiteSpace(baseType) && baseType != "System.Enum")
{ {
var baseTypeSplit = baseType.Split("."); var baseTypeSplit = baseType.Split(".");
var baseTypeName = baseTypeSplit[baseTypeSplit.Length - 1].Replace("<", "").Replace(">", ""); var baseTypeName = baseTypeSplit[baseTypeSplit.Length - 1].Replace("<", "").Replace(">", "");
var baseTypeKebabCase = "./" + baseTypeName.PascalToKebabCase(); var baseTypeKebabCase = "./" + baseTypeName.PascalToKebabCase();
if (baseType.Contains("Volo.Abp.Application.Dtos.EntityDto")) if (baseType.Contains("Volo.Abp.Application.Dtos.EntityDto"))
{
baseTypeKebabCase = "@abp/ng.core"; baseTypeKebabCase = "@abp/ng.core";
}
if (baseTypeName.Contains("guid") || baseTypeName.Contains("Guid")) if (baseTypeName.Contains("guid") || baseTypeName.Contains("Guid"))
{
baseTypeName = "string"; baseTypeName = "string";
}
modelFileText.AppendLine(string.Format("import {{ {0} }} from '{1}';", baseTypeName, baseTypeKebabCase)); modelFileText.AppendLine($"import {{ {baseTypeName} }} from '{baseTypeKebabCase}';");
extends = "extends " + baseTypeName; extends = "extends " + baseTypeName;
} }
if (baseType == "System.Enum" && (string)type.First["isEnum"] == "True") if (baseType == "System.Enum" && (string)type.First["isEnum"] == "True")
{ {
modelFileText.AppendLine(string.Format("export enum {0} {{", typeName)); modelFileText.AppendLine($"export enum {typeName} {{");
var enumDictionary = new Dictionary<string, string>();
var enumNameList = type.First["enumNames"].ToArray(); var enumNameList = type.First["enumNames"].ToArray();
var enumValueList = type.First["enumValues"].ToArray(); var enumValueList = type.First["enumValues"].ToArray();
for (int i = 0; i < enumNameList.Length; i++) for (var i = 0; i < enumNameList.Length; i++)
{ {
modelFileText.AppendLine(string.Format("{0} = {1},", enumNameList[i], enumValueList[i])); modelFileText.AppendLine($"{enumNameList[i]} = {enumValueList[i]},");
} }
modelFileText.AppendLine("}"); modelFileText.AppendLine("}");
} }
else else
{ {
modelFileText.AppendLine(string.Format("export class {0} {1} {{", typeName, extends)); modelFileText.AppendLine($"export class {typeName} {extends} {{");
foreach (var property in type["properties"]) foreach (var property in type["properties"])
{ {
@ -352,7 +370,9 @@ namespace Volo.Abp.Cli.Commands
var typeSimple = (string)property["typeSimple"]; var typeSimple = (string)property["typeSimple"];
if (typeSimple.IndexOf("[") > -1 && typeSimple.IndexOf("]") > -1) if (typeSimple.IndexOf("[") > -1 && typeSimple.IndexOf("]") > -1)
{
typeSimple = typeSimple.Replace("[", "").Replace("]", "") + "[]"; typeSimple = typeSimple.Replace("[", "").Replace("]", "") + "[]";
}
if (typeSimple.StartsWith("Volo.Abp")) if (typeSimple.StartsWith("Volo.Abp"))
{ {
@ -382,14 +402,14 @@ namespace Volo.Abp.Cli.Commands
typeSimple = "any" + (typeSimple.Contains("[]") ? "[]" : ""); typeSimple = "any" + (typeSimple.Contains("[]") ? "[]" : "");
} }
modelFileText.AppendLine(string.Format(" {0}: {1};", propertyName, typeSimple)); modelFileText.AppendLine($" {propertyName}: {typeSimple};");
} }
modelFileText.AppendLine(""); modelFileText.AppendLine("");
modelFileText.AppendLine(string.Format(" constructor(initialValues: Partial<{0}> = {{}}) {{", typeName)); modelFileText.AppendLine($" constructor(initialValues: Partial<{typeName}> = {{}}) {{");
if (!string.IsNullOrWhiteSpace(baseType)) if (!IsNullOrWhiteSpace(baseType))
{ {
modelFileText.AppendLine(" super(initialValues);"); modelFileText.AppendLine(" super(initialValues);");
modelFileText.AppendLine(" }"); modelFileText.AppendLine(" }");
@ -410,7 +430,7 @@ namespace Volo.Abp.Cli.Commands
modelFileText.AppendLine("}"); modelFileText.AppendLine("}");
} }
System.IO.File.WriteAllText(string.Format("src/app/{0}/shared/models/{1}", rootPath, typeModelName), modelFileText.ToString()); File.WriteAllText($"src/app/{rootPath}/shared/models/{typeModelName}", modelFileText.ToString());
return typeModelName.Replace(".ts", ""); return typeModelName.Replace(".ts", "");
} }
@ -478,7 +498,7 @@ namespace Volo.Abp.Cli.Commands
{ {
public static string PascalToKebabCase(this string value) public static string PascalToKebabCase(this string value)
{ {
if (string.IsNullOrEmpty(value)) if (IsNullOrEmpty(value))
return value; return value;
return Regex.Replace( return Regex.Replace(

Loading…
Cancel
Save