Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit a5ab339

Browse files
committed
Further simplify ensure payload logic
* If manifest is provided, write it. * If newLock is provided, write it. * Explicit vendor behavior: VendorOnChanged, VendorAlways, VendorNever. * Write vendor/ using newLock.
1 parent 008ff04 commit a5ab339

File tree

5 files changed

+148
-65
lines changed

5 files changed

+148
-65
lines changed

cmd/dep/ensure.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ func (cmd *ensureCommand) Run(ctx *dep.Ctx, args []string) error {
153153
if err != nil {
154154
return errors.Wrap(err, "ensure vendor is a directory")
155155
}
156-
writeV := !vendorExists && solution != nil
156+
writeV := dep.VendorOnChanged
157+
if !vendorExists && solution != nil {
158+
writeV = dep.VendorAlways
159+
}
157160

158161
var sw dep.SafeWriter
159162
var manifest *dep.Manifest

cmd/dep/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func (cmd *initCommand) Run(ctx *dep.Ctx, args []string) error {
163163
vlogf("Writing manifest and lock files.")
164164

165165
var sw dep.SafeWriter
166-
sw.Prepare(m, nil, l, true)
166+
sw.Prepare(m, nil, l, dep.VendorAlways)
167167
if err := sw.Write(root, sm); err != nil {
168168
return errors.Wrap(err, "safe write of manifest and lock")
169169
}

cmd/dep/remove.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func (cmd *removeCommand) Run(ctx *dep.Ctx, args []string) error {
181181

182182
var sw dep.SafeWriter
183183
newLock := dep.LockFromInterface(soln)
184-
sw.Prepare(p.Manifest, p.Lock, newLock, false)
184+
sw.Prepare(p.Manifest, p.Lock, newLock, dep.VendorOnChanged)
185185
if err := sw.Write(p.AbsRoot, sm); err != nil {
186186
return errors.Wrap(err, "grouped write of manifest, lock and vendor")
187187
}

txn_writer.go

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -151,39 +151,55 @@ func (diff StringDiff) MarshalJSON() ([]byte, error) {
151151
return buf.Bytes(), err
152152
}
153153

154+
// VendorBehavior defines when the vendor directory should be written.
155+
type VendorBehavior int
156+
157+
const (
158+
// VendorOnChanged indicates that the vendor directory should be written when the lock is new or changed.
159+
VendorOnChanged VendorBehavior = iota
160+
// VendorAlways forces the vendor directory to always be written.
161+
VendorAlways
162+
// VendorNever indicates the vendor directory should never be written.
163+
VendorNever
164+
)
165+
154166
// Prepare to write a set of config yaml, lock and vendor tree.
155167
//
156168
// - If manifest is provided, it will be written to the standard manifest file
157169
// name beneath root.
158-
// - If lock is provided it will be written to the standard
159-
// lock file name in the root dir, but vendor will NOT be written
160-
// - If lock and newLock are both provided and are equivalent, then neither lock
161-
// nor vendor will be written
162-
// - If lock and newLock are both provided and are not equivalent,
163-
// the newLock will be written to the same location as above, and a vendor
164-
// tree will be written to the vendor directory
165-
// - If newLock is provided and lock is not, it will write both a lock
166-
// and the vendor directory in the same way
167-
// - If the forceVendor param is true, then vendor/ will be unconditionally
168-
// written out based on newLock if present, else lock, else error.
169-
func (sw *SafeWriter) Prepare(manifest *Manifest, oldLock *Lock, newLock *Lock, forceVendor bool) {
170+
// - If newLock is provided, it will be written to the standard lock file
171+
// name beneath root.
172+
// - If vendor is VendorAlways, or is VendorOnChanged and the locks are different,
173+
// the vendor directory will be written beneath root based on newLock.
174+
// - If oldLock is provided without newLock, error.
175+
// - If vendor is VendorAlways without a newLock, error.
176+
func (sw *SafeWriter) Prepare(manifest *Manifest, oldLock, newLock *Lock, vendor VendorBehavior) error {
170177
sw.Payload = &SafeWriterPayload{
171178
Manifest: manifest,
179+
Lock: newLock,
172180
}
173181

174-
if oldLock != nil && newLock != nil {
182+
if oldLock != nil {
183+
if newLock == nil {
184+
return errors.New("must provide newLock when oldLock is specified")
185+
}
175186
sw.Payload.LockDiff = diffLocks(oldLock, newLock)
176187
}
177188

178-
if forceVendor || sw.Payload.LockDiff != nil {
179-
sw.Payload.Lock = newLock
189+
switch vendor {
190+
case VendorAlways:
180191
sw.Payload.WriteVendor = true
192+
case VendorOnChanged:
193+
if sw.Payload.LockDiff != nil || (newLock != nil && oldLock == nil) {
194+
sw.Payload.WriteVendor = true
195+
}
181196
}
182197

183-
if oldLock == nil && newLock != nil {
184-
sw.Payload.Lock = newLock
185-
sw.Payload.WriteVendor = true
198+
if sw.Payload.WriteVendor && newLock == nil {
199+
return errors.New("must provide newLock in order to write out vendor")
186200
}
201+
202+
return nil
187203
}
188204

189205
func (payload SafeWriterPayload) validate(root string, sm gps.SourceManager) error {
@@ -201,10 +217,6 @@ func (payload SafeWriterPayload) validate(root string, sm gps.SourceManager) err
201217
return errors.New("must provide a SourceManager if writing out a vendor dir")
202218
}
203219

204-
if payload.HasVendor() && payload.Lock == nil {
205-
return errors.New("must provide a lock in order to write out vendor")
206-
}
207-
208220
return nil
209221
}
210222

0 commit comments

Comments
 (0)