Skip to content

Commit

Permalink
Merge pull request #18608 from abpframework/EngincanV/cli-new-command
Browse files Browse the repository at this point in the history
CLI: Create `switch-to-prerc` command to only update npm packages to pre-rc versions.
  • Loading branch information
gizemmutukurt committed Jan 4, 2024
2 parents 391820c + 6e5a97d commit fd419a0
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public override void ConfigureServices(ServiceConfigurationContext context)
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 ABP version.";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -66,7 +66,7 @@ public class NpmPackagesUpdater : ITransientDependency

foreach (var file in fileList)
{
if (includePreviews)
if (includePreviews || includePreRc)
{
await CreateNpmrcFileAsync(Path.GetDirectoryName(file));
}
Expand All @@ -82,7 +82,9 @@ async Task UpdateAsync(string file)
{
var updated = await UpdatePackagesInFile(file, includePreviews, includeReleaseCandidates,
switchToStable,
version);
version,
includePreRc);

packagesUpdated.TryAdd(file, updated);
}

Expand Down Expand Up @@ -162,7 +164,8 @@ private static bool IsAngularProject(string fileDirectory)
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);
Expand All @@ -177,7 +180,7 @@ private static bool IsAngularProject(string fileDirectory)
foreach (var abpPackage in abpPackages)
{
var updated = await TryUpdatingPackage(filePath, abpPackage, includePreviews, includeReleaseCandidates,
switchToStable, specifiedVersion);
switchToStable, specifiedVersion, includePreRc);

if (updated)
{
Expand All @@ -198,7 +201,8 @@ private static bool IsAngularProject(string fileDirectory)
bool includePreviews = false,
bool includeReleaseCandidates = false,
bool switchToStable = false,
string specifiedVersion = null)
string specifiedVersion = null,
bool includePreRc = false)
{
var currentVersion = (string)package.Value;

Expand All @@ -221,7 +225,11 @@ private static bool IsAngularProject(string fileDirectory)
}
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)
{
Expand Down Expand Up @@ -262,9 +270,10 @@ protected virtual bool IsPrerelease(string version)
return version.Split("-", StringSplitOptions.RemoveEmptyEntries).Length > 1;
}

protected virtual async Task<string> GetLatestVersion(JProperty package, bool includeReleaseCandidates = false, bool includePreviews = false, string workingDirectory = null)
protected virtual async Task<string> 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))
{
Expand All @@ -275,7 +284,11 @@ protected virtual async Task<string> GetLatestVersion(JProperty package, bool in

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"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,52 @@ public async Task SwitchToPreview(CommandLineArgs commandLineArgs)
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<string> projects)
{
Expand Down Expand Up @@ -88,22 +134,6 @@ private async Task SwitchSolutionsToPreview(List<string> solutionPaths)
}
}

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<string> projects)
{
foreach (var project in projects)
Expand Down Expand Up @@ -156,22 +186,6 @@ private async Task SwitchSolutionsToStable(List<string> solutionPaths)
}
}

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<string> projects)
{
foreach (var project in projects)
Expand Down Expand Up @@ -220,6 +234,38 @@ private async Task SwitchSolutionsToNightlyPreview(List<string> solutionPaths)
}
}
}

private async Task SwitchNpmPackageVersionsOfProjectsToPreRc(List<string> projects)
{
foreach (var project in projects)
{
var folder = Path.GetDirectoryName(project);

await _npmPackagesUpdater.Update(
folder,
includePreRc: true);
}
}

private async Task SwitchNpmPackageVersionsOfSolutionsToPreRc(List<string> 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<string> GetSolutionPaths(CommandLineArgs commandLineArgs)
{
Expand Down

0 comments on commit fd419a0

Please sign in to comment.