From 8996d11f4f17ea3f1d67de6dacecccac5a49d549 Mon Sep 17 00:00:00 2001 From: xhd2015 Date: Sat, 2 Nov 2024 18:25:31 +0800 Subject: [PATCH] add goinfo ListRelatvieFiles --- cmd/xgo/version.go | 4 +-- support/fileutil/slashlize.go | 13 ++++++++ support/goinfo/list.go | 59 +++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 support/fileutil/slashlize.go diff --git a/cmd/xgo/version.go b/cmd/xgo/version.go index 6c4d9668..09ffca1a 100644 --- a/cmd/xgo/version.go +++ b/cmd/xgo/version.go @@ -6,8 +6,8 @@ import "fmt" // VERSION is manually updated when needed a new tag // see also runtime/core/version.go const VERSION = "1.0.50" -const REVISION = "b041912e5d7457935e52026572c003b0f0808d59+1" -const NUMBER = 314 +const REVISION = "403a999217b51a86240a37e57c09ddfc6a731851+1" +const NUMBER = 315 // the matching runtime/core's version // manually updated diff --git a/support/fileutil/slashlize.go b/support/fileutil/slashlize.go new file mode 100644 index 00000000..a5718b41 --- /dev/null +++ b/support/fileutil/slashlize.go @@ -0,0 +1,13 @@ +package fileutil + +import ( + "path/filepath" + "strings" +) + +func Slashlize(path string) string { + if filepath.Separator == '/' { + return path + } + return strings.ReplaceAll(path, string(filepath.Separator), "/") +} diff --git a/support/goinfo/list.go b/support/goinfo/list.go index b0d6f7f9..34476eb6 100644 --- a/support/goinfo/list.go +++ b/support/goinfo/list.go @@ -1,6 +1,8 @@ package goinfo import ( + "encoding/json" + "path/filepath" "strings" "github.com/xhd2015/xgo/support/cmd" @@ -20,3 +22,60 @@ func ListPackages(dir string, mod string, args []string) ([]string, error) { lines := strings.Split(output, "\n") return lines, nil } + +// ListRelatvieFiles list all go files, return a list of relative paths +func ListRelatvieFiles(dir string, args []string) ([]string, error) { + absDir, err := filepath.Abs(dir) + if err != nil { + return nil, err + } + flags := []string{"list", "-e", "-json"} + flags = append(flags, args...) + res, err := cmd.Dir(dir).Output("go", flags...) + if err != nil { + return nil, err + } + var relFiles []string + dec := json.NewDecoder(strings.NewReader(res)) + for dec.More() { + // check 'go help list' + var pkg struct { + // the abs dir + Dir string + // the file names + GoFiles []string + } + err := dec.Decode(&pkg) + if err != nil { + return nil, err + } + if len(pkg.GoFiles) > 0 { + absPkgDir, err := filepath.Abs(pkg.Dir) + if err != nil { + return nil, err + } + for _, goFile := range pkg.GoFiles { + if !strings.HasSuffix(goFile, ".go") { + // some cache files + continue + } + absGoFile := goFile + if !filepath.IsAbs(goFile) { + // go list outputs file names + // in most cases this goFile is not absolute + absGoFile = filepath.Join(absPkgDir, goFile) + } + + // this is just for compatibility + if !strings.HasPrefix(absGoFile, absDir) { + continue + } + relFile := strings.TrimPrefix(absGoFile[len(absDir):], string(filepath.Separator)) + if relFile != "" { + relFiles = append(relFiles, relFile) + } + } + } + } + return relFiles, nil +}