From d19171d7bd9500934bcdad2761c9403ec2df4ba3 Mon Sep 17 00:00:00 2001 From: Arkat Erol Date: Tue, 17 Mar 2020 12:08:46 +0300 Subject: [PATCH] types' basetype properties removed --- .../Abp/Cli/Commands/GenerateProxyCommand.cs | 167 ++++++++++++------ .../Properties/launchSettings.json | 5 +- .../Volo.Abp.Core/System/AbpTypeExtensions.cs | 5 + .../MethodParameterApiDescriptionModel.cs | 9 +- 4 files changed, 128 insertions(+), 58 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/GenerateProxyCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/GenerateProxyCommand.cs index e16f2c6c64..b792e591c6 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/GenerateProxyCommand.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/GenerateProxyCommand.cs @@ -11,12 +11,14 @@ using Volo.Abp.Cli.ProjectBuilding; using Volo.Abp.Cli.ProjectBuilding.Building; using Volo.Abp.DependencyInjection; using System.Collections.Generic; -using System.Linq; +using System.Linq; +using System.Net; namespace Volo.Abp.Cli.Commands { public class GenerateProxyCommand : IConsoleCommand, ITransientDependency { + public static Dictionary> propertyList = new Dictionary>(); public ILogger Logger { get; set; } protected TemplateProjectBuilder TemplateProjectBuilder { get; } @@ -30,13 +32,34 @@ namespace Volo.Abp.Cli.Commands public async Task ExecuteAsync(CommandLineArgs commandLineArgs) { + var angularPath = $"angular.json"; + if (!File.Exists(angularPath)) + { + throw new CliUsageException( + "angular.json file not found. You must run this command in angular folder." + + Environment.NewLine + Environment.NewLine + + GetUsageInfo() + ); + } + + var module = commandLineArgs.Options.GetOrNull(Options.Module.Short, Options.Module.Long); + module = module == null ? "app" : module.ToLower(); + var apiUrl = commandLineArgs.Options.GetOrNull(Options.ApiUrl.Short, Options.ApiUrl.Long); + if (string.IsNullOrWhiteSpace(apiUrl)) + { + var environmentJson = File.ReadAllText("src/environments/environment.ts").Split("export const environment = ")[1].Replace(";", " "); + var environment = JObject.Parse(environmentJson); + apiUrl = environment["apis"]["default"]["url"].ToString(); + } + apiUrl += "/api/abp/api-definition?IncludeTypes=true"; + var uiFramework = GetUiFramework(commandLineArgs); - //WebClient client = new WebClient(); - //string json = client.DownloadString(apiUrl); - var sr = File.OpenText("api-definition.json"); - var json = sr.ReadToEnd(); + WebClient client = new WebClient(); + string json = client.DownloadString(apiUrl); + //var sr = File.OpenText("api-definition.json"); + //var json = sr.ReadToEnd(); Logger.LogInformation("Downloading api definition..."); Logger.LogInformation("Api Url: " + apiUrl); @@ -44,15 +67,24 @@ namespace Volo.Abp.Cli.Commands var data = JObject.Parse(json); Logger.LogInformation("Modules are combining"); - var moduleList = GetCombinedModules(data); + var moduleList = GetCombinedModules(data, module); + + if (moduleList.Count < 1) + { + throw new CliUsageException( + "Module can not find!" + + Environment.NewLine + Environment.NewLine + + GetUsageInfo() + ); + } Logger.LogInformation("Modules and types are creating"); - foreach (var module in moduleList) + foreach (var moduleItem in moduleList) { - var moduleValue = JObject.Parse(module.Value); + var moduleValue = JObject.Parse(moduleItem.Value); - var rootPath = module.Key; + var rootPath = moduleItem.Key; Logger.LogInformation($"{rootPath} directory is creating"); @@ -111,7 +143,6 @@ namespace Volo.Abp.Cli.Commands var isOptional = (bool)parameter["isOptional"]; var defaultValue = (string)parameter["defaultValue"]; - var modelIndex = CreateType(data, (string)parameter["type"], rootPath, modelIndexList); if (!string.IsNullOrWhiteSpace(modelIndex)) @@ -311,30 +342,36 @@ namespace Volo.Abp.Cli.Commands } } - private Dictionary GetCombinedModules(JToken data) + private Dictionary GetCombinedModules(JToken data, string module) { var moduleList = new Dictionary(); - foreach (var module in data["modules"]) + + foreach (var moduleItem in data["modules"]) { - var rootPath = ((string)module.First["rootPath"]).ToLower(); + var rootPath = ((string)moduleItem.First["rootPath"]).ToLower(); if (moduleList.Any(p => p.Key == rootPath)) { var value = moduleList[rootPath]; - moduleList[rootPath] = value.TrimEnd('}') + "," + module.First["controllers"].ToString().TrimStart('{'); + moduleList[rootPath] = value.TrimEnd('}') + "," + moduleItem.First["controllers"].ToString().TrimStart('{'); } else { - moduleList.Add(rootPath, module.First["controllers"].ToString()); + moduleList.Add(rootPath, moduleItem.First["controllers"].ToString()); } } + if (module != "all") + { + moduleList = moduleList.Where(p => p.Key.ToLower() == module).ToDictionary(p => p.Key, s => s.Value); + } + return moduleList; } private static string CreateType(JObject data, string returnValueType, string rootPath, List modelIndexList) - { + { var type = data["types"][returnValueType]; if (type == null) @@ -357,46 +394,46 @@ namespace Volo.Abp.Cli.Commands var typeNameSplit = returnValueType.Split("."); var typeName = typeNameSplit[typeNameSplit.Length - 1]; - var typeModelName = typeName.Replace("<", "").Replace(">", "").PascalToKebabCase() + ".ts"; + var typeModelName = typeName.Replace("<", "").Replace(">", "").PascalToKebabCase() + ".ts"; - var path = $"src/app/{rootPath}/shared/models/{typeModelName}"; - if (File.Exists(path)) - { - return null; - } + var path = $"src/app/{rootPath}/shared/models/{typeModelName}"; var modelFileText = new StringBuilder(); var baseType = (string)type["baseType"]; var extends = ""; var customBaseTypeName = ""; + var baseTypeName = ""; - if (!string.IsNullOrWhiteSpace(baseType) && baseType != "System.Enum") + if (!string.IsNullOrWhiteSpace(baseType)) { var baseTypeSplit = baseType.Split("."); - var baseTypeName = baseTypeSplit[baseTypeSplit.Length - 1].Replace("<", "").Replace(">", ""); + baseTypeName = baseTypeSplit[baseTypeSplit.Length - 1].Replace("<", "").Replace(">", ""); var baseTypeKebabCase = "./" + baseTypeName.PascalToKebabCase(); - if (baseType.Contains("Volo.Abp.Application.Dtos")) + if (baseType != "System.Enum") { - baseTypeKebabCase = "@abp/ng.core"; + if (baseType.Contains("Volo.Abp.Application.Dtos")) + { + baseTypeKebabCase = "@abp/ng.core"; - baseTypeName = baseType.Split("Volo.Abp.Application.Dtos")[1].Split("<")[0].TrimStart('.'); - customBaseTypeName = baseType.Split("Volo.Abp.Application.Dtos")[1].Replace("System.Guid", "string").TrimStart('.'); - } + baseTypeName = baseType.Split("Volo.Abp.Application.Dtos")[1].Split("<")[0].TrimStart('.'); + customBaseTypeName = baseType.Split("Volo.Abp.Application.Dtos")[1].Replace("System.Guid", "string").TrimStart('.'); + } - if (baseTypeName.Contains("guid") || baseTypeName.Contains("Guid")) - { - baseTypeName = "string"; - } + if (baseTypeName.Contains("guid") || baseTypeName.Contains("Guid")) + { + baseTypeName = "string"; + } - modelFileText.AppendLine($"import {{ {baseTypeName} }} from '{baseTypeKebabCase}';"); - extends = "extends " + (!string.IsNullOrWhiteSpace(customBaseTypeName) ? customBaseTypeName : baseTypeName); + modelFileText.AppendLine($"import {{ {baseTypeName} }} from '{baseTypeKebabCase}';"); + extends = "extends " + (!string.IsNullOrWhiteSpace(customBaseTypeName) ? customBaseTypeName : baseTypeName); - var modelIndex = CreateType(data, baseType, rootPath, modelIndexList); - if (!string.IsNullOrWhiteSpace(modelIndex)) - { - modelIndexList.Add(modelIndex); + var modelIndex = CreateType(data, baseType, rootPath, modelIndexList); + if (!string.IsNullOrWhiteSpace(modelIndex)) + { + modelIndexList.Add(modelIndex); + } } } @@ -422,21 +459,14 @@ namespace Volo.Abp.Cli.Commands foreach (var property in type["properties"]) { var propertyName = (string)property["name"]; - propertyName = (char.ToLower(propertyName[0]) + propertyName.Substring(1)); - - var modelIndex = CreateType(data, (string)property["type"], rootPath, modelIndexList); - - if (!string.IsNullOrWhiteSpace(modelIndex)) + if (propertyName == "RoleNames") { - var propertyTypeSplit = ((string)property["type"]).Split("."); - var propertyType = propertyTypeSplit[propertyTypeSplit.Length - 1]; - modelFileText.Insert(0,""); - modelFileText.Insert(0, $"import {{ {propertyType} }} from '../models';"); - modelFileText.Insert(0, ""); - modelIndexList.Add(modelIndex); + } + propertyName = (char.ToLower(propertyName[0]) + propertyName.Substring(1)); + var typeSimple = (string)property["typeSimple"]; - var typeSimple = (string)property["typeSimple"]; + var modelIndex = CreateType(data, (string)property["type"], rootPath, modelIndexList); if (typeSimple.IndexOf("[") > -1 && typeSimple.IndexOf("]") > -1) { @@ -471,6 +501,32 @@ namespace Volo.Abp.Cli.Commands typeSimple = "any" + (typeSimple.Contains("[]") ? "[]" : ""); } + if (propertyList.Any(p => p.Key == baseTypeName && p.Value.Any(q => q.Key == propertyName && q.Value == typeSimple))) + { + continue; + } + + if (!string.IsNullOrWhiteSpace(modelIndex)) + { + var propertyTypeSplit = ((string)property["type"]).Split("."); + var propertyType = propertyTypeSplit[propertyTypeSplit.Length - 1]; + modelFileText.Insert(0, ""); + modelFileText.Insert(0, $"import {{ {propertyType} }} from '../models';"); + modelFileText.Insert(0, ""); + modelIndexList.Add(modelIndex); + } + + if (propertyList.Any(p => p.Key == typeName && !p.Value.Any(q => q.Key == propertyName))) + { + propertyList[typeName].Add(propertyName, typeSimple); + } + else if (!propertyList.Any(p => p.Key == typeName)) + { + var newProperty = new Dictionary(); + newProperty.Add(propertyName, typeSimple); + propertyList.Add(typeName, newProperty); + } + modelFileText.AppendLine($" {propertyName}: {typeSimple};"); } @@ -554,10 +610,11 @@ namespace Volo.Abp.Cli.Commands sb.AppendLine("Options:"); sb.AppendLine(""); sb.AppendLine("-u|--ui (default: angular)"); + sb.AppendLine("-m|--module (default: app)"); sb.AppendLine(""); sb.AppendLine("Examples:"); sb.AppendLine(""); - sb.AppendLine(" abp new --apiUrl https://www.abp.io/api/abp/api-definition?types=true"); + sb.AppendLine(" abp new --apiUrl https://www.abp.io"); sb.AppendLine(""); sb.AppendLine("See the documentation for more info: https://docs.abp.io/en/abp/latest/CLI"); @@ -587,6 +644,12 @@ namespace Volo.Abp.Cli.Commands public static class Options { + public static class Module + { + public const string Short = "m"; + public const string Long = "module"; + } + public static class ApiUrl { public const string Short = "au"; diff --git a/framework/src/Volo.Abp.Cli/Properties/launchSettings.json b/framework/src/Volo.Abp.Cli/Properties/launchSettings.json index 4182e562b2..b089ad5d59 100644 --- a/framework/src/Volo.Abp.Cli/Properties/launchSettings.json +++ b/framework/src/Volo.Abp.Cli/Properties/launchSettings.json @@ -1,9 +1,8 @@ { "profiles": { - "Volo.Abp.Cli": { + "Volo.Abp.Cli": { "commandName": "Project", - "commandLineArgs": "generate-proxy --apiUrl https://www.abp.io/api/abp/api-definition?types=true -u angular" - "commandName": "Project" + "commandLineArgs": "generate-proxy -u angular -m all" } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.Core/System/AbpTypeExtensions.cs b/framework/src/Volo.Abp.Core/System/AbpTypeExtensions.cs index fe23c77530..e7c600eff6 100644 --- a/framework/src/Volo.Abp.Core/System/AbpTypeExtensions.cs +++ b/framework/src/Volo.Abp.Core/System/AbpTypeExtensions.cs @@ -6,6 +6,11 @@ namespace System { public static class AbpTypeExtensions { + public static string GetFullNameWithAssemblyName(this Type type) + { + return type.FullName + ", " + type.Assembly.GetName().Name; + } + /// /// Determines whether an instance of this type can be assigned to /// an instance of the . diff --git a/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/MethodParameterApiDescriptionModel.cs b/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/MethodParameterApiDescriptionModel.cs index 47145adaaf..4199e29ec0 100644 --- a/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/MethodParameterApiDescriptionModel.cs +++ b/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/MethodParameterApiDescriptionModel.cs @@ -8,6 +8,8 @@ namespace Volo.Abp.Http.Modeling { public string Name { get; set; } + public string TypeAsString { get; set; } + public string Type { get; set; } public string TypeSimple { get; set; } @@ -18,7 +20,7 @@ namespace Volo.Abp.Http.Modeling private MethodParameterApiDescriptionModel() { - + } public static MethodParameterApiDescriptionModel Create(ParameterInfo parameterInfo) @@ -26,8 +28,9 @@ namespace Volo.Abp.Http.Modeling return new MethodParameterApiDescriptionModel { Name = parameterInfo.Name, - Type = ModelingTypeHelper.GetFullNameHandlingNullableAndGenerics(parameterInfo.ParameterType), - TypeSimple = ModelingTypeHelper.GetSimplifiedName(parameterInfo.ParameterType), + TypeAsString = parameterInfo.ParameterType.GetFullNameWithAssemblyName(), + Type = parameterInfo.ParameterType != null ? ModelingTypeHelper.GetFullNameHandlingNullableAndGenerics(parameterInfo.ParameterType) : null, + TypeSimple = parameterInfo.ParameterType != null ? ModelingTypeHelper.GetSimplifiedName(parameterInfo.ParameterType) : null, IsOptional = parameterInfo.IsOptional, DefaultValue = parameterInfo.HasDefaultValue ? parameterInfo.DefaultValue : null };