Browse Source

CLI can configure Entity Framework after installing a module cont.

pull/1126/head
Yunus Emre Kalkan 7 years ago
parent
commit
aaee2ef41f
  1. 45
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DbContextFileBuilderConfigureAdder.cs
  2. 55
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DerivedClassFinder.cs
  3. 44
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ModuleClassDependcyAdder.cs
  4. 2
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionModuleAdder.cs
  5. 65
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/UsingStatementAdder.cs

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

@ -1,23 +1,42 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Linq;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Cli.ProjectModification
{
public class DbContextFileBuilderConfigureAdder : ITransientDependency
{
protected UsingStatementAdder UsingStatementAdder { get; }
public ILogger<DbContextFileBuilderConfigureAdder> Logger { get; set; }
public DbContextFileBuilderConfigureAdder(UsingStatementAdder usingStatementAdder)
{
UsingStatementAdder = usingStatementAdder;
Logger = NullLogger<DbContextFileBuilderConfigureAdder>.Instance;
}
public void Add(string path, string moduleConfiguration)
{
var file = File.ReadAllText(path);
var indexToInsert = FindIndexToInsert(file);
file = UsingStatementAdder.Add(file, GetNamespace(moduleConfiguration));
var stringToAdd = GetLineToAdd(moduleConfiguration);
if (!file.Contains(stringToAdd))
{
var indexToInsert = FindIndexToInsert(file);
stringToAdd = " " + stringToAdd + Environment.NewLine;
if (indexToInsert <= 0 || indexToInsert >= file.Length)
{
Logger.LogWarning($"\"OnModelCreating(ModelBuilder builder)\" method couldn't be found in {path}");
return;
}
file = file.Insert(indexToInsert, " " + stringToAdd + Environment.NewLine + " ");
}
file = file.Insert(indexToInsert, stringToAdd);
File.WriteAllText(path, file);
}
@ -30,9 +49,15 @@ namespace Volo.Abp.Cli.ProjectModification
var stack = 1;
var index = indexOfOpeningBracket;
while (stack > 0 || index < file.Length)
while (stack > 0)
{
index++;
if (index >= file.Length)
{
break;
}
if (file[index] == '{')
{
stack++;
@ -42,12 +67,18 @@ namespace Volo.Abp.Cli.ProjectModification
stack--;
}
}
return index;
}
protected string GetLineToAdd(string moduleConfiguration)
{
return "builder.Configure" + moduleConfiguration + "();";
return "builder." + moduleConfiguration.Split('.').Last() + "();";
}
protected string GetNamespace(string moduleConfiguration)
{
return string.Join(".", moduleConfiguration.Split('.').Reverse().Skip(2));
}
}
}

55
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/DerivedClassFinder.cs

@ -1,42 +1,83 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Cli.ProjectModification
{
public class DerivedClassFinder : ITransientDependency
{
public ILogger<DerivedClassFinder> Logger { get; set; }
public DerivedClassFinder()
{
Logger = NullLogger<DerivedClassFinder>.Instance;
}
public virtual List<string> Find(string csprojFilePath, string baseClass)
{
var moduleFilePaths = new List<string>();
var csprojFileDirectory = Path.GetDirectoryName(csprojFilePath);
var binFile = Path.Combine(csprojFileDirectory, "bin");
var objFile = Path.Combine(csprojFileDirectory, "obj");
var csFiles = new DirectoryInfo(Path.GetDirectoryName(csprojFilePath))
var csFiles = new DirectoryInfo(csprojFileDirectory)
.GetFiles("*.cs", SearchOption.AllDirectories)
.Where(f => !f.DirectoryName.StartsWith(binFile))
.Where(f => !f.DirectoryName.StartsWith(objFile))
.Select(f => f.FullName)
.ToList();
foreach (var csFile in csFiles)
{
if (IsDerivedFromAbpModule(csFile, baseClass))
try
{
moduleFilePaths.Add(csFile);
if (IsDerived(csFile, baseClass))
{
moduleFilePaths.Add(csFile);
}
}
catch (Exception)
{
Logger.LogDebug($"Couldn't parse {csFile}.");
}
}
return moduleFilePaths;
}
protected bool IsDerivedFromAbpModule(string csFile, string baseClass)
protected bool IsDerived(string csFile, string baseClass)
{
Logger.LogDebug(csFile);
var root = CSharpSyntaxTree.ParseText(File.ReadAllText(csFile)).GetRoot();
var namespaceSyntax = root.DescendantNodes().OfType<NamespaceDeclarationSyntax>().First();
var classDeclaration = (namespaceSyntax.DescendantNodes().OfType<ClassDeclarationSyntax>()).First();
var baseTypeList = classDeclaration.BaseList?.Types.Select(t => t.ToString()).ToList();
if (baseTypeList == null)
{
return false;
}
foreach (var baseType in baseTypeList)
{
if (baseType.Contains('<') && baseType.Substring(0, baseType.IndexOf('<')) == baseClass)
{
return true;
}
if (baseType == baseClass)
{
return true;
}
}
return classDeclaration.BaseList?.Types
.Any(t => t.ToString().Equals(baseClass)) ?? false;
return false;
}
}
}

