Skip to content

Commit

Permalink
Simplify how we compute where to start looking for the next permissio…
Browse files Browse the repository at this point in the history
…ns table after finding the first one
  • Loading branch information
millicentachieng committed Aug 7, 2024
1 parent 5a8ad08 commit 48f6bbb
Showing 1 changed file with 65 additions and 45 deletions.
110 changes: 65 additions & 45 deletions ApiDoctor.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2645,8 +2645,8 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF
int foundPermissionTablesOrBlocks = 0, foundHttpRequestBlocks = 0;
bool finishedParsing = false, isBootstrapped = false, ignorePermissionTableUpdate = false,
foundAllPermissionTables = false, mergePermissions = false;
int insertionStartLine = -1, insertionEndLine = -1, httpRequestStartLine = -1, httpRequestEndLine = -1,
boilerplateStartLine = -1, boilerplateEndLine = -1, permissionsHeaderIndex = -1, codeBlockAnnotationEndLine = -1;
int insertionStartLine = -1, insertionEndLine = -1, httpRequestStartLine = -1, httpRequestEndLine = -1, boilerplateStartLine = -1,
boilerplateEndLine = -1, permissionsHeaderIndex = -1, codeBlockAnnotationEndLine = -1, permissionsBlockLineCount = -1;
string[] requestUrlsForPermissions = null;
for (var currentIndex = 0; currentIndex < originalFileContents.Length && !finishedParsing; currentIndex++)
{
Expand All @@ -2666,44 +2666,10 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF
ignorePermissionTableUpdate = true;
}

// Extract HTML comment
if (codeBlockAnnotationEndLine != -1 && (requestUrlsForPermissions != null || !mergePermissions))
{
var htmlComment = insertionStartLine == codeBlockAnnotationEndLine
? originalFileContents[insertionStartLine]
: string.Join(" ", originalFileContents.Skip(insertionStartLine).Take(codeBlockAnnotationEndLine + 1 - insertionStartLine));
var metadataJsonString = DocFile.StripHtmlCommentTags(htmlComment);
var annotation = CodeBlockAnnotation.ParseMetadata(metadataJsonString);
if (annotation.BlockType == CodeBlockType.Permissions)
{
requestUrlsForPermissions = annotation?.RequestUrls;
mergePermissions = annotation?.MergePermissions ?? false;
}
else
{
insertionStartLine = -1;
codeBlockAnnotationEndLine = -1;
}
}

