@@ -222,8 +222,22 @@ func MergePubfiles(llcppgPubFileName string, dir string) {
222
222
os .Remove (fileName )
223
223
}
224
224
}()
225
+ var prev , next chan int
225
226
for b := range input {
226
- w .Write (b )
227
+ next = make (chan int )
228
+ go func (wr io.Writer , prev , next chan int ) {
229
+ if prev != nil {
230
+ <- prev
231
+ }
232
+ wr .Write (b )
233
+ if next != nil {
234
+ close (next )
235
+ }
236
+ }(w , prev , next )
237
+ prev = next
238
+ }
239
+ if next != nil {
240
+ <- next
227
241
}
228
242
}(llcppgPubFileName , pubFileBytes )
229
243
wg .Wait ()
@@ -260,33 +274,6 @@ func GenAstFiles(quit chan int, dir string, fset *token.FileSet) <-chan *ast.Fil
260
274
return output
261
275
}
262
276
263
- func writeAstFiles (quit chan int , fset * token.FileSet , files <- chan * ast.File ) <- chan string {
264
- output := make (chan string )
265
- go func () {
266
- defer close (output )
267
- for file := range files {
268
- temp , err := os .CreateTemp (os .TempDir (), "file*.pub" )
269
- if err != nil {
270
- fmt .Println (err )
271
- return
272
- }
273
- defer temp .Close ()
274
- w := bufio .NewWriter (temp )
275
- defer func () {
276
- if w .Buffered () > 0 {
277
- w .Flush ()
278
- output <- temp .Name ()
279
- } else {
280
- os .Remove (temp .Name ())
281
- }
282
- }()
283
- pubWriter := NewPubWriter (w , fset )
284
- pubWriter .WriteFile (file )
285
- }
286
- }()
287
- return output
288
- }
289
-
290
277
func DoWritePubFile (w io.Writer , pubFile string ) {
291
278
quit := make (chan int )
292
279
defer func () {
@@ -295,21 +282,22 @@ func DoWritePubFile(w io.Writer, pubFile string) {
295
282
fset := token .NewFileSet ()
296
283
dir := filepath .Dir (pubFile )
297
284
astFiles := GenAstFiles (quit , dir , fset )
298
- files := writeAstFiles (quit , fset , astFiles )
299
- wg := sync.WaitGroup {}
300
- for file := range files {
301
- wg .Add (1 )
302
- go func (fileName string ) {
303
- defer func () {
304
- os .Remove (fileName )
305
- wg .Done ()
306
- }()
307
- b , err := os .ReadFile (fileName )
308
- if err != nil {
309
- panic (err )
285
+ var prev , next chan int
286
+ for astFile := range astFiles {
287
+ next = make (chan int )
288
+ go func (wr io.Writer , file * ast.File , prev , next chan int ) {
289
+ if prev != nil {
290
+ <- prev
291
+ }
292
+ pubWriter := NewPubWriter (wr , fset )
293
+ pubWriter .WriteFile (file )
294
+ if next != nil {
295
+ close (next )
310
296
}
311
- w .Write (b )
312
- }(file )
297
+ }(w , astFile , prev , next )
298
+ prev = next
299
+ }
300
+ if next != nil {
301
+ <- next
313
302
}
314
- wg .Wait ()
315
303
}
0 commit comments