public static IEnumerable<ClassDeclarationSyntax> GetAllClasses(string projectName, bool isSkipAttribute, string attribute) { var project = Solution.Projects.First(x => x.Name == projectName); var classes = new List<ClassDeclarationSyntax>(); if (project != null) { var mDocuments = project.Documents.Where(d => !d.Name.Contains(".g.cs")); var mSyntaxTrees = mDocuments.Select(d => CSharpSyntaxTree.ParseText(d.GetTextAsync().Result)).Where(t => t != null).ToList(); var classVisitor = new ClassVirtualizationVisitor(); foreach (var syntaxTree in mSyntaxTrees) { classVisitor.Visit(syntaxTree.GetRoot()); } if (!isSkipAttribute) { classes = classVisitor.Classes.Where(x => x.AttributeLists .Any(att => att.Attributes .Any(att2 => att2.Name.ToString() == attribute))).ToList(); } else { classes = classVisitor.Classes; } } return classes; }
public static void InitVisitor(IEnumerable<string> projectNames) { Visitors = new List<ClassVirtualizationVisitor>(); foreach (var name in projectNames) { var project = Solution.Projects.FirstOrDefault(x => x.Name == name); if (project != null) { var mDocuments = project.Documents.Where(d => !d.Name.Contains(".g.cs")); var mSyntaxTrees = mDocuments.Select(d => CSharpSyntaxTree.ParseText(d.GetTextAsync().Result)).Where(t => t != null).ToList(); var classVisitor = new ClassVirtualizationVisitor(); foreach (var syntaxTree in mSyntaxTrees) { classVisitor.Visit(syntaxTree.GetRoot()); } Visitors.Add(classVisitor); } } }
//public string GetMapName(ClassDeclarationSyntax element) //{ // var value = element.Identifier; // const string nameProperty = "Name"; // Attributes attributes = null; // var codeClass = element as CodeClass; // if (codeClass != null) // { // attributes = codeClass.Attributes; // } // var codeProperty = element as CodeProperty; // if (codeProperty != null) // { // attributes = codeProperty.Attributes; // } // foreach (CodeAttribute ca in attributes) // { // if (ca.Name.Contains(MapAttribute) && ca.Value.Contains(nameProperty)) // { // value = ca.Value.Remove(0, ca.Value.IndexOf(nameProperty)); // value = value.Replace(" ", ""); // if (value.Contains(",")) // { // value = value.Remove(value.IndexOf(",")); // } // value = value.Remove(0, nameProperty.Length + 1); // value = value.Replace("\"", "").ToLower(); // if (DoSuffix != null && codeClass != null) // { // if (value.EndsWith(DoSuffix.ToLower())) // { // value = value.Replace(DoSuffix.ToLower(), ""); // } // } // if (DtoSuffix != null && codeClass != null) // { // if (value.EndsWith(DtoSuffix.ToLower())) // { // value = value.Replace(DtoSuffix.ToLower(), ""); // } // } // } // } // value = value.ToLower(); // if (DoSuffix != null && codeClass != null) // { // if (value.EndsWith(DoSuffix.ToLower())) // { // value = value.Replace(DoSuffix.ToLower(), ""); // } // } // if (DtoSuffix != null && codeClass != null) // { // if (value.EndsWith(DtoSuffix.ToLower())) // { // value = value.Replace(DtoSuffix.ToLower(), ""); // } // } // return value; //} //public bool CompareTwoPropertyType(CodeProperty DOCodeProperty, CodeProperty DtoCodeProperty, // List<MapDtoAndDo> similarClasses) //{ // var DOType = DOCodeProperty.Type.AsString; // var DtoType = DtoCodeProperty.Type.AsString; // if (DOType.Contains("IEnumerable") && DtoType.Contains("IEnumerable")) // { // DOType = DOType.Remove(0, DOType.IndexOf("<") + 1); // DOType = DOType.Replace(">", ""); // DtoType = DtoType.Remove(0, DtoType.IndexOf("<") + 1); // DtoType = DtoType.Replace(">", ""); // if (CompareInMapDtoAndDoCollection(DOType, DtoType, similarClasses)) // return true; // } // if (CompareInMapDtoAndDoCollection(DOType, DtoType, similarClasses)) // return true; // if (DOType == DtoType) // { // return true; // } // return false; //} //public bool CompareTwoPropertyType(string DOType, string DtoType, List<MapDtoAndDo> similarClasses) //{ // if (DOType.Contains("IEnumerable") && DtoType.Contains("IEnumerable")) // { // DOType = DOType.Remove(0, DOType.IndexOf("<") + 1); // DOType = DOType.Replace(">", ""); // DtoType = DtoType.Remove(0, DtoType.IndexOf("<") + 1); // DtoType = DtoType.Replace(">", ""); // if (CompareInMapDtoAndDoCollection(DOType, DtoType, similarClasses)) // return true; // } // if (CompareInMapDtoAndDoCollection(DOType, DtoType, similarClasses)) // return true; // if (DOType == DtoType) // { // return true; // } // return false; //} //public bool CompareInMapDtoAndDoCollection(string DOType, string DtoType, List<MapDtoAndDo> similarClasses) //{ // var similarInterfacesDO = // similarClasses.Where(x => x.DOInterface != null).FirstOrDefault(x => x.DOInterface.FullName == DOType); // var similarInterfacesDto = // similarClasses.Where(x => x.DtoInterface != null) // .FirstOrDefault(x => x.DtoInterface.FullName == DtoType); // var checkingMapClass = new MapDtoAndDo(); // checkingMapClass = similarClasses.FirstOrDefault(x => x.DOClass.FullName == DOType); // if (checkingMapClass != null && // checkingMapClass == similarClasses.FirstOrDefault(x => x.DtoClass.FullName == DtoType)) // { // return true; // } // similarInterfacesDO = // similarClasses.Where(x => x.DOInterface != null).FirstOrDefault(x => x.DOInterface.FullName == DOType); // similarInterfacesDto = // similarClasses.Where(x => x.DtoInterface != null) // .FirstOrDefault(x => x.DtoInterface.FullName == DtoType); // if (similarInterfacesDO != null && // similarInterfacesDO == similarClasses.FirstOrDefault(x => x.DtoClass.FullName == DtoType)) // { // return true; // } // if (similarInterfacesDto != null && // similarClasses.FirstOrDefault(x => x.DOClass.FullName == DOType) == similarInterfacesDto) // { // return true; // } // if (similarInterfacesDO != null && similarInterfacesDO == similarInterfacesDto) // { // return true; // } // return false; //} //public KindOfProperty GetKindOfMapProperty(CodeProperty codeProperty, List<MapDtoAndDo> listOfSimilarClasses) //{ // var type = codeProperty.Type.AsString; // if (type.Contains("IEnumerable")) // { // type = type.Remove(0, type.IndexOf("<") + 1); // type = type.Replace(">", ""); // if (listOfSimilarClasses.Any(x => x.DOClass.FullName == type) || // listOfSimilarClasses.Where(x => x.DOInterface != null).Any(x => x.DOInterface.FullName == type)) // { // return KindOfProperty.CollectionAttributeClasses; // } // if (listOfSimilarClasses.Any(x => x.DtoClass.FullName == type) || // listOfSimilarClasses.Where(x => x.DtoInterface != null).Any(x => x.DtoInterface.FullName == type)) // { // return KindOfProperty.CollectionAttributeClasses; // } // } // if (listOfSimilarClasses.Any(x => x.DOClass.FullName == type) || // listOfSimilarClasses.Where(x => x.DOInterface != null).Any(x => x.DOInterface.FullName == type)) // { // return KindOfProperty.AttributeClass; // } // if (listOfSimilarClasses.Any(x => x.DtoClass.FullName == type) || // listOfSimilarClasses.Where(x => x.DtoInterface != null).Any(x => x.DtoInterface.FullName == type)) // { // return KindOfProperty.AttributeClass; // } // return KindOfProperty.None; //} //public KindOfProperty GetKindOfMapProperty(string type, List<MapDtoAndDo> listOfSimilarClasses) //{ // if (type.Contains("IEnumerable")) // { // type = type.Remove(0, type.IndexOf("<") + 1); // type = type.Replace(">", ""); // if (listOfSimilarClasses.Any(x => x.DOClass.FullName == type) || // listOfSimilarClasses.Where(x => x.DOInterface != null).Any(x => x.DOInterface.FullName == type)) // { // return KindOfProperty.CollectionAttributeClasses; // } // if (listOfSimilarClasses.Any(x => x.DtoClass.FullName == type) || // listOfSimilarClasses.Where(x => x.DtoInterface != null).Any(x => x.DtoInterface.FullName == type)) // { // return KindOfProperty.CollectionAttributeClasses; // } // } // if (listOfSimilarClasses.Any(x => x.DOClass.FullName == type) || // listOfSimilarClasses.Where(x => x.DOInterface != null).Any(x => x.DOInterface.FullName == type)) // { // return KindOfProperty.AttributeClass; // } // if (listOfSimilarClasses.Any(x => x.DtoClass.FullName == type) || // listOfSimilarClasses.Where(x => x.DtoInterface != null).Any(x => x.DtoInterface.FullName == type)) // { // return KindOfProperty.AttributeClass; // } // return KindOfProperty.None; //} public async Task<IEnumerable<ClassDeclarationSyntax>> GetAllClasses(string project, bool isSkipAttribute,string attribute) { _project = _solution.Projects.First(x => x.Name == project); var compilation = await _project.GetCompilationAsync(); var classVisitor = new ClassVirtualizationVisitor(); var classes = new List<ClassDeclarationSyntax>(); foreach (var syntaxTree in compilation.SyntaxTrees) { classVisitor.Visit(syntaxTree.GetRoot()); } if (!isSkipAttribute) { classes = classVisitor.Classes.Where(x => x.AttributeLists .Any(att => att.Attributes .Any(att2 => att2.Name.ToString() == attribute))).ToList(); } else { classes = classVisitor.Classes; } return classes; }