Browse Source

Add JavaScriptServiceProxyGenerator

pull/9905/head
liangshiwei 5 years ago
parent
commit
279c2100a2
  1. 4
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs
  2. 10
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliUrls.cs
  3. 17
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/ProxyCommandBase.cs
  4. 9
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/Angular/AngularServiceProxyGenerator.cs
  5. 35
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/GenerateProxyArgs.cs
  6. 86
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/JavaScript/JavaScriptProxyGenerator.cs

4
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
{

10
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";
}
}
}

17
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";
}
}
}
}

9
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<AngularServiceProxyGenerator> 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);

35
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<string, string> 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<string, string> 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<string, string>();
}
}
}

86
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<ApplicationApiDescriptionModel> GetApplicationApiDescriptionModelAsync(GenerateProxyArgs args)
{
var client = _cliHttpClientFactory.CreateClient();
var apiDefinitionResult = await client.GetStringAsync(CliUrls.GetApiDefinitionUrl(args.Url));
var apiDefinition = JsonSerializer.Deserialize<ApplicationApiDescriptionModel>(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);
}
}
}

Loading…
Cancel
Save