Skip to content

Commit

Permalink
Merge pull request #461 from visualfc/tags
Browse files Browse the repository at this point in the history
packages: support tags
  • Loading branch information
xushiwei authored Jan 12, 2025
2 parents 4aa868a + 16af060 commit 58e4e07
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
19 changes: 16 additions & 3 deletions packages/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,22 @@ type Impl struct {
cache sync.Map // map[string]*pkgCache
h PkgHash // package hash func
nlist int32 // list count
tags string
}

// New creates a new cache.
func New(h PkgHash) *Impl {
return &Impl{h: h}
}

func (p *Impl) SetTags(tags string) {
p.tags = tags
}

func (p *Impl) Tags() string {
return p.tags
}

// ----------------------------------------------------------------------------

// ListTimes returns the number of times of calling `go list`.
Expand All @@ -66,7 +75,7 @@ func (p *Impl) ListTimes() int {
// Prepare prepares the cache for a list of pkgPath.
func (p *Impl) Prepare(dir string, pkgPath ...string) (err error) {
atomic.AddInt32(&p.nlist, 1)
ret, err := golistExport(dir, pkgPath)
ret, err := golistExport(dir, pkgPath, p.tags)
if err != nil {
return
}
Expand Down Expand Up @@ -120,10 +129,14 @@ type exportPkg struct {
deps []string
}

func golistExport(dir string, pkgPath []string) (ret []exportPkg, err error) {
func golistExport(dir string, pkgPath []string, tags string) (ret []exportPkg, err error) {
var stdout, stderr bytes.Buffer
var args = make([]string, 0, 3+len(pkgPath))
args = append(args, "list", "-f={{.ImportPath}}\t{{.Export}}\t{{.Deps}}", "-export")
args = append(args, "list", "-f={{.ImportPath}}\t{{.Export}}\t{{.Deps}}")
if tags != "" {
args = append(args, "-tags="+tags)
}
args = append(args, "-export")
args = append(args, pkgPath...)
cmd := exec.Command("go", args...)
cmd.Stdout = &stdout
Expand Down
20 changes: 17 additions & 3 deletions packages/imp.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Importer struct {
dir string
m sync.RWMutex
cache Cache
tags string
}

// NewImporter creates an Importer object that meets types.ImporterFrom and types.Importer interface.
Expand All @@ -67,6 +68,14 @@ func (p *Importer) Cache() Cache {
return p.cache
}

func (p *Importer) SetTags(tags string) {
p.tags = tags
}

func (p *Importer) Tags() string {
return p.tags
}

// Import returns the imported package for the given import path.
func (p *Importer) Import(pkgPath string) (pkg *types.Package, err error) {
return p.ImportFrom(pkgPath, p.dir, 0)
Expand Down Expand Up @@ -114,17 +123,22 @@ func (p *Importer) findExport(dir, pkgPath string) (f io.ReadCloser, err error)
return c.Find(dir, pkgPath)
}
atomic.AddInt32(&nlist, 1)
data, err := golistExport(dir, pkgPath)
data, err := golistExport(dir, pkgPath, p.tags)
if err != nil {
return
}
expfile := string(bytes.TrimSuffix(data, []byte{'\n'}))
return os.Open(expfile)
}

func golistExport(dir, pkgPath string) (ret []byte, err error) {
func golistExport(dir, pkgPath string, tags string) (ret []byte, err error) {
var stdout, stderr bytes.Buffer
cmd := exec.Command("go", "list", "-f={{.Export}}", "-export", pkgPath)
args := []string{"list", "-f={{.Export}}"}
if tags != "" {
args = append(args, "-tags=", tags)
}
args = append(args, "-export", pkgPath)
cmd := exec.Command("go", args...)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Dir = dir
Expand Down

0 comments on commit 58e4e07

Please sign in to comment.