Browse Source

Merge pull request #8732 from abpframework/cli-allow-multiple-EfCoreConfigureMethod

Cli: Process multiple EfCore ModelBuilder Configure Method when adding a module
pull/8734/head
Halil İbrahim Kalkan 6 years ago
committed by GitHub
parent
commit
6324dfd29f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 34
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DbContextFileBuilderConfigureAdder.cs

34
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DbContextFileBuilderConfigureAdder.cs

@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
@ -22,26 +23,34 @@ namespace Volo.Abp.Cli.ProjectModification
{ {
var file = File.ReadAllText(path); var file = File.ReadAllText(path);
file = UsingStatementAdder.Add(file, GetNamespace(moduleConfiguration)); var parsedModuleConfiguration = moduleConfiguration.Split(", ");
var stringToAdd = GetLineToAdd(moduleConfiguration); var namespaces = parsedModuleConfiguration.Select(GetNamespace);
if (!file.Contains(stringToAdd)) var configurationLines = parsedModuleConfiguration.Select(GetLineToAdd);
var indexToInsert = FindIndexToInsert(file);
if (indexToInsert <= 0 || indexToInsert >= file.Length)
{ {
var indexToInsert = FindIndexToInsert(file); Logger.LogWarning($"\"OnModelCreating(ModelBuilder builder)\" method couldn't be found in {path}");
return false;
}
if (indexToInsert <= 0 || indexToInsert >= file.Length) foreach (var configurationLine in configurationLines)
{
if (file.Contains(configurationLine))
{ {
Logger.LogWarning($"\"OnModelCreating(ModelBuilder builder)\" method couldn't be found in {path}"); continue;
return false;
} }
file = file.Insert(indexToInsert, " " + stringToAdd + Environment.NewLine + " ");
file = file.Insert(indexToInsert, " " + configurationLine + Environment.NewLine + " ");
} }
else
foreach (var namespaceOfConfiguration in namespaces)
{ {
return false; file = UsingStatementAdder.Add(file, namespaceOfConfiguration);
} }
File.WriteAllText(path, file); File.WriteAllText(path, file);
return true; return true;
} }
@ -49,7 +58,8 @@ namespace Volo.Abp.Cli.ProjectModification
protected int FindIndexToInsert(string file) protected int FindIndexToInsert(string file)
{ {
var indexOfMethodDeclaration = file.IndexOf("OnModelCreating(", StringComparison.Ordinal); var indexOfMethodDeclaration = file.IndexOf("OnModelCreating(", StringComparison.Ordinal);
var indexOfOpeningBracket = indexOfMethodDeclaration + file.Substring(indexOfMethodDeclaration).IndexOf('{'); var indexOfOpeningBracket =
indexOfMethodDeclaration + file.Substring(indexOfMethodDeclaration).IndexOf('{');
var stack = 1; var stack = 1;
var index = indexOfOpeningBracket; var index = indexOfOpeningBracket;

Loading…
Cancel
Save