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