This repository was archived by the owner on Nov 8, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparser.go
More file actions
209 lines (174 loc) · 4.88 KB
/
parser.go
File metadata and controls
209 lines (174 loc) · 4.88 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package bake
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"github.com/Shopify/go-lua"
"github.com/flynn/bake/assets"
)
//go:generate go-bindata -ignore=assets/assets.go -o assets/assets.go -pkg assets -prefix assets/ assets
// Parser represents a recursive directory parser.
type Parser struct {
state *lua.State
target *Target // current target being built
base string // root directory
path string // working directory passed to targets
// Package being built by the parser.
// It is incrementally added to on every parse.
Package *Package
}
// NewParser returns a new instance of Parser.
func NewParser() *Parser {
p := &Parser{
state: lua.NewState(),
Package: &Package{},
}
p.init()
return p
}
// ParseDir recursively parses all bakefiles in a directory tree.
// If a directory contains a Bakefile then it is parsed and added to the package.
// All targets and their names are relative from path.
func (p *Parser) ParseDir(path string) error {
return p.parseDir(path, "")
}
func (p *Parser) parseDir(base, path string) error {
// Parse Bakefile in this directory first.
if err := p.parseFile(base, filepath.Join(path, "Bakefile.lua")); os.IsNotExist(err) {
// nop
} else if err != nil {
return err
}
// Read all files in path.
fis, err := readdir(filepath.Join(base, path))
if err != nil {
return err
}
// Recursively parse each directory.
for _, fi := range fis {
if !fi.IsDir() {
continue
}
if err := p.parseDir(base, filepath.Join(path, fi.Name())); err != nil {
return err
}
}
return nil
}
// parseFile parses a file at path.
func (p *Parser) parseFile(base, path string) error {
// Open file for reading.
f, err := os.Open(filepath.Join(base, path))
if err != nil {
return err
}
defer f.Close()
// Set the current working directory for all targets created.
// Reset after parsing file or on error.
p.base, p.path = base, filepath.Dir(path)
defer func() { p.base, p.path = "", "" }()
// Load script into state.
if err := p.state.Load(f, path, ""); err != nil {
return fmt.Errorf("%s: %s", path, err)
}
// Execute script.
if err := p.state.ProtectedCall(0, 0, 0); err != nil {
return err
}
return nil
}
func (p *Parser) init() {
// Import standard library.
lua.OpenLibraries(p.state)
// Load bake libraries.
for _, name := range []string{
"shim.lua", // intermediate layer to go-lua
"bake.lua",
"docker.lua",
"git.lua",
"go.lua",
} {
if err := lua.LoadBuffer(p.state, string(assets.MustAsset(name)), name, ""); err != nil {
panic(err)
}
p.state.Call(0, 0)
}
// Add built-in functions.
p.state.Register("__bake_begin_target", p.beginTarget)
p.state.Register("__bake_end_target", p.endTarget)
p.state.Register("__bake_set_title", p.setTitle)
p.state.Register("exec", p.exec)
p.state.Register("sh", p.sh)
p.state.Register("depends", p.depends)
}
// beginTarget initializes a target on the package.
func (p *Parser) beginTarget(l *lua.State) int {
name := lua.CheckString(l, 1)
dependencies, _ := l.ToUserData(2).(luaDependencies)
// Mark as "phony" if it starts with an at-sign.
var phony bool
if strings.HasPrefix(name, "@") {
name = strings.TrimPrefix(name, "@")
phony = true
}
p.target = &Target{
Name: path.Join(p.path, name),
Phony: phony,
WorkDir: p.path,
Dependencies: make([]string, len(dependencies)),
}
// Copy dependencies with prepended path.
for i := range dependencies {
p.target.Dependencies[i] = path.Join(p.path, dependencies[i])
}
return 0
}
// endTarget finalizes the current target and adds it to the package.
func (p *Parser) endTarget(l *lua.State) int {
p.Package.Targets = append(p.Package.Targets, p.target)
p.target = nil
return 0
}
// exec appends an "exec" command to the current target.
func (p *Parser) exec(l *lua.State) int {
cmd := &ExecCommand{}
for i, n := 1, l.Top(); i <= n; i++ {
cmd.Args = append(cmd.Args, lua.CheckString(l, i))
}
p.target.Commands = append(p.target.Commands, cmd)
return 0
}
// sh appends an "shell" command to the current target.
func (p *Parser) sh(l *lua.State) int {
p.target.Commands = append(p.target.Commands, &ShellCommand{
Source: lua.CheckString(l, 1),
})
return 0
}
// setTitle sets the title shown to users for the current target.
func (p *Parser) setTitle(l *lua.State) int {
p.target.Title = lua.CheckString(l, 1)
return 0
}
// depends returns a list of strings as dependencies.
func (p *Parser) depends(l *lua.State) int {
dependencies := make(luaDependencies, 0)
for i, n := 1, l.Top(); i <= n; i++ {
dependencies = append(dependencies, lua.CheckString(l, i))
}
l.PushUserData(dependencies)
return 1
}
// luaDependencies represents a list of dependency names.
type luaDependencies []string
// readdir returns a slice of all files in path.
func readdir(path string) ([]os.FileInfo, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
return f.Readdir(0)
}