-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.go
133 lines (119 loc) · 2.21 KB
/
build.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
package building
import (
"flag"
"fmt"
"sort"
"strings"
"sync"
"time"
)
type B struct {
root string
targets map[string]target
defaultTarget *target
tools map[string]Tool
helpers map[string]bool
mutex sync.Mutex
}
type target struct {
name string
description string
f func(*B)
}
var b *B
func Init(pkgName string) *B {
if b != nil {
panic("build.Init(...) called twice")
}
b = &B{
root: pkgName,
targets: make(map[string]target),
tools: make(map[string]Tool),
}
return b
}
func Builder() *B {
if b == nil {
panic("missing building.Init(...)")
}
return b
}
func (b *B) MakeTarget(name, description string, f func(*B)) target {
t := target{
name: name,
description: description,
f: f,
}
b.targets[name] = t
if b.defaultTarget == nil {
b.defaultTarget = &t
}
return t
}
func (b *B) Build(t target) {
b.Println(">", t.name)
start := time.Now()
t.f(b)
delta := time.Now().Sub(start)
b.Printf("< %s (took %s)", t.name, delta)
}
func (b *B) printTargets() {
fmt.Printf("\nTargets:\n")
align := 6
sorted := sort.StringSlice{}
for _, t := range b.targets {
sorted = append(sorted, t.name)
}
sorted.Sort()
for _, s := range sorted {
t := b.targets[s]
lf := ""
spaces := align - len(t.name)
if spaces <= 0 {
if len(t.description) > 0 {
lf = "\n"
}
spaces = align + 2
}
fmt.Printf(" %s%s%s\n", t.name, lf+strings.Repeat(" ", spaces), t.description)
}
}
func (b *B) Run() {
flag.Usage = func() {
fmt.Print(`Usage: build [OPTIONS] [TARGETS]
Options:
`)
flag.PrintDefaults()
b.printTargets()
}
*Quiet = false
flag.Parse()
var runs []target
args := flag.Args()
if len(args) == 0 {
if b.defaultTarget == nil {
b.Fatal("no target defined")
}
runs = append(runs, *b.defaultTarget)
}
for _, a := range args {
if t, ok := b.targets[a]; ok {
runs = append(runs, t)
} else {
b.Fatalln("invalid target", a)
}
}
b.Print("build started")
start := time.Now()
for _, t := range runs {
b.Build(t)
}
delta := time.Now().Sub(start)
b.Printf("build finished (took %s)", delta)
}
func (b *B) Exe(os string) string {
if os == "windows" {
return ".exe"
}
return ""
}