diff --git a/DotNetAstGen/Program.cs b/DotNetAstGen/Program.cs index 098ad4b..19e1dd1 100644 --- a/DotNetAstGen/Program.cs +++ b/DotNetAstGen/Program.cs @@ -224,79 +224,87 @@ private static void _SummaryForDLLFile(FileInfo filePath, string? exclusionRegex static void ProcessDll(FileInfo dllFile, string jsonPath) { - var dllPath = dllFile.FullName; - var pdbPath = Path.Combine(dllFile.DirectoryName ?? "./", - $"{Path.GetFileNameWithoutExtension(dllPath)}.pdb"); - - // check if PDB exists - if (!File.Exists(pdbPath)) + try { - _logger?.LogInformation("PDB not found! trying to generate PDB locally for: {filePath}", dllPath); - PDBGenerator pdbgen = new PDBGenerator(); - pdbgen.GeneratePDBforDLLFile(dllPath, pdbPath); - } - - // check again - if (!File.Exists(pdbPath)) { + var dllPath = dllFile.FullName; + var pdbPath = Path.Combine(dllFile.DirectoryName ?? "./", + $"{Path.GetFileNameWithoutExtension(dllPath)}.pdb"); + + // check if PDB exists + if (!File.Exists(pdbPath)) + { + _logger?.LogInformation("PDB not found! trying to generate PDB locally for: {filePath}", dllPath); + PDBGenerator pdbgen = new PDBGenerator(); + pdbgen.GeneratePDBforDLLFile(dllPath, pdbPath); + } + + // check again + if (!File.Exists(pdbPath)) + { _logger?.LogWarning("{}.dll does not have an accompanying PDB file, skipping...", Path.GetFileNameWithoutExtension(dllPath)); - } - else - { - var p = new ReaderParameters + } + else { - ReadSymbols = true - }; + var p = new ReaderParameters + { + ReadSymbols = true + }; - var classInfoList = new List(); + var classInfoList = new List(); - using var x = AssemblyDefinition.ReadAssembly(dllPath, p); - var typeFilter = new Regex("^(||.*AnonymousType|.*\\/).*", - RegexOptions.IgnoreCase); - var methodFilter = new Regex("^.*\\.(ctor|cctor)", RegexOptions.IgnoreCase); + using var x = AssemblyDefinition.ReadAssembly(dllPath, p); + var typeFilter = new Regex("^(||.*AnonymousType|.*\\/).*", + RegexOptions.IgnoreCase); + var methodFilter = new Regex("^.*\\.(ctor|cctor)", RegexOptions.IgnoreCase); - foreach (var typ in x.MainModule.GetAllTypes().DistinctBy(t => t.FullName).Where(t => t.Name != null) - .Where(t => !typeFilter.IsMatch(t.FullName))) - { - var classInfo = new ClassInfo(); - var methodInfoList = new List(); - - foreach (var method in typ.Methods.Where(m => !methodFilter.IsMatch(m.Name)).Where(m => m.IsPublic)) + foreach (var typ in x.MainModule.GetAllTypes().DistinctBy(t => t.FullName).Where(t => t.Name != null) + .Where(t => !typeFilter.IsMatch(t.FullName))) { - var methodInfo = new MethodInfo + var classInfo = new ClassInfo(); + var methodInfoList = new List(); + + foreach (var method in typ.Methods.Where(m => !methodFilter.IsMatch(m.Name)).Where(m => m.IsPublic)) { - name = method.Name, - returnType = method.ReturnType.ToString(), - isStatic = method.IsStatic - }; - var parameterTypesList = method.Parameters - .Select(param => (List) [param.Name, param.ParameterType.FullName]).ToList(); - - methodInfo.parameterTypes = parameterTypesList; - methodInfoList.Add(methodInfo); + var methodInfo = new MethodInfo + { + name = method.Name, + returnType = method.ReturnType.ToString(), + isStatic = method.IsStatic + }; + var parameterTypesList = method.Parameters + .Select(param => (List)[param.Name, param.ParameterType.FullName]).ToList(); + + methodInfo.parameterTypes = parameterTypesList; + methodInfoList.Add(methodInfo); + } + + classInfo.methods = methodInfoList; + classInfo.fields = []; + classInfo.name = typ.FullName; + classInfoList.Add(classInfo); } - classInfo.methods = methodInfoList; - classInfo.fields = []; - classInfo.name = typ.FullName; - classInfoList.Add(classInfo); - } + var namespaceStructure = new Dictionary>(); + foreach (var c in classInfoList) + { + var parentNamespace = string.Join(".", + c.name?.Split('.').Reverse().Skip(1).Reverse() ?? Array.Empty()); - var namespaceStructure = new Dictionary>(); - foreach (var c in classInfoList) - { - var parentNamespace = string.Join(".", - c.name?.Split('.').Reverse().Skip(1).Reverse() ?? Array.Empty()); + if (!namespaceStructure.ContainsKey(parentNamespace)) + namespaceStructure[parentNamespace] = []; - if (!namespaceStructure.ContainsKey(parentNamespace)) - namespaceStructure[parentNamespace] = []; + namespaceStructure[parentNamespace].Add(c); + } - namespaceStructure[parentNamespace].Add(c); + var jsonString = JsonConvert.SerializeObject(namespaceStructure, Formatting.Indented); + File.WriteAllText(jsonPath, jsonString); + _logger?.LogInformation("Successfully summarized: {filePath}", dllFile.FullName); } - - var jsonString = JsonConvert.SerializeObject(namespaceStructure, Formatting.Indented); - File.WriteAllText(jsonPath, jsonString); - _logger?.LogInformation("Successfully summarized: {filePath}", dllFile.FullName); + } + catch (Exception e) + { + _logger?.LogError(e.ToString()); } } } @@ -317,4 +325,4 @@ internal class Options [Option('e', "exclude", Required = false, HelpText = "Exclusion regex for while files to filter out.")] public string? ExclusionRegex { get; set; } = null; } -} \ No newline at end of file +}