|
| 1 | +package fingerprint |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/zeebo/xxh3" |
| 8 | + |
| 9 | + "github.com/go-task/task/v3/taskfile/ast" |
| 10 | +) |
| 11 | + |
| 12 | +type fingerprintIdentity struct { |
| 13 | + Task string `json:"task"` |
| 14 | + Dir string `json:"dir"` |
| 15 | + Sources []string `json:"sources,omitempty"` |
| 16 | + Generates []string `json:"generates,omitempty"` |
| 17 | +} |
| 18 | + |
| 19 | +func taskFingerprintKey(t *ast.Task) string { |
| 20 | + name := taskIdentityName(t) |
| 21 | + identity := fingerprintIdentity{ |
| 22 | + Task: name, |
| 23 | + Dir: t.Dir, |
| 24 | + Sources: globPatterns(t.Sources), |
| 25 | + Generates: globPatterns(t.Generates), |
| 26 | + } |
| 27 | + |
| 28 | + encoded, err := json.Marshal(identity) |
| 29 | + if err != nil { |
| 30 | + return normalizeFilename(name) |
| 31 | + } |
| 32 | + |
| 33 | + return normalizeFilename(fmt.Sprintf("%s-%x", name, xxh3.Hash(encoded))) |
| 34 | +} |
| 35 | + |
| 36 | +func taskIdentityName(t *ast.Task) string { |
| 37 | + if t.FullName != "" { |
| 38 | + return t.FullName |
| 39 | + } |
| 40 | + return t.Task |
| 41 | +} |
| 42 | + |
| 43 | +func globPatterns(globs []*ast.Glob) []string { |
| 44 | + if len(globs) == 0 { |
| 45 | + return nil |
| 46 | + } |
| 47 | + |
| 48 | + patterns := make([]string, 0, len(globs)) |
| 49 | + for _, glob := range globs { |
| 50 | + if glob == nil { |
| 51 | + continue |
| 52 | + } |
| 53 | + if glob.Negate { |
| 54 | + patterns = append(patterns, "!"+glob.Glob) |
| 55 | + continue |
| 56 | + } |
| 57 | + patterns = append(patterns, glob.Glob) |
| 58 | + } |
| 59 | + return patterns |
| 60 | +} |
0 commit comments