From ce0e1d4fe81ec4c3c5af78bcb67171f7791f6e48 Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 29 Dec 2023 11:39:19 +0800 Subject: [PATCH] Output warning if the cli version is different from the template version. Resolve #18624 --- .../Volo/Abp/Cli/CliService.cs | 46 ++----------- .../ProjectBuilding/AbpIoSourceCodeStore.cs | 69 +++++++++++++++++-- .../Angular/AngularServiceProxyGenerator.cs | 9 +-- .../Volo/Abp/Cli/Version/CliVersionService.cs | 49 +++++++++++++ 4 files changed, 125 insertions(+), 48 deletions(-) create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Version/CliVersionService.cs diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs index e84c0d5de7..2add8ed563 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs @@ -29,6 +29,7 @@ public class CliService : ITransientDependency protected IServiceScopeFactory ServiceScopeFactory { get; } protected PackageVersionCheckerService PackageVersionCheckerService { get; } public ICmdHelper CmdHelper { get; } + protected CliVersionService CliVersionService { get; } public CliService( ICommandLineArgumentParser commandLineArgumentParser, @@ -36,7 +37,8 @@ public class CliService : ITransientDependency IServiceScopeFactory serviceScopeFactory, PackageVersionCheckerService nugetService, ICmdHelper cmdHelper, - MemoryService memoryService) + MemoryService memoryService, + CliVersionService cliVersionService) { _memoryService = memoryService; CommandLineArgumentParser = commandLineArgumentParser; @@ -44,13 +46,14 @@ public class CliService : ITransientDependency ServiceScopeFactory = serviceScopeFactory; PackageVersionCheckerService = nugetService; CmdHelper = cmdHelper; + CliVersionService = cliVersionService; Logger = NullLogger.Instance; } public async Task RunAsync(string[] args) { - var currentCliVersion = await GetCurrentCliVersionInternalAsync(typeof(CliService).Assembly); + var currentCliVersion = await CliVersionService.GetCurrentCliVersionAsync(); Logger.LogInformation($"ABP CLI {currentCliVersion}"); var commandLineArgs = CommandLineArgumentParser.Parse(args); @@ -247,41 +250,6 @@ public class CliService : ITransientDependency return assembly.Location.Substring(0, assembly.Location.IndexOf(".store", StringComparison.Ordinal)); } - public async Task GetCurrentCliVersionAsync(Assembly assembly) - { - return await GetCurrentCliVersionInternalAsync(assembly); - } - - private async Task GetCurrentCliVersionInternalAsync(Assembly assembly) - { - SemanticVersion currentCliVersion = default; - - var consoleOutput = new StringReader(CmdHelper.RunCmdAndGetOutput($"dotnet tool list -g", out int exitCode)); - string line; - while ((line = await consoleOutput.ReadLineAsync()) != null) - { - if (line.StartsWith("volo.abp.cli", StringComparison.InvariantCultureIgnoreCase)) - { - var version = line.Split(new char[0], StringSplitOptions.RemoveEmptyEntries)[1]; - - SemanticVersion.TryParse(version, out currentCliVersion); - - break; - } - } - - - if (currentCliVersion == null) - { - // If not a tool executable, fallback to assembly version and treat as dev without updates - // Assembly revisions are not supported by SemVer scheme required for NuGet, trim to {major}.{minor}.{patch} - var assemblyVersion = string.Join(".", assembly.GetFileVersion().Split('.').Take(3)); - return SemanticVersion.Parse(assemblyVersion + "-dev"); - } - - return currentCliVersion; - } - private UpdateChannel GetUpdateChannel(SemanticVersion currentCliVersion) { if (!currentCliVersion.IsPrerelease) @@ -331,12 +299,12 @@ public class CliService : ITransientDependency var toolPathArg = IsGlobalTool(toolPath) ? "-g" : $"--tool-path {toolPath}"; Logger.LogWarning($"A newer {updateChannel.ToString().ToLowerInvariant()} version of the ABP CLI is available: {latestVersion}."); - + if (!string.IsNullOrWhiteSpace(message)) { Logger.LogWarning(message); } - + Logger.LogWarning(string.Empty); Logger.LogWarning("Update Command: "); diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs index a364e85f79..0415b088cc 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/AbpIoSourceCodeStore.cs @@ -12,6 +12,7 @@ using System.Text.Json; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; +using NuGet.Versioning; using Volo.Abp.Cli.GitHub; using Volo.Abp.Cli.Http; using Volo.Abp.Cli.ProjectBuilding.Templates.App; @@ -19,6 +20,7 @@ using Volo.Abp.Cli.ProjectBuilding.Templates.Console; using Volo.Abp.Cli.ProjectBuilding.Templates.Maui; using Volo.Abp.Cli.ProjectBuilding.Templates.MvcModule; using Volo.Abp.Cli.ProjectBuilding.Templates.Wpf; +using Volo.Abp.Cli.Version; using Volo.Abp.DependencyInjection; using Volo.Abp.Http; using Volo.Abp.IO; @@ -37,18 +39,21 @@ public class AbpIoSourceCodeStore : ISourceCodeStore, ITransientDependency protected ICancellationTokenProvider CancellationTokenProvider { get; } private readonly CliHttpClientFactory _cliHttpClientFactory; + protected CliVersionService CliVersionService { get; } public AbpIoSourceCodeStore( IOptions options, IJsonSerializer jsonSerializer, IRemoteServiceExceptionHandler remoteServiceExceptionHandler, ICancellationTokenProvider cancellationTokenProvider, - CliHttpClientFactory cliHttpClientFactory) + CliHttpClientFactory cliHttpClientFactory, + CliVersionService cliVersionService) { JsonSerializer = jsonSerializer; RemoteServiceExceptionHandler = remoteServiceExceptionHandler; CancellationTokenProvider = cancellationTokenProvider; _cliHttpClientFactory = cliHttpClientFactory; + CliVersionService = cliVersionService; Options = options.Value; Logger = NullLogger.Instance; @@ -63,8 +68,8 @@ public class AbpIoSourceCodeStore : ISourceCodeStore, ITransientDependency bool skipCache = false) { DirectoryHelper.CreateIfNotExists(CliPaths.TemplateCache); + var userSpecifiedVersion = version != null; var latestVersion = version ?? await GetLatestSourceCodeVersionAsync(name, type, null, includePreReleases); - if (version == null) { if (latestVersion == null) @@ -86,14 +91,68 @@ public class AbpIoSourceCodeStore : ISourceCodeStore, ITransientDependency version = latestVersion; } - else + + var currentCliVersion = await CliVersionService.GetCurrentCliVersionAsync(); + var templateVersion = SemanticVersion.Parse(version); + + var outputWarning = false; + if (currentCliVersion.Major != templateVersion.Major || currentCliVersion.Minor != templateVersion.Minor) + { + // major and minor version are different + outputWarning = true; + } + else if (currentCliVersion.Major == templateVersion.Major && + currentCliVersion.Minor == templateVersion.Minor && + currentCliVersion.Patch < templateVersion.Patch) { - if (!await IsVersionExists(name, version)) + // major and minor version are same but patch version is lower + outputWarning = true; + } + else if(currentCliVersion.Major == templateVersion.Major && + currentCliVersion.Minor == templateVersion.Minor && + currentCliVersion.Patch == templateVersion.Patch && + currentCliVersion.IsPrerelease && templateVersion.IsPrerelease) + { + // major and minor and patch version are same but prerelease version may be lower + var cliRcVersion = currentCliVersion.ReleaseLabels.LastOrDefault(); + var templateRcVersion = templateVersion.ReleaseLabels.LastOrDefault(); + if (cliRcVersion != null && templateRcVersion != null) { - throw new Exception("There is no version found with given version: " + version); + if (int.TryParse(cliRcVersion, out var cliRcVersionNumber) && int.TryParse(templateRcVersion, out var templateRcVersionNumber)) + { + if (cliRcVersionNumber < templateRcVersionNumber) + { + outputWarning = true; + } + } } } + if (outputWarning) + { + version = currentCliVersion.ToString(); + Logger.LogWarning(userSpecifiedVersion + ? $"The specified template version ({templateVersion}) is different than the CLI version ({currentCliVersion}). This may cause compatibility issues." + : $"The latest template version ({templateVersion}) is different than the CLI version ({currentCliVersion}). This may cause compatibility issues."); + Logger.LogWarning("Please upgrade/downgrade the CLI version to the template version."); + Logger.LogWarning($"> dotnet tool uninstall -g volo.abp.cli"); + Logger.LogWarning(!templateVersion.IsPrerelease + ? $"> dotnet tool install -g volo.abp.cli --version \"{templateVersion.Major}.{templateVersion.Minor}.*\"" + : $"> dotnet tool install -g volo.abp.cli --version {templateVersion}"); + + if (!userSpecifiedVersion) + { + version = currentCliVersion.ToString(); + Logger.LogWarning($"We have changed the template version as the cli version."); + Logger.LogWarning($"New version: {version}"); + } + } + + if (!await IsVersionExists(name, version)) + { + throw new Exception("There is no version found with given version: " + version); + } + var nugetVersion = (await GetTemplateNugetVersionAsync(name, type, version)) ?? version; if (!string.IsNullOrWhiteSpace(templateSource) && !IsNetworkSource(templateSource)) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxying/Angular/AngularServiceProxyGenerator.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxying/Angular/AngularServiceProxyGenerator.cs index 0707a5e8e0..3a29358350 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxying/Angular/AngularServiceProxyGenerator.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ServiceProxying/Angular/AngularServiceProxyGenerator.cs @@ -7,6 +7,7 @@ using NuGet.Versioning; using Volo.Abp.Cli.Commands; using Volo.Abp.Cli.Http; using Volo.Abp.Cli.Utils; +using Volo.Abp.Cli.Version; using Volo.Abp.DependencyInjection; using Volo.Abp.Json; @@ -16,18 +17,18 @@ public class AngularServiceProxyGenerator : ServiceProxyGeneratorBase GetCurrentCliVersionAsync() + { + SemanticVersion currentCliVersion = default; + + var consoleOutput = new StringReader(CmdHelper.RunCmdAndGetOutput($"dotnet tool list -g", out int exitCode)); + string line; + while ((line = await consoleOutput.ReadLineAsync()) != null) + { + if (line.StartsWith("volo.abp.cli", StringComparison.InvariantCultureIgnoreCase)) + { + var version = line.Split(new char[0], StringSplitOptions.RemoveEmptyEntries)[1]; + + SemanticVersion.TryParse(version, out currentCliVersion); + + break; + } + } + + if (currentCliVersion == null) + { + // If not a tool executable, fallback to assembly version and treat as dev without updates + // Assembly revisions are not supported by SemVer scheme required for NuGet, trim to {major}.{minor}.{patch} + var assemblyVersion = string.Join(".", Assembly.GetExecutingAssembly().GetFileVersion().Split('.').Take(3)); + return SemanticVersion.Parse(assemblyVersion + "-dev"); + } + + return currentCliVersion; + } +}