From 81db50a034cc8547cc22f77b7121ba445f1e6d58 Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 31 May 2021 15:48:16 +0800 Subject: [PATCH] Fallback to the EntityFrameworkCore project if EntityFrameworkCore.DbMigrations project does not exists for the application template. --- .../ProjectBuilding/TemplateProjectBuilder.cs | 6 ++ .../Templates/App/AppTemplateBase.cs | 56 ++++++++++--- ...eSwitchEntityFrameworkCoreToMongoDbStep.cs | 84 +++++++++++++------ 3 files changed, 111 insertions(+), 35 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/TemplateProjectBuilder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/TemplateProjectBuilder.cs index 0f224a20cb..52eb6fe5de 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/TemplateProjectBuilder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/TemplateProjectBuilder.cs @@ -5,6 +5,7 @@ using System; using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.Configuration; +using NuGet.Versioning; using Volo.Abp.Cli.Commands; using Volo.Abp.Cli.Licensing; using Volo.Abp.Cli.ProjectBuilding.Analyticses; @@ -111,6 +112,11 @@ namespace Volo.Abp.Cli.ProjectBuilding args ); + if (context.Template is AppTemplateBase appTemplateBase) + { + appTemplateBase.HasDbMigrations = SemanticVersion.Parse(templateFile.Version) >= new SemanticVersion(4, 4, 0); + } + TemplateProjectBuildPipelineBuilder.Build(context).Execute(); if (!templateInfo.DocumentUrl.IsNullOrEmpty()) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/App/AppTemplateBase.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/App/AppTemplateBase.cs index d515253659..d1f4940e26 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/App/AppTemplateBase.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/App/AppTemplateBase.cs @@ -9,9 +9,12 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.App { public abstract class AppTemplateBase : TemplateInfo { + public bool HasDbMigrations { get; set; } = false; + protected AppTemplateBase(string templateName) : base(templateName, DatabaseProvider.EntityFrameworkCore, UiFramework.Mvc) { + } public static bool IsAppTemplate(string templateName) @@ -40,20 +43,35 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.App return steps; } - private static void ConfigureTenantSchema(ProjectBuildContext context, List steps) + private void ConfigureTenantSchema(ProjectBuildContext context, List steps) { if (context.BuildArgs.ExtraProperties.ContainsKey("separate-tenant-schema")) { - steps.Add(new RemoveProjectFromSolutionStep("MyCompanyName.MyProjectName.EntityFrameworkCore.DbMigrations")); - steps.Add(new TemplateProjectRenameStep("MyCompanyName.MyProjectName.EntityFrameworkCore.SeparateDbMigrations", "MyCompanyName.MyProjectName.EntityFrameworkCore.DbMigrations")); + if (HasDbMigrations) + { + steps.Add(new RemoveProjectFromSolutionStep("MyCompanyName.MyProjectName.EntityFrameworkCore.DbMigrations")); + steps.Add(new TemplateProjectRenameStep("MyCompanyName.MyProjectName.EntityFrameworkCore.SeparateDbMigrations", "MyCompanyName.MyProjectName.EntityFrameworkCore.DbMigrations")); + } + else + { + steps.Add(new RemoveProjectFromSolutionStep("MyCompanyName.MyProjectName.EntityFrameworkCore")); + steps.Add(new TemplateProjectRenameStep("MyCompanyName.MyProjectName.EntityFrameworkCoreWithSeparateDbContext", "MyCompanyName.MyProjectName.EntityFrameworkCore")); + } } else { - steps.Add(new RemoveProjectFromSolutionStep("MyCompanyName.MyProjectName.EntityFrameworkCore.SeparateDbMigrations")); + if (HasDbMigrations) + { + steps.Add(new RemoveProjectFromSolutionStep("MyCompanyName.MyProjectName.EntityFrameworkCore.SeparateDbMigrations")); + } + else + { + steps.Add(new RemoveProjectFromSolutionStep("MyCompanyName.MyProjectName.EntityFrameworkCoreWithSeparateDbContext")); + } } } - private static void SwitchDatabaseProvider(ProjectBuildContext context, List steps) + private void SwitchDatabaseProvider(ProjectBuildContext context, List steps) { if (context.BuildArgs.DatabaseProvider == DatabaseProvider.MongoDb) { @@ -62,9 +80,17 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.App if (context.BuildArgs.DatabaseProvider != DatabaseProvider.EntityFrameworkCore) { - steps.Add(new RemoveProjectFromSolutionStep("MyCompanyName.MyProjectName.EntityFrameworkCore")); - steps.Add(new RemoveProjectFromSolutionStep("MyCompanyName.MyProjectName.EntityFrameworkCore.DbMigrations")); - steps.Add(new RemoveProjectFromSolutionStep("MyCompanyName.MyProjectName.EntityFrameworkCore.Tests", projectFolderPath: "/aspnet-core/test/MyCompanyName.MyProjectName.EntityFrameworkCore.Tests")); + if (HasDbMigrations) + { + steps.Add(new RemoveProjectFromSolutionStep("MyCompanyName.MyProjectName.EntityFrameworkCore")); + steps.Add(new RemoveProjectFromSolutionStep("MyCompanyName.MyProjectName.EntityFrameworkCore.DbMigrations")); + steps.Add(new RemoveProjectFromSolutionStep("MyCompanyName.MyProjectName.EntityFrameworkCore.Tests", projectFolderPath: "/aspnet-core/test/MyCompanyName.MyProjectName.EntityFrameworkCore.Tests")); + } + else + { + steps.Add(new RemoveProjectFromSolutionStep("MyCompanyName.MyProjectName.EntityFrameworkCore")); + steps.Add(new RemoveProjectFromSolutionStep("MyCompanyName.MyProjectName.EntityFrameworkCore.Tests", projectFolderPath: "/aspnet-core/test/MyCompanyName.MyProjectName.EntityFrameworkCore.Tests")); + } } else { @@ -380,13 +406,21 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.App steps.Add(new UpdateNuGetConfigStep("/aspnet-core/NuGet.Config")); } - private static void RemoveMigrations(ProjectBuildContext context, List steps) + private void RemoveMigrations(ProjectBuildContext context, List steps) { if (string.IsNullOrWhiteSpace(context.BuildArgs.Version) || SemanticVersion.Parse(context.BuildArgs.Version) > new SemanticVersion(4,1,99)) { - steps.Add(new RemoveFolderStep("/aspnet-core/src/MyCompanyName.MyProjectName.EntityFrameworkCore.DbMigrations/Migrations")); - steps.Add(new RemoveFolderStep("/aspnet-core/src/MyCompanyName.MyProjectName.EntityFrameworkCore.DbMigrations/TenantMigrations")); + if (HasDbMigrations) + { + steps.Add(new RemoveFolderStep("/aspnet-core/src/MyCompanyName.MyProjectName.EntityFrameworkCore.DbMigrations/Migrations")); + steps.Add(new RemoveFolderStep("/aspnet-core/src/MyCompanyName.MyProjectName.EntityFrameworkCore.DbMigrations/TenantMigrations")); + } + else + { + steps.Add(new RemoveFolderStep("/aspnet-core/src/MyCompanyName.MyProjectName.EntityFrameworkCore/Migrations")); + steps.Add(new RemoveFolderStep("/aspnet-core/src/MyCompanyName.MyProjectName.EntityFrameworkCore/TenantMigrations")); + } } } diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/App/AppTemplateSwitchEntityFrameworkCoreToMongoDbStep.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/App/AppTemplateSwitchEntityFrameworkCoreToMongoDbStep.cs index 309d0a1552..ea1423d766 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/App/AppTemplateSwitchEntityFrameworkCoreToMongoDbStep.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Templates/App/AppTemplateSwitchEntityFrameworkCoreToMongoDbStep.cs @@ -1,6 +1,4 @@ using System; -using System.Linq; -using Volo.Abp.Cli.Commands; using Volo.Abp.Cli.ProjectBuilding.Building; namespace Volo.Abp.Cli.ProjectBuilding.Templates.App @@ -14,7 +12,7 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.App ChangeProjectReference( context, "/aspnet-core/src/MyCompanyName.MyProjectName.Web/MyCompanyName.MyProjectName.Web.csproj", - "EntityFrameworkCore.DbMigrations", + new[] {"EntityFrameworkCore.DbMigrations", "EntityFrameworkCore"}, "MongoDB" ); @@ -23,7 +21,7 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.App "/aspnet-core/src/MyCompanyName.MyProjectName.Web/MyProjectNameWebModule.cs", "MyCompanyName.MyProjectName.EntityFrameworkCore", "MyCompanyName.MyProjectName.MongoDB", - "MyProjectNameEntityFrameworkCoreDbMigrationsModule", + new[] {"MyProjectNameEntityFrameworkCoreDbMigrationsModule", "MyProjectNameEntityFrameworkCoreModule"}, "MyProjectNameMongoDbModule" ); @@ -37,7 +35,7 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.App ChangeProjectReference( context, "/aspnet-core/src/MyCompanyName.MyProjectName.IdentityServer/MyCompanyName.MyProjectName.IdentityServer.csproj", - "EntityFrameworkCore.DbMigrations", + new[] {"EntityFrameworkCore.DbMigrations", "EntityFrameworkCore"}, "MongoDB" ); @@ -46,7 +44,7 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.App "/aspnet-core/src/MyCompanyName.MyProjectName.IdentityServer/MyProjectNameIdentityServerModule.cs", "MyCompanyName.MyProjectName.EntityFrameworkCore", "MyCompanyName.MyProjectName.MongoDB", - "MyProjectNameEntityFrameworkCoreDbMigrationsModule", + new[] {"MyProjectNameEntityFrameworkCoreDbMigrationsModule", "MyProjectNameEntityFrameworkCoreModule"}, "MyProjectNameMongoDbModule" ); @@ -60,7 +58,7 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.App ChangeProjectReference( context, "/aspnet-core/src/MyCompanyName.MyProjectName.HttpApi.Host/MyCompanyName.MyProjectName.HttpApi.Host.csproj", - "EntityFrameworkCore.DbMigrations", + new[] {"EntityFrameworkCore.DbMigrations", "EntityFrameworkCore"}, "MongoDB" ); @@ -69,7 +67,7 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.App "/aspnet-core/src/MyCompanyName.MyProjectName.HttpApi.Host/MyProjectNameHttpApiHostModule.cs", "MyCompanyName.MyProjectName.EntityFrameworkCore", "MyCompanyName.MyProjectName.MongoDB", - "MyProjectNameEntityFrameworkCoreDbMigrationsModule", + new[] {"MyProjectNameEntityFrameworkCoreDbMigrationsModule", "MyProjectNameEntityFrameworkCoreModule"}, "MyProjectNameMongoDbModule" ); @@ -83,7 +81,7 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.App ChangeProjectReference( context, "/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server/MyCompanyName.MyProjectName.Blazor.Server.csproj", - "EntityFrameworkCore.DbMigrations", + new[] {"EntityFrameworkCore.DbMigrations", "EntityFrameworkCore"}, "MongoDB" ); @@ -92,7 +90,7 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.App "/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server/MyProjectNameBlazorModule.cs", "MyCompanyName.MyProjectName.EntityFrameworkCore", "MyCompanyName.MyProjectName.MongoDB", - "MyProjectNameEntityFrameworkCoreDbMigrationsModule", + new[] {"MyProjectNameEntityFrameworkCoreDbMigrationsModule", "MyProjectNameEntityFrameworkCoreModule"}, "MyProjectNameMongoDbModule" ); @@ -106,7 +104,7 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.App ChangeProjectReference( context, "/aspnet-core/src/MyCompanyName.MyProjectName.HttpApi.HostWithIds/MyCompanyName.MyProjectName.HttpApi.HostWithIds.csproj", - "EntityFrameworkCore.DbMigrations", + new[] {"EntityFrameworkCore.DbMigrations", "EntityFrameworkCore"}, "MongoDB" ); @@ -115,7 +113,7 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.App "/aspnet-core/src/MyCompanyName.MyProjectName.HttpApi.HostWithIds/MyProjectNameHttpApiHostModule.cs", "MyCompanyName.MyProjectName.EntityFrameworkCore", "MyCompanyName.MyProjectName.MongoDB", - "MyProjectNameEntityFrameworkCoreDbMigrationsModule", + new[] {"MyProjectNameEntityFrameworkCoreDbMigrationsModule", "MyProjectNameEntityFrameworkCoreModule"}, "MyProjectNameMongoDbModule" ); @@ -129,7 +127,7 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.App ChangeProjectReference( context, "/aspnet-core/src/MyCompanyName.MyProjectName.DbMigrator/MyCompanyName.MyProjectName.DbMigrator.csproj", - "EntityFrameworkCore.DbMigrations", + new[] {"EntityFrameworkCore.DbMigrations", "EntityFrameworkCore"}, "MongoDB" ); @@ -138,7 +136,7 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.App "/aspnet-core/src/MyCompanyName.MyProjectName.DbMigrator/MyProjectNameDbMigratorModule.cs", "MyCompanyName.MyProjectName.EntityFrameworkCore", "MyCompanyName.MyProjectName.MongoDB", - "MyProjectNameEntityFrameworkCoreDbMigrationsModule", + new[] {"MyProjectNameEntityFrameworkCoreDbMigrationsModule", "MyProjectNameEntityFrameworkCoreModule"}, "MyProjectNameMongoDbModule" ); @@ -152,7 +150,7 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.App ChangeProjectReference( context, "/aspnet-core/test/MyCompanyName.MyProjectName.Domain.Tests/MyCompanyName.MyProjectName.Domain.Tests.csproj", - "EntityFrameworkCore.Tests", + new[] {"EntityFrameworkCore.Tests"}, "MongoDB.Tests" ); @@ -201,7 +199,7 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.App ChangeProjectReference( context, "/aspnet-core/src/MyCompanyName.MyProjectName.Web.Public/MyCompanyName.MyProjectName.Web.Public.csproj", - "EntityFrameworkCore.DbMigrations", + new[] {"EntityFrameworkCore.DbMigrations", "EntityFrameworkCore"}, "MongoDB" ); @@ -210,7 +208,7 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.App "/aspnet-core/src/MyCompanyName.MyProjectName.Web.Public/MyProjectNameWebPublicModule.cs", "MyCompanyName.MyProjectName.EntityFrameworkCore", "MyCompanyName.MyProjectName.MongoDB", - "MyProjectNameEntityFrameworkCoreDbMigrationsModule", + new []{"MyProjectNameEntityFrameworkCoreDbMigrationsModule","MyProjectNameEntityFrameworkCoreModule"}, "MyProjectNameMongoDbModule" ); @@ -226,6 +224,15 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.App string targetProjectFilePath, string oldReference, string newReference) + { + ChangeProjectReference(context, targetProjectFilePath, new[] {oldReference}, newReference); + } + + private void ChangeProjectReference( + ProjectBuildContext context, + string targetProjectFilePath, + string[] oldReferences, + string newReference) { var file = context.FindFile(targetProjectFilePath); @@ -239,15 +246,27 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.App var lines = file.GetLines(); for (var i = 0; i < lines.Length; i++) { - if (lines[i].Contains("ProjectReference") && lines[i].Contains(oldReference)) + if (lines[i].Contains("ProjectReference")) { - lines[i] = lines[i].Replace(oldReference, newReference); - file.SetLines(lines); - return; + var changed = false; + foreach (var oldReference in oldReferences) + { + if (lines[i].Contains(oldReference)) + { + lines[i] = lines[i].Replace(oldReference, newReference); + file.SetLines(lines); + changed = true; + } + } + + if (changed) + { + return; + } } } - throw new ApplicationException($"Could not find the '{oldReference}' reference in the project '{targetProjectFilePath}'!"); + throw new ApplicationException($"Could not find the '{string.Join(",", oldReferences)}' reference in the project '{targetProjectFilePath}'!"); } private void ChangeNamespaceAndKeyword( @@ -257,6 +276,17 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.App string newNamespace, string oldKeyword, string newKeyword) + { + ChangeNamespaceAndKeyword(context, targetModuleFilePath, oldNamespace, newNamespace, new[] {oldKeyword}, newKeyword); + } + + private void ChangeNamespaceAndKeyword( + ProjectBuildContext context, + string targetModuleFilePath, + string oldNamespace, + string newNamespace, + string[] oldKeywords, + string newKeyword) { var file = context.FindFile(targetModuleFilePath); @@ -275,9 +305,15 @@ namespace Volo.Abp.Cli.ProjectBuilding.Templates.App { lines[i] = $"using {newNamespace};"; } - else if (lines[i].Contains(oldKeyword)) + else { - lines[i] = lines[i].Replace(oldKeyword, newKeyword); + foreach (var oldKeyword in oldKeywords) + { + if (lines[i].Contains(oldKeyword)) + { + lines[i] = lines[i].Replace(oldKeyword, newKeyword); + } + } } }