From 33f45d504b1b3d4f78a7d45b9a994c198c91a744 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 22 Feb 2021 14:06:56 +0300 Subject: [PATCH 1/3] CLI CreateMigrationAndRunMigrator: check if EF Core global tool installed resolves https://github.com/abpframework/abp/issues/7800 --- .../Commands/CreateMigrationAndRunMigrator.cs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CreateMigrationAndRunMigrator.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CreateMigrationAndRunMigrator.cs index 982ef77065..b6eadd4183 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CreateMigrationAndRunMigrator.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CreateMigrationAndRunMigrator.cs @@ -2,6 +2,8 @@ using System.IO; using System.Linq; using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.Cli.Args; using Volo.Abp.Cli.Utils; using Volo.Abp.DependencyInjection; @@ -10,6 +12,13 @@ namespace Volo.Abp.Cli.Commands { public class CreateMigrationAndRunMigrator : IConsoleCommand, ITransientDependency { + public ILogger Logger { get; set; } + + public CreateMigrationAndRunMigrator() + { + Logger = NullLogger.Instance; + } + public virtual async Task ExecuteAsync(CommandLineArgs commandLineArgs) { if (commandLineArgs.Target.IsNullOrEmpty()) @@ -26,6 +35,8 @@ namespace Volo.Abp.Cli.Commands throw new Exception("DbMigrator is not found!"); } + await CheckAndInstallDotnetEfIfNeededAsync(); + var output = CmdHelper.RunCmdAndGetOutput($"cd \"{commandLineArgs.Target}\" && dotnet ef migrations add Initial -s \"{dbMigratorProjectPath}\""); if (output.Contains("Done.") && output.Contains("To undo this action") && output.Contains("ef migrations remove")) // Migration added successfully @@ -38,6 +49,22 @@ namespace Volo.Abp.Cli.Commands } } + private async Task CheckAndInstallDotnetEfIfNeededAsync() + { + var output = CmdHelper.RunCmdAndGetOutput("dotnet tool list -g"); + + if (output.Contains("dotnet-ef")) + { + return; + } + + Logger.LogInformation("Installing dotnet-ef tool..."); + + CmdHelper.RunCmd("dotnet tool install --global dotnet-ef"); + + Logger.LogInformation("dotnet-ef tool is installed."); + } + private string GetDbMigratorProjectPath(string dbMigrationsFolderPath) { var srcFolder = Directory.GetParent(dbMigrationsFolderPath); From 37973efb459ec0615cad14b36e94168635b95a02 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Mon, 22 Feb 2021 14:07:43 +0300 Subject: [PATCH 2/3] rename CreateMigrationAndRunMigrator to CreateMigrationAndRunMigratorCommand --- .../Volo.Abp.Cli.Core/Volo/Abp/Cli/AbpCliCoreModule.cs | 2 +- ...igrator.cs => CreateMigrationAndRunMigratorCommand.cs} | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) rename framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/{CreateMigrationAndRunMigrator.cs => CreateMigrationAndRunMigratorCommand.cs} (89%) 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 9208357d4a..7ee6700d43 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 @@ -40,7 +40,7 @@ namespace Volo.Abp.Cli options.Commands["translate"] = typeof(TranslateCommand); options.Commands["build"] = typeof(BuildCommand); options.Commands["bundle"] = typeof(BundleCommand); - options.Commands["create-migration-and-run-migrator"] = typeof(CreateMigrationAndRunMigrator); + options.Commands["create-migration-and-run-migrator"] = typeof(CreateMigrationAndRunMigratorCommand); }); } } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CreateMigrationAndRunMigrator.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CreateMigrationAndRunMigratorCommand.cs similarity index 89% rename from framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CreateMigrationAndRunMigrator.cs rename to framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CreateMigrationAndRunMigratorCommand.cs index b6eadd4183..3946ca35bb 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CreateMigrationAndRunMigrator.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CreateMigrationAndRunMigratorCommand.cs @@ -10,13 +10,13 @@ using Volo.Abp.DependencyInjection; namespace Volo.Abp.Cli.Commands { - public class CreateMigrationAndRunMigrator : IConsoleCommand, ITransientDependency + public class CreateMigrationAndRunMigratorCommand : IConsoleCommand, ITransientDependency { - public ILogger Logger { get; set; } + public ILogger Logger { get; set; } - public CreateMigrationAndRunMigrator() + public CreateMigrationAndRunMigratorCommand() { - Logger = NullLogger.Instance; + Logger = NullLogger.Instance; } public virtual async Task ExecuteAsync(CommandLineArgs commandLineArgs) From 9a81666557e07162043b6854b3195e1557d51672 Mon Sep 17 00:00:00 2001 From: Alper Ebicoglu Date: Tue, 23 Feb 2021 10:10:50 +0300 Subject: [PATCH 3/3] refactor CreateMigrationAndRunMigratorCommand.cs --- .../CreateMigrationAndRunMigratorCommand.cs | 58 ++++++++++--------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CreateMigrationAndRunMigratorCommand.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CreateMigrationAndRunMigratorCommand.cs index 3946ca35bb..68fae779ce 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CreateMigrationAndRunMigratorCommand.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CreateMigrationAndRunMigratorCommand.cs @@ -23,60 +23,66 @@ namespace Volo.Abp.Cli.Commands { if (commandLineArgs.Target.IsNullOrEmpty()) { - throw new CliUsageException( - "DbMigrations folder path is missing!" - ); + throw new CliUsageException("DbMigrations folder path is missing!"); } var dbMigratorProjectPath = GetDbMigratorProjectPath(commandLineArgs.Target); - if (dbMigratorProjectPath == null) { throw new Exception("DbMigrator is not found!"); } - await CheckAndInstallDotnetEfIfNeededAsync(); + if (!IsDotNetEfToolInstalled()) + { + InstallDotnetEfTool(); + } - var output = CmdHelper.RunCmdAndGetOutput($"cd \"{commandLineArgs.Target}\" && dotnet ef migrations add Initial -s \"{dbMigratorProjectPath}\""); + var addMigrationCmd = $"cd \"{commandLineArgs.Target}\" && " + + $"dotnet ef migrations add Initial -s \"{dbMigratorProjectPath}\""; - if (output.Contains("Done.") && output.Contains("To undo this action") && output.Contains("ef migrations remove")) // Migration added successfully + var output = CmdHelper.RunCmdAndGetOutput(addMigrationCmd); + if (output.Contains("Done.") && + output.Contains("To undo this action") && + output.Contains("ef migrations remove")) { + // Migration added successfully CmdHelper.RunCmd("cd \"" + Path.GetDirectoryName(dbMigratorProjectPath) + "\" && dotnet run"); + await Task.CompletedTask; } else { - throw new Exception("Migrations failed: " + output); + var exceptionMsg = "Migrations failed! The following command didn't run successfully:" + + Environment.NewLine + + addMigrationCmd + + Environment.NewLine + output; + + Logger.LogError(exceptionMsg); + throw new Exception(exceptionMsg); } } - private async Task CheckAndInstallDotnetEfIfNeededAsync() + private static bool IsDotNetEfToolInstalled() { var output = CmdHelper.RunCmdAndGetOutput("dotnet tool list -g"); + return output.Contains("dotnet-ef"); + } - if (output.Contains("dotnet-ef")) - { - return; - } - + private void InstallDotnetEfTool() + { Logger.LogInformation("Installing dotnet-ef tool..."); - CmdHelper.RunCmd("dotnet tool install --global dotnet-ef"); - Logger.LogInformation("dotnet-ef tool is installed."); } - private string GetDbMigratorProjectPath(string dbMigrationsFolderPath) + private static string GetDbMigratorProjectPath(string dbMigrationsFolderPath) { var srcFolder = Directory.GetParent(dbMigrationsFolderPath); + var dbMigratorDirectory = Directory.GetDirectories(srcFolder.FullName) + .FirstOrDefault(d => d.EndsWith(".DbMigrator")); - var dbMigratorFolderPath = Directory.GetDirectories(srcFolder.FullName).FirstOrDefault(d => d.EndsWith(".DbMigrator")); - - if (dbMigratorFolderPath == null) - { - return null; - } - - return Directory.GetFiles(dbMigratorFolderPath).FirstOrDefault(f => f.EndsWith(".csproj")); + return dbMigratorDirectory == null + ? null + : Directory.GetFiles(dbMigratorDirectory).FirstOrDefault(f => f.EndsWith(".csproj")); } public string GetUsageInfo() @@ -89,4 +95,4 @@ namespace Volo.Abp.Cli.Commands return string.Empty; } } -} +} \ No newline at end of file