From d42f027d79a3e6c98e22f539d983d31e967863a8 Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 7 Jun 2022 18:06:49 +0800 Subject: [PATCH] Add `delaySeconds` paramter to CmdHelper. --- .../Volo/Abp/Cli/Commands/CliCommand.cs | 13 ++-------- .../Volo/Abp/Cli/Utils/CmdHelper.cs | 25 ++++++++++++++++--- .../Volo/Abp/Cli/Utils/ICmdHelper.cs | 8 +++--- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CliCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CliCommand.cs index c0f832908e..7a6926ecdd 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CliCommand.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CliCommand.cs @@ -59,7 +59,6 @@ public class CliCommand : IConsoleCommand, ITransientDependency private async Task UpdateCliAsync(string version = null, bool preview = false) { - RemoveCli(); var infoText = "Updating ABP CLI "; if (version != null) { @@ -94,15 +93,7 @@ public class CliCommand : IConsoleCommand, ITransientDependency versionOption = $" --version {version}"; } - _cmdHelper.RunCmd( - $"dotnet tool update {CliPackageName}{versionOption} -g", - out var exitCode - ); - - if (exitCode != 0) - { - ShowCliManualUpdateCommand(); - } + _cmdHelper.RunCmdAndExit($"dotnet tool update {CliPackageName}{versionOption} -g", delaySeconds: 2); } catch (Exception ex) { @@ -131,7 +122,7 @@ public class CliCommand : IConsoleCommand, ITransientDependency private void RemoveCli() { Logger.LogInformation("Removing CLI..."); - _cmdHelper.RunCmd("dotnet tool uninstall " + CliPackageName + " -g"); + _cmdHelper.RunCmdAndExit("dotnet tool uninstall " + CliPackageName + " -g", delaySeconds: 2); } public string GetUsageInfo() diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Utils/CmdHelper.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Utils/CmdHelper.cs index 741390dc73..c6325a4681 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Utils/CmdHelper.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Utils/CmdHelper.cs @@ -1,4 +1,5 @@ -using System.Diagnostics; +using System; +using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; using Volo.Abp.DependencyInjection; @@ -125,15 +126,31 @@ public class CmdHelper : ICmdHelper, ITransientDependency return output.Trim(); } - public string GetArguments(string command) + public void RunCmdAndExit(string command, string workingDirectory = null, int? delaySeconds = null) + { + var procStartInfo = new ProcessStartInfo( + GetFileName(), + GetArguments(command, delaySeconds) + ); + + if (!string.IsNullOrEmpty(workingDirectory)) + { + procStartInfo.WorkingDirectory = workingDirectory; + } + + Process.Start(procStartInfo); + Environment.Exit(0); + } + + public string GetArguments(string command, int? delaySeconds = null) { if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { - return "-c \"" + command + "\""; + return delaySeconds == null ? "-c \"" + command + "\"" : "-c \"" + $"sleep {delaySeconds}s > /dev/null && " + command + "\""; } //Windows default. - return "/C \"" + command + "\""; + return delaySeconds == null ? "/C \"" + command + "\"" : "/C \"" + $"timeout /nobreak /t {delaySeconds} >null && " + command + "\""; } public string GetFileName() diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Utils/ICmdHelper.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Utils/ICmdHelper.cs index 648215d24f..f96697b1cc 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Utils/ICmdHelper.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Utils/ICmdHelper.cs @@ -8,20 +8,22 @@ public interface ICmdHelper void Run(string file, string arguments); - string GetArguments(string command); + string GetArguments(string command, int? delaySeconds = null); string GetFileName(); void RunCmd(string command, string workingDirectory = null); Process RunCmdAndGetProcess(string command, string workingDirectory = null); - + void RunCmd(string command, out int exitCode, string workingDirectory = null); string RunCmdAndGetOutput(string command, string workingDirectory = null); - string RunCmdAndGetOutput(string command, out bool isExitCodeSuccessful, string workingDirectory = null); string RunCmdAndGetOutput(string command, out int exitCode, string workingDirectory = null); + + void RunCmdAndExit(string command, string workingDirectory = null, int? delaySeconds = null); + }