diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/ProjectCreationCommandBase.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/ProjectCreationCommandBase.cs index 4eff9df5e6..2ee023445c 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/ProjectCreationCommandBase.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/Commands/ProjectCreationCommandBase.cs @@ -450,7 +450,7 @@ public abstract class ProjectCreationCommandBase var efCoreProjectPath = string.Empty; bool isLayeredTemplate; - + var isModuleTemplate = false; switch (projectArgs.TemplateName) { case AppTemplate.TemplateName: @@ -464,11 +464,16 @@ public abstract class ProjectCreationCommandBase ?? Directory.GetFiles(projectArgs.OutputFolder, "*.csproj", SearchOption.AllDirectories).FirstOrDefault(); isLayeredTemplate = false; break; + case ModuleTemplate.TemplateName: + case ModuleProTemplate.TemplateName: + isModuleTemplate = true; + isLayeredTemplate = false; + break; default: return; } - if (string.IsNullOrWhiteSpace(efCoreProjectPath)) + if (string.IsNullOrWhiteSpace(efCoreProjectPath) && !isModuleTemplate) { Logger.LogWarning("Couldn't find the project to create initial migrations!"); return; @@ -479,7 +484,20 @@ public abstract class ProjectCreationCommandBase Message = "Creating the initial DB migration" }, false); - await InitialMigrationCreator.CreateAsync(Path.GetDirectoryName(efCoreProjectPath), isLayeredTemplate); + if (!isModuleTemplate) + { + await InitialMigrationCreator.CreateAsync(Path.GetDirectoryName(efCoreProjectPath), isLayeredTemplate); + } + else + { + var hostProjectsWithEfCore = Directory.GetFiles(projectArgs.OutputFolder, "*.csproj", SearchOption.AllDirectories) + .Where(x => File.ReadAllText(x).Contains("Microsoft.EntityFrameworkCore.Tools")) + .ToList(); + foreach (var project in hostProjectsWithEfCore) + { + await InitialMigrationCreator.CreateAsync(Path.GetDirectoryName(project)); + } + } } protected Task CreateOpenIddictPfxFilesAsync(ProjectBuildArgs projectArgs) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/AppModuleDatabaseManagementSystemChangeStep.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/AppModuleDatabaseManagementSystemChangeStep.cs new file mode 100644 index 0000000000..362fc14713 --- /dev/null +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/AppModuleDatabaseManagementSystemChangeStep.cs @@ -0,0 +1,113 @@ +using System; +using System.Linq; +using Volo.Abp.Cli.ProjectBuilding.Files; +using Volo.Abp.Cli.ProjectBuilding.Templates.App; + +namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps; + +public class AppModuleDatabaseManagementSystemChangeStep : ProjectBuildPipelineStep +{ + public override void Execute(ProjectBuildContext context) + { + switch (context.BuildArgs.DatabaseManagementSystem) + { + case DatabaseManagementSystem.MySQL: + ChangeEntityFrameworkCoreDependency(context, "Volo.Abp.EntityFrameworkCore.MySQL", + "Volo.Abp.EntityFrameworkCore.MySQL", + "AbpEntityFrameworkCoreMySQLModule"); + AddMySqlServerVersion(context); + ChangeUseSqlServer(context, "UseMySQL", "UseMySql"); + break; + + case DatabaseManagementSystem.PostgreSQL: + ChangeEntityFrameworkCoreDependency(context, "Volo.Abp.EntityFrameworkCore.PostgreSql", + "Volo.Abp.EntityFrameworkCore.PostgreSql", + "AbpEntityFrameworkCorePostgreSqlModule"); + ChangeUseSqlServer(context, "UseNpgsql"); + break; + + case DatabaseManagementSystem.Oracle: + ChangeEntityFrameworkCoreDependency(context, "Volo.Abp.EntityFrameworkCore.Oracle", + "Volo.Abp.EntityFrameworkCore.Oracle", + "AbpEntityFrameworkCoreOracleModule"); + AdjustOracleDbContextOptionsBuilder(context); + ChangeUseSqlServer(context, "UseOracle"); + break; + + case DatabaseManagementSystem.OracleDevart: + ChangeEntityFrameworkCoreDependency(context, "Volo.Abp.EntityFrameworkCore.Oracle.Devart", + "Volo.Abp.EntityFrameworkCore.Oracle.Devart", + "AbpEntityFrameworkCoreOracleDevartModule"); + AdjustOracleDbContextOptionsBuilder(context); + ChangeUseSqlServer(context, "UseOracle"); + break; + + case DatabaseManagementSystem.SQLite: + ChangeEntityFrameworkCoreDependency(context, "Volo.Abp.EntityFrameworkCore.Sqlite", + "Volo.Abp.EntityFrameworkCore.Sqlite", + "AbpEntityFrameworkCoreSqliteModule"); + ChangeUseSqlServer(context, "UseSqlite"); + break; + + default: + return; + } + } + + private void AdjustOracleDbContextOptionsBuilder(ProjectBuildContext context) + { + var dbContextFactoryFiles = context.Files.Where(f => f.Name.EndsWith("DbContextFactory.cs", StringComparison.OrdinalIgnoreCase)); + foreach (var dbContextFactoryFile in dbContextFactoryFiles) + { + dbContextFactoryFile?.ReplaceText("new DbContextOptionsBuilder", + $"(DbContextOptionsBuilder<{context.BuildArgs.SolutionName.ProjectName}{(false ? "Migrations" : string.Empty)}DbContext>) new DbContextOptionsBuilder"); + } + } + + private void AddMySqlServerVersion(ProjectBuildContext context) + { + var dbContextFactoryFiles = context.Files.Where(f => f.Name.EndsWith("DbContextFactory.cs", StringComparison.OrdinalIgnoreCase)); + foreach (var dbContextFactoryFile in dbContextFactoryFiles) + { + dbContextFactoryFile?.ReplaceText("configuration.GetConnectionString(\"Default\")", "configuration.GetConnectionString(\"Default\"), MySqlServerVersion.LatestSupportedServerVersion"); + } + } + + private void ChangeEntityFrameworkCoreDependency(ProjectBuildContext context, string newPackageName, string newModuleNamespace, string newModuleClass) + { + var efCoreProjectFiles = context.Files.Where(f => f.Name.EndsWith(".csproj", StringComparison.OrdinalIgnoreCase)); + foreach (var efCoreProjectFile in efCoreProjectFiles) + { + efCoreProjectFile?.ReplaceText("Volo.Abp.EntityFrameworkCore.SqlServer", newPackageName); + } + + var efCoreModuleClasses = context.Files.Where(f => f.Name.EndsWith("Module.cs", StringComparison.OrdinalIgnoreCase)); + foreach (var efCoreModuleClass in efCoreModuleClasses) + { + efCoreModuleClass?.ReplaceText("Volo.Abp.EntityFrameworkCore.SqlServer", newModuleNamespace); + efCoreModuleClass?.ReplaceText("AbpEntityFrameworkCoreSqlServerModule", newModuleClass); + } + } + + private void ChangeUseSqlServer(ProjectBuildContext context, string newUseMethodForEfModule, string newUseMethodForDbContext = null) + { + if (newUseMethodForDbContext == null) + { + newUseMethodForDbContext = newUseMethodForEfModule; + } + + const string oldUseMethod = "UseSqlServer"; + + var efCoreModuleClasses = context.Files.Where(f => f.Name.EndsWith("Module.cs", StringComparison.OrdinalIgnoreCase)); + foreach (var efCoreModuleClass in efCoreModuleClasses) + { + efCoreModuleClass.ReplaceText(oldUseMethod, newUseMethodForEfModule); + } + + var dbContextFactoryFiles = context.Files.Where(f => f.Name.EndsWith("DbContextFactory.cs", StringComparison.OrdinalIgnoreCase)); + foreach (var dbContextFactoryFile in dbContextFactoryFiles) + { + dbContextFactoryFile?.ReplaceText(oldUseMethod, newUseMethodForDbContext); + } + } +} diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/TemplateProjectBuildPipelineBuilder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/TemplateProjectBuildPipelineBuilder.cs index d991c7101e..a453173095 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/TemplateProjectBuildPipelineBuilder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/TemplateProjectBuildPipelineBuilder.cs @@ -43,6 +43,12 @@ public static class TemplateProjectBuildPipelineBuilder pipeline.Steps.Add(new AppNoLayersDatabaseManagementSystemChangeStep()); // todo: move to custom steps? } + if (context.Template.Name == ModuleTemplate.TemplateName || + context.Template.Name == ModuleProTemplate.TemplateName) + { + pipeline.Steps.Add(new AppModuleDatabaseManagementSystemChangeStep()); // todo: move to custom steps? + } + if ((context.BuildArgs.UiFramework == UiFramework.Mvc || context.BuildArgs.UiFramework == UiFramework.Blazor || context.BuildArgs.UiFramework == UiFramework.BlazorServer) && context.BuildArgs.MobileApp == MobileApp.None && context.Template.Name != MicroserviceProTemplate.TemplateName && context.Template.Name != MicroserviceServiceProTemplate.TemplateName) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/Module/ModuleTemplateBase.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/Module/ModuleTemplateBase.cs index 428c1061a1..60cf5e7442 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/Module/ModuleTemplateBase.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/Module/ModuleTemplateBase.cs @@ -27,6 +27,7 @@ public abstract class ModuleTemplateBase : TemplateInfo DeleteUnrelatedProjects(context, steps); RandomizeSslPorts(context, steps); UpdateNuGetConfig(context, steps); + RemoveMigrations(context, steps); ChangeConnectionString(context, steps); CleanupFolderHierarchy(context, steps); @@ -103,6 +104,17 @@ public abstract class ModuleTemplateBase : TemplateInfo steps.Add(new UpdateNuGetConfigStep("/NuGet.Config")); } + protected void RemoveMigrations(ProjectBuildContext context, List steps) + { + steps.Add(new RemoveFolderStep("/aspnet-core/host/MyCompanyName.MyProjectName.AuthServer/Migrations")); + steps.Add(new RemoveFolderStep("/aspnet-core/host/MyCompanyName.MyProjectName.Blazor.Server.Host/Migrations")); + steps.Add(new RemoveFolderStep("/aspnet-core/host/MyCompanyName.MyProjectName.Web.Unified/Migrations")); + if (context.BuildArgs.TemplateName == ModuleProTemplate.TemplateName) + { + steps.Add(new RemoveFolderStep("/aspnet-core/host/MyCompanyName.MyProjectName.HttpApi.Host/Migrations")); + } + } + private void ChangeConnectionString(ProjectBuildContext context, List steps) { if (context.BuildArgs.ConnectionString != null)