@@ -2034,7 +2034,8 @@ private static string GenerateSnippetsTabSectionForMethod(MethodDefinition metho
2034
2034
{
2035
2035
sampleCodeIncludeText = $ "[!INCLUDE [sample-code](../{ ReplaceWindowsByLinuxPathSeparators ( Path . Combine ( relativePathFolder , codeFenceString , versionString , snippetFileName ) ) } )]";
2036
2036
}
2037
- else {
2037
+ else
2038
+ {
2038
2039
sampleCodeIncludeText = $ "[!INCLUDE [sample-code](../{ ReplaceWindowsByLinuxPathSeparators ( Path . Combine ( relativePathFolder , codeFenceString , snippetFileName ) ) } )]";
2039
2040
}
2040
2041
var tabText = $ "# [{ language } ](#tab/{ codeFenceString } )\r \n " +
@@ -2047,7 +2048,7 @@ private static string GenerateSnippetsTabSectionForMethod(MethodDefinition metho
2047
2048
if ( codeSnippet != null )
2048
2049
{
2049
2050
var snippetFileContents = "---\r \n description: \" Automatically generated file. DO NOT MODIFY\" \r \n ---\r \n \r \n " + //header
2050
- $ "```{ codeFenceString . Replace ( "cli" , "bash" ) } \r \n \r \n " + // code fence
2051
+ $ "```{ codeFenceString . Replace ( "cli" , "bash" ) } \r \n \r \n " + // code fence
2051
2052
$ "{ codeSnippet } \r \n \r \n " + // generated code snippet
2052
2053
"```" ; // closing fence
2053
2054
@@ -2645,8 +2646,8 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF
2645
2646
int foundPermissionTablesOrBlocks = 0 , foundHttpRequestBlocks = 0 ;
2646
2647
bool finishedParsing = false , isBootstrapped = false , ignorePermissionTableUpdate = false ,
2647
2648
foundAllPermissionTables = false , mergePermissions = false ;
2648
- int insertionStartLine = - 1 , insertionEndLine = - 1 , httpRequestStartLine = - 1 , httpRequestEndLine = - 1 ,
2649
- boilerplateStartLine = - 1 , boilerplateEndLine = - 1 , permissionsHeaderIndex = - 1 , codeBlockAnnotationEndLine = - 1 ;
2649
+ int insertionStartLine = - 1 , insertionEndLine = - 1 , httpRequestStartLine = - 1 , httpRequestEndLine = - 1 , boilerplateStartLine = - 1 ,
2650
+ boilerplateEndLine = - 1 , permissionsHeaderIndex = - 1 , codeBlockAnnotationEndLine = - 1 , permissionsBlockLineCount = - 1 ;
2650
2651
string [ ] requestUrlsForPermissions = null ;
2651
2652
for ( var currentIndex = 0 ; currentIndex < originalFileContents . Length && ! finishedParsing ; currentIndex ++ )
2652
2653
{
@@ -2666,33 +2667,10 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF
2666
2667
ignorePermissionTableUpdate = true ;
2667
2668
}
2668
2669
2669
- // Extract HTML comment
2670
- if ( codeBlockAnnotationEndLine != - 1 && ( requestUrlsForPermissions != null || ! mergePermissions ) )
2671
- {
2672
- var htmlComment = insertionStartLine == codeBlockAnnotationEndLine
2673
- ? originalFileContents [ insertionStartLine ]
2674
- : string . Join ( " " , originalFileContents . Skip ( insertionStartLine ) . Take ( codeBlockAnnotationEndLine + 1 - insertionStartLine ) ) ;
2675
- var metadataJsonString = DocFile . StripHtmlCommentTags ( htmlComment ) ;
2676
- var annotation = CodeBlockAnnotation . ParseMetadata ( metadataJsonString ) ;
2677
- requestUrlsForPermissions = annotation ? . RequestUrls ;
2678
- mergePermissions = annotation ? . MergePermissions ?? false ;
2679
- }
2680
-
2681
- if ( currentLine . StartsWith ( "<!-- {" ) || currentLine . Contains ( "<!--{" ) )
2682
- {
2683
- insertionStartLine = currentIndex ;
2684
- if ( currentLine . Contains ( "} -->" ) || currentLine . Contains ( "}-->" ) )
2685
- {
2686
- codeBlockAnnotationEndLine = currentIndex ;
2687
- }
2688
- }
2689
- else if ( currentLine . Contains ( "} -->" ) || currentLine . Contains ( "}-->" ) )
2690
- {
2691
- codeBlockAnnotationEndLine = currentIndex ;
2692
- }
2693
- else if ( currentLine . Contains ( "[!INCLUDE [permissions-table](" , StringComparison . OrdinalIgnoreCase ) ) // bootstrapping already took place
2670
+ if ( currentLine . Contains ( "[!INCLUDE [permissions-table](" , StringComparison . OrdinalIgnoreCase ) ) // bootstrapping already took place
2694
2671
{
2695
2672
foundPermissionTablesOrBlocks ++ ;
2673
+ insertionEndLine = currentIndex ; // [!INCLUDE [permissions-table]... is the end of the insertion block
2696
2674
if ( ignorePermissionTableUpdate )
2697
2675
{
2698
2676
FancyConsole . WriteLine ( ConsoleColor . Yellow , $ "Skipping update of permissions table ({ foundPermissionTablesOrBlocks } ) in { docFile . DisplayName } ") ;
@@ -2701,13 +2679,65 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF
2701
2679
}
2702
2680
2703
2681
isBootstrapped = true ;
2682
+
2704
2683
if ( ! options . BootstrappingOnly )
2705
2684
{
2706
- insertionEndLine = currentIndex ; // [!INCLUDE [permissions-table]... is the end of the insertion block
2707
- parseStatus = ( requestUrlsForPermissions ? . Length ?? 0 ) == 0
2708
- ? PermissionsInsertionState . FindHttpRequestHeading
2709
- : PermissionsInsertionState . InsertPermissionBlock ;
2710
- break ;
2685
+ // find the permissions block start line
2686
+ var previousContents = "" ;
2687
+ for ( var i = currentIndex - 1 ; i > 0 ; i -- )
2688
+ {
2689
+ var lineContents = originalFileContents [ i ] . Trim ( ) ;
2690
+ if ( lineContents . EndsWith ( "-->" ) )
2691
+ {
2692
+ codeBlockAnnotationEndLine = i ;
2693
+ }
2694
+ if ( lineContents . StartsWith ( "<!--" ) && ( lineContents [ 4 ..] . Trim ( ) . StartsWith ( '{' ) || previousContents . StartsWith ( '{' ) ) )
2695
+ {
2696
+ insertionStartLine = i ;
2697
+ parseStatus = PermissionsInsertionState . FindHttpRequestHeading ;
2698
+ break ;
2699
+ }
2700
+ previousContents = lineContents ;
2701
+ }
2702
+
2703
+ // Extract HTML comment
2704
+ if ( codeBlockAnnotationEndLine != - 1 && insertionStartLine != - 1 )
2705
+ {
2706
+ var htmlComment = insertionStartLine == codeBlockAnnotationEndLine
2707
+ ? originalFileContents [ insertionStartLine ]
2708
+ : string . Join ( " " , originalFileContents . Skip ( insertionStartLine ) . Take ( codeBlockAnnotationEndLine + 1 - insertionStartLine ) ) ;
2709
+ var metadataJsonString = DocFile . StripHtmlCommentTags ( htmlComment ) ;
2710
+ try
2711
+ {
2712
+ var annotation = CodeBlockAnnotation . ParseMetadata ( metadataJsonString ) ;
2713
+ if ( annotation . BlockType == CodeBlockType . Permissions )
2714
+ {
2715
+ requestUrlsForPermissions = annotation ? . RequestUrls ;
2716
+ mergePermissions = annotation ? . MergePermissions ?? false ;
2717
+ }
2718
+ else // Something's wrong with the metadata
2719
+ {
2720
+ ignorePermissionTableUpdate = true ;
2721
+ parseStatus = PermissionsInsertionState . FindNextPermissionBlock ;
2722
+ FancyConsole . WriteLine ( ConsoleColor . Red , $ "The HTML metadata for permissions table({ foundPermissionTablesOrBlocks } ) in { docFile . DisplayName } is wrong)") ;
2723
+ break ;
2724
+ }
2725
+ }
2726
+ catch ( Exception ex )
2727
+ {
2728
+ ignorePermissionTableUpdate = true ;
2729
+ parseStatus = PermissionsInsertionState . FindNextPermissionBlock ;
2730
+ FancyConsole . WriteLine ( ConsoleColor . Red , $ "Unable to parse permissions block metadata in '{ docFile . DisplayName } ', line: { insertionStartLine + 1 } ", ex ) ;
2731
+ break ;
2732
+ }
2733
+ }
2734
+ else
2735
+ {
2736
+ // If we are here, the metadata for the permissions table is missing or incomplete. Aim to add it.
2737
+ codeBlockAnnotationEndLine = insertionStartLine = currentIndex ;
2738
+ parseStatus = PermissionsInsertionState . FindHttpRequestHeading ;
2739
+ break ;
2740
+ }
2711
2741
}
2712
2742
}
2713
2743
else if ( currentLine . Contains ( '|' ) && currentLine . Contains ( "Permission type" , StringComparison . OrdinalIgnoreCase ) ) // found the permissions table
@@ -2730,12 +2760,12 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF
2730
2760
for ( int index = permissionsHeaderIndex + 1 ; index < currentIndex ; index ++ )
2731
2761
{
2732
2762
// if the line is not empty and is not a sub header, this is the boilerplate start line
2733
- if ( ! string . IsNullOrWhiteSpace ( originalFileContents [ index ] ) && ! originalFileContents [ index ] . StartsWith ( '#' ) )
2734
- {
2763
+ if ( ! string . IsNullOrWhiteSpace ( originalFileContents [ index ] ) && ! originalFileContents [ index ] . StartsWith ( '#' ) )
2764
+ {
2735
2765
if ( boilerplateStartLine == permissionsHeaderIndex )
2736
2766
boilerplateStartLine = index ;
2737
2767
boilerplateEndLine = index ;
2738
- }
2768
+ }
2739
2769
}
2740
2770
}
2741
2771
}
@@ -2826,7 +2856,7 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF
2826
2856
if ( ! isBootstrapped )
2827
2857
{
2828
2858
var existingPermissionsTable = originalFileContents . Skip ( insertionStartLine + 2 ) . Take ( insertionEndLine - insertionStartLine - 1 ) ;
2829
- permissionFileContents = $ "{ includeFileMetadata } { ConvertToThreeColumnPermissionsTable ( existingPermissionsTable ) } ";
2859
+ permissionFileContents = $ "{ includeFileMetadata } { ConvertToThreeColumnPermissionsTable ( existingPermissionsTable ) } ";
2830
2860
}
2831
2861
2832
2862
if ( ! options . BootstrappingOnly )
@@ -2843,7 +2873,7 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF
2843
2873
var httpRequests = ( requestUrlsForPermissions ? . Length ?? 0 ) != 0
2844
2874
? requestUrlsForPermissions
2845
2875
: originalFileContents . Skip ( httpRequestStartLine + 1 ) . Take ( httpRequestEndLine - httpRequestStartLine - 1 ) . Where ( x => ! string . IsNullOrWhiteSpace ( x ) ) ;
2846
- var newPermissionFileContents = GetPermissionsMarkdownTableForHttpRequestBlock ( permissionsDocument , httpRequests ,
2876
+ var newPermissionFileContents = GetPermissionsMarkdownTableForHttpRequestBlock ( permissionsDocument , httpRequests ,
2847
2877
mergePermissions , docFile . DisplayName , foundPermissionTablesOrBlocks ) ; // get from Kibali
2848
2878
if ( ! string . IsNullOrWhiteSpace ( newPermissionFileContents ) )
2849
2879
{
@@ -2868,15 +2898,16 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF
2868
2898
if ( foundPermissionTablesOrBlocks == 1 )
2869
2899
{
2870
2900
// We do not have a boilerplate text in this case, add new next line
2871
- if ( boilerplateStartLine == permissionsHeaderIndex )
2901
+ if ( boilerplateStartLine == permissionsHeaderIndex )
2872
2902
{
2873
2903
// insert a new line to hold boilerplate text
2874
2904
originalFileContents = FileSplicer ( originalFileContents , boilerplateStartLine , Constants . PermissionConstants . DefaultBoilerPlateText ) . ToArray ( ) ;
2875
2905
boilerplateStartLine ++ ;
2876
2906
insertionStartLine ++ ;
2877
2907
insertionEndLine ++ ;
2878
2908
}
2879
- else {
2909
+ else
2910
+ {
2880
2911
if ( boilerplateEndLine > boilerplateStartLine )
2881
2912
{
2882
2913
int extraLinesToRemove = boilerplateEndLine - boilerplateStartLine ;
@@ -2887,7 +2918,8 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF
2887
2918
originalFileContents [ boilerplateStartLine ] = Constants . PermissionConstants . DefaultBoilerPlateText ;
2888
2919
}
2889
2920
}
2890
- else if ( foundPermissionTablesOrBlocks == 2 ) {
2921
+ else if ( foundPermissionTablesOrBlocks == 2 )
2922
+ {
2891
2923
originalFileContents [ boilerplateStartLine ] = Constants . PermissionConstants . MultipleTableBoilerPlateText ;
2892
2924
}
2893
2925
}
@@ -2908,7 +2940,8 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF
2908
2940
await File . WriteAllTextAsync ( permissionsMarkdownFilePath , permissionFileContents ) ;
2909
2941
2910
2942
// insert permissions block text into doc file
2911
- var permissionsBlockText = GeneratePermissionsBlockText ( docFileName , Path . Combine ( permissionsFileRelativePath , permissionsFileName ) , requestUrlsForPermissions , mergePermissions ) ;
2943
+ var permissionsBlockText = GeneratePermissionsBlockText ( docFileName , Path . Combine ( permissionsFileRelativePath , permissionsFileName ) , requestUrlsForPermissions , mergePermissions ) ;
2944
+ permissionsBlockLineCount = permissionsBlockText . Split ( Environment . NewLine ) . Length ;
2912
2945
IEnumerable < string > updatedFileContents = originalFileContents ;
2913
2946
updatedFileContents = updatedFileContents . Splice ( insertionStartLine , insertionEndLine + 1 - insertionStartLine ) ;
2914
2947
updatedFileContents = FileSplicer ( updatedFileContents . ToArray ( ) , insertionStartLine - 1 , permissionsBlockText ) ;
@@ -2919,11 +2952,12 @@ private static async Task<bool> GeneratePermissionFilesAsync(GeneratePermissionF
2919
2952
if ( ! ignorePermissionTableUpdate )
2920
2953
{
2921
2954
var newFileContents = await File . ReadAllLinesAsync ( docFile . FullPath ) ;
2922
- currentIndex = newFileContents . Length == originalFileContents . Length
2955
+ currentIndex = newFileContents . Length == originalFileContents . Length
2923
2956
? insertionEndLine
2924
- : codeBlockAnnotationEndLine - ( originalFileContents . Length - newFileContents . Length ) + 1 ;
2957
+ : insertionStartLine + permissionsBlockLineCount - 1 ;
2925
2958
originalFileContents = newFileContents ;
2926
- insertionStartLine = insertionEndLine = httpRequestStartLine = httpRequestEndLine = codeBlockAnnotationEndLine = - 1 ;
2959
+ insertionStartLine = insertionEndLine = httpRequestStartLine = httpRequestEndLine =
2960
+ codeBlockAnnotationEndLine = permissionsBlockLineCount = - 1 ;
2927
2961
mergePermissions = false ;
2928
2962
requestUrlsForPermissions = null ;
2929
2963
foundHttpRequestBlocks = 0 ;
@@ -2996,11 +3030,11 @@ private static string GeneratePermissionsBlockText(string docFileName, string pe
2996
3030
return permissionsBlockText ;
2997
3031
}
2998
3032
2999
- private static string GetPermissionsMarkdownTableForHttpRequestBlock ( PermissionsDocument permissionsDocument , IEnumerable < string > httpRequests , bool mergePermissions ,
3033
+ private static string GetPermissionsMarkdownTableForHttpRequestBlock ( PermissionsDocument permissionsDocument , IEnumerable < string > httpRequests , bool mergePermissions ,
3000
3034
string docFileName , int permissionsTablePosition )
3001
3035
{
3002
3036
var requestPaths = new List < HttpRequest > ( ) ;
3003
-
3037
+
3004
3038
// check validity of request paths
3005
3039
foreach ( var request in httpRequests )
3006
3040
{
@@ -3051,7 +3085,7 @@ private static string GetPermissionsMarkdownTableForHttpRequestBlock(Permissions
3051
3085
var requestPermissions = generator . GenerateTable ( ) ;
3052
3086
newPermissionFileContents ??= requestPermissions ;
3053
3087
3054
- if ( ! newPermissionFileContents . Equals ( requestPermissions , StringComparison . OrdinalIgnoreCase ) )
3088
+ if ( ! newPermissionFileContents . Equals ( requestPermissions , StringComparison . OrdinalIgnoreCase ) )
3055
3089
{
3056
3090
FancyConsole . WriteLine ( ConsoleColor . Yellow , $ "Encountered request URL(s) for permissions table ({ permissionsTablePosition } ) in { docFileName } with a different set of permissions") ;
3057
3091
}
0 commit comments