1
1
package action
2
2
3
3
import (
4
+ "encoding/hex"
5
+ "fmt"
4
6
"io/ioutil"
7
+ "os"
5
8
"path/filepath"
6
9
7
10
"github.com/Masterminds/glide/cfg"
8
11
"github.com/Masterminds/glide/dependency"
9
12
"github.com/Masterminds/glide/msg"
10
13
gpath "github.com/Masterminds/glide/path"
11
14
"github.com/Masterminds/glide/repo"
15
+ "github.com/Sirupsen/logrus"
16
+ "github.com/sdboyer/vsolver"
12
17
)
13
18
14
19
// Install installs a vendor directory based on an existing Glide configuration.
@@ -19,58 +24,66 @@ func Install(installer *repo.Installer, strip, stripVendor bool) {
19
24
EnsureVendorDir ()
20
25
conf := EnsureConfig ()
21
26
22
- // Lockfile exists
23
- if ! gpath .HasLock (base ) {
24
- msg .Info ("Lock file (glide.lock) does not exist. Performing update." )
25
- Update (installer , false , strip , stripVendor )
26
- return
27
- }
28
- // Load lockfile
29
- lock , err := LoadLockfile (base , conf )
27
+ // Create the SourceManager for this run
28
+ sm , err := vsolver .NewSourceManager (filepath .Join (installer .Home , "cache" ), base , true , false , dependency.Analyzer {})
30
29
if err != nil {
31
- msg .Die ("Could not load lockfile." )
30
+ msg .Die (err . Error () )
32
31
}
33
32
34
- // Delete unused packages
35
- if installer .DeleteUnused {
36
- // It's unclear whether this should operate off of the lock, or off
37
- // of the glide.yaml file. I'd think that doing this based on the
38
- // lock would be much more reliable.
39
- dependency .DeleteUnused (conf )
33
+ opts := vsolver.SolveOpts {
34
+ Name : vsolver .ProjectName (filepath .Dir (installer .Vendor )),
35
+ Root : filepath .Dir (installer .Vendor ),
36
+ M : conf ,
40
37
}
41
38
42
- // Install
43
- newConf , err := installer .Install (lock , conf )
44
- if err != nil {
45
- msg .Die ("Failed to install: %s" , err )
39
+ if gpath .HasLock (base ) {
40
+ opts .L , err = LoadLockfile (base , conf )
41
+ if err != nil {
42
+ msg .Die ("Could not load lockfile." )
43
+ }
44
+ if opts .L .InputHash () == hex .EncodeToString (opts .HashInputs ()) {
45
+ // Digests match; no solver run is necessary. Just need to actually
46
+ // write out the vendor dir
47
+ err = writeVendor (installer .Vendor , l , sm )
48
+ if err != nil {
49
+ msg .Die (err )
50
+ }
51
+ }
46
52
}
47
53
48
- msg .Info ("Setting references." )
54
+ // There is no lock, so we have to solve first
55
+ s := vsolver .NewSolver (sm , logrus .New ())
56
+ r , err := s .Solve (opts )
57
+ if err != nil {
58
+ // TODO better error handling
59
+ msg .Die (err )
60
+ }
49
61
50
- // Set reference
51
- if err := repo . SetReference ( newConf ); err != nil {
52
- msg .Err ( "Failed to set references: %s (Skip to cleanup)" , err )
62
+ err = writeVendor ( installer . Vendor , r , sm )
63
+ if err != nil {
64
+ msg .Die ( err )
53
65
}
66
+ }
54
67
55
- // VendoredCleanup. This should ONLY be run if UpdateVendored was specified.
56
- // When stripping VCS happens this will happen as well. No need for double
57
- // effort.
58
- if installer . UpdateVendored && ! strip {
59
- repo . VendoredCleanup ( newConf )
68
+ // TODO This will almost certainly need to be renamed and move somewhere else
69
+ func writeVendor ( vendor string , l vsolver. Lock , sm vsolver. SourceManager ) error {
70
+ td , err := ioutil . TempDir ( os . TempDir (), "glide" )
71
+ if err != nil {
72
+ return fmt . Errorf ( "Error while creating temp dir for vendor directory: %s" , err )
60
73
}
74
+ defer os .RemoveAll (td )
61
75
62
- if strip {
63
- msg . Info ( "Removing version control data from vendor directory..." )
64
- gpath . StripVcs ( )
76
+ err = vsolver . CreateVendorTree ( td , l , sm )
77
+ if err != nil {
78
+ return fmt . Errorf ( "Error while generating vendor tree: %s" , err )
65
79
}
66
80
67
- if stripVendor {
68
- msg .Info ("Removing nested vendor and Godeps/_workspace directories..." )
69
- err := gpath .StripVendor ()
70
- if err != nil {
71
- msg .Err ("Unable to strip vendor directories: %s" , err )
72
- }
81
+ err = os .Rename (td , installer .Vendor )
82
+ if err != nil {
83
+ return fmt .Errorf ("Error while moving generated vendor directory into place: %s" , err )
73
84
}
85
+
86
+ return nil
74
87
}
75
88
76
89
// LoadLockfile loads the contents of a glide.lock file.
0 commit comments