44import java .nio .file .Path ;
55import java .nio .file .Paths ;
66import java .nio .file .StandardOpenOption ;
7- import java .util .Objects ;
7+ import java .util .* ;
88
9- public class AggregateSnippets {
10- public static void main (String [] args ) throws Throwable {
11- final var repoRoot = Paths .get ("" ).toAbsolutePath ();
12- final var snippetsSrcRoot = repoRoot .resolve ("src/main/java/com/vonage/quickstart" );
13- final var classFileName = "AggregateSnippets.java" ;
14- var sb = new StringBuilder (1 << 17 )
15- .append ("# Vonage Java SDK Code Snippets\n " )
9+ public final class AggregateSnippets {
10+ record CodeSnippetFile (
11+ Path file ,
12+ int mainStartIndex , int mainEndIndex ,
13+ int clientStartIndex , int clientEndIndex
14+ ) {
15+
16+ }
17+
18+ private final StringBuilder sb = new StringBuilder (1 << 17 );
19+ private final Path snippetsSrcRoot ;
20+ private final Collection <CodeSnippetFile > snippetFiles = new LinkedHashSet <>(256 );
21+
22+ public AggregateSnippets (Path snippetsSrcRoot ) {
23+ this .snippetsSrcRoot = Objects .requireNonNull (snippetsSrcRoot );
24+ }
25+
26+ public String computeContents () throws IOException {
27+ final String classFileName = "AggregateSnippets.java" ;
28+ sb .append ("# Vonage Java SDK Code Snippets\n " )
1629 .append ("Here are all the snippets in this repository.\n " )
1730 .append ("This file was generated by running [" ).append (classFileName )
1831 .append ("](src/main/java/" ).append (classFileName )
1932 .append (") from the root of the repository." )
2033 .append ("\n \n ## Contents" );
2134
22- var allDirs = Files .list (snippetsSrcRoot )
23- .filter (Files ::isDirectory )
35+ var allDirs = getAllDirsSorted ();
36+
37+ for (var file : allDirs ) {
38+ var title = toHeadingTitle (file .getName ());
39+ sb .append ("\n - [**" ).append (title ).append ("**](#" )
40+ .append (title .toLowerCase ().replace (' ' , '-' )).append (")" );
41+ }
42+ sb .append ("\n " );
43+
44+ for (var file : allDirs ) {
45+ appendSnippetContent (file , 2 );
46+ }
47+
48+ return sb .toString ();
49+ }
50+
51+ private List <File > getAllDirsSorted () {
52+ try (var pathStream = Files .list (snippetsSrcRoot )) {
53+ return pathStream .filter (Files ::isDirectory )
2454 .map (Path ::toFile )
2555 .sorted ((f1 , f2 ) -> {
2656 if (isInitialize (f1 )) return -1 ;
2757 if (isInitialize (f2 )) return 1 ;
2858 return f1 .getName ().compareToIgnoreCase (f2 .getName ());
2959 })
3060 .toList ();
61+ }
62+ catch (IOException ex ) {
63+ System .err .println ("Could not read " +snippetsSrcRoot +": " + ex .getMessage ());
64+ return List .of ();
65+ }
66+ }
3167
32- for ( var file : allDirs ) {
33- var title = toHeadingTitle ( file .getName () );
34- sb . append ( " \n - [**" ). append ( title ). append ( "**](#" )
35- . append ( title . toLowerCase (). replace ( ' ' , '-' )). append ( ")" );
68+ private void appendSnippetContent ( File path , int level ) throws IOException {
69+ var fileName = path .getName ();
70+ if ( path . isFile ()) {
71+ fileName = fileName . substring ( 0 , fileName . lastIndexOf ( '.' ) );
3672 }
37- sb . append ( " \n " ) ;
73+ if ( fileName . trim (). length () < 3 ) return ;
3874
39- for (var file : allDirs ) {
40- appendSnippetContent (sb , file , 2 );
75+ sb .append ("#" .repeat (level )).append (' ' ).append (toHeadingTitle (fileName )).append ('\n' );
76+ if (path .isDirectory ()) {
77+ for (var file : Objects .requireNonNull (path .listFiles ())) {
78+ appendSnippetContent (file , level + 1 );
79+ }
4180 }
81+ else if (level > 2 && path .getName ().endsWith (".java" )) {
82+ final String fileContent = Files .readString (path .toPath ()),
83+ clientInitEndStr = ".build();\n \n " ,
84+ clientInitStartStr = "VonageClient client" ;
4285
43- var destPath = repoRoot .resolve ("SNIPPETS.md" );
44- Files .deleteIfExists (destPath );
45- Files .writeString (destPath , sb .toString (), StandardOpenOption .CREATE_NEW );
86+ final int clientInitStartIndex = fileContent .indexOf (clientInitStartStr ) - 8 ,
87+ clientInitEndIndex = fileContent .indexOf (clientInitEndStr ) + 11 ,
88+ endIndex = fileContent .lastIndexOf ('}' , fileContent .lastIndexOf ('}' ) - 1 ) - 1 ,
89+ startIndex = Math .min (endIndex , clientInitStartIndex > 0 ?
90+ (isInitialize (path .getParentFile ()) ? clientInitStartIndex : clientInitEndIndex ) :
91+ fileContent .indexOf ('{' , fileContent .indexOf ('{' ) + 1 ) + 2
92+ );
93+
94+ final String nugget = fileContent .substring (startIndex , endIndex )
95+ .stripTrailing ().stripIndent ().replace ("\t " , " " );
96+
97+ sb .append ("\n ```java\n " ).append (nugget ).append ("\n ```\n " );
98+ }
4699 }
47100
48- static boolean isInitialize (File file ) {
101+ private static boolean isInitialize (File file ) {
49102 return file .getName ().equals ("initialize" );
50103 }
51104
52- static String toHeadingTitle (String title ) {
105+ private static String toHeadingTitle (String title ) {
53106 var acronyms = new String []{
54107 "jwt" , "id" , "uuid" , "url" , "sim" ,
55108 "sms" , "rcs" , "mms" , "psd2" , "dlr" , "cnam" ,
@@ -74,33 +127,12 @@ static String toHeadingTitle(String title) {
74127 return result ;
75128 }
76129
77- static void appendSnippetContent (StringBuilder contentBuilder , File path , int level ) throws IOException {
78- var fileName = path .getName ();
79- if (path .isFile ()) {
80- fileName = fileName .substring (0 , fileName .lastIndexOf ('.' ));
81- }
82- if (fileName .trim ().length () < 3 ) return ;
83-
84- contentBuilder .append ("#" .repeat (level )).append (' ' ).append (toHeadingTitle (fileName )).append ('\n' );
85- if (path .isDirectory ()) {
86- for (var file : Objects .requireNonNull (path .listFiles ())) {
87- appendSnippetContent (contentBuilder , file , level + 1 );
88- }
89- }
90- else if (level > 2 && path .getName ().endsWith (".java" )) {
91- final var fileContent = Files .readString (path .toPath ());
92- final var clientInitEndStr = ".build();\n \n " ;
93- final var clientInitStartStr = "VonageClient client" ;
94- final int endIndex = fileContent .lastIndexOf ('}' , fileContent .lastIndexOf ('}' ) - 1 ) - 1 ;
95- final int startIndex = Math .min (endIndex , fileContent .contains (clientInitStartStr ) ?
96- (isInitialize (path .getParentFile ()) ? fileContent .indexOf (clientInitStartStr ) - 8 :
97- fileContent .indexOf (clientInitEndStr ) + clientInitEndStr .length ()) :
98- fileContent .indexOf ('{' , fileContent .indexOf ('{' ) + 1 ) + 2
99- );
100-
101- final var nugget = fileContent .substring (startIndex , endIndex )
102- .stripTrailing ().stripIndent ().replace ("\t " , " " );
103- contentBuilder .append ("\n ```java\n " ).append (nugget ).append ("\n ```\n " );
104- }
130+ public static void main (String [] args ) throws Throwable {
131+ final var repoRoot = Paths .get ("" ).toAbsolutePath ();
132+ final var snippetsSrcRoot = repoRoot .resolve ("src/main/java/com/vonage/quickstart" );
133+ final String contents = new AggregateSnippets (snippetsSrcRoot ).computeContents ();
134+ var destPath = repoRoot .resolve ("SNIPPETS.md" );
135+ Files .deleteIfExists (destPath );
136+ Files .writeString (destPath , contents , StandardOpenOption .CREATE_NEW );
105137 }
106138}
0 commit comments