github.com/andrewlunde/glide@v0.20.1/godep/godep.go (about)

     1  // Package godep provides basic importing of Godep dependencies.
     2  //
     3  // This is not a complete implementation of Godep.
     4  package godep
     5  
     6  import (
     7  	"encoding/json"
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  
    12  	"github.com/andrewlunde/glide/cfg"
    13  	"github.com/andrewlunde/glide/msg"
    14  	gpath "github.com/andrewlunde/glide/path"
    15  	"github.com/andrewlunde/glide/util"
    16  )
    17  
    18  // This file contains commands for working with Godep.
    19  
    20  // The Godeps struct from Godep.
    21  //
    22  // https://raw.githubusercontent.com/tools/godep/master/dep.go
    23  //
    24  // We had to copy this because it's in the package main for Godep.
    25  type Godeps struct {
    26  	ImportPath string
    27  	GoVersion  string
    28  	Packages   []string `json:",omitempty"` // Arguments to save, if any.
    29  	Deps       []Dependency
    30  
    31  	outerRoot string
    32  }
    33  
    34  // Dependency is a modified version of Godep's Dependency struct.
    35  // It drops all of the unexported fields.
    36  type Dependency struct {
    37  	ImportPath string
    38  	Comment    string `json:",omitempty"` // Description of commit, if present.
    39  	Rev        string // VCS-specific commit ID.
    40  }
    41  
    42  // Has is a command to detect if a package contains a Godeps.json file.
    43  func Has(dir string) bool {
    44  	path := filepath.Join(dir, "Godeps/Godeps.json")
    45  	_, err := os.Stat(path)
    46  	return err == nil
    47  }
    48  
    49  // Parse parses a Godep's Godeps file.
    50  //
    51  // It returns the contents as a dependency array.
    52  func Parse(dir string) ([]*cfg.Dependency, error) {
    53  	path := filepath.Join(dir, "Godeps/Godeps.json")
    54  	if _, err := os.Stat(path); err != nil {
    55  		return []*cfg.Dependency{}, nil
    56  	}
    57  	msg.Info("Found Godeps.json file in %s", gpath.StripBasepath(dir))
    58  	msg.Info("--> Parsing Godeps metadata...")
    59  
    60  	buf := []*cfg.Dependency{}
    61  
    62  	godeps := &Godeps{}
    63  
    64  	// Get a handle to the file.
    65  	file, err := os.Open(path)
    66  	if err != nil {
    67  		return buf, err
    68  	}
    69  	defer file.Close()
    70  
    71  	dec := json.NewDecoder(file)
    72  	if err := dec.Decode(godeps); err != nil {
    73  		return buf, err
    74  	}
    75  
    76  	seen := map[string]bool{}
    77  	for _, d := range godeps.Deps {
    78  		pkg, sub := util.NormalizeName(d.ImportPath)
    79  		if _, ok := seen[pkg]; ok {
    80  			if len(sub) == 0 {
    81  				continue
    82  			}
    83  			// Modify existing dep with additional subpackages.
    84  			for _, dep := range buf {
    85  				if dep.Name == pkg {
    86  					dep.Subpackages = append(dep.Subpackages, sub)
    87  				}
    88  			}
    89  		} else {
    90  			seen[pkg] = true
    91  			dep := &cfg.Dependency{Name: pkg, Reference: d.Rev}
    92  			if sub != "" {
    93  				dep.Subpackages = []string{sub}
    94  			}
    95  			buf = append(buf, dep)
    96  		}
    97  	}
    98  
    99  	return buf, nil
   100  }
   101  
   102  // RemoveGodepSubpackages strips subpackages from a cfg.Config dependencies that
   103  // contain "Godeps/_workspace/src" as part of the path.
   104  func RemoveGodepSubpackages(c *cfg.Config) *cfg.Config {
   105  	for _, d := range c.Imports {
   106  		n := []string{}
   107  		for _, v := range d.Subpackages {
   108  			if !strings.HasPrefix(v, "Godeps/_workspace/src") {
   109  				n = append(n, v)
   110  			}
   111  		}
   112  		d.Subpackages = n
   113  	}
   114  
   115  	for _, d := range c.DevImports {
   116  		n := []string{}
   117  		for _, v := range d.Subpackages {
   118  			if !strings.HasPrefix(v, "Godeps/_workspace/src") {
   119  				n = append(n, v)
   120  			}
   121  		}
   122  		d.Subpackages = n
   123  	}
   124  
   125  	return c
   126  }