mirror of https://github.com/abpframework/abp.git
5 changed files with 156 additions and 3 deletions
@ -0,0 +1,53 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Text; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Cli.ProjectModification |
|||
{ |
|||
public class DbContextFileBuilderConfigureAdder : ITransientDependency |
|||
{ |
|||
public void Add(string path, string moduleConfiguration) |
|||
{ |
|||
var file = File.ReadAllText(path); |
|||
|
|||
var indexToInsert = FindIndexToInsert(file); |
|||
var stringToAdd = GetLineToAdd(moduleConfiguration); |
|||
|
|||
stringToAdd = " " + stringToAdd + Environment.NewLine; |
|||
|
|||
file = file.Insert(indexToInsert, stringToAdd); |
|||
|
|||
File.WriteAllText(path, file); |
|||
} |
|||
|
|||
protected int FindIndexToInsert(string file) |
|||
{ |
|||
var indexOfMethodDeclaration = file.IndexOf("OnModelCreating(", StringComparison.Ordinal); |
|||
var indexOfOpeningBracket = indexOfMethodDeclaration + file.Substring(indexOfMethodDeclaration).IndexOf('{'); |
|||
|
|||
var stack = 1; |
|||
var index = indexOfOpeningBracket; |
|||
|
|||
while (stack > 0 || index < file.Length) |
|||
{ |
|||
index++; |
|||
if (file[index] == '{') |
|||
{ |
|||
stack++; |
|||
} |
|||
else if (file[index] == '}') |
|||
{ |
|||
stack--; |
|||
} |
|||
} |
|||
return index; |
|||
} |
|||
|
|||
protected string GetLineToAdd(string moduleConfiguration) |
|||
{ |
|||
return "builder.Configure" + moduleConfiguration + "();"; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,48 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Diagnostics; |
|||
using System.Text; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Cli.ProjectModification |
|||
{ |
|||
public class EfCoreMigrationAdder : ITransientDependency |
|||
{ |
|||
public void AddMigration(string csprojFile, string module, bool updateDatabase = true) |
|||
{ |
|||
var moduleName = ParseModuleName(module); |
|||
var migrationName = "Added_" + moduleName + "_Module" + GetUniquePostFix(); |
|||
|
|||
var process = Process.Start("CMD.exe", "/C cd \"" + csprojFile + "\" & dotnet ef migrations add " + migrationName); |
|||
process.WaitForExit(); |
|||
|
|||
if (updateDatabase) |
|||
{ |
|||
UpdateDatabase(csprojFile); |
|||
} |
|||
} |
|||
|
|||
protected void UpdateDatabase(string csprojFile) |
|||
{ |
|||
var process = Process.Start("CMD.exe", "/C cd \"" + csprojFile + "\" & dotnet ef database update"); |
|||
process.WaitForExit(); |
|||
} |
|||
|
|||
protected virtual string ParseModuleName(string fullModuleName) |
|||
{ |
|||
var words = fullModuleName?.Split('.'); |
|||
|
|||
if (words == null || words.Length <= 1) |
|||
{ |
|||
return ""; |
|||
} |
|||
|
|||
return words[words.Length - 1]; |
|||
} |
|||
|
|||
protected virtual string GetUniquePostFix() |
|||
{ |
|||
return "_" + new Random().Next(1,99999); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue