-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move logic from Processor to Pipeline and add tests
- Loading branch information
1 parent
801c218
commit c6b9abd
Showing
7 changed files
with
379 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,107 @@ | ||
package stylist | ||
|
||
// pipeline = NewPipeline(config) | ||
// diagnostics, err = pipeline.Check(ctx, pathSpecs) | ||
// processors = pipeline.Index(ctx, pathSpecs) | ||
// diagnostics := []*Diagnostic{} | ||
// for p in processors { | ||
// pd, err := p.Check(ctx) | ||
// if err != nil { | ||
// diagnostics, err | ||
// } | ||
// diagnostics = append(diagnostics, pd...) | ||
// } | ||
// diagnostics, nil | ||
|
||
// diagnostics = pipeline.Fix(ctx, pathSpecs) | ||
// processors = pipeline.Index(ctx, pathSpecs) | ||
import ( | ||
"context" | ||
"sort" | ||
|
||
"github.com/bmatcuk/doublestar/v4" | ||
) | ||
|
||
func NewPipeline(processors []*Processor, excludes []string) *Pipeline { | ||
// Always ignore git dirs. | ||
excludes = append(excludes, ".git/**") | ||
return &Pipeline{ | ||
processors: processors, | ||
excludes: excludes, | ||
} | ||
} | ||
|
||
type Pipeline struct { | ||
processors []*Processor | ||
excludes []string | ||
} | ||
|
||
// Index populates the paths for each processor in the pipeline. | ||
// | ||
// The source paths are resolved from each path spec; matched against | ||
// any global exclude patterns; then matched against each processor's | ||
// individual type, include, and exclude patterns. | ||
func (p *Pipeline) Index(ctx context.Context, pathSpecs []string) error { | ||
// Aggregate each processor's file types and include patterns | ||
fileTypes := []string{} | ||
includes := []string{} | ||
for _, processor := range p.processors { | ||
fileTypes = append(fileTypes, processor.Types...) | ||
includes = append(includes, processor.Includes...) | ||
} | ||
|
||
// TODO: support passing Context to the indexer | ||
// Create an index of paths (resolved from the path specs), | ||
// matching any of the types and include patterns used by our processors. | ||
// Doing this once is _much_ faster than once per-processor, | ||
// especially when dealing w/ very large projects and many processors or patterns. | ||
indexer := NewPathIndexer(fileTypes, includes, p.excludes) | ||
if err := indexer.Index(pathSpecs...); err != nil { | ||
return err | ||
} | ||
|
||
// For each processor... | ||
for _, processor := range p.processors { | ||
// Gather all paths matching the file types and include patterns | ||
// configured for this processor. | ||
pathSet := NewPathSet() | ||
for _, ft := range processor.Types { | ||
if ftPaths, ok := indexer.PathsByFileType[ft]; ok { | ||
pathSet = pathSet.Union(ftPaths) | ||
} | ||
} | ||
for _, inc := range processor.Includes { | ||
if incPaths, ok := indexer.PathsByInclude[inc]; ok { | ||
pathSet = pathSet.Union(incPaths) | ||
} | ||
} | ||
|
||
// Now, filter out anything _this processor_ is configured to ignore. | ||
// (vs. the global excludes we passed in to the indexer). | ||
paths := []string{} | ||
for path := range pathSet.Iter() { | ||
excluded := false | ||
for _, pattern := range processor.Excludes { | ||
ok, err := doublestar.PathMatch(pattern, path) | ||
if err != nil { | ||
return err | ||
} | ||
if ok { | ||
excluded = true | ||
} | ||
} | ||
if !excluded { | ||
paths = append(paths, path) | ||
} | ||
} | ||
|
||
sort.Strings(paths) | ||
processor.paths = paths | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Check executes the check command for each processor in the pipeline. | ||
func (p *Pipeline) Check(ctx context.Context, pathSpecs []string) ([]*Result, error) { | ||
if err := p.Index(ctx, pathSpecs); err != nil { | ||
return nil, err | ||
} | ||
results := []*Result{} | ||
for _, processor := range p.processors { | ||
if processor.CheckCommand == nil { | ||
continue | ||
} | ||
pr, err := processor.CheckCommand.Execute(ctx, processor.Paths()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
results = append(results, pr...) | ||
} | ||
return results, nil | ||
} |
Oops, something went wrong.