Browse Source

Merge pull request #14288 from abpframework/service-type

Add `service type` parameter to `ServiceProxyGenerator`.
pull/14557/head
liangshiwei 4 years ago
committed by GitHub
parent
commit
388d792da1
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      docs/en/CLI.md
  2. 17
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApiDescriptionExtensions.cs
  3. 1
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProvider.cs
  4. 21
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/ProxyCommandBase.cs
  5. 13
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxying/Angular/AngularServiceProxyGenerator.cs
  6. 7
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxying/CSharp/CSharpServiceProxyGenerator.cs
  7. 4
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxying/GenerateProxyArgs.cs
  8. 7
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxying/JavaScript/JavaScriptServiceProxyGenerator.cs
  9. 20
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxying/ServiceProxyGeneratorBase.cs
  10. 8
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxying/ServiceType.cs
  11. 6
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ControllerApiDescriptionModel.cs
  12. 4
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ModuleApiDescriptionModel.cs

1
docs/en/CLI.md

@ -365,6 +365,7 @@ abp generate-proxy -t csharp -url https://localhost:44302/
* `--module` or `-m`: Specifies the name of the backend module you wish to generate proxies for. Default value: `app`. * `--module` or `-m`: Specifies the name of the backend module you wish to generate proxies for. Default value: `app`.
* `--working-directory` or `-wd`: Execution directory. For `csharp` and `js` client types. * `--working-directory` or `-wd`: Execution directory. For `csharp` and `js` client types.
* `--url` or `-u`: API definition URL from. * `--url` or `-u`: API definition URL from.
* `--service-type` or `-st`: Specifies the service type to generate. `application`, `integration` and `all`, Default value: `all` for C#, `application` for JavaScript / Angular.
> See the [Angular Service Proxies document](UI/Angular/Service-Proxies.md) for more. > See the [Angular Service Proxies document](UI/Angular/Service-Proxies.md) for more.

17
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApiDescriptionExtensions.cs

