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/CreateMigrationAndRunMigrator.cs deleted file mode 100644 index 982ef77065..0000000000 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CreateMigrationAndRunMigrator.cs +++ /dev/null @@ -1,65 +0,0 @@ -using System; -using System.IO; -using System.Linq; -using System.Threading.Tasks; -using Volo.Abp.Cli.Args; -using Volo.Abp.Cli.Utils; -using Volo.Abp.DependencyInjection; - -namespace Volo.Abp.Cli.Commands -{ - public class CreateMigrationAndRunMigrator : IConsoleCommand, ITransientDependency - { - public virtual async Task ExecuteAsync(CommandLineArgs commandLineArgs) - { - if (commandLineArgs.Target.IsNullOrEmpty()) - { - throw new CliUsageException( - "DbMigrations folder path is missing!" - ); - } - - var dbMigratorProjectPath = GetDbMigratorProjectPath(commandLineArgs.Target); - - if (dbMigratorProjectPath == null) - { - throw new Exception("DbMigrator is not found!"); - } - - 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 - { - CmdHelper.RunCmd("cd \"" + Path.GetDirectoryName(dbMigratorProjectPath) + "\" && dotnet run"); - } - else - { - throw new Exception("Migrations failed: " + output); - } - } - - private string GetDbMigratorProjectPath(string dbMigrationsFolderPath) - { - var srcFolder = Directory.GetParent(dbMigrationsFolderPath); - - 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")); - } - - public string GetUsageInfo() - { - return string.Empty; - } - - public string GetShortDescription() - { - return string.Empty; - } - } -} 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 new file mode 100644 index 0000000000..68fae779ce --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/CreateMigrationAndRunMigratorCommand.cs @@ -0,0 +1,98 @@ +using System; +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; + +namespace Volo.Abp.Cli.Commands +{ + public class CreateMigrationAndRunMigratorCommand : IConsoleCommand, ITransientDependency + { + public ILogger Logger { get; set; } + + public CreateMigrationAndRunMigratorCommand() + { + Logger = NullLogger.Instance; + } + + public virtual async Task ExecuteAsync(CommandLineArgs commandLineArgs) + { + if (commandLineArgs.Target.IsNullOrEmpty()) + { + throw new CliUsageException("DbMigrations folder path is missing!"); + } + + var dbMigratorProjectPath = GetDbMigratorProjectPath(commandLineArgs.Target); + if (dbMigratorProjectPath == null) + { + throw new Exception("DbMigrator is not found!"); + } + + if (!IsDotNetEfToolInstalled()) + { + InstallDotnetEfTool(); + } + + var addMigrationCmd = $"cd \"{commandLineArgs.Target}\" && " + + $"dotnet ef migrations add Initial -s \"{dbMigratorProjectPath}\""; + + 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 + { + 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 static bool IsDotNetEfToolInstalled() + { + var output = CmdHelper.RunCmdAndGetOutput("dotnet tool list -g"); + return output.Contains("dotnet-ef"); + } + + 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 static string GetDbMigratorProjectPath(string dbMigrationsFolderPath) + { + var srcFolder = Directory.GetParent(dbMigrationsFolderPath); + var dbMigratorDirectory = Directory.GetDirectories(srcFolder.FullName) + .FirstOrDefault(d => d.EndsWith(".DbMigrator")); + + return dbMigratorDirectory == null + ? null + : Directory.GetFiles(dbMigratorDirectory).FirstOrDefault(f => f.EndsWith(".csproj")); + } + + public string GetUsageInfo() + { + return string.Empty; + } + + public string GetShortDescription() + { + return string.Empty; + } + } +} \ No newline at end of file