44
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/ModuleClassDependcyAdder.cs

@ -7,16 +7,20 @@ namespace Volo.Abp.Cli.ProjectModification
{
public class ModuleClassDependcyAdder : ITransientDependency
{
protected UsingStatementAdder UsingStatementAdder { get; }
public ModuleClassDependcyAdder(UsingStatementAdder usingStatementAdder)
{
UsingStatementAdder = usingStatementAdder;
}
public virtual void Add(string path, string module)
{
ParseModuleNameAndNameSpace(module, out var nameSpace, out var moduleName);
var file = File.ReadAllText(path);
if (!file.Contains(GetUsingStatement(nameSpace)) )
{
file = InsertUsingStatement(file, nameSpace);
}
file = UsingStatementAdder.Add(file, nameSpace);
if (!file.Contains(moduleName) )
{
@ -34,38 +38,6 @@ namespace Volo.Abp.Cli.ProjectModification
return file.Insert(indexOfPublicClassDeclaration, dependsOnAttribute);
}
protected virtual string InsertUsingStatement(string file, string nameSpace)
{
var indexOfTheEndOfTheLastUsingStatement = GetIndexOfTheEndOfTheLastUsingStatement(file);
return file.Insert(indexOfTheEndOfTheLastUsingStatement, Environment.NewLine + GetUsingStatement(nameSpace));
}
protected virtual int GetIndexOfTheEndOfTheLastUsingStatement(string file)
{
var indexOfPublicClassDeclaration = GetIndexOfWhereDependsOnWillBeAdded(file);
file = file.Substring(0, indexOfPublicClassDeclaration);
var indexOfTheStartOfLastUsingStatement =
file.LastIndexOf("using ", StringComparison.Ordinal);
if (indexOfTheStartOfLastUsingStatement < 0)
{
return 0;
}
var indexOfFirstSemiColonAfterLastUsingStatement =
file.Substring(indexOfTheStartOfLastUsingStatement).IndexOf(';');
if (indexOfFirstSemiColonAfterLastUsingStatement < 0)
{
return 0;
}
return indexOfTheStartOfLastUsingStatement
+ indexOfFirstSemiColonAfterLastUsingStatement + 1;
}
protected virtual int GetIndexOfWhereDependsOnWillBeAdded(string file)
{
var indexOfPublicClassDeclaration = file.IndexOf("public class", StringComparison.Ordinal);

2
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/SolutionModuleAdder.cs

@ -97,7 +97,7 @@ namespace Volo.Abp.Cli.ProjectModification
}
var dbMigrationsProject = projectFiles.FirstOrDefault(p => p.EndsWith(".DbMigrations.csproj"));
if (dbMigrationsProject == null)
{
Logger.LogDebug("Solution doesn't have a \".DbMigrations\" project.");

65
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectModification/UsingStatementAdder.cs

@ -0,0 +1,65 @@
using System;
using System.Collections.Generic;
using System.Text;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.Cli.ProjectModification
{
public class UsingStatementAdder : ITransientDependency
{
public string Add(string fileContent, string nameSpace)
{
if (fileContent.Contains(nameSpace))
{
return fileContent;
}
var index = GetIndexOfTheEndOfTheLastUsingStatement(fileContent);
if (index < 0 || index >= fileContent.Length)
{
index = 0;
}
var usingStatement = GetUsingStatement(nameSpace);
return fileContent.Insert(index, usingStatement);
}
protected string GetUsingStatement(string nameSpace)
{
return Environment.NewLine + "using " + nameSpace + ";";
}
protected virtual int GetIndexOfTheEndOfTheLastUsingStatement(string fileContent)
{
var indexOfNamespaceDeclaration = fileContent.IndexOf("namespace", StringComparison.Ordinal);
if (indexOfNamespaceDeclaration < 0)
{
return 0;
}
fileContent = fileContent.Substring(0, indexOfNamespaceDeclaration);
var indexOfTheStartOfLastUsingStatement =
fileContent.LastIndexOf("using ", StringComparison.Ordinal);
if (indexOfTheStartOfLastUsingStatement < 0)
{
return 0;
}
var indexOfFirstSemiColonAfterLastUsingStatement =
fileContent.Substring(indexOfTheStartOfLastUsingStatement).IndexOf(';');
if (indexOfFirstSemiColonAfterLastUsingStatement < 0)
{
return 0;
}
return indexOfTheStartOfLastUsingStatement
+ indexOfFirstSemiColonAfterLastUsingStatement + 1;
}
}
}
Loading…
Cancel
Save