Skip to content

Commit 011a3fe

Browse files
author
Christian Weichel
committed
[variants] Support component-level exclusion
whereby excluded components are simply not present and all dependencies to their packages are silently removed.
1 parent 7b9b923 commit 011a3fe

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

pkg/leeway/package.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,9 @@ type packageVariantInternal struct {
502502
Include []string `yaml:"include"`
503503
Exclude []string `yaml:"exclude"`
504504
} `yaml:"srcs"`
505+
Components struct {
506+
Exclude []string `yaml:"exclude"`
507+
} `yaml:"components"`
505508
Environment []string `yaml:"env"`
506509
RawConfig map[PackageType]yaml.Node `yaml:"config"`
507510
}
@@ -551,6 +554,16 @@ func (v *PackageVariant) Config(t PackageType) (cfg PackageConfig, ok bool) {
551554
return
552555
}
553556

557+
// ExcludeComponent returns true if this variants excludes the component
558+
func (v *PackageVariant) ExcludeComponent(name string) bool {
559+
for _, c := range v.Components.Exclude {
560+
if c == name {
561+
return true
562+
}
563+
}
564+
return false
565+
}
566+
554567
// ResolveSources lists all files which are explicitely included or excluded by this variant.
555568
// Inclusion takes precedence over exclusion.
556569
func (v *PackageVariant) ResolveSources(workspace *Workspace, loc string) (incl []string, excl []string, err error) {

pkg/leeway/workspace.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,11 @@ func discoverComponents(ctx context.Context, workspace *Workspace, args Argument
481481
defer wg.Done()
482482

483483
for c := range cchan {
484+
// filter variant-excluded components and all their packages
485+
if filterExcludedComponents(variant, c) {
486+
continue
487+
}
488+
484489
comps = append(comps, c)
485490
}
486491
}()
@@ -494,6 +499,33 @@ func discoverComponents(ctx context.Context, workspace *Workspace, args Argument
494499
return comps, nil
495500
}
496501

502+
// filterExcludedComponents returns true if the component is excluded by the variant.
503+
// This function also removes all dependencies to excluded components.
504+
func filterExcludedComponents(variant *PackageVariant, c *Component) (ignoreComponent bool) {
505+
if variant == nil {
506+
return false
507+
}
508+
if variant.ExcludeComponent(c.Name) {
509+
log.WithField("component", c.Name).Debug("selected variant excludes this component")
510+
return true
511+
}
512+
513+
for _, p := range c.Packages {
514+
for i, dep := range p.Dependencies {
515+
segs := strings.Split(dep, ":")
516+
if len(segs) != 2 {
517+
continue
518+
}
519+
520+
if variant.ExcludeComponent(segs[0]) {
521+
p.Dependencies[i] = p.Dependencies[len(p.Dependencies)-1]
522+
p.Dependencies = p.Dependencies[:len(p.Dependencies)-1]
523+
}
524+
}
525+
}
526+
return false
527+
}
528+
497529
// loadComponent loads a component from a BUILD.yaml file
498530
func loadComponent(ctx context.Context, workspace *Workspace, path string, args Arguments, variant *PackageVariant) (c Component, err error) {
499531
defer trace.StartRegion(context.Background(), "loadComponent").End()

0 commit comments

Comments
 (0)