From 35fad05912db693a545e3f7668b9a909d0c59601 Mon Sep 17 00:00:00 2001 From: Hariom Verma Date: Wed, 9 Aug 2023 14:10:36 +0530 Subject: [PATCH] Parse and print imports --- gnovm/cmd/gno/mod.go | 68 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/gnovm/cmd/gno/mod.go b/gnovm/cmd/gno/mod.go index bf784a11d0e..ad0476a1333 100644 --- a/gnovm/cmd/gno/mod.go +++ b/gnovm/cmd/gno/mod.go @@ -4,8 +4,12 @@ import ( "context" "flag" "fmt" + "go/parser" + "go/token" "os" "path/filepath" + "sort" + "strings" "github.com/gnolang/gno/gnovm/pkg/gnomod" "github.com/gnolang/gno/tm2/pkg/commands" @@ -139,7 +143,69 @@ func execModTidy(args []string, io *commands.IO) error { return flag.ErrHelp } - // TODO: Implementation + wd, err := os.Getwd() + if err != nil { + return err + } + // TODO: allow from sub dir? + modPath := filepath.Join(wd, "gno.mod") + if !isFileExist(modPath) { + return errors.New("gno.mod not found") + } + + imports, err := getGnoImports(wd) + if err != nil { + return err + } + + // Print imports + // TODO: remove + for i := range imports { + fmt.Println(imports[i]) + } + + // TODO: Add imports to gno.mod file return nil } + +// getGnoImports returns the list of gno imports from a given path recursively. +// TODO: move this to better location. +func getGnoImports(path string) ([]string, error) { + paths, err := gnoFilesFromArgs([]string{path}) + if err != nil { + return nil, err + } + + allImports := make([]string, 0) + seen := make(map[string]struct{}) + for _, path := range paths { + filename := filepath.Base(path) + if strings.HasSuffix(filename, "_filetest.gno") { + continue + } + data, err := os.ReadFile(path) + if err != nil { + return nil, err + } + fs := token.NewFileSet() + f, err := parser.ParseFile(fs, filename, data, parser.ImportsOnly) + if err != nil { + return nil, err + } + for _, imp := range f.Imports { + importPath := strings.TrimPrefix(strings.TrimSuffix(imp.Path.Value, `"`), `"`) + if !strings.HasPrefix(importPath, "gno.land/") { + continue + } + if _, ok := seen[importPath]; ok { + continue + } + allImports = append(allImports, importPath) + seen[importPath] = struct{}{} + } + } + sort.Strings(allImports) + + return allImports, nil +}