2022-11-18 09:57:31 +00:00
|
|
|
package mutator
|
|
|
|
|
|
|
|
import (
|
2022-11-28 09:59:43 +00:00
|
|
|
"context"
|
2022-11-18 09:57:31 +00:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
|
2022-11-28 09:59:43 +00:00
|
|
|
"github.com/databricks/bricks/bundle"
|
2022-11-18 09:57:31 +00:00
|
|
|
"github.com/databricks/bricks/bundle/config"
|
|
|
|
"golang.org/x/exp/slices"
|
|
|
|
)
|
|
|
|
|
|
|
|
type processRootIncludes struct{}
|
|
|
|
|
|
|
|
// ProcessRootIncludes expands the patterns in the configuration's include list
|
|
|
|
// into a list of mutators for each matching file.
|
2022-11-28 09:59:43 +00:00
|
|
|
func ProcessRootIncludes() bundle.Mutator {
|
2022-11-18 09:57:31 +00:00
|
|
|
return &processRootIncludes{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *processRootIncludes) Name() string {
|
|
|
|
return "ProcessRootIncludes"
|
|
|
|
}
|
|
|
|
|
2022-11-28 09:59:43 +00:00
|
|
|
func (m *processRootIncludes) Apply(_ context.Context, b *bundle.Bundle) ([]bundle.Mutator, error) {
|
|
|
|
var out []bundle.Mutator
|
2022-11-18 09:57:31 +00:00
|
|
|
|
|
|
|
// Map with files we've already seen to avoid loading them twice.
|
|
|
|
var seen = map[string]bool{
|
|
|
|
config.FileName: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// For each glob, find all files to load.
|
|
|
|
// Ordering of the list of globs is maintained in the output.
|
|
|
|
// For matches that appear in multiple globs, only the first is kept.
|
2022-11-28 09:59:43 +00:00
|
|
|
for _, entry := range b.Config.Include {
|
2022-11-18 09:57:31 +00:00
|
|
|
// Include paths must be relative.
|
|
|
|
if filepath.IsAbs(entry) {
|
|
|
|
return nil, fmt.Errorf("%s: includes must be relative paths", entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Anchor includes to the bundle root path.
|
2022-11-28 09:59:43 +00:00
|
|
|
matches, err := filepath.Glob(filepath.Join(b.Config.Path, entry))
|
2022-11-18 09:57:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter matches to ones we haven't seen yet.
|
|
|
|
var includes []string
|
|
|
|
for _, match := range matches {
|
2022-11-28 09:59:43 +00:00
|
|
|
rel, err := filepath.Rel(b.Config.Path, match)
|
2022-11-18 09:57:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, ok := seen[rel]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
seen[rel] = true
|
|
|
|
includes = append(includes, rel)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add matches to list of mutators to return.
|
|
|
|
slices.Sort(includes)
|
|
|
|
for _, include := range includes {
|
2022-11-28 09:59:43 +00:00
|
|
|
out = append(out, ProcessInclude(filepath.Join(b.Config.Path, include), include))
|
2022-11-18 09:57:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|