@ -1,5 +1,4 @@
using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.AspNetCore.Mvc.Controllers;
using Volo.Abp.Reflection; using Volo.Abp.Reflection;
@ -7,9 +6,9 @@ namespace Volo.Abp.AspNetCore.Mvc;
public static class ApiDescriptionExtensions public static class ApiDescriptionExtensions
{ {
public static bool IsRemoteService(this ApiDescription actionDescriptor) public static bool IsRemoteService(this ApiDescription apiDescriptor)
{ {
if (actionDescriptor.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor) if (apiDescriptor.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor)
{ {
var remoteServiceAttr = ReflectionHelper.GetSingleAttributeOfMemberOrDeclaringTypeOrDefault<RemoteServiceAttribute>(controllerActionDescriptor.MethodInfo); var remoteServiceAttr = ReflectionHelper.GetSingleAttributeOfMemberOrDeclaringTypeOrDefault<RemoteServiceAttribute>(controllerActionDescriptor.MethodInfo);
if (remoteServiceAttr != null && remoteServiceAttr.IsEnabled) if (remoteServiceAttr != null && remoteServiceAttr.IsEnabled)
@ -31,4 +30,14 @@ public static class ApiDescriptionExtensions
return false; return false;
} }
public static bool IsIntegrationService(this ApiDescription apiDescriptor)
{
if (apiDescriptor.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor)
{
return IntegrationServiceAttribute.IsDefinedOrInherited(controllerActionDescriptor.ControllerTypeInfo);
}
return false;
}
} }

1
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProvider.cs

@ -103,6 +103,7 @@ public class AspNetCoreApiDescriptionModelProvider : IApiDescriptionModelProvide
_options.ControllerNameGenerator(controllerType, setting), _options.ControllerNameGenerator(controllerType, setting),
FindGroupName(controllerType) ?? apiDescription.GroupName, FindGroupName(controllerType) ?? apiDescription.GroupName,
apiDescription.IsRemoteService(), apiDescription.IsRemoteService(),
apiDescription.IsIntegrationService(),
apiDescription.GetProperty<ApiVersion>()?.ToString(), apiDescription.GetProperty<ApiVersion>()?.ToString(),
controllerType, controllerType,
_modelOptions.IgnoredInterfaces _modelOptions.IgnoredInterfaces

21
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/ProxyCommandBase.cs

@ -68,10 +68,22 @@ public abstract class ProxyCommandBase<T> : IConsoleCommand, ITransientDependenc
var source = commandLineArgs.Options.GetOrNull(Options.Source.Short, Options.Source.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(); var workDirectory = commandLineArgs.Options.GetOrNull(Options.WorkDirectory.Short, Options.WorkDirectory.Long) ?? Directory.GetCurrentDirectory();
var folder = commandLineArgs.Options.GetOrNull(Options.Folder.Long); var folder = commandLineArgs.Options.GetOrNull(Options.Folder.Long);
var serviceTypeArg = commandLineArgs.Options.GetOrNull(Options.Module.Short, Options.ServiceType.Long);
ServiceType? serviceType = null;
if (!serviceTypeArg.IsNullOrWhiteSpace())
{
serviceType = serviceTypeArg.ToLower() == "application"
? ServiceType.Application
: serviceTypeArg.ToLower() == "integration"
? ServiceType.Integration
: null;
}
var withoutContracts = commandLineArgs.Options.ContainsKey(Options.WithoutContracts.Short) || var withoutContracts = commandLineArgs.Options.ContainsKey(Options.WithoutContracts.Short) ||
commandLineArgs.Options.ContainsKey(Options.WithoutContracts.Long); commandLineArgs.Options.ContainsKey(Options.WithoutContracts.Long);
return new GenerateProxyArgs(CommandName, workDirectory, module, url, output, target, apiName, source, folder, withoutContracts, commandLineArgs.Options); return new GenerateProxyArgs(CommandName, workDirectory, module, url, output, target, apiName, source, folder, serviceType, withoutContracts, commandLineArgs.Options);
} }
public virtual string GetUsageInfo() public virtual string GetUsageInfo()
@ -165,6 +177,13 @@ public abstract class ProxyCommandBase<T> : IConsoleCommand, ITransientDependenc
public const string Long = "working-directory"; public const string Long = "working-directory";
} }
public static class ServiceType
{
public const string Short = "st";
public const string Long = "service-type";
}
public static class WithoutContracts public static class WithoutContracts
{ {
public const string Short = "c"; public const string Short = "c";

13
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxying/Angular/AngularServiceProxyGenerator.cs

@ -30,7 +30,7 @@ public class AngularServiceProxyGenerator : ServiceProxyGeneratorBase<AngularSer
_cliService = cliService; _cliService = cliService;
} }
public override async Task GenerateProxyAsync(GenerateProxyArgs args) public async override Task GenerateProxyAsync(GenerateProxyArgs args)
{ {
CheckAngularJsonFile(); CheckAngularJsonFile();
await CheckNgSchematicsAsync(); await CheckNgSchematicsAsync();
@ -76,9 +76,20 @@ public class AngularServiceProxyGenerator : ServiceProxyGeneratorBase<AngularSer
commandBuilder.Append($" --url {url}"); commandBuilder.Append($" --url {url}");
} }
var serviceType = GetServiceType(args);
if (args.ServiceType != null)
{
commandBuilder.Append($" --service-type {serviceType.ToString().ToLower()}");
}
_cmdhelper.RunCmd(commandBuilder.ToString()); _cmdhelper.RunCmd(commandBuilder.ToString());
} }
protected override ServiceType? GetDefaultServiceType(GenerateProxyArgs args)
{
return ServiceType.Application;
}
private async Task CheckNgSchematicsAsync() private async Task CheckNgSchematicsAsync()
{ {
var packageJsonPath = $"package.json"; var packageJsonPath = $"package.json";

7
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxying/CSharp/CSharpServiceProxyGenerator.cs

@ -144,6 +144,11 @@ public class CSharpServiceProxyGenerator : ServiceProxyGeneratorBase<CSharpServi
await CreateJsonFile(args, applicationApiDescriptionModel); await CreateJsonFile(args, applicationApiDescriptionModel);
} }
protected override ServiceType? GetDefaultServiceType(GenerateProxyArgs args)
{
return ServiceType.All;
}
private async Task CreateJsonFile(GenerateProxyArgs args, ApplicationApiDescriptionModel applicationApiDescriptionModel) private async Task CreateJsonFile(GenerateProxyArgs args, ApplicationApiDescriptionModel applicationApiDescriptionModel)
{ {
var folder = args.Folder.IsNullOrWhiteSpace() ? ProxyDirectory : args.Folder; var folder = args.Folder.IsNullOrWhiteSpace() ? ProxyDirectory : args.Folder;
@ -190,7 +195,7 @@ public class CSharpServiceProxyGenerator : ServiceProxyGeneratorBase<CSharpServi
$"using {GetTypeNamespace(appServiceTypeFullName)};" $"using {GetTypeNamespace(appServiceTypeFullName)};"
}; };
if (!controllerApiDescription.IntegrationService) if (!controllerApiDescription.IsIntegrationService)
{ {
classTemplate.Replace($"{Environment.NewLine}[IntegrationService]", string.Empty); classTemplate.Replace($"{Environment.NewLine}[IntegrationService]", string.Empty);
} }

