github.com/comcast/canticle@v0.0.0-20161108184242-c53cface56e8/canticles/dependency.go (about)

     1  package canticles
     2  
     3  import "fmt"
     4  
     5  // A Dependency defines all information about this package.
     6  type Dependency struct {
     7  	// ImportPath for this string as it would appear in a go file.
     8  	ImportPath string
     9  	// ImportedFrom is a list of packages which import
    10  	// this dependency.
    11  	ImportedFrom StringSet
    12  	// Imports is the set of remote imports for this dep.
    13  	Imports StringSet
    14  	// Attempt to read the package caused an error.
    15  	Err error
    16  }
    17  
    18  func NewDependency(importPath string) *Dependency {
    19  	return &Dependency{
    20  		ImportedFrom: NewStringSet(),
    21  		Imports:      NewStringSet(),
    22  		ImportPath:   importPath,
    23  	}
    24  }
    25  
    26  // Dependencies is the set of Dependencies for a package. This is
    27  // stored as a map from Root to Dependency.
    28  type Dependencies map[string]*Dependency
    29  
    30  // NewDependencies creates a new empty dependencies structure.
    31  func NewDependencies() Dependencies {
    32  	return make(map[string]*Dependency)
    33  }
    34  
    35  func (d Dependencies) Dependency(importPath string) *Dependency {
    36  	return d[importPath]
    37  }
    38  
    39  // AddDependencies ranges of the dependencies in deps and calls
    40  // AddDependency on them.
    41  func (d Dependencies) AddDependencies(deps Dependencies) {
    42  	for _, dep := range deps {
    43  		d.AddDependency(dep)
    44  	}
    45  }
    46  
    47  // AddDependency adds a
    48  func (d Dependencies) AddDependency(dep *Dependency) {
    49  	already := d[dep.ImportPath]
    50  	if already == nil {
    51  		d[dep.ImportPath] = dep
    52  		return
    53  	}
    54  
    55  	already.Err = dep.Err
    56  	already.ImportedFrom.Union(dep.ImportedFrom)
    57  	already.Imports.Union(dep.Imports)
    58  }
    59  
    60  func (d Dependencies) AddDeps(deps ...string) {
    61  	for _, dep := range deps {
    62  		if d[dep] == nil {
    63  			d[dep] = NewDependency(dep)
    64  		}
    65  	}
    66  }
    67  
    68  // String will print this out as newline seperated %+v values.
    69  func (d Dependencies) String() string {
    70  	str := ""
    71  	for path, dep := range d {
    72  		str += fmt.Sprintf("%s: %+v\n", path, dep)
    73  	}
    74  	return str
    75  }
    76  
    77  type CanticleDependency struct {
    78  	// SourcePath is the source of the dependencies VCS
    79  	SourcePath string `json:",omitempty"`
    80  	// Root is the root VCS for the import paths. It must be
    81  	// a prefix of all import paths.
    82  	Root string
    83  	// Revision is the VCS specific commit id
    84  	Revision string `json:",omitempty"`
    85  	// All means walks this VCS from the root for nonhidden files. This will save and
    86  	// fetch the subdirs of package.
    87  	All bool `json:",omitempty"`
    88  }
    89  
    90  type CanticleDependencies []*CanticleDependency
    91  
    92  func (cd CanticleDependencies) Len() int {
    93  	return len(cd)
    94  }
    95  
    96  func (cd CanticleDependencies) Less(i, j int) bool {
    97  	return cd[i].Root < cd[j].Root
    98  }
    99  
   100  func (cd CanticleDependencies) Swap(i, j int) {
   101  	cd[i], cd[j] = cd[j], cd[i]
   102  }