This repository has been archived by the owner on Feb 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
example.go
72 lines (62 loc) · 2.42 KB
/
example.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
// +build ignore
package main
import (
"go/build"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/sdboyer/gps"
"github.com/sdboyer/gps/pkgtree"
)
// This is probably the simplest possible implementation of gps. It does the
// substantive work that `go get` does, except:
// 1. It drops the resulting tree into vendor instead of GOPATH
// 2. It prefers semver tags (if available) over branches
// 3. It removes any vendor directories nested within dependencies
//
// This will compile and work...and then blow away any vendor directory present
// in the cwd. Be careful!
func main() {
// Assume the current directory is correctly placed on a GOPATH, and that it's the
// root of the project.
root, _ := os.Getwd()
srcprefix := filepath.Join(build.Default.GOPATH, "src") + string(filepath.Separator)
importroot := filepath.ToSlash(strings.TrimPrefix(root, srcprefix))
// Set up params, including tracing
params := gps.SolveParameters{
RootDir: root,
Trace: true,
TraceLogger: log.New(os.Stdout, "", 0),
ProjectAnalyzer: NaiveAnalyzer{},
}
// Perform static analysis on the current project to find all of its imports.
params.RootPackageTree, _ = pkgtree.ListPackages(root, importroot)
// Set up a SourceManager. This manages interaction with sources (repositories).
tempdir, _ := ioutil.TempDir("", "gps-repocache")
sourcemgr, _ := gps.NewSourceManager(filepath.Join(tempdir))
defer sourcemgr.Release()
// Prep and run the solver
solver, _ := gps.Prepare(params, sourcemgr)
solution, err := solver.Solve()
if err == nil {
// If no failure, blow away the vendor dir and write a new one out,
// stripping nested vendor directories as we go.
os.RemoveAll(filepath.Join(root, "vendor"))
gps.WriteDepTree(filepath.Join(root, "vendor"), solution, sourcemgr, true)
}
}
type NaiveAnalyzer struct{}
// DeriveManifestAndLock is called when the solver needs manifest/lock data
// for a particular dependency project (identified by the gps.ProjectRoot
// parameter) at a particular version. That version will be checked out in a
// directory rooted at path.
func (a NaiveAnalyzer) DeriveManifestAndLock(path string, n gps.ProjectRoot) (gps.Manifest, gps.Lock, error) {
return nil, nil, nil
}
// Reports the name and version of the analyzer. This is used internally as part
// of gps' hashing memoization scheme.
func (a NaiveAnalyzer) Info() (name string, version int) {
return "example-analyzer", 1
}