-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (140 loc) · 3 KB
/
main.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
154
155
156
157
package main
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"strings"
"github.com/willf/pad"
"gopkg.in/alecthomas/kingpin.v2"
)
const version = "0.1.0"
const targetRegex = `^((?P<name>(-|_|\w)+)|(\${(?P<varname>(-|_|\w)+)}))\s*:.*\#\#+\s*@(?P<category>(\w+))\s+(?P<doc>(.*))`
const varRegex = `^(?P<name>(\w)+)\s*(\?)?=\s*(?P<def>([^\#]+))(\s+\#\#+\s*@(?P<category>(\w+))(:)?\s+(?P<doc>(.*))|\s*$)`
var (
parseVars = kingpin.Flag("variables", "optional flag to parse variables").Bool()
makefiles = kingpin.Arg("makefiles", "paths to makefiles").Required().ExistingFiles()
_ = kingpin.Version(version)
variables = VarInfo{}
varRe = regexp.MustCompile(varRegex)
targets = MakeTargets{}
vargets = MakeTargets{}
targetRe = regexp.MustCompile(targetRegex)
)
type VarInfo map[string]string
type MakeTarget struct {
name string
cat string
doc string
def string
isVar bool
}
type MakeTargets map[string][]MakeTarget
func parseLine(line string, re *regexp.Regexp) (target MakeTarget) {
if !re.MatchString(line) {
return target
}
var cat string
var def string
var doc string
var name string
var isVar bool
matches := re.FindStringSubmatch(line)
for i, n := range re.SubexpNames() {
switch n {
case "name":
if name == "" {
name = matches[i]
isVar = false
}
case "varname":
if name == "" {
name = matches[i]
isVar = true
}
case "def":
def = strings.TrimSpace(matches[i])
case "doc":
doc = strings.TrimSpace(matches[i])
case "category":
cat = matches[i]
}
}
if name != "" {
_, ok := variables[name]
if !ok {
variables[name] = def
}
}
return MakeTarget{
name: name,
cat: cat,
doc: doc,
def: def,
isVar: isVar,
}
}
func parseTargets(path string) {
file, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
target := parseLine(line, targetRe)
if target.cat != "" {
targets[target.cat] = append(targets[target.cat], target)
}
varget := parseLine(line, varRe)
if varget.cat != "" {
vargets[varget.cat] = append(vargets[varget.cat], varget)
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}
func subVars() {
for _, rules := range targets {
for _, rule := range rules {
rule.name = variables[rule.name]
}
}
}
func printHelp(cats MakeTargets) {
var colSize int
for _, rules := range cats {
for _, rule := range rules {
l := len(rule.name)
if l > colSize {
colSize = l
}
}
}
for cat, rules := range cats {
fmt.Printf("\n%s:\n", cat)
for _, rule := range rules {
defVal := rule.def
if defVal != "" {
defVal = " Default : " + defVal
}
fmt.Printf("\t%s\t%s.%s\n", pad.Right(rule.name, colSize, " "), rule.doc, defVal)
}
}
}
func main() {
kingpin.Parse()
for _, m := range *makefiles {
parseTargets(m)
}
if *parseVars {
printHelp(vargets)
} else {
subVars()
fmt.Println("Usage: make [target] [VARIABLE=value]")
printHelp(targets)
}
}