mirror of https://github.com/abpframework/abp.git
6 changed files with 219 additions and 98 deletions
@ -1,80 +0,0 @@ |
|||||
using System; |
|
||||
using Volo.Abp.Cli.ProjectBuilding.Files; |
|
||||
|
|
||||
namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps |
|
||||
{ |
|
||||
public class SwitchEntityFrameworkCoreToMongoDbStep : ProjectBuildPipelineStep |
|
||||
{ |
|
||||
public override void Execute(ProjectBuildContext context) |
|
||||
{ |
|
||||
ChangeProjectReference(context); |
|
||||
ChangeWebModuleUsage(context); |
|
||||
ChangeConnectionString(context); |
|
||||
} |
|
||||
|
|
||||
private void ChangeProjectReference(ProjectBuildContext context) |
|
||||
{ |
|
||||
var file = context.GetFile("/src/MyCompanyName.MyProjectName.Web/MyCompanyName.MyProjectName.Web.csproj"); |
|
||||
|
|
||||
file.NormalizeLineEndings(); |
|
||||
|
|
||||
var lines = file.GetLines(); |
|
||||
for (var i = 0; i < lines.Length; i++) |
|
||||
{ |
|
||||
if (lines[i].Contains("ProjectReference") && lines[i].Contains("MyCompanyName.MyProjectName.EntityFrameworkCore.DbMigrations")) |
|
||||
{ |
|
||||
lines[i] = lines[i].Replace("EntityFrameworkCore.DbMigrations", "MongoDB"); |
|
||||
file.SetLines(lines); |
|
||||
return; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
throw new ApplicationException("Could not find the EntityFrameworkCore reference in the MyCompanyName.MyProjectName.Web.csproj!"); |
|
||||
} |
|
||||
|
|
||||
private void ChangeWebModuleUsage(ProjectBuildContext context) |
|
||||
{ |
|
||||
var file = context.GetFile("/src/MyCompanyName.MyProjectName.Web/MyProjectNameWebModule.cs"); |
|
||||
|
|
||||
file.NormalizeLineEndings(); |
|
||||
|
|
||||
file.RemoveTemplateCodeIfNot("EntityFrameworkCore"); |
|
||||
|
|
||||
var lines = file.GetLines(); |
|
||||
|
|
||||
for (var i = 0; i < lines.Length; i++) |
|
||||
{ |
|
||||
if (lines[i].Contains("using MyCompanyName.MyProjectName.EntityFrameworkCore")) |
|
||||
{ |
|
||||
lines[i] = "using MyCompanyName.MyProjectName.MongoDb;"; |
|
||||
} |
|
||||
else if (lines[i].Contains("MyProjectNameEntityFrameworkCoreModule")) |
|
||||
{ |
|
||||
lines[i] = lines[i].Replace("MyProjectNameEntityFrameworkCoreModule", "MyProjectNameMongoDbModule"); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
file.SetLines(lines); |
|
||||
} |
|
||||
|
|
||||
private void ChangeConnectionString(ProjectBuildContext context) |
|
||||
{ |
|
||||
var file = context.GetFile("/src/MyCompanyName.MyProjectName.Web/appsettings.json"); |
|
||||
|
|
||||
file.NormalizeLineEndings(); |
|
||||
|
|
||||
var lines = file.GetLines(); |
|
||||
for (var i = 0; i < lines.Length; i++) |
|
||||
{ |
|
||||
if (lines[i].Contains("Default") && lines[i].Contains("Database")) |
|
||||
{ |
|
||||
lines[i] = " \"Default\": \"mongodb://localhost:27017|MyProjectName\""; |
|
||||
file.SetLines(lines); |
|
||||
return; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
throw new ApplicationException("Could not find the 'Default' connection string in appsettings.json file!"); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,173 @@ |
|||||
|
using System; |
||||
|
using Volo.Abp.Cli.ProjectBuilding.Building; |
||||
|
|
||||
|
namespace Volo.Abp.Cli.ProjectBuilding.Templates.Mvc |
||||
|
{ |
||||
|
public class SwitchEntityFrameworkCoreToMongoDbStep : ProjectBuildPipelineStep |
||||
|
{ |
||||
|
public override void Execute(ProjectBuildContext context) |
||||
|
{ |
||||
|
//MyCompanyName.MyProjectName.Web
|
||||
|
|
||||
|
ChangeProjectReference( |
||||
|
context, |
||||
|
"/src/MyCompanyName.MyProjectName.Web/MyCompanyName.MyProjectName.Web.csproj", |
||||
|
"EntityFrameworkCore.DbMigrations", |
||||
|
"MongoDB" |
||||
|
); |
||||
|
|
||||
|
ChangeModuleDependency( |
||||
|
context, |
||||
|
"/src/MyCompanyName.MyProjectName.Web/MyProjectNameWebModule.cs", |
||||
|
"MyCompanyName.MyProjectName.EntityFrameworkCore", |
||||
|
"MyCompanyName.MyProjectName.MongoDb", |
||||
|
"MyProjectNameEntityFrameworkCoreModule", |
||||
|
"MyProjectNameMongoDbModule" |
||||
|
); |
||||
|
|
||||
|
ChangeConnectionStringToMongoDb( |
||||
|
context, |
||||
|
"/src/MyCompanyName.MyProjectName.Web/appsettings.json" |
||||
|
); |
||||
|
|
||||
|
//MyCompanyName.MyProjectName.IdentityServer
|
||||
|
|
||||
|
ChangeProjectReference( |
||||
|
context, |
||||
|
"/src/MyCompanyName.MyProjectName.IdentityServer/MyCompanyName.MyProjectName.IdentityServer.csproj", |
||||
|
"EntityFrameworkCore.DbMigrations", |
||||
|
"MongoDB" |
||||
|
); |
||||
|
|
||||
|
ChangeModuleDependency( |
||||
|
context, |
||||
|
"/src/MyCompanyName.MyProjectName.IdentityServer/MyProjectNameIdentityServerModule.cs", |
||||
|
"MyCompanyName.MyProjectName.EntityFrameworkCore", |
||||
|
"MyCompanyName.MyProjectName.MongoDb", |
||||
|
"MyProjectNameEntityFrameworkCoreModule", |
||||
|
"MyProjectNameMongoDbModule" |
||||
|
); |
||||
|
|
||||
|
ChangeConnectionStringToMongoDb( |
||||
|
context, |
||||
|
"/src/MyCompanyName.MyProjectName.IdentityServer/appsettings.json" |
||||
|
); |
||||
|
|
||||
|
//MyCompanyName.MyProjectName.HttpApi.Host
|
||||
|
|
||||
|
ChangeProjectReference( |
||||
|
context, |
||||
|
"/src/MyCompanyName.MyProjectName.HttpApi.Host/MyCompanyName.MyProjectName.HttpApi.Host.csproj", |
||||
|
"EntityFrameworkCore.DbMigrations", |
||||
|
"MongoDB" |
||||
|
); |
||||
|
|
||||
|
ChangeModuleDependency( |
||||
|
context, |
||||
|
"/src/MyCompanyName.MyProjectName.HttpApi.Host/MyProjectNameHttpApiHostModule.cs", |
||||
|
"MyCompanyName.MyProjectName.EntityFrameworkCore", |
||||
|
"MyCompanyName.MyProjectName.MongoDb", |
||||
|
"MyProjectNameEntityFrameworkCoreModule", |
||||
|
"MyProjectNameMongoDbModule" |
||||
|
); |
||||
|
|
||||
|
ChangeConnectionStringToMongoDb( |
||||
|
context, |
||||
|
"/src/MyCompanyName.MyProjectName.HttpApi.Host/appsettings.json" |
||||
|
); |
||||
|
|
||||
|
//MyCompanyName.MyProjectName.Domain.Tests
|
||||
|
|
||||
|
ChangeProjectReference( |
||||
|
context, |
||||
|
"/test/MyCompanyName.MyProjectName.Domain.Tests/MyCompanyName.MyProjectName.Domain.Tests.csproj", |
||||
|
"EntityFrameworkCore.Tests", |
||||
|
"MongoDB.Tests" |
||||
|
); |
||||
|
|
||||
|
ChangeModuleDependency( |
||||
|
context, |
||||
|
"/src/MyCompanyName.MyProjectName.Domain.Tests/MyProjectNameDomainTestModule.cs", |
||||
|
"MyCompanyName.MyProjectName.EntityFrameworkCore", |
||||
|
"MyCompanyName.MyProjectName.MongoDb", |
||||
|
"MyProjectNameEntityFrameworkCoreModule", |
||||
|
"MyProjectNameMongoDbModule" |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
private void ChangeProjectReference( |
||||
|
ProjectBuildContext context, |
||||
|
string targetProjectFilePath, |
||||
|
string oldReference, |
||||
|
string newReference) |
||||
|
{ |
||||
|
var file = context.GetFile(targetProjectFilePath); |
||||
|
|
||||
|
file.NormalizeLineEndings(); |
||||
|
|
||||
|
var lines = file.GetLines(); |
||||
|
for (var i = 0; i < lines.Length; i++) |
||||
|
{ |
||||
|
if (lines[i].Contains("ProjectReference") && lines[i].Contains(oldReference)) |
||||
|
{ |
||||
|
lines[i] = lines[i].Replace(oldReference, newReference); |
||||
|
file.SetLines(lines); |
||||
|
return; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
throw new ApplicationException($"Could not find the '{oldReference}' reference in the project '{targetProjectFilePath}'!"); |
||||
|
} |
||||
|
|
||||
|
private void ChangeModuleDependency( |
||||
|
ProjectBuildContext context, |
||||
|
string targetModuleFilePath, |
||||
|
string oldNamespace, |
||||
|
string newNamespace, |
||||
|
string oldModuleName, |
||||
|
string newModuleName) |
||||
|
{ |
||||
|
var file = context.GetFile(targetModuleFilePath); |
||||
|
|
||||
|
file.NormalizeLineEndings(); |
||||
|
|
||||
|
var lines = file.GetLines(); |
||||
|
|
||||
|
for (var i = 0; i < lines.Length; i++) |
||||
|
{ |
||||
|
if (lines[i].Contains($"using {oldNamespace};")) |
||||
|
{ |
||||
|
lines[i] = $"using {newNamespace};"; |
||||
|
} |
||||
|
else if (lines[i].Contains(oldModuleName)) |
||||
|
{ |
||||
|
lines[i] = lines[i].Replace(oldModuleName, newModuleName); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
file.SetLines(lines); |
||||
|
} |
||||
|
|
||||
|
private void ChangeConnectionStringToMongoDb( |
||||
|
ProjectBuildContext context, |
||||
|
string appsettingFilePath) |
||||
|
{ |
||||
|
var file = context.GetFile(appsettingFilePath); |
||||
|
|
||||
|
file.NormalizeLineEndings(); |
||||
|
|
||||
|
var lines = file.GetLines(); |
||||
|
for (var i = 0; i < lines.Length; i++) |
||||
|
{ |
||||
|
if (lines[i].Contains("Default") && lines[i].Contains("Database")) |
||||
|
{ |
||||
|
lines[i] = " \"Default\": \"mongodb://localhost:27017|MyProjectName\""; |
||||
|
file.SetLines(lines); |
||||
|
return; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
throw new ApplicationException("Could not find the 'Default' connection string in appsettings.json file!"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue