Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip permissions table conversion to 3 columns if it already is #288

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)

}
}
}
}
Loading