From f06b1d7b6cbc16874532f4458c6cef691c5b00bb Mon Sep 17 00:00:00 2001 From: Engincan VESKE <43685404+EngincanV@users.noreply.github.com> Date: Wed, 27 Dec 2023 11:44:01 +0300 Subject: [PATCH] CLI: Add `switch-to-prerc` command. --- .../Volo/Abp/Cli/AbpCliCoreModule.cs | 1 + .../Abp/Cli/Commands/SwitchToPreRcCommand.cs | 45 +++++++ .../ProjectModification/NpmPackagesUpdater.cs | 33 ++++-- .../PackagePreviewSwitcher.cs | 110 +++++++++++++----- 4 files changed, 147 insertions(+), 42 deletions(-) create mode 100644 framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchToPreRcCommand.cs diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs index e7289b7edd..3dbe72f014 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs @@ -56,6 +56,7 @@ public class AbpCliCoreModule : AbpModule options.Commands[SwitchToPreviewCommand.Name] = typeof(SwitchToPreviewCommand); options.Commands[SwitchToStableCommand.Name] = typeof(SwitchToStableCommand); options.Commands[SwitchToNightlyCommand.Name] = typeof(SwitchToNightlyCommand); + options.Commands[SwitchToPreRcCommand.Name] = typeof(SwitchToPreRcCommand); options.Commands[SwitchToLocal.Name] = typeof(SwitchToLocal); options.Commands[TranslateCommand.Name] = typeof(TranslateCommand); options.Commands[BuildCommand.Name] = typeof(BuildCommand); diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchToPreRcCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchToPreRcCommand.cs new file mode 100644 index 0000000000..078a593f18 --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchToPreRcCommand.cs @@ -0,0 +1,45 @@ +using System.Text; +using System.Threading.Tasks; +using Volo.Abp.Cli.Args; +using Volo.Abp.Cli.ProjectModification; +using Volo.Abp.DependencyInjection; + +namespace Volo.Abp.Cli.Commands; + +public class SwitchToPreRcCommand : IConsoleCommand, ITransientDependency +{ + public const string Name = "switch-to-prerc"; + + private readonly PackagePreviewSwitcher _packagePreviewSwitcher; + + public SwitchToPreRcCommand(PackagePreviewSwitcher packagePreviewSwitcher) + { + _packagePreviewSwitcher = packagePreviewSwitcher; + } + + public async Task ExecuteAsync(CommandLineArgs commandLineArgs) + { + await _packagePreviewSwitcher.SwitchToPreRc(commandLineArgs); + } + + public string GetUsageInfo() + { + var sb = new StringBuilder(); + + sb.AppendLine(""); + sb.AppendLine("Usage:"); + sb.AppendLine(" abp switch-to-prerc [options]"); + sb.AppendLine(""); + sb.AppendLine("Options:"); + sb.AppendLine("-d|--directory"); + sb.AppendLine(""); + sb.AppendLine("See the documentation for more info: https://docs.abp.io/en/abp/latest/CLI"); + + return sb.ToString(); + } + + public string GetShortDescription() + { + return "Switches npm packages to pre-rc preview ABP version."; + } +} \ No newline at end of file diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/NpmPackagesUpdater.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/NpmPackagesUpdater.cs index d016fad655..e9964ce947 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/NpmPackagesUpdater.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/NpmPackagesUpdater.cs @@ -53,7 +53,7 @@ public class NpmPackagesUpdater : ITransientDependency public async Task Update(string rootDirectory, bool includePreviews = false, bool includeReleaseCandidates = false, - bool switchToStable = false, string version = null) + bool switchToStable = false, string version = null, bool includePreRc = false) { var fileList = _packageJsonFileFinder.Find(rootDirectory); @@ -66,7 +66,7 @@ public class NpmPackagesUpdater : ITransientDependency foreach (var file in fileList) { - if (includePreviews) + if (includePreviews || includePreRc) { await CreateNpmrcFileAsync(Path.GetDirectoryName(file)); } @@ -82,7 +82,9 @@ public class NpmPackagesUpdater : ITransientDependency { var updated = await UpdatePackagesInFile(file, includePreviews, includeReleaseCandidates, switchToStable, - version); + version, + includePreRc); + packagesUpdated.TryAdd(file, updated); } @@ -162,7 +164,8 @@ public class NpmPackagesUpdater : ITransientDependency bool includePreviews = false, bool includeReleaseCandidates = false, bool switchToStable = false, - string specifiedVersion = null) + string specifiedVersion = null, + bool includePreRc = false) { var packagesUpdated = false; var fileContent = File.ReadAllText(filePath); @@ -177,7 +180,7 @@ public class NpmPackagesUpdater : ITransientDependency foreach (var abpPackage in abpPackages) { var updated = await TryUpdatingPackage(filePath, abpPackage, includePreviews, includeReleaseCandidates, - switchToStable, specifiedVersion); + switchToStable, specifiedVersion, includePreRc); if (updated) { @@ -198,7 +201,8 @@ public class NpmPackagesUpdater : ITransientDependency bool includePreviews = false, bool includeReleaseCandidates = false, bool switchToStable = false, - string specifiedVersion = null) + string specifiedVersion = null, + bool includePreRc = false) { var currentVersion = (string)package.Value; @@ -221,7 +225,11 @@ public class NpmPackagesUpdater : ITransientDependency } else { - if ((includePreviews || + if (includePreRc && !includeReleaseCandidates) + { + version = await GetLatestVersion(package, includePreRc: true, workingDirectory: filePath.RemovePostFix("package.json")); + } + else if ((includePreviews || (!switchToStable && (currentVersion != null && currentVersion.Contains("-preview")))) && !includeReleaseCandidates) { @@ -262,9 +270,10 @@ public class NpmPackagesUpdater : ITransientDependency return version.Split("-", StringSplitOptions.RemoveEmptyEntries).Length > 1; } - protected virtual async Task GetLatestVersion(JProperty package, bool includeReleaseCandidates = false, bool includePreviews = false, string workingDirectory = null) + protected virtual async Task GetLatestVersion(JProperty package, bool includeReleaseCandidates = false, bool includePreviews = false, string workingDirectory = null, bool includePreRc = false) { - var key = package.Name + (includePreviews ? "(preview)" : string.Empty); + var postfix = includePreviews ? "(preview)" : (includePreRc ? "(prerc)" : string.Empty); + var key = package.Name + postfix; if (_fileVersionStorage.ContainsKey(key)) { @@ -275,7 +284,11 @@ public class NpmPackagesUpdater : ITransientDependency string newVersion = string.Empty; - if (includePreviews) + if (includePreRc) + { + newVersion = versionList.FirstOrDefault(v => v.Contains("-prerc")); + } + else if (includePreviews) { newVersion = versionList.FirstOrDefault(v => v.Contains("-preview")); } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/PackagePreviewSwitcher.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/PackagePreviewSwitcher.cs index 9e65595182..2b9a595742 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/PackagePreviewSwitcher.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/PackagePreviewSwitcher.cs @@ -44,6 +44,52 @@ public class PackagePreviewSwitcher : ITransientDependency await SwitchProjectsToPreview(projectPaths); } } + + public async Task SwitchToStable(CommandLineArgs commandLineArgs) + { + var solutionPaths = GetSolutionPaths(commandLineArgs); + + if (solutionPaths.Any()) + { + await SwitchSolutionsToStable(solutionPaths); + } + else + { + var projectPaths = GetProjectPaths(commandLineArgs); + + await SwitchProjectsToStable(projectPaths); + } + } + + public async Task SwitchToNightlyPreview(CommandLineArgs commandLineArgs) + { + var solutionPaths = GetSolutionPaths(commandLineArgs); + + if (solutionPaths.Any()) + { + await SwitchSolutionsToNightlyPreview(solutionPaths); + } + else + { + var projectPaths = GetProjectPaths(commandLineArgs); + + await SwitchProjectsToNightlyPreview(projectPaths); + } + } + + public async Task SwitchToPreRc(CommandLineArgs commandLineArgs) + { + var solutionPaths = GetSolutionPaths(commandLineArgs); + + if (solutionPaths.Any()) + { + await SwitchNpmPackageVersionsOfSolutionsToPreRc(solutionPaths); + } + else + { + await SwitchNpmPackageVersionsOfProjectsToPreRc(GetProjectPaths(commandLineArgs)); + } + } private async Task SwitchProjectsToPreview(List projects) { @@ -88,22 +134,6 @@ public class PackagePreviewSwitcher : ITransientDependency } } - public async Task SwitchToStable(CommandLineArgs commandLineArgs) - { - var solutionPaths = GetSolutionPaths(commandLineArgs); - - if (solutionPaths.Any()) - { - await SwitchSolutionsToStable(solutionPaths); - } - else - { - var projectPaths = GetProjectPaths(commandLineArgs); - - await SwitchProjectsToStable(projectPaths); - } - } - private async Task SwitchProjectsToStable(List projects) { foreach (var project in projects) @@ -156,22 +186,6 @@ public class PackagePreviewSwitcher : ITransientDependency } } - public async Task SwitchToNightlyPreview(CommandLineArgs commandLineArgs) - { - var solutionPaths = GetSolutionPaths(commandLineArgs); - - if (solutionPaths.Any()) - { - await SwitchSolutionsToNightlyPreview(solutionPaths); - } - else - { - var projectPaths = GetProjectPaths(commandLineArgs); - - await SwitchProjectsToNightlyPreview(projectPaths); - } - } - private async Task SwitchProjectsToNightlyPreview(List projects) { foreach (var project in projects) @@ -220,6 +234,38 @@ public class PackagePreviewSwitcher : ITransientDependency } } } + + private async Task SwitchNpmPackageVersionsOfProjectsToPreRc(List projects) + { + foreach (var project in projects) + { + var folder = Path.GetDirectoryName(project); + + await _npmPackagesUpdater.Update( + folder, + includePreRc: true); + } + } + + private async Task SwitchNpmPackageVersionsOfSolutionsToPreRc(List solutionPaths) + { + foreach (var solutionPath in solutionPaths) + { + var solutionFolder = Path.GetDirectoryName(solutionPath); + var solutionAngularFolder = GetSolutionAngularFolder(solutionFolder); + + await _npmPackagesUpdater.Update( + solutionFolder, + includePreRc: true); + + if (solutionAngularFolder != null) + { + await _npmPackagesUpdater.Update( + solutionAngularFolder, + includePreRc: true); + } + } + } private List GetSolutionPaths(CommandLineArgs commandLineArgs) {