goldsmith/plugins/rule/rule.go

167 lines
3.0 KiB
Go
Raw Normal View History

2024-02-17 06:35:49 +00:00
package rule
import (
2024-02-20 00:26:41 +00:00
"io"
2024-02-17 06:35:49 +00:00
"path/filepath"
"sync"
"git.foosoft.net/alex/goldsmith"
"git.foosoft.net/alex/goldsmith/filters/operator"
"git.foosoft.net/alex/goldsmith/filters/wildcard"
"github.com/BurntSushi/toml"
)
type rule struct {
Accept []string
Reject []string
baseDir string
}
type ruleApply struct {
rule
Props goldsmith.FileProps
2024-02-17 06:35:49 +00:00
}
type ruleDrop struct {
rule
}
func (self *rule) accept(inputFile *goldsmith.File) bool {
2024-02-17 06:35:49 +00:00
if !wildcard.New(filepath.Join(self.baseDir, "**")).Accept(inputFile) {
return false
}
var acceptPaths []string
for _, path := range self.Accept {
acceptPaths = append(acceptPaths, filepath.Join(self.baseDir, path))
}
if wildcard.New(acceptPaths...).Accept(inputFile) {
return true
}
var rejectPaths []string
for _, path := range self.Reject {
rejectPaths = append(rejectPaths, filepath.Join(self.baseDir, path))
}
if len(rejectPaths) == 0 {
return false
}
return operator.Not(wildcard.New(rejectPaths...)).Accept(inputFile)
}
func (self *ruleApply) apply(inputFile *goldsmith.File) {
2024-02-17 06:35:49 +00:00
if self.accept(inputFile) {
for name, value := range self.Props {
inputFile.SetProp(name, value)
}
}
}
func (self *ruleDrop) drop(inputFile *goldsmith.File) bool {
2024-02-17 06:35:49 +00:00
return self.accept(inputFile)
}
type ruleSet struct {
Apply []*ruleApply
Drop []*ruleDrop
}
func newRuleSet(inputFile *goldsmith.File) (*ruleSet, error) {
2024-02-20 00:26:41 +00:00
data, err := io.ReadAll(inputFile)
2024-02-17 06:35:49 +00:00
if err != nil {
return nil, err
}
var ruleSet ruleSet
if err := toml.Unmarshal(data, &ruleSet); err != nil {
return nil, err
}
for _, rule := range ruleSet.Apply {
rule.baseDir = inputFile.Dir()
}
for _, rule := range ruleSet.Drop {
rule.baseDir = inputFile.Dir()
}
return &ruleSet, nil
}
func (self *ruleSet) process(inputFile *goldsmith.File) bool {
2024-02-17 06:35:49 +00:00
for _, rule := range self.Apply {
rule.apply(inputFile)
}
for _, rule := range self.Drop {
if rule.drop(inputFile) {
return false
}
}
return true
}
// Rule chainable context.
type Rule struct {
filename string
ruleSets []*ruleSet
inputFiles []*goldsmith.File
2024-02-17 06:35:49 +00:00
mutex sync.Mutex
}
// New creates a new instance of the Rule plugin.
func New() *Rule {
return &Rule{filename: "rules.toml"}
}
func (self *Rule) Filename(filename string) *Rule {
self.filename = filename
return self
}
func (*Rule) Name() string {
return "rule"
}
func (self *Rule) Process(context *goldsmith.Context, inputFile *goldsmith.File) error {
2024-02-17 06:35:49 +00:00
if inputFile.Name() == self.filename {
ruleSet, err := newRuleSet(inputFile)
if err != nil {
return err
}
self.mutex.Lock()
self.ruleSets = append(self.ruleSets, ruleSet)
self.mutex.Unlock()
} else {
self.mutex.Lock()
self.inputFiles = append(self.inputFiles, inputFile)
self.mutex.Unlock()
}
return nil
}
func (self *Rule) Finalize(context *goldsmith.Context) error {
2024-02-17 06:35:49 +00:00
for _, inputFile := range self.inputFiles {
var block bool
for _, ruleSet := range self.ruleSets {
if !ruleSet.process(inputFile) {
block = true
break
}
}
if !block {
context.DispatchFile(inputFile)
}
}
return nil
}