From 66283771374b24cbe6b3e6cd9461211581848eae Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Tue, 24 Aug 2021 14:07:10 +0800 Subject: [PATCH] Abstract IServiceProxyGenerator --- .../Volo/Abp/Cli/AbpCliCoreModule.cs | 9 ++ .../Abp/Cli/Commands/GenerateProxyCommand.cs | 12 +- .../Volo/Abp/Cli/Commands/ProxyCommandBase.cs | 125 ++++++------------ .../Abp/Cli/Commands/RemoveProxyCommand.cs | 14 +- .../ServiceProxy/AbpCliServiceProxyOptions.cs | 15 +++ .../Angular/AngularServiceProxyGenerator.cs | 114 ++++++++++++++++ .../Abp/Cli/ServiceProxy/GenerateProxyArgs.cs | 30 +++++ .../ServiceProxy/IServiceProxyGenerator.cs | 9 ++ .../JavaScript/JavaScriptProxyGenerator.cs | 15 +++ 9 files changed, 252 insertions(+), 91 deletions(-) create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/AbpCliServiceProxyOptions.cs create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/Angular/AngularServiceProxyGenerator.cs create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/GenerateProxyArgs.cs create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/IServiceProxyGenerator.cs create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/JavaScript/JavaScriptProxyGenerator.cs diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs index 08da2e3d00..442d55cd6c 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs @@ -3,6 +3,9 @@ using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Cli.Commands; using Volo.Abp.Cli.Http; using Volo.Abp.Cli.LIbs; +using Volo.Abp.Cli.ServiceProxy; +using Volo.Abp.Cli.ServiceProxy.Angular; +using Volo.Abp.Cli.ServiceProxy.JavaScript; using Volo.Abp.Domain; using Volo.Abp.IdentityModel; using Volo.Abp.Json; @@ -58,6 +61,12 @@ namespace Volo.Abp.Cli options.Commands["create-migration-and-run-migrator"] = typeof(CreateMigrationAndRunMigratorCommand); options.Commands["install-libs"] = typeof(InstallLibsCommand); }); + + Configure(options => + { + options.Generators[JavaScriptServiceProxyGenerator.Name] = typeof(JavaScriptServiceProxyGenerator); + options.Generators[AngularServiceProxyGenerator.Name] = typeof(AngularServiceProxyGenerator); + }); } } } 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 b56dd19cb0..ec61d86cc1 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 @@ -1,3 +1,7 @@ +using Microsoft.Extensions.Options; +using Volo.Abp.Cli.ServiceProxy; +using Volo.Abp.DependencyInjection; + namespace Volo.Abp.Cli.Commands { public class GenerateProxyCommand : ProxyCommandBase @@ -6,10 +10,10 @@ namespace Volo.Abp.Cli.Commands protected override string CommandName => Name; - protected override string SchematicsCommandName => "proxy-add"; - - public GenerateProxyCommand(CliService cliService) - : base(cliService) + public GenerateProxyCommand( + IOptions serviceProxyOptions, + IHybridServiceScopeFactory serviceScopeFactory) + : base(serviceProxyOptions, serviceScopeFactory) { } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/ProxyCommandBase.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/ProxyCommandBase.cs index 1745db2fd2..ce395a0e55 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/ProxyCommandBase.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/ProxyCommandBase.cs @@ -1,121 +1,66 @@ using System; -using System.IO; using System.Text; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; -using Newtonsoft.Json.Linq; -using NuGet.Versioning; +using Microsoft.Extensions.Options; using Volo.Abp.Cli.Args; -using Volo.Abp.Cli.Utils; +using Volo.Abp.Cli.ServiceProxy; using Volo.Abp.DependencyInjection; namespace Volo.Abp.Cli.Commands { public abstract class ProxyCommandBase : IConsoleCommand, ITransientDependency { - public CliService CliService { get; } public ILogger Logger { get; set; } protected abstract string CommandName { get; } - protected abstract string SchematicsCommandName { get; } + protected AbpCliServiceProxyOptions ServiceProxyOptions { get; } - public ProxyCommandBase(CliService cliService) + protected IHybridServiceScopeFactory ServiceScopeFactory { get; } + + public ProxyCommandBase( + IOptions serviceProxyOptions, + IHybridServiceScopeFactory serviceScopeFactory) { - CliService = cliService; + ServiceScopeFactory = serviceScopeFactory; + ServiceProxyOptions = serviceProxyOptions.Value; Logger = NullLogger.Instance; } public async Task ExecuteAsync(CommandLineArgs commandLineArgs) { - CheckAngularJsonFile(); - await CheckNgSchematicsAsync(); - - var prompt = commandLineArgs.Options.ContainsKey("p") || commandLineArgs.Options.ContainsKey("prompt"); - var defaultValue = prompt ? null : "__default"; - - var module = commandLineArgs.Options.GetOrNull(Options.Module.Short, Options.Module.Long) ?? defaultValue; - var apiName = commandLineArgs.Options.GetOrNull(Options.ApiName.Short, Options.ApiName.Long) ?? defaultValue; - var source = commandLineArgs.Options.GetOrNull(Options.Source.Short, Options.Source.Long) ?? defaultValue; - var target = commandLineArgs.Options.GetOrNull(Options.Target.Short, Options.Target.Long) ?? defaultValue; - - var commandBuilder = new StringBuilder("npx ng g @abp/ng.schematics:" + SchematicsCommandName); - - if (module != null) - { - commandBuilder.Append($" --module {module}"); - } - - if (apiName != null) - { - commandBuilder.Append($" --api-name {apiName}"); - } - - if (source != null) - { - commandBuilder.Append($" --source {source}"); - } - - if (target != null) - { - commandBuilder.Append($" --target {target}"); - } - - CmdHelper.RunCmd(commandBuilder.ToString()); - } - - private async Task CheckNgSchematicsAsync() - { - var packageJsonPath = $"package.json"; + var generateType = commandLineArgs.Options.GetOrNull(Options.GenerateType.Short, Options.GenerateType.Long)?.ToUpper(); - if (!File.Exists(packageJsonPath)) + if (string.IsNullOrWhiteSpace(generateType)) { - throw new CliUsageException( - "package.json file not found" + + throw new CliUsageException("Option Type is required" + Environment.NewLine + - GetUsageInfo() - ); + GetUsageInfo()); } - var schematicsVersion = - (string) JObject.Parse(File.ReadAllText(packageJsonPath))["devDependencies"]?["@abp/ng.schematics"]; - - if (schematicsVersion == null) + if (!ServiceProxyOptions.Generators.ContainsKey(generateType)) { - throw new CliUsageException( - "\"@abp/ng.schematics\" NPM package should be installed to the devDependencies before running this command!" + + throw new CliUsageException("Option Type value is invalid" + Environment.NewLine + - GetUsageInfo() - ); + GetUsageInfo()); } - var parseError = SemanticVersion.TryParse(schematicsVersion.TrimStart('~', '^', 'v'), out var semanticSchematicsVersion); - if (parseError) + using (var scope = ServiceScopeFactory.CreateScope()) { - Logger.LogWarning("Couldn't determinate version of \"@abp/ng.schematics\" package."); - return; - } + var generatorType = ServiceProxyOptions.Generators[generateType]; + var serviceProxyGenerator = scope.ServiceProvider.GetService(generatorType).As(); - var cliVersion = await CliService.GetCurrentCliVersionAsync(typeof(CliService).Assembly); - if (semanticSchematicsVersion < cliVersion) - { - Logger.LogWarning("\"@abp/ng.schematics\" version is lower than ABP Cli version."); - return; + await serviceProxyGenerator.GenerateProxyAsync(BuildArgs(commandLineArgs)); } } - private void CheckAngularJsonFile() + private GenerateProxyArgs BuildArgs(CommandLineArgs commandLineArgs) { - var angularPath = $"angular.json"; - if (!File.Exists(angularPath)) - { - throw new CliUsageException( - "angular.json file not found. You must run this command in the angular folder." + - Environment.NewLine + Environment.NewLine + - GetUsageInfo() - ); - } + var module = commandLineArgs.Options.GetOrNull(Options.Module.Short, Options.Module.Long); + var url = commandLineArgs.Options.GetOrNull(Options.Url.Long); + return new GenerateProxyArgs(CommandName, module, url, commandLineArgs.Options); } public string GetUsageInfo() @@ -132,7 +77,7 @@ namespace Volo.Abp.Cli.Commands sb.AppendLine("-m|--module (default: 'app') The name of the backend module you wish to generate proxies for."); sb.AppendLine("-a|--api-name (default: 'default') The name of the API endpoint defined in the /src/environments/environment.ts."); sb.AppendLine("-s|--source (default: 'defaultProject') Angular project name to resolve the root namespace & API definition URL from."); - sb.AppendLine("-t|--target (default: 'defaultProject') Angular project name to place generated code in."); + sb.AppendLine("-o|--output (default: 'defaultProject') Angular project name to place generated code in."); sb.AppendLine("-p|--prompt Asks the options from the command line prompt (for the missing options)"); sb.AppendLine(""); sb.AppendLine("See the documentation for more info: https://docs.abp.io/en/abp/latest/CLI"); @@ -162,9 +107,20 @@ namespace Volo.Abp.Cli.Commands public const string Long = "source"; } - public static class Target + public static class GenerateType { public const string Short = "t"; + public const string Long = "type"; + } + + public static class Output + { + public const string Short = "o"; + public const string Long = "output"; + } + + public static class Target + { public const string Long = "target"; } @@ -173,6 +129,11 @@ namespace Volo.Abp.Cli.Commands public const string Short = "p"; public const string Long = "prompt"; } + + public static class Url + { + public const string Long = "url"; + } } } } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/RemoveProxyCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/RemoveProxyCommand.cs index 7c55c0cf71..dc193e0773 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/RemoveProxyCommand.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/RemoveProxyCommand.cs @@ -1,4 +1,8 @@ -namespace Volo.Abp.Cli.Commands +using Microsoft.Extensions.Options; +using Volo.Abp.Cli.ServiceProxy; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Cli.Commands { public class RemoveProxyCommand : ProxyCommandBase { @@ -6,10 +10,10 @@ protected override string CommandName => Name; - protected override string SchematicsCommandName => "proxy-remove"; - - public RemoveProxyCommand(CliService cliService) - : base(cliService) + public RemoveProxyCommand( + IOptions serviceProxyOptions, + IHybridServiceScopeFactory serviceScopeFactory) + : base(serviceProxyOptions, serviceScopeFactory) { } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/AbpCliServiceProxyOptions.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/AbpCliServiceProxyOptions.cs new file mode 100644 index 0000000000..967960fc42 --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/AbpCliServiceProxyOptions.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; + +namespace Volo.Abp.Cli.ServiceProxy +{ + public class AbpCliServiceProxyOptions + { + public IDictionary Generators { get; } + + public AbpCliServiceProxyOptions() + { + Generators = new Dictionary(); + } + } +} diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/Angular/AngularServiceProxyGenerator.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/Angular/AngularServiceProxyGenerator.cs new file mode 100644 index 0000000000..46ed61a757 --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/Angular/AngularServiceProxyGenerator.cs @@ -0,0 +1,114 @@ +using System.IO; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; +using Newtonsoft.Json.Linq; +using NuGet.Versioning; +using Volo.Abp.Cli.Commands; +using Volo.Abp.Cli.Utils; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Cli.ServiceProxy.Angular +{ + public class AngularServiceProxyGenerator : IServiceProxyGenerator , ITransientDependency + { + public const string Name = "NG"; + + public CliService CliService { get; } + + public ILogger Logger { get; set; } + + public AngularServiceProxyGenerator(CliService cliService) + { + CliService = cliService; + Logger = NullLogger.Instance; + } + + public async Task GenerateProxyAsync(GenerateProxyArgs args) + { + CheckAngularJsonFile(); + await CheckNgSchematicsAsync(); + + var prompt = args.ExtraProperties.ContainsKey("p") || args.ExtraProperties.ContainsKey("prompt"); + var defaultValue = prompt ? null : "__default"; + + var module = args.Module ?? defaultValue; + var schematicsCommandName = args.CommandName == RemoveProxyCommand.Name ? "proxy-remove" : "proxy-add"; + var apiName = args.ExtraProperties.GetOrNull(ProxyCommandBase.Options.ApiName.Short, ProxyCommandBase.Options.ApiName.Long) ?? defaultValue; + var source = args.ExtraProperties.GetOrNull(ProxyCommandBase.Options.Source.Short, ProxyCommandBase.Options.Source.Long) ?? defaultValue; + var target = args.ExtraProperties.GetOrNull(ProxyCommandBase.Options.Target.Long) ?? defaultValue; + + + var commandBuilder = new StringBuilder("npx ng g @abp/ng.schematics:" + schematicsCommandName); + + if (module != null) + { + commandBuilder.Append($" --module {module}"); + } + + if (apiName != null) + { + commandBuilder.Append($" --api-name {apiName}"); + } + + if (source != null) + { + commandBuilder.Append($" --source {source}"); + } + + if (target != null) + { + commandBuilder.Append($" --target {target}"); + } + + CmdHelper.RunCmd(commandBuilder.ToString()); + } + + private async Task CheckNgSchematicsAsync() + { + var packageJsonPath = $"package.json"; + + if (!File.Exists(packageJsonPath)) + { + throw new CliUsageException( + "package.json file not found" + ); + } + + var schematicsVersion = + (string) JObject.Parse(File.ReadAllText(packageJsonPath))["devDependencies"]?["@abp/ng.schematics"]; + + if (schematicsVersion == null) + { + throw new CliUsageException( + "\"@abp/ng.schematics\" NPM package should be installed to the devDependencies before running this command!" + ); + } + + var parseError = SemanticVersion.TryParse(schematicsVersion.TrimStart('~', '^', 'v'), out var semanticSchematicsVersion); + if (parseError) + { + Logger.LogWarning("Couldn't determinate version of \"@abp/ng.schematics\" package."); + return; + } + + var cliVersion = await CliService.GetCurrentCliVersionAsync(typeof(CliService).Assembly); + if (semanticSchematicsVersion < cliVersion) + { + Logger.LogWarning("\"@abp/ng.schematics\" version is lower than ABP Cli version."); + } + } + + private void CheckAngularJsonFile() + { + var angularPath = $"angular.json"; + if (!File.Exists(angularPath)) + { + throw new CliUsageException( + "angular.json file not found. You must run this command in the angular folder." + ); + } + } + } +} diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/GenerateProxyArgs.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/GenerateProxyArgs.cs new file mode 100644 index 0000000000..0b5be7b5af --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/GenerateProxyArgs.cs @@ -0,0 +1,30 @@ +using JetBrains.Annotations; +using Volo.Abp.Cli.Args; + +namespace Volo.Abp.Cli.ServiceProxy +{ + public class GenerateProxyArgs + { + [NotNull] + public string CommandName { get; } + + public string Module { get; } + + public string Url { get; } + + [NotNull] + public AbpCommandLineOptions ExtraProperties { get; set; } + + public GenerateProxyArgs( + [NotNull] string commandName, + string module, + string url, + AbpCommandLineOptions extraProperties = null) + { + CommandName = Check.NotNullOrWhiteSpace(commandName, nameof(commandName)); + Module = module; + Url = url; + ExtraProperties = extraProperties ?? new AbpCommandLineOptions(); + } + } +} diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/IServiceProxyGenerator.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/IServiceProxyGenerator.cs new file mode 100644 index 0000000000..c487839804 --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/IServiceProxyGenerator.cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; + +namespace Volo.Abp.Cli.ServiceProxy +{ + public interface IServiceProxyGenerator + { + Task GenerateProxyAsync(GenerateProxyArgs args); + } +} diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/JavaScript/JavaScriptProxyGenerator.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/JavaScript/JavaScriptProxyGenerator.cs new file mode 100644 index 0000000000..fc3a3e1fa9 --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/JavaScript/JavaScriptProxyGenerator.cs @@ -0,0 +1,15 @@ +using System.Threading.Tasks; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Cli.ServiceProxy.JavaScript +{ + public class JavaScriptServiceProxyGenerator : IServiceProxyGenerator, ITransientDependency + { + public const string Name = "JS"; + + public Task GenerateProxyAsync(GenerateProxyArgs args) + { + throw new System.NotImplementedException(); + } + } +}