4
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxying/GenerateProxyArgs.cs

@ -25,6 +25,8 @@ public class GenerateProxyArgs
public string Folder { get; } public string Folder { get; }
public ServiceType? ServiceType { get; }
public bool WithoutContracts { get; } public bool WithoutContracts { get; }
[NotNull] [NotNull]
@ -40,6 +42,7 @@ public class GenerateProxyArgs
string apiName, string apiName,
string source, string source,
string folder, string folder,
ServiceType? serviceType,
bool withoutContracts, bool withoutContracts,
Dictionary<string, string> extraProperties = null) Dictionary<string, string> extraProperties = null)
{ {
@ -52,6 +55,7 @@ public class GenerateProxyArgs
ApiName = apiName; ApiName = apiName;
Source = source; Source = source;
Folder = folder; Folder = folder;
ServiceType = serviceType;
WithoutContracts = withoutContracts; WithoutContracts = withoutContracts;
ExtraProperties = extraProperties ?? new Dictionary<string, string>(); ExtraProperties = extraProperties ?? new Dictionary<string, string>();
} }

7
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxying/JavaScript/JavaScriptServiceProxyGenerator.cs

@ -28,7 +28,7 @@ public class JavaScriptServiceProxyGenerator : ServiceProxyGeneratorBase<JavaScr
_jQueryProxyScriptGenerator = jQueryProxyScriptGenerator; _jQueryProxyScriptGenerator = jQueryProxyScriptGenerator;
} }
public override async Task GenerateProxyAsync(GenerateProxyArgs args) public async override Task GenerateProxyAsync(GenerateProxyArgs args)
{ {
CheckWorkDirectory(args.WorkDirectory); CheckWorkDirectory(args.WorkDirectory);
@ -57,6 +57,11 @@ public class JavaScriptServiceProxyGenerator : ServiceProxyGeneratorBase<JavaScr
Logger.LogInformation($"Create {GetLoggerOutputPath(output, args.WorkDirectory)}"); Logger.LogInformation($"Create {GetLoggerOutputPath(output, args.WorkDirectory)}");
} }
protected override ServiceType? GetDefaultServiceType(GenerateProxyArgs args)
{
return ServiceType.Application;
}
private void RemoveProxy(GenerateProxyArgs args, string filePath) private void RemoveProxy(GenerateProxyArgs args, string filePath)
{ {
if (File.Exists(filePath)) if (File.Exists(filePath))

20
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxying/ServiceProxyGeneratorBase.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -42,13 +43,30 @@ public abstract class ServiceProxyGeneratorBase<T> : IServiceProxyGenerator wher
throw new CliUsageException($"Module name: {args.Module} is invalid"); throw new CliUsageException($"Module name: {args.Module} is invalid");
} }
var serviceType = GetServiceType(args);
switch (serviceType)
{
case ServiceType.Application:
moduleDefinition.Controllers.RemoveAll(x => !x.Value.IsRemoteService);
break;
case ServiceType.Integration:
moduleDefinition.Controllers.RemoveAll(x => !x.Value.IsIntegrationService);
break;
}
var apiDescriptionModel = ApplicationApiDescriptionModel.Create(); var apiDescriptionModel = ApplicationApiDescriptionModel.Create();
apiDescriptionModel.Types = apiDefinition.Types; apiDescriptionModel.Types = apiDefinition.Types;
apiDescriptionModel.AddModule(moduleDefinition); apiDescriptionModel.AddModule(moduleDefinition);
return apiDescriptionModel; return apiDescriptionModel;
} }
protected virtual ServiceType? GetServiceType(GenerateProxyArgs args)
{
return args.ServiceType ?? GetDefaultServiceType(args);
}
protected abstract ServiceType? GetDefaultServiceType(GenerateProxyArgs args);
protected string GetLoggerOutputPath(string path, string workDirectory) protected string GetLoggerOutputPath(string path, string workDirectory)
{ {
return path.Replace(workDirectory, string.Empty).TrimStart(Path.DirectorySeparatorChar); return path.Replace(workDirectory, string.Empty).TrimStart(Path.DirectorySeparatorChar);

8
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxying/ServiceType.cs

@ -0,0 +1,8 @@
namespace Volo.Abp.Cli.ServiceProxying;
public enum ServiceType : byte
{
All = 0,
Application = 1,
Integration = 2
}

6
framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ControllerApiDescriptionModel.cs

@ -14,7 +14,7 @@ public class ControllerApiDescriptionModel
public bool IsRemoteService { get; set; } public bool IsRemoteService { get; set; }
public bool IntegrationService { get; set; } public bool IsIntegrationService { get; set; }
public string ApiVersion { get; set; } public string ApiVersion { get; set; }
@ -29,14 +29,14 @@ public class ControllerApiDescriptionModel
} }
public static ControllerApiDescriptionModel Create(string controllerName, string groupName, bool isRemoteService, string apiVersion, Type type, [CanBeNull] HashSet<Type> ignoredInterfaces = null) public static ControllerApiDescriptionModel Create(string controllerName, string groupName, bool isRemoteService, bool isIntegrationService, string apiVersion, Type type, [CanBeNull] HashSet<Type> ignoredInterfaces = null)
{ {
return new ControllerApiDescriptionModel return new ControllerApiDescriptionModel
{ {
ControllerName = controllerName, ControllerName = controllerName,
ControllerGroupName = groupName, ControllerGroupName = groupName,
IsRemoteService = isRemoteService, IsRemoteService = isRemoteService,
IntegrationService = IntegrationServiceAttribute.IsDefinedOrInherited(type), IsIntegrationService = isIntegrationService, //IntegrationServiceAttribute.IsDefinedOrInherited(type),
ApiVersion = apiVersion, ApiVersion = apiVersion,
Type = type.FullName, Type = type.FullName,
Actions = new Dictionary<string, ActionApiDescriptionModel>(), Actions = new Dictionary<string, ActionApiDescriptionModel>(),

4
framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ModuleApiDescriptionModel.cs

@ -49,10 +49,10 @@ public class ModuleApiDescriptionModel
return Controllers[controller.Type] = controller; return Controllers[controller.Type] = controller;
} }
public ControllerApiDescriptionModel GetOrAddController(string name, string groupName, bool isRemoteService, string apiVersion, Type type, [CanBeNull] HashSet<Type> ignoredInterfaces = null) public ControllerApiDescriptionModel GetOrAddController(string name, string groupName, bool isRemoteService, bool isIntegrationService, string apiVersion, Type type, [CanBeNull] HashSet<Type> ignoredInterfaces = null)
{ {
var key = apiVersion.IsNullOrWhiteSpace() ? type.FullName : $"{apiVersion + "."}{type.FullName}"; var key = apiVersion.IsNullOrWhiteSpace() ? type.FullName : $"{apiVersion + "."}{type.FullName}";
return Controllers.GetOrAdd(key, () => ControllerApiDescriptionModel.Create(name, groupName, isRemoteService, apiVersion, type, ignoredInterfaces)); return Controllers.GetOrAdd(key, () => ControllerApiDescriptionModel.Create(name, groupName, isRemoteService, isIntegrationService, apiVersion, type, ignoredInterfaces));
} }
public ModuleApiDescriptionModel CreateSubModel(string[] controllers, string[] actions) public ModuleApiDescriptionModel CreateSubModel(string[] controllers, string[] actions)

Loading…
Cancel
Save