Skip to content

Commit

Permalink
wrap symbol reader in a try-catch
Browse files Browse the repository at this point in the history
  • Loading branch information
karan-batavia committed Apr 1, 2024
1 parent 0a8a223 commit 3127d6f
Showing 1 changed file with 67 additions and 59 deletions.
126 changes: 67 additions & 59 deletions DotNetAstGen/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ClassInfo>();
var classInfoList = new List<ClassInfo>();

using var x = AssemblyDefinition.ReadAssembly(dllPath, p);
var typeFilter = new Regex("^(<PrivateImplementationDetails>|<Module>|.*AnonymousType|.*\\/).*",
RegexOptions.IgnoreCase);
var methodFilter = new Regex("^.*\\.(ctor|cctor)", RegexOptions.IgnoreCase);
using var x = AssemblyDefinition.ReadAssembly(dllPath, p);
var typeFilter = new Regex("^(<PrivateImplementationDetails>|<Module>|.*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<MethodInfo>();

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<MethodInfo>();

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<string>) [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<string>)[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<string, List<ClassInfo>>();
foreach (var c in classInfoList)
{
var parentNamespace = string.Join(".",
c.name?.Split('.').Reverse().Skip(1).Reverse() ?? Array.Empty<string>());

var namespaceStructure = new Dictionary<string, List<ClassInfo>>();
foreach (var c in classInfoList)
{
var parentNamespace = string.Join(".",
c.name?.Split('.').Reverse().Skip(1).Reverse() ?? Array.Empty<string>());
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());
}
}
}
Expand All @@ -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;
}
}
}

0 comments on commit 3127d6f

Please sign in to comment.