Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxy Generator: ABP CLI should warn developer when project has @abp/ng.schematics with a different version #5563

Merged
merged 3 commits into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 10 additions & 5 deletions framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private async Task CheckCliVersionAsync()
{
var assembly = typeof(CliService).Assembly;
var toolPath = GetToolPath(assembly);
var currentCliVersion = await GetCurrentCliVersion(assembly);
var currentCliVersion = await GetCurrentCliVersionInternalAsync(assembly);
var updateChannel = GetUpdateChannel(currentCliVersion);

Logger.LogInformation($"Version {currentCliVersion} ({updateChannel})");
Expand All @@ -94,7 +94,7 @@ private async Task CheckCliVersionAsync()
}
}

private static string GetToolPath(Assembly assembly)
private string GetToolPath(Assembly assembly)
{
if (!assembly.Location.Contains(".store"))
{
Expand All @@ -104,7 +104,12 @@ private static string GetToolPath(Assembly assembly)
return assembly.Location.Substring(0, assembly.Location.IndexOf(".store", StringComparison.Ordinal));
}

private static async Task<SemanticVersion> GetCurrentCliVersion(Assembly assembly)
public async Task<SemanticVersion> GetCurrentCliVersionAsync(Assembly assembly)
{
return await GetCurrentCliVersionInternalAsync(assembly);
}

private async Task<SemanticVersion> GetCurrentCliVersionInternalAsync(Assembly assembly)
{
SemanticVersion currentCliVersion = default;

Expand Down Expand Up @@ -134,7 +139,7 @@ private static async Task<SemanticVersion> GetCurrentCliVersion(Assembly assembl
return currentCliVersion;
}

private static UpdateChannel GetUpdateChannel(SemanticVersion currentCliVersion)
private UpdateChannel GetUpdateChannel(SemanticVersion currentCliVersion)
{
if (!currentCliVersion.IsPrerelease)
{
Expand Down Expand Up @@ -172,7 +177,7 @@ private async Task<SemanticVersion> GetLatestVersion(UpdateChannel updateChannel
}
}

private static bool IsGlobalTool(string toolPath)
private bool IsGlobalTool(string toolPath)
{
var globalPaths = new[] { @"%USERPROFILE%\.dotnet\tools\", "%HOME%/.dotnet/tools/", };
return globalPaths.Select(Environment.ExpandEnvironmentVariables).Contains(toolPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ public class GenerateProxyCommand : ProxyCommandBase
protected override string CommandName => Name;

protected override string SchematicsCommandName => "proxy-add";

public GenerateProxyCommand(CliService cliService)
: base(cliService)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
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.Args;
using Volo.Abp.Cli.Utils;
using Volo.Abp.DependencyInjection;
Expand All @@ -11,14 +14,23 @@ 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; }

public Task ExecuteAsync(CommandLineArgs commandLineArgs)
public ProxyCommandBase(CliService cliService)
{
CliService = cliService;
Logger = NullLogger<HelpCommand>.Instance;
}

public async Task ExecuteAsync(CommandLineArgs commandLineArgs)
{
CheckAngularJsonFile();
CheckNgSchematics();
await CheckNgSchematicsAsync();

var prompt = commandLineArgs.Options.ContainsKey("p") || commandLineArgs.Options.ContainsKey("prompt");
var defaultValue = prompt ? null : "__default";
Expand Down Expand Up @@ -51,11 +63,9 @@ public Task ExecuteAsync(CommandLineArgs commandLineArgs)
}

CmdHelper.RunCmd(commandBuilder.ToString());

return Task.CompletedTask;
}

private void CheckNgSchematics()
private async Task CheckNgSchematicsAsync()
{
var packageJsonPath = $"package.json";

Expand All @@ -68,17 +78,31 @@ private void CheckNgSchematics()
);
}

var schematicsPackageNode =
var schematicsVersion =
(string) JObject.Parse(File.ReadAllText(packageJsonPath))["devDependencies"]?["@abp/ng.schematics"];

if (schematicsPackageNode == null)
if (schematicsVersion == null)
{
throw new CliUsageException(
"\"@abp/ng.schematics\" NPM package should be installed to the devDependencies before running this command!" +
Environment.NewLine +
GetUsageInfo()
);
}

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.");
return;
}
}

private void CheckAngularJsonFile()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@ public class RemoveProxyCommand : ProxyCommandBase
protected override string CommandName => Name;

protected override string SchematicsCommandName => "proxy-remove";

public RemoveProxyCommand(CliService cliService)
: base(cliService)
{
}
}
}
}