Refactor CLI

pull/1466/head^2
Halil ibrahim Kalkan 7 years ago
parent be2bb4ba27
commit 93345d0819

@ -0,0 +1,64 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.ProjectModification
{
public class AbpModuleClassFinder : ITransientDependency
{
public virtual List<string> Find(string csprojFilePath)
{
var modules = new List<string>();
var csFiles = GetAllCsFilesUnderDirectory(Path.GetDirectoryName(csprojFilePath), new List<string>());
foreach (var csFile in csFiles)
{
var root = CSharpSyntaxTree.ParseText(File.ReadAllText(csFile)).GetRoot();
var namespaceSyntaxs = root.DescendantNodes().OfType<NamespaceDeclarationSyntax>();
foreach (var namespaceSyntax in namespaceSyntaxs)
{
var classDeclarationSyntaxs = namespaceSyntax.DescendantNodes().OfType<ClassDeclarationSyntax>();
var syntaxsArray = classDeclarationSyntaxs as ClassDeclarationSyntax[] ?? classDeclarationSyntaxs.ToArray();
foreach (var classDeclaration in syntaxsArray)
{
var classDerivedFromAbpModule = classDeclaration.BaseList?.Types.FirstOrDefault(t => t.ToString().Contains("AbpModule"));
if (classDerivedFromAbpModule != null)
{
modules.Add(csFile);
}
}
}
}
return modules;
}
protected virtual List<string> GetAllCsFilesUnderDirectory(string path, List<string> allCsFileList)
{
var directory = new DirectoryInfo(path);
var files = directory.GetFiles("*.cs").Select(f => f.DirectoryName + "\\" + f.Name).ToList();
foreach (var s in files)
{
allCsFileList.Add(s);
}
var directories = directory.GetDirectories().Select(d => path + "\\" + d.Name).ToList();
foreach (var subDirectory in directories)
{
allCsFileList = GetAllCsFilesUnderDirectory(subDirectory, allCsFileList);
}
return allCsFileList;
}
}
}

@ -1,71 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Volo.Abp.ProjectModification
{
public class AbpModuleFinder
{
public List<string> Find(string csprojFilePath)
{
var modules = new List<string>();
var csFiles = GetAllCsFilesUnderDirectory(Path.GetDirectoryName(csprojFilePath), new List<string>());
foreach (var csFile in csFiles)
{
try
{
var root = CSharpSyntaxTree.ParseText(File.ReadAllText(csFile)).GetRoot();
var namespaceSyntaxs = root.DescendantNodes().OfType<NamespaceDeclarationSyntax>();
foreach (var namespaceSyntax in namespaceSyntaxs)
{
var classDeclarationSyntaxs = namespaceSyntax.DescendantNodes().OfType<ClassDeclarationSyntax>();
var syntaxsArray = classDeclarationSyntaxs as ClassDeclarationSyntax[] ?? classDeclarationSyntaxs.ToArray();
foreach (var classDeclaration in syntaxsArray)
{
var classDerivedFromAbpModule = classDeclaration.BaseList?.Types.FirstOrDefault(t => t.ToString().Contains("AbpModule"));
if (classDerivedFromAbpModule != null)
{
modules.Add(csFile);
}
}
}
}
catch (Exception)
{
//ignored!
}
}
return modules;
}
private static List<string> GetAllCsFilesUnderDirectory(string path, List<string> allCsFileList)
{
var directory = new DirectoryInfo(path);
var files = directory.GetFiles("*.cs").Select(f => f.DirectoryName + "\\" + f.Name).ToList();
foreach (var s in files)
{
allCsFileList.Add(s);
}
var directories = directory.GetDirectories().Select(d => path + "\\" + d.Name).ToList();
foreach (var subDirectory in directories)
{
allCsFileList = GetAllCsFilesUnderDirectory(subDirectory, allCsFileList);
}
return allCsFileList;
}
}
}

@ -1,12 +1,13 @@
using System;
using System.IO;
using System.Linq;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.ProjectModification
{
public class DependsOnAdder
public class ModuleClassDependcyAdder : ITransientDependency
{
public void Add(string path, string module)
public virtual void Add(string path, string module)
{
ParseModuleNameAndNameSpace(module, out var nameSpace, out var moduleName);
@ -22,7 +23,7 @@ namespace Volo.Abp.ProjectModification
File.WriteAllText(path, file);
}
private void ParseModuleNameAndNameSpace(string module, out string nameSpace, out string moduleName)
protected virtual void ParseModuleNameAndNameSpace(string module, out string nameSpace, out string moduleName)
{
var words = module?.Split('.');
@ -37,7 +38,7 @@ namespace Volo.Abp.ProjectModification
nameSpace = string.Join(".", words.Take(words.Length - 1));
}
private string GetUsingStatement(string nameSpace)
protected virtual string GetUsingStatement(string nameSpace)
{
return "using " + nameSpace + ";";
}

@ -18,13 +18,19 @@ namespace Volo.Abp.ProjectModification
protected IJsonSerializer JsonSerializer { get; }
protected ProjectNpmPackageAdder NpmPackageAdder { get; }
protected AbpModuleClassFinder ModuleClassFinder { get; }
protected ModuleClassDependcyAdder ModuleClassDependcyAdder { get; }
public ProjectNugetPackageAdder(
IJsonSerializer jsonSerializer,
ProjectNpmPackageAdder npmPackageAdder)
ProjectNpmPackageAdder npmPackageAdder,
AbpModuleClassFinder moduleClassFinder,
ModuleClassDependcyAdder moduleClassDependcyAdder)
{
JsonSerializer = jsonSerializer;
NpmPackageAdder = npmPackageAdder;
ModuleClassFinder = moduleClassFinder;
ModuleClassDependcyAdder = moduleClassDependcyAdder;
Logger = NullLogger<ProjectNugetPackageAdder>.Instance;
}
@ -40,8 +46,9 @@ namespace Volo.Abp.ProjectModification
var procStartInfo = new ProcessStartInfo("dotnet", "add package " + packageName);
Process.Start(procStartInfo).WaitForExit();
var moduleFile = new AbpModuleFinder().Find(projectFile).First();
new DependsOnAdder().Add(moduleFile, packageInfo.ModuleClass);
var moduleFile = ModuleClassFinder.Find(projectFile).First();
//TODO: Handle multiple module classes!
ModuleClassDependcyAdder.Add(moduleFile, packageInfo.ModuleClass);
if (packageInfo.DependedNpmPackage != null)
{

@ -1,12 +1,16 @@
using System.Threading.Tasks;
using JetBrains.Annotations;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.ProjectModification
{
public class SolutionModuleAdder : ITransientDependency
{
public async Task AddAsync(string solutionFile, string moduleName)
public virtual async Task AddAsync([NotNull] string solutionFile, [NotNull] string moduleName)
{
Check.NotNull(solutionFile, nameof(solutionFile));
Check.NotNull(moduleName, nameof(moduleName));
}
}

Loading…
Cancel
Save