@@ -34,8 +34,8 @@ import (
3434var _ uniast.Writer = (* Writer )(nil )
3535
3636type Options struct {
37- RepoDir string
38- OutDir string
37+ // RepoDir string
38+ // OutDir string
3939 GoVersion string
4040}
4141
@@ -61,19 +61,19 @@ func NewWriter(opts Options) *Writer {
6161 }
6262}
6363
64- func (w * Writer ) WriteRepo (repo * uniast.Repository ) error {
64+ func (w * Writer ) WriteRepo (repo * uniast.Repository , outDir string ) error {
6565 for m , mod := range repo .Modules {
6666 if strings .Contains (m , "@" ) {
6767 continue
6868 }
69- if err := w .WriteModule (repo , m ); err != nil {
69+ if err := w .WriteModule (repo , m , outDir ); err != nil {
7070 return fmt .Errorf ("write module %s failed: %v" , mod .Name , err )
7171 }
7272 }
7373 return nil
7474}
7575
76- func (w * Writer ) WriteModule (repo * uniast.Repository , modPath string ) error {
76+ func (w * Writer ) WriteModule (repo * uniast.Repository , modPath string , outDir string ) error {
7777 mod := repo .Modules [modPath ]
7878 if mod == nil {
7979 return fmt .Errorf ("module %s not found" , modPath )
@@ -84,7 +84,7 @@ func (w *Writer) WriteModule(repo *uniast.Repository, modPath string) error {
8484 }
8585 }
8686
87- outdir := filepath .Join (w . Options . OutDir , mod .Dir )
87+ outdir := filepath .Join (outDir , mod .Dir )
8888 for dir , pkg := range w .visited {
8989 rel := strings .TrimPrefix (dir , mod .Name )
9090 pkgDir := filepath .Join (outdir , rel )
@@ -260,16 +260,12 @@ func (w *Writer) IdToImport(id uniast.Identity) (uniast.Import, error) {
260260 return uniast.Import {Path : strconv .Quote (id .PkgPath )}, nil
261261}
262262
263- func (p * Writer ) PatchImports (file * uniast.File ) ([]byte , error ) {
264- bs , err := os .ReadFile (filepath .Join (p .Options .RepoDir , file .Path ))
265- if err != nil {
266- return nil , utils .WrapError (err , "fail read file %s" , file .Path )
267- }
263+ func (p * Writer ) PatchImports (impts []uniast.Import , file []byte ) ([]byte , error ) {
268264
269265 fs := token .NewFileSet ()
270- f , err := parser .ParseFile (fs , file . Path , bs , parser .ImportsOnly )
266+ f , err := parser .ParseFile (fs , "default.go" , file , parser .ImportsOnly )
271267 if err != nil {
272- return nil , utils .WrapError (err , "fail parse file %s" , file . Path )
268+ return nil , utils .WrapError (err , "fail parse file %s" , file )
273269 }
274270
275271 old := make ([]uniast.Import , 0 , len (f .Imports ))
@@ -284,9 +280,9 @@ func (p *Writer) PatchImports(file *uniast.File) ([]byte, error) {
284280 old = append (old , i )
285281 }
286282
287- impts : = mergeImports (old , file . Imports )
283+ impts = mergeImports (old , impts )
288284 if len (impts ) == len (old ) {
289- return bs , nil
285+ return file , nil
290286 }
291287
292288 var sb strings.Builder
@@ -295,19 +291,47 @@ func (p *Writer) PatchImports(file *uniast.File) ([]byte, error) {
295291
296292 imptStart := fs .Position (f .Name .End ()).Offset + 1
297293 if len (f .Imports ) > 0 {
298- for imptStart < len (bs ) && bs [imptStart ] != 'i' {
294+ for imptStart < len (file ) && file [imptStart ] != 'i' {
299295 imptStart ++
300296 }
301297 }
302298 imptEnd := imptStart
303299 if len (f .Imports ) > 1 {
304300 imptEnd = fs .Position (f .Imports [len (f .Imports )- 1 ].End ()).Offset
305- for len (old ) > 1 && imptEnd < len (bs ) && (bs [imptEnd ] != ')' ) {
301+ for len (old ) > 1 && imptEnd < len (file ) && (file [imptEnd ] != ')' ) {
306302 imptEnd ++
307303 }
308304 imptEnd += 2 // for `)`
309305 }
310- r1 := append (bs [:imptStart :imptStart ], final ... )
311- ret := append (r1 , bs [imptEnd :]... )
306+ r1 := append (file [:imptStart :imptStart ], final ... )
307+ ret := append (r1 , file [imptEnd :]... )
312308 return ret , nil
313309}
310+
311+ func (p * Writer ) CreateFile (fi * uniast.File , mod * uniast.Module ) ([]byte , error ) {
312+ var sb strings.Builder
313+ sb .WriteString ("package " )
314+ pkgName := filepath .Base (filepath .Dir (fi .Path ))
315+ if fi .Package != nil {
316+ pkg := mod .Packages [* fi .Package ]
317+ if pkg != nil {
318+ if pkg .IsMain {
319+ pkgName = "main"
320+ } else {
321+ pkgName = filepath .Base (pkg .PkgPath )
322+ }
323+ }
324+ }
325+ if pkgName == "" {
326+ return nil , fmt .Errorf ("package name is empty" )
327+ }
328+ sb .WriteString (pkgName )
329+ sb .WriteString ("\n \n " )
330+
331+ if len (fi .Imports ) > 0 {
332+ writeImport (& sb , fi .Imports )
333+ }
334+
335+ bs := sb .String ()
336+ return []byte (bs ), nil
337+ }
0 commit comments