This repository has been archived by the owner on May 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
depresolve.go
104 lines (87 loc) · 2.59 KB
/
depresolve.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"fmt"
"go/build"
"log"
"strings"
"sync"
"sourcegraph.com/sourcegraph/srclib-go/depresolve"
"sourcegraph.com/sourcegraph/srclib/dep"
)
func init() {
_, err := flagParser.AddCommand("depresolve",
"resolve a Go package's imports",
"Resolve a Go package's imports to their repository clone URL.",
&depResolveCmd,
)
if err != nil {
log.Fatal(err)
}
}
type DepResolveCmd struct {
Config []string `long:"config" description:"config property from Srcfile" value-name:"KEY=VALUE"`
}
var depResolveCmd DepResolveCmd
func (c *DepResolveCmd) Execute(args []string) error {
fmt.Println("[]")
return nil
}
// targetCache caches (dep).ResolvedTarget's for importPaths
type targetCache struct {
data map[string]*dep.ResolvedTarget
mu sync.Mutex
}
// Get returns the cached (dep).ResolvedTarget for the given import path or nil.
func (t *targetCache) Get(path string) *dep.ResolvedTarget {
t.mu.Lock()
defer t.mu.Unlock()
return t.data[path]
}
// Put puts a new entry into the cache at the specified import path.
func (t *targetCache) Put(path string, target *dep.ResolvedTarget) {
t.mu.Lock()
defer t.mu.Unlock()
if t.data == nil {
t.data = make(map[string]*dep.ResolvedTarget)
}
t.data[path] = target
}
var resolveCache targetCache
func ResolveDep(importPath string) (*dep.ResolvedTarget, error) {
// Look up in cache.
if target := resolveCache.Get(importPath); target != nil {
return target, nil
}
target, err := doResolveDep(importPath)
if err != nil {
return nil, err
}
// Save in cache.
resolveCache.Put(importPath, target)
return target, nil
}
func doResolveDep(importPath string) (*dep.ResolvedTarget, error) {
// Check if this import path is in this tree. If refs refer to vendored deps, they are linked to the vendored code
// inside this repository (i.e., NOT linked to the external repository from which the code was vendored).
if pkg, err := buildContext.Import(strings.TrimSuffix(importPath, "_test"), "", build.FindOnly); err == nil {
if pathHasPrefix(pkg.Dir, cwd) {
if name, isVendored := vendoredUnitName(pkg); isVendored {
if strings.HasSuffix(importPath, "_test") {
name += "_test"
}
return &dep.ResolvedTarget{
ToRepoCloneURL: "", // empty ToRepoCloneURL to indicate it's from this repository
ToUnit: name,
ToUnitType: "GoPackage",
}, nil
} else {
return &dep.ResolvedTarget{
ToRepoCloneURL: "", // empty ToRepoCloneURL to indicate it's from this repository
ToUnit: importPath,
ToUnitType: "GoPackage",
}, nil
}
}
}
return depresolve.ResolveImportPath(importPath)
}