From 3a5238836bbfbf905f319d66c34996fa2fb056ff Mon Sep 17 00:00:00 2001 From: David Baker Effendi Date: Tue, 5 Dec 2023 16:44:04 +0200 Subject: [PATCH] Do Not Write JSON for Error Files Excludes writing the JSON for files that failed to parse. --- DotNetAstGen/Program.cs | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/DotNetAstGen/Program.cs b/DotNetAstGen/Program.cs index 42259b5..9772a61 100644 --- a/DotNetAstGen/Program.cs +++ b/DotNetAstGen/Program.cs @@ -122,28 +122,27 @@ private static void _AstForFile( else { _logger?.LogInformation("Successfully parsed: {filePath}", fullPath); - } - - var astGenResult = new AstGenWrapper(fullPath, tree); - var jsonString = JsonConvert.SerializeObject(astGenResult, Formatting.Indented, - new JsonSerializerSettings + var astGenResult = new AstGenWrapper(fullPath, tree); + var jsonString = JsonConvert.SerializeObject(astGenResult, Formatting.Indented, + new JsonSerializerSettings + { + ReferenceLoopHandling = ReferenceLoopHandling.Ignore, + ContractResolver = + new SyntaxNodePropertiesResolver() // Comment this to see the unfiltered parser output + }); + var outputName = Path.Combine(filePath.DirectoryName ?? "./", + $"{Path.GetFileNameWithoutExtension(fullPath)}.json") + .Replace(rootInputPath.FullName, rootOutputPath.FullName); + + // Create dirs if they do not exist + var outputParentDir = Path.GetDirectoryName(outputName); + if (outputParentDir != null) { - ReferenceLoopHandling = ReferenceLoopHandling.Ignore, - ContractResolver = - new SyntaxNodePropertiesResolver() // Comment this to see the unfiltered parser output - }); - var outputName = Path.Combine(filePath.DirectoryName ?? "./", - $"{Path.GetFileNameWithoutExtension(fullPath)}.json") - .Replace(rootInputPath.FullName, rootOutputPath.FullName); + Directory.CreateDirectory(outputParentDir); + } - // Create dirs if they do not exist - var outputParentDir = Path.GetDirectoryName(outputName); - if (outputParentDir != null) - { - Directory.CreateDirectory(outputParentDir); + File.WriteAllText(outputName, jsonString); } - - File.WriteAllText(outputName, jsonString); } catch (Exception e) {