@@ -21,6 +21,7 @@ import (
2121 "os"
2222 "path"
2323 "path/filepath"
24+ "regexp"
2425 "strings"
2526
2627 "github.com/cloudwego/kitex/tool/internal_pkg/generator"
@@ -68,7 +69,7 @@ func (pg *PrutalGen) generateClientServerFiles(f *prutalgen.Proto, p *generator.
6869 return err
6970 }
7071 for _ , f := range fs {
71- writeGoFile (f .Name , []byte (f .Content ))
72+ writeFile (f .Name , []byte (f .Content ))
7273 }
7374 }
7475 return nil
@@ -134,7 +135,7 @@ func (pg *PrutalGen) Process() error {
134135 return err
135136 }
136137 for _ , f := range fs {
137- writeGoFile (f .Name , []byte (f .Content ))
138+ writeFile (f .Name , []byte (f .Content ))
138139 }
139140 }
140141
@@ -148,7 +149,7 @@ func (pg *PrutalGen) Process() error {
148149 return err
149150 }
150151 for _ , f := range fs {
151- writeGoFile (f .Name , []byte (f .Content ))
152+ writeFile (f .Name , []byte (f .Content ))
152153 }
153154 }
154155 return nil
@@ -280,7 +281,7 @@ func genStructsAndKitexInterfaces(f *prutalgen.Proto, c *generator.Config, dir s
280281 fn := strings .TrimSuffix (path .Base (f .ProtoFile ), ".proto" ) + ".pb.go"
281282 out := filepath .Join (dir , fn )
282283 f .Infof ("generating %s" , out )
283- writeGoFile (out , w .Bytes ())
284+ writeFile (out , w .Bytes ())
284285}
285286
286287func genKitexServiceInterface (f * prutalgen.Proto , w * prutalgen.CodeWriter , streamx bool ) {
@@ -338,17 +339,43 @@ func getImportPath(pkg, module, prefix string) (string, bool) {
338339 return path .Join (prefix , pkg ), true
339340}
340341
341- func writeGoFile (fn string , data []byte ) {
342- formatted , err := format .Source (data )
343- if err != nil {
344- log .Errorf ("format file %q err: %s" , fn , err )
345- formatted = data
342+ func writeFile (fn string , data []byte ) {
343+ if strings .HasSuffix (fn , ".go" ) {
344+ formatted , err := format .Source (data )
345+ if err != nil {
346+ log .Errorf ("format file %q err: %s" , fn , err )
347+ } else {
348+ data = formatted
349+ }
350+ truncateFastPBFile (fn )
346351 }
347- err = os .MkdirAll (filepath .Dir (fn ), 0o755 )
352+ err : = os .MkdirAll (filepath .Dir (fn ), 0o755 )
348353 if err == nil {
349- err = os .WriteFile (fn , formatted , 0o644 )
354+ err = os .WriteFile (fn , data , 0o644 )
350355 }
351356 if err != nil {
352357 log .Errorf ("write file %q err: %s" , fn , err )
353358 }
354359}
360+
361+ var packageRE = regexp .MustCompile (`package\s+([a-zA-Z0-9_]+)` )
362+
363+ // truncateFastPBFile truncates xxx.pb.fast.go to avoid using FastWrite/FastRead methods.
364+ //
365+ // NOTE: or we can remove the file directly?
366+ // since *.pb.fast.go files possibly maintained by git, modifying it would be better?
367+ func truncateFastPBFile (fn string ) {
368+ if ! strings .HasSuffix (fn , ".pb.go" ) {
369+ return
370+ }
371+ fn = strings .TrimSuffix (fn , ".pb.go" ) + ".pb.fast.go"
372+ content , err := os .ReadFile (fn )
373+ if err != nil {
374+ // not exists?
375+ return
376+ }
377+ b := []byte ("// This file was generated by Fastpb, and it's deprecated by Kitex. Free to delete.\n \n " )
378+ b = append (b , packageRE .Find (content )... )
379+ b = append (b , '\n' )
380+ os .WriteFile (fn , b , 0o644 )
381+ }
0 commit comments