77import java .util .*;
88
99public final class AggregateSnippets {
10- record CodeSnippetFile (
10+ public record CodeSnippetFile (
1111 Path file ,
1212 int mainStartIndex , int mainEndIndex ,
1313 int clientStartIndex , int clientEndIndex
1414 ) {
1515
1616 }
1717
18- private final StringBuilder sb = new StringBuilder ( 1 << 17 ) ;
18+ private StringBuilder sb ;
1919 private final Path snippetsSrcRoot ;
20- private final Collection <CodeSnippetFile > snippetFiles = new LinkedHashSet <>( 256 ) ;
20+ private Collection <CodeSnippetFile > snippetFiles ;
2121
2222 public AggregateSnippets (Path snippetsSrcRoot ) {
2323 this .snippetsSrcRoot = Objects .requireNonNull (snippetsSrcRoot );
2424 }
2525
26- public String computeContents () throws IOException {
27- final String classFileName = "AggregateSnippets.java" ;
28- sb .append ("# Vonage Java SDK Code Snippets\n " )
26+ private void checkComputed () {
27+ if (sb == null || sb .isEmpty ()) {
28+ throw new IllegalStateException ("Contents not computed yet." );
29+ }
30+ }
31+
32+ public Collection <CodeSnippetFile > getLineNumbers () {
33+ checkComputed ();
34+ return snippetFiles ;
35+ }
36+
37+ public String getContents () {
38+ checkComputed ();
39+ return sb .toString ();
40+ }
41+
42+ public void saveToFile (Path destPath ) throws IOException {
43+ Files .writeString (destPath , getContents (), StandardOpenOption .CREATE );
44+ }
45+
46+ public void computeContents () throws IOException {
47+ snippetFiles = new LinkedHashSet <>(256 );
48+ final String classFileName = getClass ().getSimpleName () + ".java" ;
49+ sb = new StringBuilder (1 << 17 )
50+ .append ("# Vonage Java SDK Code Snippets\n " )
2951 .append ("Here are all the snippets in this repository.\n " )
3052 .append ("This file was generated by running [" ).append (classFileName )
3153 .append ("](src/main/java/" ).append (classFileName )
@@ -39,13 +61,11 @@ public String computeContents() throws IOException {
3961 sb .append ("\n - [**" ).append (title ).append ("**](#" )
4062 .append (title .toLowerCase ().replace (' ' , '-' )).append (")" );
4163 }
42- sb .append ("\n " );
64+ sb .append ("\n \n " );
4365
4466 for (var file : allDirs ) {
4567 appendSnippetContent (file , 2 );
4668 }
47-
48- return sb .toString ();
4969 }
5070
5171 private List <File > getAllDirsSorted () {
@@ -95,6 +115,10 @@ else if (level > 2 && path.getName().endsWith(".java")) {
95115 .stripTrailing ().stripIndent ().replace ("\t " , " " );
96116
97117 sb .append ("\n ```java\n " ).append (nugget ).append ("\n ```\n " );
118+
119+ snippetFiles .add (new CodeSnippetFile (
120+ path .toPath (), startIndex , endIndex , clientInitStartIndex , clientInitEndIndex
121+ ));
98122 }
99123 }
100124
@@ -130,9 +154,9 @@ private static String toHeadingTitle(String title) {
130154 public static void main (String [] args ) throws Throwable {
131155 final var repoRoot = Paths .get ("" ).toAbsolutePath ();
132156 final var snippetsSrcRoot = repoRoot .resolve ("src/main/java/com/vonage/quickstart" );
133- final String contents = new AggregateSnippets (snippetsSrcRoot ).computeContents ();
157+ final var aggregator = new AggregateSnippets (snippetsSrcRoot );
158+ aggregator .computeContents ();
134159 var destPath = repoRoot .resolve ("SNIPPETS.md" );
135- Files .deleteIfExists (destPath );
136- Files .writeString (destPath , contents , StandardOpenOption .CREATE_NEW );
160+ aggregator .saveToFile (destPath );
137161 }
138162}
0 commit comments