From 279c2100a2c905aba62ca6658ec713ab3177924a Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Tue, 24 Aug 2021 18:01:46 +0800 Subject: [PATCH] Add JavaScriptServiceProxyGenerator --- .../Volo/Abp/Cli/AbpCliCoreModule.cs | 4 +- .../Volo.Abp.Cli.Core/Volo/Abp/Cli/CliUrls.cs | 10 ++- .../Volo/Abp/Cli/Commands/ProxyCommandBase.cs | 17 +++- .../Angular/AngularServiceProxyGenerator.cs | 9 +- .../Abp/Cli/ServiceProxy/GenerateProxyArgs.cs | 35 ++++++-- .../JavaScript/JavaScriptProxyGenerator.cs | 86 ++++++++++++++++++- 6 files changed, 142 insertions(+), 19 deletions(-) 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 442d55cd6c..2e7a270918 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 @@ -7,6 +7,7 @@ using Volo.Abp.Cli.ServiceProxy; using Volo.Abp.Cli.ServiceProxy.Angular; using Volo.Abp.Cli.ServiceProxy.JavaScript; using Volo.Abp.Domain; +using Volo.Abp.Http; using Volo.Abp.IdentityModel; using Volo.Abp.Json; using Volo.Abp.Json.SystemTextJson; @@ -19,7 +20,8 @@ namespace Volo.Abp.Cli typeof(AbpDddDomainModule), typeof(AbpJsonModule), typeof(AbpIdentityModelModule), - typeof(AbpMinifyModule) + typeof(AbpMinifyModule), + typeof(AbpHttpModule) )] public class AbpCliCoreModule : AbpModule { diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliUrls.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliUrls.cs index 8d2b10d3ca..15d03236ae 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliUrls.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliUrls.cs @@ -1,4 +1,6 @@ -namespace Volo.Abp.Cli +using System; + +namespace Volo.Abp.Cli { public static class CliUrls { @@ -33,5 +35,11 @@ { return $"{NuGetRootPath}{apiKey}/v3/package/{packageId}/index.json"; } + + public static string GetApiDefinitionUrl(string url) + { + url = url.EnsureEndsWith('/'); + return $"{url}api/abp/api-definition"; + } } } 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 ce395a0e55..4765f8540e 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,4 +1,5 @@ using System; +using System.IO; using System.Text; using System.Threading.Tasks; using Microsoft.Extensions.Logging; @@ -58,9 +59,15 @@ namespace Volo.Abp.Cli.Commands private GenerateProxyArgs BuildArgs(CommandLineArgs commandLineArgs) { - 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); + var target = commandLineArgs.Options.GetOrNull(Options.Target.Long); + var module = commandLineArgs.Options.GetOrNull(Options.Module.Short, Options.Module.Long) ?? "app"; + var output = commandLineArgs.Options.GetOrNull(Options.Output.Short, Options.Output.Long); + var apiName = commandLineArgs.Options.GetOrNull(Options.ApiName.Short, Options.ApiName.Long); + var source = commandLineArgs.Options.GetOrNull(Options.Source.Short, Options.Source.Long); + var workDirectory = commandLineArgs.Options.GetOrNull(Options.WorkDirectory.Short, Options.WorkDirectory.Long) ?? Directory.GetCurrentDirectory(); + + return new GenerateProxyArgs(CommandName, workDirectory, module.ToLower(), url, output, target, apiName, source, commandLineArgs.Options); } public string GetUsageInfo() @@ -134,6 +141,12 @@ namespace Volo.Abp.Cli.Commands { public const string Long = "url"; } + + public static class WorkDirectory + { + public const string Short = "wd"; + public const string Long = "working-directory"; + } } } } 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 index 46ed61a757..8a4e2c3da7 100644 --- 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 @@ -16,7 +16,6 @@ namespace Volo.Abp.Cli.ServiceProxy.Angular public const string Name = "NG"; public CliService CliService { get; } - public ILogger Logger { get; set; } public AngularServiceProxyGenerator(CliService cliService) @@ -30,14 +29,14 @@ namespace Volo.Abp.Cli.ServiceProxy.Angular CheckAngularJsonFile(); await CheckNgSchematicsAsync(); + var schematicsCommandName = args.CommandName == RemoveProxyCommand.Name ? "proxy-remove" : "proxy-add"; 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 apiName = args.ApiName ?? defaultValue; + var source = args.Source ?? defaultValue; + var target = args.Target ?? defaultValue; var commandBuilder = new StringBuilder("npx ng g @abp/ng.schematics:" + schematicsCommandName); 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 index 0b5be7b5af..10a89e4be7 100644 --- 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 @@ -1,5 +1,5 @@ -using JetBrains.Annotations; -using Volo.Abp.Cli.Args; +using System.Collections.Generic; +using JetBrains.Annotations; namespace Volo.Abp.Cli.ServiceProxy { @@ -8,23 +8,44 @@ namespace Volo.Abp.Cli.ServiceProxy [NotNull] public string CommandName { get; } + [NotNull] + public string WorkDirectory { get; } + public string Module { get; } public string Url { get; } + public string Output { get; } + + public string Target { get; } + + public string ApiName { get; } + + public string Source { get; } + [NotNull] - public AbpCommandLineOptions ExtraProperties { get; set; } + public Dictionary ExtraProperties { get; set; } public GenerateProxyArgs( [NotNull] string commandName, - string module, - string url, - AbpCommandLineOptions extraProperties = null) + [NotNull] string workDirectory, + string module, + string url, + string output, + string target, + string apiName, + string source, + Dictionary extraProperties = null) { CommandName = Check.NotNullOrWhiteSpace(commandName, nameof(commandName)); + WorkDirectory = Check.NotNullOrWhiteSpace(workDirectory, nameof(workDirectory)); Module = module; Url = url; - ExtraProperties = extraProperties ?? new AbpCommandLineOptions(); + Output = output; + Target = target; + ApiName = apiName; + Source = source; + ExtraProperties = extraProperties ?? new Dictionary(); } } } 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 index fc3a3e1fa9..f0f80d694e 100644 --- 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 @@ -1,15 +1,95 @@ -using System.Threading.Tasks; +using System; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Volo.Abp.Cli.Http; using Volo.Abp.DependencyInjection; +using Volo.Abp.Http.Modeling; +using Volo.Abp.Http.ProxyScripting.Generators.JQuery; +using Volo.Abp.Json; namespace Volo.Abp.Cli.ServiceProxy.JavaScript { public class JavaScriptServiceProxyGenerator : IServiceProxyGenerator, ITransientDependency { public const string Name = "JS"; + public const string EventTriggerScript = "abp.event.trigger('abp.serviceProxyScriptInitialized');"; + public const string DefaultOutput = "wwwroot/client-proxies"; - public Task GenerateProxyAsync(GenerateProxyArgs args) + public IJsonSerializer JsonSerializer { get; } + + private readonly CliHttpClientFactory _cliHttpClientFactory; + + private readonly JQueryProxyScriptGenerator _jQueryProxyScriptGenerator; + + public JavaScriptServiceProxyGenerator( + CliHttpClientFactory cliHttpClientFactory, + IJsonSerializer jsonSerializer, + JQueryProxyScriptGenerator jQueryProxyScriptGenerator) + { + JsonSerializer = jsonSerializer; + _cliHttpClientFactory = cliHttpClientFactory; + _jQueryProxyScriptGenerator = jQueryProxyScriptGenerator; + } + + public async Task GenerateProxyAsync(GenerateProxyArgs args) + { + Check.NotNullOrWhiteSpace(args.Url, nameof(args.Url)); + CheckWorkDirectory(args.WorkDirectory); + + var apiDescriptionModel = await GetApplicationApiDescriptionModelAsync(args); + var script = RemoveInitializedEventTrigger(_jQueryProxyScriptGenerator.CreateScript(apiDescriptionModel)); + + var output = $"{args.WorkDirectory}/{DefaultOutput}/{args.Module}-proxy.js"; + if (!args.Output.IsNullOrWhiteSpace()) + { + output = !args.Output.EndsWith(".js") ? $"{Path.GetDirectoryName(args.Output)}/{args.Module}-proxy.js" : args.Output; + } + + Directory.CreateDirectory(Path.GetDirectoryName(output)); + + using (var writer = new StreamWriter(output)) + { + await writer.WriteAsync(script); + } + } + + private async Task GetApplicationApiDescriptionModelAsync(GenerateProxyArgs args) + { + var client = _cliHttpClientFactory.CreateClient(); + + var apiDefinitionResult = await client.GetStringAsync(CliUrls.GetApiDefinitionUrl(args.Url)); + var apiDefinition = JsonSerializer.Deserialize(apiDefinitionResult); + + if (!apiDefinition.Modules.TryGetValue(args.Module, out var moduleDefinition)) + { + throw new CliUsageException($"Module name: {args.Module} is invalid"); + } + + var apiDescriptionModel = ApplicationApiDescriptionModel.Create(); + apiDescriptionModel.AddModule(moduleDefinition); + + return apiDescriptionModel; + } + + private static void CheckWorkDirectory(string directory) + { + if (!Directory.Exists(directory)) + { + throw new CliUsageException("Specified directory does not exist."); + } + + var projectFiles = Directory.GetFiles(directory, "*.csproj"); + if (!projectFiles.Any()) + { + throw new CliUsageException( + "No project file found in the directory. The working directory must have a Web project file."); + } + } + + private static string RemoveInitializedEventTrigger(string script) { - throw new System.NotImplementedException(); + return script.Replace(EventTriggerScript, string.Empty); } } }