Skip to content

Commit

Permalink
First pass at converting Install procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
sdboyer committed Apr 21, 2016
1 parent ccda305 commit f535027
Showing 1 changed file with 50 additions and 37 deletions.
87 changes: 50 additions & 37 deletions action/install.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package action

import (
"encoding/hex"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/Masterminds/glide/cfg"
"github.com/Masterminds/glide/dependency"
"github.com/Masterminds/glide/msg"
gpath "github.com/Masterminds/glide/path"
"github.com/Masterminds/glide/repo"
"github.com/Sirupsen/logrus"
"github.com/sdboyer/vsolver"
)

// Install installs a vendor directory based on an existing Glide configuration.
Expand All @@ -19,58 +24,66 @@ func Install(installer *repo.Installer, strip, stripVendor bool) {
EnsureVendorDir()
conf := EnsureConfig()

// Lockfile exists
if !gpath.HasLock(base) {
msg.Info("Lock file (glide.lock) does not exist. Performing update.")
Update(installer, false, strip, stripVendor)
return
}
// Load lockfile
lock, err := LoadLockfile(base, conf)
// Create the SourceManager for this run
sm, err := vsolver.NewSourceManager(filepath.Join(installer.Home, "cache"), base, true, false, dependency.Analyzer{})
if err != nil {
msg.Die("Could not load lockfile.")
msg.Die(err.Error())
}

// Delete unused packages
if installer.DeleteUnused {
// It's unclear whether this should operate off of the lock, or off
// of the glide.yaml file. I'd think that doing this based on the
// lock would be much more reliable.
dependency.DeleteUnused(conf)
opts := vsolver.SolveOpts{
Name: vsolver.ProjectName(filepath.Dir(installer.Vendor)),
Root: filepath.Dir(installer.Vendor),
M: conf,
}

// Install
newConf, err := installer.Install(lock, conf)
if err != nil {
msg.Die("Failed to install: %s", err)
if gpath.HasLock(base) {
opts.L, err = LoadLockfile(base, conf)
if err != nil {
msg.Die("Could not load lockfile.")
}
if opts.L.InputHash() == hex.EncodeToString(opts.HashInputs()) {
// Digests match; no solver run is necessary. Just need to actually
// write out the vendor dir
err = writeVendor(installer.Vendor, l, sm)
if err != nil {
msg.Die(err)
}
}
}

msg.Info("Setting references.")
// There is no lock, so we have to solve first
s := vsolver.NewSolver(sm, logrus.New())
r, err := s.Solve(opts)
if err != nil {
// TODO better error handling
msg.Die(err)
}

// Set reference
if err := repo.SetReference(newConf); err != nil {
msg.Err("Failed to set references: %s (Skip to cleanup)", err)
err = writeVendor(installer.Vendor, r, sm)
if err != nil {
msg.Die(err)
}
}

// VendoredCleanup. This should ONLY be run if UpdateVendored was specified.
// When stripping VCS happens this will happen as well. No need for double
// effort.
if installer.UpdateVendored && !strip {
repo.VendoredCleanup(newConf)
// TODO This will almost certainly need to be renamed and move somewhere else
func writeVendor(vendor string, l vsolver.Lock, sm vsolver.SourceManager) error {
td, err := ioutil.TempDir(os.TempDir(), "glide")
if err != nil {
return fmt.Errorf("Error while creating temp dir for vendor directory: %s", err)
}
defer os.RemoveAll(td)

if strip {
msg.Info("Removing version control data from vendor directory...")
gpath.StripVcs()
err = vsolver.CreateVendorTree(td, l, sm)
if err != nil {
return fmt.Errorf("Error while generating vendor tree: %s", err)
}

if stripVendor {
msg.Info("Removing nested vendor and Godeps/_workspace directories...")
err := gpath.StripVendor()
if err != nil {
msg.Err("Unable to strip vendor directories: %s", err)
}
err = os.Rename(td, installer.Vendor)
if err != nil {
return fmt.Errorf("Error while moving generated vendor directory into place: %s", err)
}

return nil
}

// LoadLockfile loads the contents of a glide.lock file.
Expand Down

0 comments on commit f535027

Please sign in to comment.