Browse Source

Abstract IServiceProxyGenerator

pull/9905/head
liangshiwei 5 years ago
parent
commit
6628377137
  1. 9
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs
  2. 12
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/GenerateProxyCommand.cs
  3. 125
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/ProxyCommandBase.cs
  4. 14
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/RemoveProxyCommand.cs
  5. 15
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/AbpCliServiceProxyOptions.cs
  6. 114
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/Angular/AngularServiceProxyGenerator.cs
  7. 30
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/GenerateProxyArgs.cs
  8. 9
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/IServiceProxyGenerator.cs
  9. 15
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxy/JavaScript/JavaScriptProxyGenerator.cs

9
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<AbpCliServiceProxyOptions>(options =>
{
options.Generators[JavaScriptServiceProxyGenerator.Name] = typeof(JavaScriptServiceProxyGenerator);
options.Generators[AngularServiceProxyGenerator.Name] = typeof(AngularServiceProxyGenerator);
});
}
}
}

12
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<AbpCliServiceProxyOptions> serviceProxyOptions,
IHybridServiceScopeFactory serviceScopeFactory)
: base(serviceProxyOptions, serviceScopeFactory)
{
}

125
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<HelpCommand> 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<AbpCliServiceProxyOptions> serviceProxyOptions,
IHybridServiceScopeFactory serviceScopeFactory)
{
CliService = cliService;
ServiceScopeFactory = serviceScopeFactory;
ServiceProxyOptions = serviceProxyOptions.Value;
Logger = NullLogger<HelpCommand>.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<IServiceProxyGenerator>();
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 <module-name> (default: 'app') The name of the backend module you wish to generate proxies for.");
sb.AppendLine("-a|--api-name <module-name> (default: 'default') The name of the API endpoint defined in the /src/environments/environment.ts.");
sb.AppendLine("-s|--source <source-name> (default: 'defaultProject') Angular project name to resolve the root namespace & API definition URL from.");
sb.AppendLine("-t|--target <target-name> (default: 'defaultProject') Angular project name to place generated code in.");
sb.AppendLine("-o|--output <output-name> (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";
}
}
}
}

14
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<AbpCliServiceProxyOptions> serviceProxyOptions,
IHybridServiceScopeFactory serviceScopeFactory)
: base(serviceProxyOptions, serviceScopeFactory)
{
}

15
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<string, Type> Generators { get; }
public AbpCliServiceProxyOptions()
{
Generators = new Dictionary<string, Type>();
}
}
}

114
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<AngularServiceProxyGenerator> Logger { get; set; }
public AngularServiceProxyGenerator(CliService cliService)
{
CliService = cliService;
Logger = NullLogger<AngularServiceProxyGenerator>.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."
);
}
}
}
}

30
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();
}
}
}

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

15
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();
}
}
}
Loading…
Cancel
Save