-
Notifications
You must be signed in to change notification settings - Fork 5
/
make2help.go
50 lines (44 loc) · 927 Bytes
/
make2help.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
package make2help
import (
"bufio"
"os"
"regexp"
"strings"
"golang.org/x/xerrors"
)
var (
ruleReg = regexp.MustCompile(`^(\S+):`)
builtinTargetReg = regexp.MustCompile(`^\.[A-Z_]{5,}`) // ex. ".PHONY"
)
func scan(filepath string) (rules, error) {
f, err := os.Open(filepath)
if err != nil {
return nil, xerrors.Errorf("failed to open file: %w", err)
}
r := rules{}
buf := []string{}
sc := bufio.NewScanner(f)
for sc.Scan() {
line := sc.Text()
if strings.HasPrefix(line, "## ") {
buf = append(buf, line[3:])
continue
}
if matches := ruleReg.FindStringSubmatch(line); len(matches) > 1 {
target := matches[1]
if target == ".PHONY" {
continue
}
if !builtinTargetReg.MatchString(target) {
r[target] = buf
}
}
if len(buf) > 0 {
buf = []string{}
}
}
if err = sc.Err(); err != nil {
return nil, xerrors.Errorf("scan failed: %w", err)
}
return r, nil
}