From f9050f93b9ae4a572f6a029c9325cb42948958ac Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Tue, 17 Mar 2020 11:37:40 +0300 Subject: [PATCH] Fix: Switc-to-preview/stable command unable to switch npm packages resolves https://github.com/abpframework/abp/issues/3154 --- docs/en/CLI.md | 4 +- .../Commands/SwitchNightlyPreviewCommand.cs | 2 +- .../Abp/Cli/Commands/SwitchStableCommand.cs | 2 +- .../PackageSourceSwitcher.cs | 53 +++++++++++++------ 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/docs/en/CLI.md b/docs/en/CLI.md index b4f97b6d8f..13110c5ae5 100644 --- a/docs/en/CLI.md +++ b/docs/en/CLI.md @@ -141,7 +141,7 @@ abp switch-to-preview [options] ```` #### Options -`--solution-path` or `-sp`: Specifies the solution (.sln) file path. If not specified, CLI tries to find a .sln file in the current directory. +`--solution-directory` or `-sd`: Specifies the directory. The solution should be in that directory or in any of its sub directories. If not specified, default is the current directory. ### switch-to-stable @@ -154,7 +154,7 @@ abp switch-to-stable [options] ```` #### Options -`--solution-path` or `-sp`: Specifies the solution (.sln) file path. If not specified, CLI tries to find a .sln file in the current directory. +`--solution-directory` or `-sd`: Specifies the directory. The solution should be in that directory or in any of its sub directories. If not specified, default is the current directory. ### login diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchNightlyPreviewCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchNightlyPreviewCommand.cs index 9f0862258f..883ac4db2f 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchNightlyPreviewCommand.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchNightlyPreviewCommand.cs @@ -29,7 +29,7 @@ namespace Volo.Abp.Cli.Commands sb.AppendLine(" abp switch-to-preview [options]"); sb.AppendLine(""); sb.AppendLine("Options:"); - sb.AppendLine("-sp|--solution-path"); + sb.AppendLine("-sd|--solution-directory"); sb.AppendLine(""); sb.AppendLine("See the documentation for more info: https://docs.abp.io/en/abp/latest/CLI"); diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchStableCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchStableCommand.cs index 333583e96d..54db7bc082 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchStableCommand.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/SwitchStableCommand.cs @@ -29,7 +29,7 @@ namespace Volo.Abp.Cli.Commands sb.AppendLine(" abp switch-to-stable [options]"); sb.AppendLine(""); sb.AppendLine("Options:"); - sb.AppendLine("-sp|--solution-path"); + sb.AppendLine("-sd|--solution-directory"); sb.AppendLine(""); sb.AppendLine("See the documentation for more info: https://docs.abp.io/en/abp/latest/CLI"); diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/PackageSourceSwitcher.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/PackageSourceSwitcher.cs index e860a68ba8..f6c89be039 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/PackageSourceSwitcher.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/PackageSourceSwitcher.cs @@ -31,55 +31,74 @@ namespace Volo.Abp.Cli.ProjectModification { _packageSourceAdder.Add("ABP Nightly", "https://www.myget.org/F/abp-nightly/api/v3/index.json"); + var solutionPath = GetSolutionPath(commandLineArgs); + var solutionFolder = GetSolutionFolder(commandLineArgs); + await _nugetPackagesVersionUpdater.UpdateSolutionAsync( - GetSolutionPath(commandLineArgs), + solutionPath, true); await _npmPackagesUpdater.Update( - Path.GetFileName(GetSolutionPath(commandLineArgs)), + solutionFolder, true); } public async Task SwitchToStable(CommandLineArgs commandLineArgs) { + var solutionPath = GetSolutionPath(commandLineArgs); + var solutionFolder = GetSolutionFolder(commandLineArgs); + await _nugetPackagesVersionUpdater.UpdateSolutionAsync( - GetSolutionPath(commandLineArgs), + solutionPath, false, true); await _npmPackagesUpdater.Update( - Path.GetFileName(GetSolutionPath(commandLineArgs)), - false, + solutionFolder, + false, true); } - private string GetSolutionPath(CommandLineArgs commandLineArgs) { - var solutionPath = commandLineArgs.Options.GetOrNull(Options.SolutionPath.Short, Options.SolutionPath.Long); + var directory = commandLineArgs.Options.GetOrNull(Options.SolutionDirectory.Short, Options.SolutionDirectory.Long) + ?? Directory.GetCurrentDirectory(); + + var solutionPath = Directory.GetFiles(directory, "*.sln").FirstOrDefault(); if (solutionPath == null) { - try - { - solutionPath = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.sln").Single(); - } - catch (Exception) + var subDirectories = Directory.GetDirectories(directory); + + foreach (var subDirectory in subDirectories) { - Logger.LogError("There is no solution or more that one solution in current directory."); - throw; + var slnInSubDirectory = Directory.GetFiles(subDirectory, "*.sln").FirstOrDefault(); + + if (slnInSubDirectory != null) + { + return Path.Combine(subDirectory, slnInSubDirectory); + } } + + Logger.LogError("There is no solution or more that one solution in current directory."); + return null; } return solutionPath; } + private string GetSolutionFolder(CommandLineArgs commandLineArgs) + { + return commandLineArgs.Options.GetOrNull(Options.SolutionDirectory.Short, Options.SolutionDirectory.Long) + ?? Directory.GetCurrentDirectory(); + } + public static class Options { - public static class SolutionPath + public static class SolutionDirectory { - public const string Short = "sp"; - public const string Long = "solution-path"; + public const string Short = "sd"; + public const string Long = "solution-directory"; } } }