From 4f46d8fea151f4b0f91982e3bbc0f141e7deac61 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Fri, 17 May 2019 11:31:43 +0300 Subject: [PATCH 1/4] Automatically install CLI dependencies if not installed before Resolves #1121 --- .../Volo/Abp/Cli/CliService.cs | 72 ++++++++++++++++++- 1 file changed, 69 insertions(+), 3 deletions(-) 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 50784a8d90..c87e6818dc 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 @@ -1,4 +1,6 @@ using System; +using System.Diagnostics; +using System.IO; using System.Reflection; using Microsoft.Extensions.DependencyInjection; using System.Threading.Tasks; @@ -40,7 +42,9 @@ namespace Volo.Abp.Cli Logger.LogInformation("https://abp.io"); await CheckForNewVersion(); - + + CheckDependencies(); + var commandLineArgs = CommandLineArgumentParser.Parse(args); var commandType = CommandSelector.Select(commandLineArgs); @@ -63,6 +67,68 @@ namespace Volo.Abp.Cli } } + private void CheckDependencies() + { + var installedNpmPackages = GetInstalledNpmPackages(); + + if (!installedNpmPackages.Contains(" yarn@")) + { + InstallYarn(); + } + if (!installedNpmPackages.Contains(" gulp@")) + { + InstallGulp(); + } + } + + private string GetInstalledNpmPackages() + { + var output = ""; + + using (var process = new Process()) + { + process.StartInfo = new ProcessStartInfo("CMD.exe") + { + Arguments = "/C npm list -g --depth 0", + UseShellExecute = false, + CreateNoWindow = true, + RedirectStandardOutput = true, + RedirectStandardError = true + }; + process.Start(); + + using (var stdOut = process.StandardOutput) + { + using (var stdErr = process.StandardError) + { + output = stdOut.ReadToEnd(); + output += stdErr.ReadToEnd(); + + stdErr.Close(); + stdOut.Close(); + } + } + + process.Close(); + } + + return output; + } + + private void InstallYarn() + { + Logger.LogInformation("Installing yarn..."); + var process = Process.Start("CMD.exe", "/C npm install yarn -g"); + process.WaitForExit(); + } + + private void InstallGulp() + { + Logger.LogInformation("Installing gulp..."); + var process = Process.Start("CMD.exe", "/C npm install gulp -g"); + process.WaitForExit(); + } + private async Task CheckForNewVersion() { try @@ -73,7 +139,7 @@ namespace Volo.Abp.Cli if (!latestVersion.IsNullOrEmpty() && currentVersion != latestVersion) { Logger.LogInformation(""); - Logger.LogWarning("ABP CLI has a newer version (" + latestVersion + "). Please update to get the latest features and fixes."); + Logger.LogWarning("ABP CLI has a newer version. Please update to get the latest features and fixes."); Logger.LogWarning(""); Logger.LogWarning("Update Command: "); Logger.LogWarning(" dotnet tool update -g Volo.Abp.Cli"); @@ -86,7 +152,7 @@ namespace Volo.Abp.Cli Logger.LogWarning(e.Message); } } - + private static string GetCliVersion() { var version = typeof(CliService) From 0ebc343024b0b80f17cdd1d7a22b5a64301b54f4 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 20 May 2019 08:50:34 +0300 Subject: [PATCH 2/4] Update CliService.cs --- framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c87e6818dc..a0877af68f 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 @@ -139,7 +139,7 @@ namespace Volo.Abp.Cli if (!latestVersion.IsNullOrEmpty() && currentVersion != latestVersion) { Logger.LogInformation(""); - Logger.LogWarning("ABP CLI has a newer version. Please update to get the latest features and fixes."); + Logger.LogWarning("ABP CLI has a newer version (" + latestVersion + "). Please update to get the latest features and fixes."); Logger.LogWarning(""); Logger.LogWarning("Update Command: "); Logger.LogWarning(" dotnet tool update -g Volo.Abp.Cli"); From cc99ee5205271c6f95d3fbf4c96fb73f965f0c0d Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 20 May 2019 10:58:15 +0300 Subject: [PATCH 3/4] removed redundant disposes --- framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliService.cs | 5 ----- 1 file changed, 5 deletions(-) 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 a0877af68f..46ce98439e 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 @@ -103,13 +103,8 @@ namespace Volo.Abp.Cli { output = stdOut.ReadToEnd(); output += stdErr.ReadToEnd(); - - stdErr.Close(); - stdOut.Close(); } } - - process.Close(); } return output; From 7f2f24d3c121cf6b95322e3e3ecbf1d690e94dc7 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 20 May 2019 12:58:37 +0300 Subject: [PATCH 4/4] CmdHelper improvements --- .../Volo/Abp/Cli/CliService.cs | 14 +++++----- .../EfCoreMigrationAdder.cs | 7 +++-- .../Volo/Abp/Cli/Utils/CmdHelper.cs | 26 ++++++++++++++++++- 3 files changed, 35 insertions(+), 12 deletions(-) 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 46ce98439e..3c2a88094f 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 @@ -2,6 +2,7 @@ using System.Diagnostics; using System.IO; using System.Reflection; +using System.Runtime.InteropServices; using Microsoft.Extensions.DependencyInjection; using System.Threading.Tasks; using Microsoft.Extensions.Logging; @@ -9,6 +10,7 @@ using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.Cli.Args; using Volo.Abp.Cli.Commands; using Volo.Abp.Cli.NuGet; +using Volo.Abp.Cli.Utils; using Volo.Abp.DependencyInjection; namespace Volo.Abp.Cli @@ -70,7 +72,7 @@ namespace Volo.Abp.Cli private void CheckDependencies() { var installedNpmPackages = GetInstalledNpmPackages(); - + if (!installedNpmPackages.Contains(" yarn@")) { InstallYarn(); @@ -87,9 +89,9 @@ namespace Volo.Abp.Cli using (var process = new Process()) { - process.StartInfo = new ProcessStartInfo("CMD.exe") + process.StartInfo = new ProcessStartInfo(CmdHelper.GetFileName()) { - Arguments = "/C npm list -g --depth 0", + Arguments = CmdHelper.GetArguments("npm list -g --depth 0"), UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, @@ -113,15 +115,13 @@ namespace Volo.Abp.Cli private void InstallYarn() { Logger.LogInformation("Installing yarn..."); - var process = Process.Start("CMD.exe", "/C npm install yarn -g"); - process.WaitForExit(); + CmdHelper.RunCmd("npm install yarn -g"); } private void InstallGulp() { Logger.LogInformation("Installing gulp..."); - var process = Process.Start("CMD.exe", "/C npm install gulp -g"); - process.WaitForExit(); + CmdHelper.RunCmd("npm install gulp -g"); } private async Task CheckForNewVersion() diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationAdder.cs index d3178adcb3..d3c324431b 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationAdder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/EfCoreMigrationAdder.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics; using System.IO; +using Volo.Abp.Cli.Utils; using Volo.Abp.DependencyInjection; namespace Volo.Abp.Cli.ProjectModification @@ -12,8 +13,7 @@ namespace Volo.Abp.Cli.ProjectModification var moduleName = ParseModuleName(module); var migrationName = "Added_" + moduleName + "_Module" + GetUniquePostFix(); - var process = Process.Start("CMD.exe", "/C cd \"" + Path.GetDirectoryName(csprojFile) + "\" & dotnet ef migrations add " + migrationName); - process.WaitForExit(); + CmdHelper.RunCmd("cd \"" + Path.GetDirectoryName(csprojFile) + "\" & dotnet ef migrations add " + migrationName); if (updateDatabase) { @@ -23,8 +23,7 @@ namespace Volo.Abp.Cli.ProjectModification protected void UpdateDatabase(string csprojFile) { - var process = Process.Start("CMD.exe", "/C cd \"" + Path.GetDirectoryName(csprojFile) + "\" & dotnet ef database update"); - process.WaitForExit(); + CmdHelper.RunCmd("cd \"" + Path.GetDirectoryName(csprojFile) + "\" & dotnet ef database update"); } protected virtual string ParseModuleName(string fullModuleName) 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 01a0f00af0..c9225b62f0 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.Runtime.InteropServices; namespace Volo.Abp.Cli.Utils { @@ -12,8 +13,31 @@ namespace Volo.Abp.Cli.Utils public static void RunCmd(string command) { - var procStartInfo = new ProcessStartInfo("cmd.exe", "/C " + command); + var procStartInfo = new ProcessStartInfo(GetFileName(), GetArguments(command)); Process.Start(procStartInfo).WaitForExit(); } + + public static string GetArguments(string command) + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + return "-c \"" + command + "\""; + } + + //Windows default. + return "/C \"" + command + "\""; + } + + public static string GetFileName() + { + if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + //TODO: Test this. it should work for both operaion systems. + return "/bin/bash"; + } + + //Windows default. + return "cmd.exe"; + } } }