Skip to content

Commit

Permalink
Skip permissions table conversion to 3 columns if it already is (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
millicentachieng authored Aug 27, 2024
1 parent b785f01 commit 6804550
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions ApiDoctor.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2662,7 +2662,7 @@ private static async Task<bool> 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;
Expand All @@ -2679,10 +2679,10 @@ private static async Task<bool> 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);
Expand Down Expand Up @@ -2981,7 +2981,7 @@ private static async Task<bool> 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 "-->"
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

Expand All @@ -3046,6 +3046,13 @@ private static string ConvertToThreeColumnPermissionsTable(IEnumerable<string> 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();
Expand Down Expand Up @@ -3346,4 +3353,4 @@ public static ConsoleColor ConsoleColor(this ValidationOutcome outcome)

}
}
}
}

0 comments on commit 6804550

Please sign in to comment.