private static IList <Namespace> ExtractNamespacesFromText(ref string fileContent) { IList <string> namespaces = CSharpFileParser.GetNamespaces(fileContent); List <Namespace> output = new List <Namespace>(); for (int i = 0; i < namespaces.Count(); i++) { string current = namespaces[i]; bool isLast = i == namespaces.Count() - 1; string end = isLast ? "" : @"\s+namespace\s+"; string pattern = $@"namespace\s+{current}\s+\{{(?<namespaceContent>[\w\W]+)\}}{end}"; Match match = Regex.Match(fileContent, pattern); string content = match.Groups["namespaceContent"].Value.TrimEnd(); Namespace namespaceItem = new Namespace { Name = current, Imports = CSharpFileParser.ExtractImportsFromText(ref content), Content = content, HasMainMethod = CSharpFileParser.CheckHasMainMethod(content), }; output.Add(namespaceItem); fileContent = Regex.Replace(fileContent, pattern, isLast ? "" : "namespace "); } return(output); }
public static CSharpFileInfo ParseFile(string filePath) { string fileText = File.ReadAllText(filePath); return(new CSharpFileInfo { Name = Path.GetFileName(filePath), FullPathName = filePath, HasMainMethod = CSharpFileParser.CheckHasMainMethod(fileText), NamespacesContent = CSharpFileParser.ExtractNamespacesFromText(ref fileText), Imports = CSharpFileParser.ExtractImportsFromText(ref fileText), }); }
public string MergeFiles() { this.importsStringBuilder.Clear(); this.importsPerNamespaceSet.Clear(); this.imports.Clear(); List <CSharpFileInfo> files = this.FilesToMerge.Select(filePath => CSharpFileParser.ParseFile(filePath)).ToList(); files.Sort(); foreach (CSharpFileInfo file in files) { string headerComment = this.GetFileHeaderComment(file.Name); foreach (Namespace namespaceItem in file.NamespacesContent) { if (string.IsNullOrEmpty(namespaceItem.Name)) { continue; } if (!this.contentPerNamespaceStringBuilder.ContainsKey(namespaceItem.Name)) { this.contentPerNamespaceStringBuilder[namespaceItem.Name] = new StringBuilder(); } ISet <string> importSet = this.GetImportSetForNamespace(namespaceItem); StringBuilder importStringBuilder = this.GetImportStringBuilderForNamespace(namespaceItem); foreach (string import in file.Imports) { importSet.Add(import); } foreach (string import in namespaceItem.Imports) { importSet.Add(import); } StringBuilder contentStringBuilder = this.contentPerNamespaceStringBuilder[namespaceItem.Name]; contentStringBuilder.AppendLine(headerComment.ToString()); contentStringBuilder.AppendLine(namespaceItem.Content); } } string mergedNamespacesContent = this.MergeNamespacesContent(); return(mergedNamespacesContent); }