var nextLine = currentIndex + 1 < originalFileContents.Length ? originalFileContents[currentIndex + 1].Trim() : "";
if (currentLine.StartsWith("<!-- {")
|| currentLine.StartsWith("<!--{")
|| (currentLine.StartsWith("<!--") && nextLine.StartsWith('{')))
{
insertionStartLine = currentIndex;
if (currentLine.EndsWith("-->"))
{
codeBlockAnnotationEndLine = currentIndex;
}
}
else if (insertionStartLine >= 0 && currentLine.EndsWith("-->"))
{
codeBlockAnnotationEndLine = currentIndex;
}
else if (currentLine.Contains("[!INCLUDE [permissions-table](", StringComparison.OrdinalIgnoreCase)) // bootstrapping already took place
if (currentLine.Contains("[!INCLUDE [permissions-table](", StringComparison.OrdinalIgnoreCase)) // bootstrapping already took place
{
foundPermissionTablesOrBlocks++;
insertionEndLine = currentIndex; // [!INCLUDE [permissions-table]... is the end of the insertion block
if (ignorePermissionTableUpdate)
{
FancyConsole.WriteLine(ConsoleColor.Yellow, $"Skipping update of permissions table ({foundPermissionTablesOrBlocks}) in {docFile.DisplayName}");
Expand All @@ -2712,13 +2678,65 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF
}

isBootstrapped = true;

if (!options.BootstrappingOnly)
{
insertionEndLine = currentIndex; // [!INCLUDE [permissions-table]... is the end of the insertion block
parseStatus = (requestUrlsForPermissions?.Length ?? 0) == 0
? PermissionsInsertionState.FindHttpRequestHeading
: PermissionsInsertionState.InsertPermissionBlock;
break;
// find the permissions block start line
var previousContents = "";
for (var i = currentIndex; i > 0; i--)
{
var lineContents = originalFileContents[i].Trim();
if (lineContents.EndsWith("-->"))
{
codeBlockAnnotationEndLine = i;
}
else if (lineContents.StartsWith("<!--") && (lineContents[4..].Trim().StartsWith('{') || previousContents.StartsWith('{')))
{
insertionStartLine = i;
parseStatus = PermissionsInsertionState.FindHttpRequestHeading;
break;
}
previousContents = lineContents;
}

// Extract HTML comment
if (codeBlockAnnotationEndLine != -1 && insertionStartLine != -1)
{
var htmlComment = insertionStartLine == codeBlockAnnotationEndLine
? originalFileContents[insertionStartLine]
: string.Join(" ", originalFileContents.Skip(insertionStartLine).Take(codeBlockAnnotationEndLine + 1 - insertionStartLine));
var metadataJsonString = DocFile.StripHtmlCommentTags(htmlComment);
try
{
var annotation = CodeBlockAnnotation.ParseMetadata(metadataJsonString);
if (annotation.BlockType == CodeBlockType.Permissions)
{
requestUrlsForPermissions = annotation?.RequestUrls;
mergePermissions = annotation?.MergePermissions ?? false;
}
else // Something's wrong with the metadata
{
ignorePermissionTableUpdate = true;
parseStatus = PermissionsInsertionState.FindNextPermissionBlock;
FancyConsole.WriteLine(ConsoleColor.Red, $"The HTML metadata for permissions table({foundPermissionTablesOrBlocks}) in {docFile.DisplayName} is wrong)");
break;
}
}
catch (Exception ex)
{
ignorePermissionTableUpdate = true;
parseStatus = PermissionsInsertionState.FindNextPermissionBlock;
FancyConsole.WriteLine(ConsoleColor.Red, $"Unable to parse permissions block metadata in '{docFile.DisplayName}', line: {insertionStartLine + 1}", ex);
break;
}
}
else
{
// If we are here, the metadata for the permissions table is missing or incomplete. Aim to add it.
codeBlockAnnotationEndLine = insertionStartLine = currentIndex;
parseStatus = PermissionsInsertionState.FindHttpRequestHeading;
break;
}
}
}
else if (currentLine.Contains('|') && currentLine.Contains("Permission type", StringComparison.OrdinalIgnoreCase)) // found the permissions table
Expand Down Expand Up @@ -2920,6 +2938,7 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF

// insert permissions block text into doc file
var permissionsBlockText = GeneratePermissionsBlockText(docFileName, Path.Combine(permissionsFileRelativePath, permissionsFileName), requestUrlsForPermissions, mergePermissions);
permissionsBlockLineCount = permissionsBlockText.Split(Environment.NewLine).Length;
IEnumerable<string> updatedFileContents = originalFileContents;
updatedFileContents = updatedFileContents.Splice(insertionStartLine, insertionEndLine + 1 - insertionStartLine);
updatedFileContents = FileSplicer(updatedFileContents.ToArray(), insertionStartLine - 1, permissionsBlockText);
Expand All @@ -2932,9 +2951,10 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF
var newFileContents = await File.ReadAllLinesAsync(docFile.FullPath);
currentIndex = newFileContents.Length == originalFileContents.Length
? insertionEndLine
: codeBlockAnnotationEndLine - (originalFileContents.Length - newFileContents.Length) + 1;
: insertionStartLine + permissionsBlockLineCount - 1;
originalFileContents = newFileContents;
insertionStartLine = insertionEndLine = httpRequestStartLine = httpRequestEndLine = codeBlockAnnotationEndLine = - 1;
insertionStartLine = insertionEndLine = httpRequestStartLine = httpRequestEndLine =
codeBlockAnnotationEndLine = permissionsBlockLineCount = -1;
mergePermissions = false;
requestUrlsForPermissions = null;
foundHttpRequestBlocks = 0;
Expand Down

0 comments on commit 48f6bbb

Please sign in to comment.