From 6804550cf5aa545818f3a66802be8316714e6cdb Mon Sep 17 00:00:00 2001 From: Millicent Achieng Date: Tue, 27 Aug 2024 11:10:38 +0300 Subject: [PATCH] Skip permissions table conversion to 3 columns if it already is (#288) --- ApiDoctor.Console/Program.cs | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/ApiDoctor.Console/Program.cs b/ApiDoctor.Console/Program.cs index f9861acf..769013f5 100644 --- a/ApiDoctor.Console/Program.cs +++ b/ApiDoctor.Console/Program.cs @@ -2662,7 +2662,7 @@ private static async Task GeneratePermissionFilesAsync(GeneratePermissionF } break; case PermissionsInsertionState.FindInsertionStartLine: - if (foundPermissionTablesOrBlocks == 0 && currentLine.Equals(Constants.PermissionsConstants.DefaultBoilerPlateText, StringComparison.OrdinalIgnoreCase) + if (foundPermissionTablesOrBlocks == 0 && currentLine.Equals(Constants.PermissionsConstants.DefaultBoilerPlateText, StringComparison.OrdinalIgnoreCase) || currentLine.Equals(Constants.PermissionsConstants.MultipleTableBoilerPlateText, StringComparison.OrdinalIgnoreCase)) { hasBoilerplateText = true; @@ -2679,10 +2679,10 @@ private static async Task GeneratePermissionFilesAsync(GeneratePermissionF if (!options.BootstrappingOnly) { var annotation = ExtractCodeBlockAnnotationForPermissionsTable( - docFile.DisplayName, - originalFileContents, - currentIndex, - ref codeBlockAnnotationEndLine, + docFile.DisplayName, + originalFileContents, + currentIndex, + ref codeBlockAnnotationEndLine, ref insertionStartLine, ref foundPermissionTablesOrBlocks, ref ignorePermissionTableUpdate); @@ -2981,7 +2981,7 @@ private static async Task GeneratePermissionFilesAsync(GeneratePermissionF "Not applicable." }; - private static CodeBlockAnnotation ExtractCodeBlockAnnotationForPermissionsTable(string docFileName, string[] originalFileContents, int currentIndex, + private static CodeBlockAnnotation ExtractCodeBlockAnnotationForPermissionsTable(string docFileName, string[] originalFileContents, int currentIndex, ref int codeBlockAnnotationEndLine, ref int insertionStartLine, ref int foundPermissionTablesOrBlocks, ref bool ignorePermissionTableUpdate) { // Check if permissions table has annotations. We expect the first encounter of a non empty line to contain "-->" @@ -3012,7 +3012,7 @@ private static CodeBlockAnnotation ExtractCodeBlockAnnotationForPermissionsTable var htmlComment = insertionStartLine == codeBlockAnnotationEndLine ? originalFileContents[insertionStartLine] : string.Join(" ", originalFileContents.Skip(insertionStartLine).Take(codeBlockAnnotationEndLine + 1 - insertionStartLine)); - + var metadataJsonString = DocFile.StripHtmlCommentTags(htmlComment); try @@ -3035,7 +3035,7 @@ private static CodeBlockAnnotation ExtractCodeBlockAnnotationForPermissionsTable ignorePermissionTableUpdate = true; FancyConsole.WriteLine(ConsoleColor.Red, $"Unable to parse permissions block metadata in '{docFileName}', line: {insertionStartLine + 1}", ex); } - } + } return null; } @@ -3046,6 +3046,13 @@ private static string ConvertToThreeColumnPermissionsTable(IEnumerable t foreach (string row in tableRows) { string[] cells = Regex.Split(row.Trim(), @"\s*\|\s*").Where(static x => !string.IsNullOrWhiteSpace(x)).ToArray(); + + // We already have the 3 column permissions table, abort + if (cells.Length == 3) + { + return string.Join("\r\n", tableRows); + } + var allPermissions = cells[1].Trim().Split(',', StringSplitOptions.TrimEntries) .Where(x => !string.IsNullOrWhiteSpace(x) && !PermissionKeywordsToIgnore.Contains(x)) .ToList(); @@ -3346,4 +3353,4 @@ public static ConsoleColor ConsoleColor(this ValidationOutcome outcome) } } -} +} \ No newline at end of file