github.com/sdboyer/gps@v0.16.3/example.go (about) 1 // +build ignore 2 3 package main 4 5 import ( 6 "go/build" 7 "io/ioutil" 8 "log" 9 "os" 10 "path/filepath" 11 "strings" 12 13 "github.com/sdboyer/gps" 14 "github.com/sdboyer/gps/pkgtree" 15 ) 16 17 // This is probably the simplest possible implementation of gps. It does the 18 // substantive work that `go get` does, except: 19 // 1. It drops the resulting tree into vendor instead of GOPATH 20 // 2. It prefers semver tags (if available) over branches 21 // 3. It removes any vendor directories nested within dependencies 22 // 23 // This will compile and work...and then blow away any vendor directory present 24 // in the cwd. Be careful! 25 func main() { 26 // Assume the current directory is correctly placed on a GOPATH, and that it's the 27 // root of the project. 28 root, _ := os.Getwd() 29 srcprefix := filepath.Join(build.Default.GOPATH, "src") + string(filepath.Separator) 30 importroot := filepath.ToSlash(strings.TrimPrefix(root, srcprefix)) 31 32 // Set up params, including tracing 33 params := gps.SolveParameters{ 34 RootDir: root, 35 Trace: true, 36 TraceLogger: log.New(os.Stdout, "", 0), 37 ProjectAnalyzer: NaiveAnalyzer{}, 38 } 39 // Perform static analysis on the current project to find all of its imports. 40 params.RootPackageTree, _ = pkgtree.ListPackages(root, importroot) 41 42 // Set up a SourceManager. This manages interaction with sources (repositories). 43 tempdir, _ := ioutil.TempDir("", "gps-repocache") 44 sourcemgr, _ := gps.NewSourceManager(filepath.Join(tempdir)) 45 defer sourcemgr.Release() 46 47 // Prep and run the solver 48 solver, _ := gps.Prepare(params, sourcemgr) 49 solution, err := solver.Solve() 50 if err == nil { 51 // If no failure, blow away the vendor dir and write a new one out, 52 // stripping nested vendor directories as we go. 53 os.RemoveAll(filepath.Join(root, "vendor")) 54 gps.WriteDepTree(filepath.Join(root, "vendor"), solution, sourcemgr, true) 55 } 56 } 57 58 type NaiveAnalyzer struct{} 59 60 // DeriveManifestAndLock is called when the solver needs manifest/lock data 61 // for a particular dependency project (identified by the gps.ProjectRoot 62 // parameter) at a particular version. That version will be checked out in a 63 // directory rooted at path. 64 func (a NaiveAnalyzer) DeriveManifestAndLock(path string, n gps.ProjectRoot) (gps.Manifest, gps.Lock, error) { 65 return nil, nil, nil 66 } 67 68 // Reports the name and version of the analyzer. This is used internally as part 69 // of gps' hashing memoization scheme. 70 func (a NaiveAnalyzer) Info() (name string, version int) { 71 return "example-analyzer", 1 72 }