Skip to content

Commit

Permalink
feat: add pipeline priority field
Browse files Browse the repository at this point in the history
Allows for fine-grained control of execution order.

Signed-off-by: Brian McGee <[email protected]>
  • Loading branch information
brianmcgee committed Apr 25, 2024
1 parent c71d690 commit 6ae0e4f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
12 changes: 12 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ func TestReadConfigFile(t *testing.T) {
as.Nil(alejandra.Options)
as.Equal([]string{"*.nix"}, alejandra.Includes)
as.Equal([]string{"examples/nix/sources.nix"}, alejandra.Excludes)
as.Equal("nix", alejandra.Pipeline)
as.Equal(1, alejandra.Priority)

// deadnix
deadnix, ok := cfg.Formatters["deadnix"]
as.True(ok, "deadnix formatter not found")
as.Equal("deadnix", deadnix.Command)
as.Nil(deadnix.Options)
as.Equal([]string{"*.nix"}, deadnix.Includes)
as.Nil(deadnix.Excludes)
as.Equal("nix", deadnix.Pipeline)
as.Equal(2, deadnix.Priority)

// ruby
ruby, ok := cfg.Formatters["ruby"]
Expand Down
4 changes: 3 additions & 1 deletion config/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ type Formatter struct {
Includes []string
// Excludes is an optional list of glob patterns used to exclude certain files from this Formatter.
Excludes []string
//
// Indicates this formatter should be executed as part of a group of formatters all sharing the same pipeline key.
Pipeline string
// Indicates the order of precedence when executing as part of a pipeline.
Priority int
}
9 changes: 8 additions & 1 deletion format/pipeline.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package format

import "context"
import (
"context"
"slices"
)

type Pipeline struct {
sequence []*Formatter
}

func (p *Pipeline) Add(f *Formatter) {
p.sequence = append(p.sequence, f)
// sort by priority in ascending order
slices.SortFunc(p.sequence, func(a, b *Formatter) int {
return a.config.Priority - b.config.Priority
})
}

func (p *Pipeline) Wants(path string) bool {
Expand Down
2 changes: 2 additions & 0 deletions test/examples/treefmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ includes = ["*.nix"]
# Act as an example on how to exclude specific files
excludes = ["examples/nix/sources.nix"]
pipeline = "nix"
priority = 1

[formatter.deadnix]
command = "deadnix"
includes = ["*.nix"]
pipeline = "nix"
priority = 2

[formatter.ruby]
command = "rufo"
Expand Down

0 comments on commit 6ae0e4f

Please sign in to comment.