Browse Source

Update generate proxy command

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

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

@ -49,43 +49,66 @@ namespace Volo.Abp.Cli.Commands
Logger.LogInformation("Downloading api definition..."); Logger.LogInformation("Downloading api definition...");
Logger.LogInformation("Api Url: " + apiUrl); Logger.LogInformation("Api Url: " + apiUrl);
JObject data = JObject.Parse(json); JObject data = JObject.Parse(json);
string rootPath = (string)data["modules"]["app"]["rootPath"];
System.IO.Directory.CreateDirectory(string.Format("src/app/{0}/shared/models", rootPath));
System.IO.Directory.CreateDirectory(string.Format("src/app/{0}/shared/services", rootPath));
var serviceIndexList = new List<string>(); var serviceIndexList = new List<string>();
var modelIndexList = new List<string>();
var moduleList = new Dictionary<string, string>();
foreach (var module in data["modules"]) foreach (var module in data["modules"])
{ {
foreach (var item in module.First["controllers"]) string rootPath = ((string)module.First["rootPath"]).ToLower();
if (moduleList.Any(p => p.Key == rootPath))
{ {
var controller = item.First; var value = moduleList[rootPath];
var controllerName = (string)controller["controllerName"]; moduleList[rootPath] = value.TrimEnd('}') + "," + module.First["controllers"].ToString().TrimStart('{');
var controllerServiceName = controllerName.PascalToKebabCase() + ".service.ts"; }
else {
moduleList.Add(rootPath, module.First["controllers"].ToString());
}
}
foreach (var module in moduleList)
{
JObject moduleValue = JObject.Parse(module.Value);
StringBuilder serviceFileText = new StringBuilder(); string rootPath = module.Key;
var controllerName = "";
var controllerServiceName = "";
serviceFileText.AppendLine("firstTypeList"); Directory.CreateDirectory(string.Format("src/app/{0}/shared/models", rootPath));
serviceFileText.AppendLine("import { Injectable } from '@angular/core';"); Directory.CreateDirectory(string.Format("src/app/{0}/shared/services", rootPath));
serviceFileText.AppendLine("import { Observable } from 'rxjs';");
serviceFileText.AppendLine("secondTypeList");
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>(); StringBuilder serviceFileText = new StringBuilder();
var secondTypeList = new List<string>();
foreach (var controllerItem in controller["actions"]) serviceFileText.AppendLine("firstTypeList");
serviceFileText.AppendLine("import { Injectable } from '@angular/core';");
serviceFileText.AppendLine("import { Observable } from 'rxjs';");
serviceFileText.AppendLine("secondTypeList");
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>();
var secondTypeList = new List<string>();
foreach (var item in moduleValue.Root.ToList())
{
var controller = item.First;
controllerName = (string)controller["controllerName"];
controllerServiceName = controllerName.PascalToKebabCase() + ".service.ts";
foreach (var actionItem in controller["actions"])
{ {
var action = controllerItem.First; var action = actionItem.First;
var actionName = (string)action["name"]; var actionName = (string)action["uniqueName"];
actionName = (char.ToLower(actionName[0]) + actionName.Substring(1)).Replace("Async", "").Replace("Controller", ""); actionName = (char.ToLower(actionName[0]) + actionName.Substring(1)).Replace("Async", "").Replace("Controller", "");
@ -182,6 +205,12 @@ namespace Volo.Abp.Cli.Commands
serviceFileText.AppendLine(string.Format(" {0}({1}): Observable<{2}> {{", actionName, parametersText.ToString(), type)); serviceFileText.AppendLine(string.Format(" {0}({1}): Observable<{2}> {{", actionName, parametersText.ToString(), type));
secondTypeList.Add(type); secondTypeList.Add(type);
} }
var modelIndex = CreateType(data, returnValueType, rootPath);
if (!string.IsNullOrWhiteSpace(modelIndex))
modelIndexList.Add(modelIndex);
} }
if (modelBindingExtraList != null && modelBindingExtraList.Count > 0) if (modelBindingExtraList != null && modelBindingExtraList.Count > 0)
@ -194,125 +223,196 @@ namespace Volo.Abp.Cli.Commands
serviceFileText.AppendLine(string.Format(" return this.restService.request({{ url: '/{0}', method: '{1}'{2}{3} }});", url, httpMethod, bodyExtra, modelBindingExtra)); serviceFileText.AppendLine(string.Format(" return this.restService.request({{ url: '/{0}', method: '{1}'{2}{3} }});", url, httpMethod, bodyExtra, modelBindingExtra));
serviceFileText.AppendLine(" }"); serviceFileText.AppendLine(" }");
} }
serviceFileText.AppendLine("}"); serviceIndexList.Add(controllerServiceName.Replace(".ts", ""));
}
if (firstTypeList != null && firstTypeList.Count > 0) if (firstTypeList != null && firstTypeList.Count > 0)
{ {
var firstTypeListDistinct = ", " + String.Join(", ", firstTypeList.Where(p => p != "void").Distinct().ToArray()); var firstTypeListDistinct = ", " + String.Join(", ", firstTypeList.Where(p => p != "void").Distinct().ToArray());
serviceFileText.Replace("firstTypeList", string.Format("import {{ RestService {0}}} from '@abp/ng.core';", firstTypeListDistinct)); serviceFileText.Replace("firstTypeList", string.Format("import {{ RestService {0}}} from '@abp/ng.core';", firstTypeListDistinct));
} }
else else
{ {
serviceFileText.Replace("firstTypeList", ""); serviceFileText.Replace("firstTypeList", "");
} }
if (secondTypeList != null && secondTypeList.Count > 0) if (secondTypeList != null && secondTypeList.Count > 0)
{ {
var secondTypeListDistinct = String.Join(", ", secondTypeList.Where(p => p != "void").Distinct().ToArray()); var secondTypeListDistinct = String.Join(", ", secondTypeList.Where(p => p != "void").Distinct().ToArray());
serviceFileText.Replace("secondTypeList", string.Format("import {{{0}}} from '../models';", secondTypeListDistinct)); serviceFileText.Replace("secondTypeList", string.Format("import {{{0}}} from '../models';", secondTypeListDistinct));
} }
else else
{ {
serviceFileText.Replace("secondTypeList", ""); serviceFileText.Replace("secondTypeList", "");
} }
System.IO.File.WriteAllText(string.Format("src/app/{0}/shared/services/{1}", rootPath, controllerServiceName), serviceFileText.ToString()); serviceFileText.AppendLine("}");
serviceIndexList.Add(controllerServiceName.Replace(".ts", "")); serviceFileText.Replace("[ControllerName]", controllerName);
} System.IO.File.WriteAllText(string.Format("src/app/{0}/shared/services/{1}", rootPath, controllerServiceName), serviceFileText.ToString());
} }
StringBuilder serviceIndexFileText = new StringBuilder(); //StringBuilder serviceIndexFileText = new StringBuilder();
foreach (var serviceIndexItem in serviceIndexList) //foreach (var serviceIndexItem in serviceIndexList)
{ //{
serviceIndexFileText.AppendLine(string.Format("export * from './{0}';", serviceIndexItem)); // serviceIndexFileText.AppendLine(string.Format("export * from './{0}';", serviceIndexItem));
} //}
System.IO.File.WriteAllText(string.Format("src/app/{0}/shared/services/index.ts", rootPath), serviceIndexFileText.ToString()); //System.IO.File.WriteAllText(string.Format("src/app/{0}/shared/services/index.ts", rootPath), serviceIndexFileText.ToString());
var modelIndexList = new List<string>(); //StringBuilder modelIndexFileText = new StringBuilder();
foreach (var type in data["types"]) //foreach (var modelIndexItem in modelIndexList)
{ //{
var typeFullName = ((JProperty)type.First.Parent).Name; // modelIndexFileText.AppendLine(string.Format("export * from './{0}';", modelIndexItem));
//}
//System.IO.File.WriteAllText(string.Format("src/app/{0}/shared/models/index.ts", rootPath), modelIndexFileText.ToString());
}
catch (Exception ex)
{
throw;
}
}
var typeNameSplit = typeFullName.Split("."); private string CreateType(JObject data, string returnValueType, string rootPath)
var typeName = typeNameSplit[typeNameSplit.Length - 1]; {
var type = data["types"][returnValueType];
var typeModelName = typeName.Replace("<","").Replace(">", "").PascalToKebabCase() + ".ts"; if (returnValueType.StartsWith("Volo.Abp.Application.Dtos")
|| returnValueType.StartsWith("System.Collections")
|| returnValueType == "System.String"
|| returnValueType == "System.Void"
|| returnValueType.Contains("IActionResult")
|| returnValueType.Contains("IStringValueType")
|| returnValueType.Contains("IValueValidator")
)
return null;
StringBuilder modelFileText = new StringBuilder(); var typeNameSplit = returnValueType.Split(".");
var typeName = typeNameSplit[typeNameSplit.Length - 1];
var typeModelName = typeName.Replace("<", "").Replace(">", "").PascalToKebabCase() + ".ts";
var baseType = (string)type.First["baseType"]; var path = string.Format("src/app/{0}/shared/models/{1}", rootPath, typeModelName);
var extends = ""; if (File.Exists(path))
return null;
if (!string.IsNullOrWhiteSpace(baseType))
{
var baseTypeSplit = baseType.Split(".");
var baseTypeName = baseTypeSplit[baseTypeSplit.Length - 1];
var baseTypeKebabCase = baseTypeName.PascalToKebabCase();
modelFileText.AppendLine(string.Format("import {{ {0} }} from '@abp/ng.core';", baseTypeName, baseTypeKebabCase)); StringBuilder modelFileText = new StringBuilder();
extends = "extends " + baseTypeName;
}
modelFileText.AppendLine(string.Format("export class {0} {1} {{", typeName, extends));
foreach (var property in type.First["properties"]) var baseType = (string)type["baseType"];
{ var extends = "";
var propertyName = (string)property["name"];
propertyName = (char.ToLower(propertyName[0]) + propertyName.Substring(1));
var typeSimple = (string)property["typeSimple"]; if (!string.IsNullOrWhiteSpace(baseType) && baseType != "System.Enum")
{
var baseTypeSplit = baseType.Split(".");
var baseTypeName = baseTypeSplit[baseTypeSplit.Length - 1].Replace("<", "").Replace(">", "");
var baseTypeKebabCase = "./" + baseTypeName.PascalToKebabCase();
modelFileText.AppendLine(string.Format(" {0}: {1};", propertyName, typeSimple)); if (baseType.Contains("Volo.Abp.Application.Dtos.EntityDto"))
} baseTypeKebabCase = "@abp/ng.core";
modelFileText.AppendLine(""); if (baseTypeName.Contains("guid") || baseTypeName.Contains("Guid"))
baseTypeName = "string";
modelFileText.AppendLine(string.Format(" constructor(initialValues: Partial<{0}> = {{}}) {{", typeName)); modelFileText.AppendLine(string.Format("import {{ {0} }} from '{1}';", baseTypeName, baseTypeKebabCase));
extends = "extends " + baseTypeName;
}
if (baseType == "System.Enum" && (string)type.First["isEnum"] == "True")
{
modelFileText.AppendLine(string.Format("export enum {0} {{", typeName));
var enumDictionary = new Dictionary<string, string>();
var enumNameList = type.First["enumNames"].ToArray();
var enumValueList = type.First["enumValues"].ToArray();
if (!string.IsNullOrWhiteSpace(baseType)) for (int i = 0; i < enumNameList.Length; i++)
{
modelFileText.AppendLine(string.Format("{0} = {1},", enumNameList[i], enumValueList[i]));
}
modelFileText.AppendLine("}");
}
else
{
modelFileText.AppendLine(string.Format("export class {0} {1} {{", typeName, extends));
foreach (var property in type["properties"])
{
var propertyName = (string)property["name"];
propertyName = (char.ToLower(propertyName[0]) + propertyName.Substring(1));
var typeSimple = (string)property["typeSimple"];
if (typeSimple.IndexOf("[") > -1 && typeSimple.IndexOf("]") > -1)
typeSimple = typeSimple.Replace("[", "").Replace("]", "") + "[]";
if (typeSimple.StartsWith("Volo.Abp"))
{ {
modelFileText.AppendLine(" super(initialValues);"); var typeSimpleSplit = typeSimple.Split(".");
typeSimple = typeSimpleSplit[typeSimpleSplit.Length - 1];
} }
else
if (typeSimple.StartsWith("System.Object"))
{ {
modelFileText.AppendLine(" if (initialValues) {"); typeSimple = typeSimple.Replace("System.Object", "object");
modelFileText.AppendLine(" for (const key in initialValues) {");
modelFileText.AppendLine(" if (initialValues.hasOwnProperty(key)) {");
modelFileText.AppendLine(" this[key] = initialValues[key];");
modelFileText.AppendLine(" }");
modelFileText.AppendLine(" }");
modelFileText.AppendLine(" }");
modelFileText.AppendLine(" }");
} }
modelFileText.AppendLine("}"); if (typeSimple.Contains("?"))
{
typeSimple = typeSimple.Replace("?", "");
propertyName += "?";
}
System.IO.File.WriteAllText(string.Format("src/app/{0}/shared/models/{1}", rootPath, typeModelName), modelFileText.ToString()); if (typeSimple != "boolean"
&& typeSimple != "string"
&& typeSimple != "number"
&& typeSimple != "boolean[]"
&& typeSimple != "string[]"
&& typeSimple != "number[]"
)
{
typeSimple = "any" + (typeSimple.Contains("[]") ? "[]" : "");
}
modelIndexList.Add(typeModelName.Replace(".ts", "")); modelFileText.AppendLine(string.Format(" {0}: {1};", propertyName, typeSimple));
} }
StringBuilder modelIndexFileText = new StringBuilder(); modelFileText.AppendLine("");
modelFileText.AppendLine(string.Format(" constructor(initialValues: Partial<{0}> = {{}}) {{", typeName));
foreach (var modelIndexItem in modelIndexList) if (!string.IsNullOrWhiteSpace(baseType))
{ {
modelIndexFileText.AppendLine(string.Format("export * from './{0}';", modelIndexItem)); modelFileText.AppendLine(" super(initialValues);");
modelFileText.AppendLine(" }");
}
else
{
modelFileText.AppendLine(" if (initialValues) {");
modelFileText.AppendLine(" for (const key in initialValues) {");
modelFileText.AppendLine(" if (initialValues.hasOwnProperty(key)) {");
modelFileText.AppendLine(" this[key] = initialValues[key];");
modelFileText.AppendLine(" }");
modelFileText.AppendLine(" }");
modelFileText.AppendLine(" }");
modelFileText.AppendLine(" }");
} }
System.IO.File.WriteAllText(string.Format("src/app/{0}/shared/models/index.ts", rootPath), modelIndexFileText.ToString()); modelFileText.AppendLine("}");
}
catch (Exception ex)
{
throw;
} }
System.IO.File.WriteAllText(string.Format("src/app/{0}/shared/models/{1}", rootPath, typeModelName), modelFileText.ToString());
return typeModelName.Replace(".ts", "");
} }
public string GetUsageInfo() public string GetUsageInfo()

Loading…
Cancel
Save