github.com/fibonacci1729/glide@v0.0.0-20160513190140-d9640dc62d0f/action/update.go (about)

     1  package action
     2  
     3  import (
     4  	"io/ioutil"
     5  	"path/filepath"
     6  
     7  	"github.com/Masterminds/glide/cfg"
     8  	"github.com/Masterminds/glide/dependency"
     9  	"github.com/Masterminds/glide/godep"
    10  	"github.com/Masterminds/glide/msg"
    11  	gpath "github.com/Masterminds/glide/path"
    12  	"github.com/Masterminds/glide/repo"
    13  )
    14  
    15  // Update updates repos and the lock file from the main glide yaml.
    16  func Update(installer *repo.Installer, skipRecursive, strip, stripVendor bool) {
    17  	base := "."
    18  	EnsureGopath()
    19  	EnsureVendorDir()
    20  	conf := EnsureConfig()
    21  
    22  	// Delete unused packages
    23  	if installer.DeleteUnused {
    24  		dependency.DeleteUnused(conf)
    25  	}
    26  
    27  	// Try to check out the initial dependencies.
    28  	if err := installer.Checkout(conf, false); err != nil {
    29  		msg.Die("Failed to do initial checkout of config: %s", err)
    30  	}
    31  
    32  	// Set the versions for the initial dependencies so that resolved dependencies
    33  	// are rooted in the correct version of the base.
    34  	if err := repo.SetReference(conf); err != nil {
    35  		msg.Die("Failed to set initial config references: %s", err)
    36  	}
    37  
    38  	// Prior to resolving dependencies we need to start working with a clone
    39  	// of the conf because we'll be making real changes to it.
    40  	confcopy := conf.Clone()
    41  
    42  	if !skipRecursive {
    43  		// Get all repos and update them.
    44  		err := installer.Update(confcopy)
    45  		if err != nil {
    46  			msg.Die("Could not update packages: %s", err)
    47  		}
    48  
    49  		// TODO: There is no support here for importing Godeps, GPM, and GB files.
    50  		// I think that all we really need to do now is hunt for these files, and then
    51  		// roll their version numbers into the config file.
    52  
    53  		// Set references. There may be no remaining references to set since the
    54  		// installer set them as it went to make sure it parsed the right imports
    55  		// from the right version of the package.
    56  		msg.Info("Setting references for remaining imports")
    57  		if err := repo.SetReference(confcopy); err != nil {
    58  			msg.Err("Failed to set references: %s (Skip to cleanup)", err)
    59  		}
    60  	}
    61  	// Vendored cleanup
    62  	// VendoredCleanup. This should ONLY be run if UpdateVendored was specified.
    63  	// When stripping VCS happens this will happen as well. No need for double
    64  	// effort.
    65  	if installer.UpdateVendored && !strip {
    66  		repo.VendoredCleanup(confcopy)
    67  	}
    68  
    69  	// Write glide.yaml (Why? Godeps/GPM/GB?)
    70  	// I think we don't need to write a new Glide file because update should not
    71  	// change anything important. It will just generate information about
    72  	// transative dependencies, all of which belongs exclusively in the lock
    73  	// file, not the glide.yaml file.
    74  	// TODO(mattfarina): Detect when a new dependency has been added or removed
    75  	// from the project. A removed dependency should warn and an added dependency
    76  	// should be added to the glide.yaml file. See issue #193.
    77  
    78  	if stripVendor {
    79  		confcopy = godep.RemoveGodepSubpackages(confcopy)
    80  	}
    81  
    82  	if !skipRecursive {
    83  		// Write lock
    84  		hash, err := conf.Hash()
    85  		if err != nil {
    86  			msg.Die("Failed to generate config hash. Unable to generate lock file.")
    87  		}
    88  		lock := cfg.NewLockfile(confcopy.Imports, hash)
    89  		wl := true
    90  		if gpath.HasLock(base) {
    91  			yml, err := ioutil.ReadFile(filepath.Join(base, gpath.LockFile))
    92  			if err == nil {
    93  				l2, err := cfg.LockfileFromYaml(yml)
    94  				if err == nil {
    95  					f1, err := l2.Fingerprint()
    96  					f2, err2 := lock.Fingerprint()
    97  					if err == nil && err2 == nil && f1 == f2 {
    98  						wl = false
    99  					}
   100  				}
   101  			}
   102  		}
   103  		if wl {
   104  			if err := lock.WriteFile(filepath.Join(base, gpath.LockFile)); err != nil {
   105  				msg.Err("Could not write lock file to %s: %s", base, err)
   106  				return
   107  			}
   108  		} else {
   109  			msg.Info("Versions did not change. Skipping glide.lock update.")
   110  		}
   111  
   112  		msg.Info("Project relies on %d dependencies.", len(confcopy.Imports))
   113  	} else {
   114  		msg.Warn("Skipping lockfile generation because full dependency tree is not being calculated")
   115  	}
   116  
   117  	if strip {
   118  		msg.Info("Removing version control data from vendor directory...")
   119  		gpath.StripVcs()
   120  	}
   121  
   122  	if stripVendor {
   123  		msg.Info("Removing nested vendor and Godeps/_workspace directories...")
   124  		err := gpath.StripVendor()
   125  		if err != nil {
   126  			msg.Err("Unable to strip vendor directories: %s", err)
   127  		}
   128  	}
   129  }