Skip to content

Commit af0c473

Browse files
committed
Support cached execution for components
1 parent da7d606 commit af0c473

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

cmd/exec.go

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func executeCommandInLocations(rawExecCmd []string, locs []commandExecLocation,
243243
}
244244

245245
for _, loc := range locs {
246-
if ok, _ := cache.NeedsExecution(context.Background(), loc.Package); !ok {
246+
if ok, _ := cache.NeedsExecution(context.Background(), loc); !ok {
247247
continue
248248
}
249249

@@ -284,7 +284,7 @@ func executeCommandInLocations(rawExecCmd []string, locs []commandExecLocation,
284284
eg.Go(func() error {
285285
err := cmd.Wait()
286286
if err == nil {
287-
err = cache.MarkExecuted(context.Background(), loc.Package)
287+
err = cache.MarkExecuted(context.Background(), loc)
288288
if err != nil {
289289
log.WithError(err).Warn("cannot mark package as executed")
290290
}
@@ -296,7 +296,7 @@ func executeCommandInLocations(rawExecCmd []string, locs []commandExecLocation,
296296
} else {
297297
err = cmd.Wait()
298298
if err == nil {
299-
err = cache.MarkExecuted(context.Background(), loc.Package)
299+
err = cache.MarkExecuted(context.Background(), loc)
300300
if err != nil {
301301
log.WithError(err).Warn("cannot mark package as executed")
302302
}
@@ -332,25 +332,25 @@ func init() {
332332
}
333333

334334
type execCache interface {
335-
NeedsExecution(ctx context.Context, pkg *leeway.Package) (bool, error)
336-
MarkExecuted(ctx context.Context, pkg *leeway.Package) error
335+
NeedsExecution(ctx context.Context, loc commandExecLocation) (bool, error)
336+
MarkExecuted(ctx context.Context, loc commandExecLocation) error
337337
}
338338

339339
type noExecCache struct{}
340340

341-
func (noExecCache) MarkExecuted(ctx context.Context, pkg *leeway.Package) error { return nil }
342-
func (noExecCache) NeedsExecution(ctx context.Context, pkg *leeway.Package) (bool, error) {
341+
func (noExecCache) MarkExecuted(ctx context.Context, loc commandExecLocation) error { return nil }
342+
func (noExecCache) NeedsExecution(ctx context.Context, loc commandExecLocation) (bool, error) {
343343
return true, nil
344344
}
345345

346346
type filesystemExecCache string
347347

348-
func (c filesystemExecCache) MarkExecuted(ctx context.Context, pkg *leeway.Package) error {
348+
func (c filesystemExecCache) MarkExecuted(ctx context.Context, loc commandExecLocation) error {
349349
err := os.MkdirAll(string(c), 0755)
350350
if err != nil {
351351
return err
352352
}
353-
fn, err := c.filename(pkg)
353+
fn, err := c.filename(loc)
354354
if err != nil {
355355
return err
356356
}
@@ -359,20 +359,31 @@ func (c filesystemExecCache) MarkExecuted(ctx context.Context, pkg *leeway.Packa
359359
return err
360360
}
361361
f.Close()
362-
log.WithField("name", pkg.FullName()).Debug("marked package as executed")
362+
log.WithField("name", fn).Debug("marked executed")
363363
return nil
364364
}
365365

366-
func (c filesystemExecCache) filename(pkg *leeway.Package) (string, error) {
367-
v, err := pkg.Version()
368-
if err != nil {
369-
return "", err
366+
func (c filesystemExecCache) filename(loc commandExecLocation) (string, error) {
367+
var id string
368+
if loc.Package != nil {
369+
v, err := loc.Package.Version()
370+
if err != nil {
371+
return "", err
372+
}
373+
id = v
374+
} else if loc.Component != nil {
375+
id = leeway.FilesystemSafeName(loc.Component.Name)
376+
} else if loc.Dir != "" {
377+
id = leeway.FilesystemSafeName(loc.Dir)
378+
} else if loc.Name != "" {
379+
id = loc.Name
370380
}
371-
return filepath.Join(string(c), fmt.Sprintf("%s.executed", v)), nil
381+
382+
return filepath.Join(string(c), fmt.Sprintf("%s.executed", id)), nil
372383
}
373384

374-
func (c filesystemExecCache) NeedsExecution(ctx context.Context, pkg *leeway.Package) (bool, error) {
375-
fn, err := c.filename(pkg)
385+
func (c filesystemExecCache) NeedsExecution(ctx context.Context, loc commandExecLocation) (bool, error) {
386+
fn, err := c.filename(loc)
376387
if err != nil {
377388
return false, err
378389
}

pkg/leeway/package.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -772,12 +772,15 @@ func (p *Package) FullName() string {
772772

773773
// FilesystemSafeName returns a string that is safe to use in a Unix filesystem as directory or filename
774774
func (p *Package) FilesystemSafeName() string {
775-
pkgdir := p.FullName()
776-
pkgdir = strings.Replace(pkgdir, "/", "-", -1)
777-
pkgdir = strings.Replace(pkgdir, ":", "--", -1)
775+
return FilesystemSafeName(p.FullName())
776+
}
777+
778+
func FilesystemSafeName(fn string) string {
779+
res := strings.Replace(fn, "/", "-", -1)
780+
res = strings.Replace(res, ":", "--", -1)
778781
// components in the workspace root would otherwise start with - which breaks a lot of shell commands
779-
pkgdir = strings.TrimLeft(pkgdir, "-")
780-
return pkgdir
782+
res = strings.TrimLeft(res, "-")
783+
return res
781784
}
782785

783786
func (p *Package) resolveBuiltinVariables() error {

0 commit comments

Comments
 (0)