gopkg.in/tools/godep.v41@v41.0.0-20151217180337-fe5ce707f879/godepfile.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "io" 6 "log" 7 "os" 8 "path/filepath" 9 ) 10 11 var ( 12 godepsFile = filepath.Join("Godeps", "Godeps.json") 13 oldGodepsFile = filepath.Join("Godeps") 14 ) 15 16 // Godeps describes what a package needs to be rebuilt reproducibly. 17 // It's the same information stored in file Godeps. 18 type Godeps struct { 19 ImportPath string 20 GoVersion string 21 Packages []string `json:",omitempty"` // Arguments to save, if any. 22 Deps []Dependency 23 isOldFile bool 24 } 25 26 func createGodepsFile() (*os.File, error) { 27 return os.Create(godepsFile) 28 } 29 30 func loadGodepsFile(path string) (Godeps, error) { 31 var g Godeps 32 f, err := os.Open(path) 33 if err != nil { 34 return g, err 35 } 36 defer f.Close() 37 err = json.NewDecoder(f).Decode(&g) 38 return g, err 39 } 40 41 func loadDefaultGodepsFile() (Godeps, error) { 42 var err error 43 g, err1 := loadGodepsFile(godepsFile) 44 if err1 != nil { 45 if os.IsNotExist(err1) { 46 g, err = loadGodepsFile(oldGodepsFile) 47 if err == nil { 48 g.isOldFile = true 49 } 50 } 51 } 52 return g, err1 53 } 54 55 // pkgs is the list of packages to read dependencies for 56 func (g *Godeps) fill(pkgs []*Package, destImportPath string) error { 57 var err1 error 58 var path, testImports []string 59 for _, p := range pkgs { 60 if p.Standard { 61 log.Println("ignoring stdlib package:", p.ImportPath) 62 continue 63 } 64 if p.Error.Err != "" { 65 log.Println(p.Error.Err) 66 err1 = errorLoadingPackages 67 continue 68 } 69 path = append(path, p.ImportPath) 70 path = append(path, p.Deps...) 71 testImports = append(testImports, p.TestImports...) 72 testImports = append(testImports, p.XTestImports...) 73 } 74 ps, err := LoadPackages(testImports...) 75 if err != nil { 76 return err 77 } 78 for _, p := range ps { 79 if p.Standard { 80 continue 81 } 82 if p.Error.Err != "" { 83 log.Println(p.Error.Err) 84 err1 = errorLoadingPackages 85 continue 86 } 87 path = append(path, p.ImportPath) 88 path = append(path, p.Deps...) 89 } 90 for i, p := range path { 91 path[i] = unqualify(p) 92 } 93 path = uniq(path) 94 ps, err = LoadPackages(path...) 95 if err != nil { 96 return err 97 } 98 seen := []string{destImportPath} 99 for _, pkg := range ps { 100 if pkg.Error.Err != "" { 101 log.Println(pkg.Error.Err) 102 err1 = errorLoadingDeps 103 continue 104 } 105 if pkg.Standard || containsPathPrefix(seen, pkg.ImportPath) { 106 continue 107 } 108 seen = append(seen, pkg.ImportPath) 109 vcs, reporoot, err := VCSFromDir(pkg.Dir, filepath.Join(pkg.Root, "src")) 110 if err != nil { 111 log.Println(err) 112 err1 = errorLoadingDeps 113 continue 114 } 115 id, err := vcs.identify(pkg.Dir) 116 if err != nil { 117 log.Println(err) 118 err1 = errorLoadingDeps 119 continue 120 } 121 if vcs.isDirty(pkg.Dir, id) { 122 log.Println("dirty working tree (please commit changes):", pkg.Dir) 123 err1 = errorLoadingDeps 124 continue 125 } 126 comment := vcs.describe(pkg.Dir, id) 127 g.Deps = append(g.Deps, Dependency{ 128 ImportPath: pkg.ImportPath, 129 Rev: id, 130 Comment: comment, 131 dir: pkg.Dir, 132 ws: pkg.Root, 133 root: filepath.ToSlash(reporoot), 134 vcs: vcs, 135 }) 136 } 137 return err1 138 } 139 140 func (g *Godeps) copy() *Godeps { 141 h := *g 142 h.Deps = make([]Dependency, len(g.Deps)) 143 copy(h.Deps, g.Deps) 144 return &h 145 } 146 147 func (g *Godeps) file() string { 148 if g.isOldFile { 149 return oldGodepsFile 150 } 151 return godepsFile 152 } 153 154 func (g *Godeps) save() (int64, error) { 155 f, err := os.Create(g.file()) 156 if err != nil { 157 return 0, err 158 } 159 defer f.Close() 160 return g.writeTo(f) 161 } 162 163 func (g *Godeps) writeTo(w io.Writer) (int64, error) { 164 b, err := json.MarshalIndent(g, "", "\t") 165 if err != nil { 166 return 0, err 167 } 168 n, err := w.Write(append(b, '\n')) 169 return int64(n), err 170 }