Skip to content

Commit

Permalink
add goinfo ListRelatvieFiles
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed Nov 2, 2024
1 parent 403a999 commit 8996d11
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions support/fileutil/slashlize.go
Original file line number Diff line number Diff line change
@@ -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), "/")
}
59 changes: 59 additions & 0 deletions support/goinfo/list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package goinfo

import (
"encoding/json"
"path/filepath"
"strings"

"github.com/xhd2015/xgo/support/cmd"
Expand All @@ -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

Check warning on line 26 in support/goinfo/list.go

View workflow job for this annotation

GitHub Actions / check-build

"Relatvie" should be "Relative".
func ListRelatvieFiles(dir string, args []string) ([]string, error) {

Check warning on line 27 in support/goinfo/list.go

View workflow job for this annotation

GitHub Actions / check-build

"Relatvie" should be "Relative".
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
}

0 comments on commit 8996d11

Please sign in to comment.