gopkg.in/tools/godep.v42@v42.0.0-20151223001600-f221061cd941/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  	debugln("fill", destImportPath)
    58  	ppln(pkgs)
    59  	var err1 error
    60  	var path, testImports []string
    61  	for _, p := range pkgs {
    62  		if p.Standard {
    63  			log.Println("ignoring stdlib package:", p.ImportPath)
    64  			continue
    65  		}
    66  		if p.Error.Err != "" {
    67  			log.Println(p.Error.Err)
    68  			err1 = errorLoadingPackages
    69  			continue
    70  		}
    71  		path = append(path, p.ImportPath)
    72  		path = append(path, p.Deps...)
    73  		testImports = append(testImports, p.TestImports...)
    74  		testImports = append(testImports, p.XTestImports...)
    75  	}
    76  	ps, err := LoadPackages(testImports...)
    77  	if err != nil {
    78  		return err
    79  	}
    80  	for _, p := range ps {
    81  		if p.Standard {
    82  			continue
    83  		}
    84  		if p.Error.Err != "" {
    85  			log.Println(p.Error.Err)
    86  			err1 = errorLoadingPackages
    87  			continue
    88  		}
    89  		path = append(path, p.ImportPath)
    90  		path = append(path, p.Deps...)
    91  	}
    92  	debugln("path", path)
    93  	for i, p := range path {
    94  		path[i] = unqualify(p)
    95  	}
    96  	path = uniq(path)
    97  	debugln("uniq, unqualify'd path", path)
    98  	ps, err = LoadPackages(path...)
    99  	if err != nil {
   100  		return err
   101  	}
   102  	seen := []string{destImportPath}
   103  	for _, pkg := range ps {
   104  		if pkg.Error.Err != "" {
   105  			log.Println(pkg.Error.Err)
   106  			err1 = errorLoadingDeps
   107  			continue
   108  		}
   109  		if pkg.Standard || containsPathPrefix(seen, pkg.ImportPath) {
   110  			continue
   111  		}
   112  		seen = append(seen, pkg.ImportPath)
   113  		vcs, reporoot, err := VCSFromDir(pkg.Dir, filepath.Join(pkg.Root, "src"))
   114  		if err != nil {
   115  			log.Println(err)
   116  			err1 = errorLoadingDeps
   117  			continue
   118  		}
   119  		id, err := vcs.identify(pkg.Dir)
   120  		if err != nil {
   121  			log.Println(err)
   122  			err1 = errorLoadingDeps
   123  			continue
   124  		}
   125  		if vcs.isDirty(pkg.Dir, id) {
   126  			log.Println("dirty working tree (please commit changes):", pkg.Dir)
   127  			err1 = errorLoadingDeps
   128  			continue
   129  		}
   130  		comment := vcs.describe(pkg.Dir, id)
   131  		g.Deps = append(g.Deps, Dependency{
   132  			ImportPath: pkg.ImportPath,
   133  			Rev:        id,
   134  			Comment:    comment,
   135  			dir:        pkg.Dir,
   136  			ws:         pkg.Root,
   137  			root:       filepath.ToSlash(reporoot),
   138  			vcs:        vcs,
   139  		})
   140  	}
   141  	return err1
   142  }
   143  
   144  func (g *Godeps) copy() *Godeps {
   145  	h := *g
   146  	h.Deps = make([]Dependency, len(g.Deps))
   147  	copy(h.Deps, g.Deps)
   148  	return &h
   149  }
   150  
   151  func (g *Godeps) file() string {
   152  	if g.isOldFile {
   153  		return oldGodepsFile
   154  	}
   155  	return godepsFile
   156  }
   157  
   158  func (g *Godeps) save() (int64, error) {
   159  	f, err := os.Create(g.file())
   160  	if err != nil {
   161  		return 0, err
   162  	}
   163  	defer f.Close()
   164  	return g.writeTo(f)
   165  }
   166  
   167  func (g *Godeps) writeTo(w io.Writer) (int64, error) {
   168  	b, err := json.MarshalIndent(g, "", "\t")
   169  	if err != nil {
   170  		return 0, err
   171  	}
   172  	n, err := w.Write(append(b, '\n'))
   173  	return int64(n), err
   174  }