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