From 0844678cad1ac5f44faac09698c602546d5eb9db Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Fri, 10 May 2019 08:41:04 +0300 Subject: [PATCH] ModuleClassDependcyAdder refactor --- .../ModuleClassDependcyAdder.cs | 80 ++++++++++++++++--- 1 file changed, 68 insertions(+), 12 deletions(-) diff --git a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectModification/ModuleClassDependcyAdder.cs b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectModification/ModuleClassDependcyAdder.cs index cf2c8e579f..be41884ff3 100644 --- a/framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectModification/ModuleClassDependcyAdder.cs +++ b/framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectModification/ModuleClassDependcyAdder.cs @@ -5,8 +5,6 @@ using Volo.Abp.DependencyInjection; namespace Volo.Abp.ProjectModification { - //TODO: Don't add if added before! - public class ModuleClassDependcyAdder : ITransientDependency { public virtual void Add(string path, string module) @@ -15,16 +13,79 @@ namespace Volo.Abp.ProjectModification var file = File.ReadAllText(path); - var indexOfEndingPublicClass = file.IndexOf("public class", StringComparison.Ordinal); - - var dependsOnAttribute = "[DependsOn(" + moduleName + ")]" + Environment.NewLine + " "; + if (file.Contains(GetUsingStatement(nameSpace)) && file.Contains(moduleName)) + { + return; + } - file = file.Insert(indexOfEndingPublicClass, dependsOnAttribute); - file = file.Insert(0, GetUsingStatement(nameSpace) + Environment.NewLine); //TODO: Add as the last item in the using list! + file = InsertDependsOnAttribute(file, moduleName); + file = InsertUsingStatement(file, nameSpace); File.WriteAllText(path, file); } + protected virtual string InsertDependsOnAttribute(string file, string moduleName) + { + var indexOfPublicClassDeclaration = GetIndexOfWhereDependsOnWillBeAdded(file); + var dependsOnAttribute = GetDependsOnAttribute(moduleName); + + 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); + + if (indexOfPublicClassDeclaration < 0) + { + throw new Exception("\"public class\" declaration not found!"); + } + + return indexOfPublicClassDeclaration; + } + + protected virtual string GetDependsOnAttribute(string moduleName) + { + return "[DependsOn(" + moduleName + ")]" + Environment.NewLine + " "; + } + + protected virtual string GetUsingStatement(string nameSpace) + { + return "using " + nameSpace + ";"; + } + protected virtual void ParseModuleNameAndNameSpace(string module, out string nameSpace, out string moduleName) { var words = module?.Split('.'); @@ -39,10 +100,5 @@ namespace Volo.Abp.ProjectModification moduleName = words[words.Length - 1]; nameSpace = string.Join(".", words.Take(words.Length - 1)); } - - protected virtual string GetUsingStatement(string nameSpace) - { - return "using " + nameSpace + ";"; - } } }