Browse Source

ModuleClassDependcyAdder refactor

pull/1120/head
Yunus Emre Kalkan 7 years ago
parent
commit
0844678cad
  1. 80
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/ProjectModification/ModuleClassDependcyAdder.cs

80
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 + ";";
}
}
}

Loading…
Cancel
Save