forked from DavidGamba/dgtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paths.go
153 lines (145 loc) · 4.15 KB
/
paths.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// This file is part of fsmodtime.
//
// Copyright (C) 2021 David Gamba Rios
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package fsmodtime
import (
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
"time"
)
var ErrNotFound = fmt.Errorf("not found")
// ExpandEnv - like os.ExpandEnv on many lines, except that it reports error if any of the env vars is
// not found.
func ExpandEnv(lines []string) ([]string, error) {
expanded := []string{}
var err error
notFound := make(map[string]struct{})
mappingFn := func(name string) string {
if value, ok := os.LookupEnv(name); ok {
return value
}
notFound[name] = struct{}{}
return ""
}
for _, line := range lines {
expanded = append(expanded, os.Expand(line, mappingFn))
}
if len(notFound) > 0 {
notFoundList := []string{}
for k := range notFound {
notFoundList = append(notFoundList, k)
}
err = fmt.Errorf("%w: %q", ErrNotFound, notFoundList)
}
return expanded, err
}
// Glob - like io/fs.Glob on many paths.
// It allows to stop globbing patterns once it has found a pattern that has no matches.
// Glob syntax described here: https://golang.org/pkg/path/filepath/#Match
//
// Returns the list of matches, a bool indicating if there is a pattern that had no matches and an error
func Glob(fsys fs.FS, stop bool, patterns []string) ([]string, bool, error) {
matches := []string{}
for _, p := range patterns {
m, err := fs.Glob(fsys, p)
if err != nil {
return matches, false, err
}
if stop && len(m) == 0 {
return matches, true, nil
}
matches = append(matches, m...)
}
return matches, false, nil
}
// Target - Given a list of targets it indicates whether or not the sources have modifications past the targets last.
// The first return is the file modified.
func Target(fsys fs.FS, targets []string, sources []string) ([]string, bool, error) {
targets, err := ExpandEnv(targets)
if err != nil {
return nil, false, err
}
targets, stopped, err := Glob(fsys, true, targets)
if err != nil {
return nil, false, err
}
if stopped {
// targets don't exist, re-create them
return []string{}, true, nil
}
Logger.Printf("targets: %q\n", targets)
p, fi, err := Last(fsys, targets)
if err != nil {
return nil, false, err
}
Logger.Printf("last target: %q\n", path.Join(p, fi.Name()))
return TargetTime(fsys, fi.ModTime(), sources)
}
// TargetTime - Given a time it indicates whether or not the sources have modifications past the time.
// The first return is the file modified.
func TargetTime(fsys fs.FS, targetTime time.Time, sources []string) ([]string, bool, error) {
sources, err := ExpandEnv(sources)
if err != nil {
return nil, false, err
}
sources, _, err = Glob(fsys, false, sources)
if err != nil {
return nil, false, err
}
Logger.Printf("sources: %q\n", sources)
// TODO: This is the wrong implementation, I don't need the times of all files, only one newer than targetTime
p, fi, err := Last(fsys, sources)
if err != nil {
return nil, false, err
}
if fi == nil {
return nil, false, ErrNotFound
}
Logger.Printf("last source: %q\n", path.Join(p, fi.Name()))
if targetTime.Before(fi.ModTime()) {
// targets are older, re-create them
return []string{path.Join(p, fi.Name())}, true, nil
}
// targets are newer, nothing to do
return []string{}, false, nil
}
// ParentDir - Given a list of paths, it finds the common parent dir.
// Assumes / as the filepath separator.
func ParentDir(paths []string) (string, error) {
switch len(paths) {
case 0:
return "", nil
case 1:
return filepath.Clean(paths[0]), nil
}
p, err := filepath.Abs(paths[0])
if err != nil {
return "", fmt.Errorf("failed to get absolute path: %w", err)
}
pl := strings.Split(p, "/")
for _, v := range paths[1:] {
v, err := filepath.Abs(v)
if err != nil {
return "", fmt.Errorf("failed to get absolute path: %w", err)
}
vl := strings.Split(v, "/")
if len(vl) < len(pl) {
pl = pl[:len(vl)]
}
for i := 0; i < len(pl); i++ {
if vl[i] != pl[i] {
pl = pl[:i]
}
}
}
r := "/" + filepath.Join(pl...)
return r, nil
}