2023-03-10 20:27:30 +02:00
|
|
|
package fingerprint
|
2017-09-16 16:44:13 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-09-30 19:40:11 +02:00
|
|
|
"regexp"
|
2017-09-16 16:44:13 +02:00
|
|
|
"strings"
|
2022-08-06 23:19:07 +02:00
|
|
|
|
|
|
|
"github.com/go-task/task/v3/internal/filepathext"
|
2023-03-10 20:27:30 +02:00
|
|
|
"github.com/go-task/task/v3/taskfile"
|
2017-09-16 16:44:13 +02:00
|
|
|
)
|
|
|
|
|
2023-03-10 20:27:30 +02:00
|
|
|
// ChecksumChecker validates if a task is up to date by calculating its source
|
2017-09-16 16:44:13 +02:00
|
|
|
// files checksum
|
2023-03-10 20:27:30 +02:00
|
|
|
type ChecksumChecker struct {
|
|
|
|
tempDir string
|
|
|
|
dry bool
|
2017-09-16 16:44:13 +02:00
|
|
|
}
|
|
|
|
|
2023-03-10 20:27:30 +02:00
|
|
|
func NewChecksumChecker(tempDir string, dry bool) *ChecksumChecker {
|
|
|
|
return &ChecksumChecker{
|
|
|
|
tempDir: tempDir,
|
|
|
|
dry: dry,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (checker *ChecksumChecker) IsUpToDate(t *taskfile.Task) (bool, error) {
|
|
|
|
if len(t.Sources) == 0 {
|
2019-09-09 03:51:56 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2023-03-10 20:27:30 +02:00
|
|
|
checksumFile := checker.checksumFilePath(t)
|
2017-09-16 16:44:13 +02:00
|
|
|
|
2021-12-21 04:00:34 +02:00
|
|
|
data, _ := os.ReadFile(checksumFile)
|
2017-09-16 16:44:13 +02:00
|
|
|
oldMd5 := strings.TrimSpace(string(data))
|
|
|
|
|
2023-03-23 23:09:16 +02:00
|
|
|
newMd5, err := checker.checksum(t)
|
2017-09-16 16:44:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2023-03-10 20:27:30 +02:00
|
|
|
if !checker.dry {
|
|
|
|
_ = os.MkdirAll(filepathext.SmartJoin(checker.tempDir, "checksum"), 0o755)
|
2022-08-17 19:37:58 +02:00
|
|
|
if err = os.WriteFile(checksumFile, []byte(newMd5+"\n"), 0o644); err != nil {
|
2019-02-09 14:41:19 +02:00
|
|
|
return false, err
|
|
|
|
}
|
2017-09-16 16:44:13 +02:00
|
|
|
}
|
2019-08-21 06:35:16 +02:00
|
|
|
|
2023-03-10 20:27:30 +02:00
|
|
|
if len(t.Generates) > 0 {
|
2019-08-25 21:29:32 +02:00
|
|
|
// For each specified 'generates' field, check whether the files actually exist
|
2023-03-10 20:27:30 +02:00
|
|
|
for _, g := range t.Generates {
|
|
|
|
generates, err := Glob(t.Dir, g)
|
2019-08-25 21:29:32 +02:00
|
|
|
if os.IsNotExist(err) {
|
2019-08-21 06:35:16 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
2019-08-25 21:29:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2019-08-21 06:35:16 +02:00
|
|
|
if len(generates) == 0 {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-16 16:44:13 +02:00
|
|
|
return oldMd5 == newMd5, nil
|
|
|
|
}
|
|
|
|
|
2023-03-30 22:03:59 +02:00
|
|
|
func (checker *ChecksumChecker) Value(t *taskfile.Task) (any, error) {
|
2023-03-23 23:09:16 +02:00
|
|
|
return checker.checksum(t)
|
2023-03-10 20:27:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (checker *ChecksumChecker) OnError(t *taskfile.Task) error {
|
|
|
|
if len(t.Sources) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return os.Remove(checker.checksumFilePath(t))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*ChecksumChecker) Kind() string {
|
|
|
|
return "checksum"
|
|
|
|
}
|
|
|
|
|
2023-03-23 23:09:16 +02:00
|
|
|
func (c *ChecksumChecker) checksum(t *taskfile.Task) (string, error) {
|
|
|
|
sources, err := globs(t.Dir, t.Sources)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-09-16 16:44:13 +02:00
|
|
|
|
2023-03-23 23:09:16 +02:00
|
|
|
h := md5.New()
|
|
|
|
for _, f := range sources {
|
2019-08-21 06:34:58 +02:00
|
|
|
// also sum the filename, so checksum changes for renaming a file
|
|
|
|
if _, err := io.Copy(h, strings.NewReader(filepath.Base(f))); err != nil {
|
2017-09-16 16:44:13 +02:00
|
|
|
return "", err
|
|
|
|
}
|
2019-08-21 06:34:58 +02:00
|
|
|
f, err := os.Open(f)
|
2017-09-19 19:03:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-11-02 14:31:34 +02:00
|
|
|
if _, err = io.Copy(h, f); err != nil {
|
2017-09-16 16:44:13 +02:00
|
|
|
return "", err
|
|
|
|
}
|
2023-01-14 22:18:26 +02:00
|
|
|
f.Close()
|
2017-09-16 16:44:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("%x", h.Sum(nil)), nil
|
|
|
|
}
|
|
|
|
|
2023-03-10 20:27:30 +02:00
|
|
|
func (checker *ChecksumChecker) checksumFilePath(t *taskfile.Task) string {
|
|
|
|
return filepath.Join(checker.tempDir, "checksum", normalizeFilename(t.Name()))
|
2017-10-01 20:05:09 +02:00
|
|
|
}
|
|
|
|
|
2017-09-30 19:40:11 +02:00
|
|
|
var checksumFilenameRegexp = regexp.MustCompile("[^A-z0-9]")
|
|
|
|
|
2023-03-10 20:27:30 +02:00
|
|
|
// replaces invalid characters on filenames with "-"
|
2023-01-14 22:18:26 +02:00
|
|
|
func normalizeFilename(f string) string {
|
2017-09-30 19:40:11 +02:00
|
|
|
return checksumFilenameRegexp.ReplaceAllString(f, "-")
|
|
|
|
}
|