This repository has been archived by the owner on Mar 8, 2020. It is now read-only.
/
sdk_update.go
312 lines (260 loc) · 6.78 KB
/
sdk_update.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package build
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"text/template"
"unicode"
"unicode/utf8"
"gopkg.in/bblfsh/sdk.v2/assets/skeleton"
"gopkg.in/bblfsh/sdk.v2/driver/manifest"
)
const (
tplExt = ".tpl"
manifestTpl = manifest.Filename + tplExt
)
func genEnvBool(key string) bool {
str := os.Getenv(key)
if str == "" {
return false
}
v, err := strconv.ParseBool(str)
if err != nil {
panic(fmt.Errorf("invalid value %q for environment flag %q: %v", str, key, err))
}
return v
}
var overwriteManagedFiles = genEnvBool("BABELFISH_OVERWRITE_MANAGED")
// managedFiles are files that always are overwritten
var managedFiles = map[string]bool{
".travis.yml": true,
"Makefile": true,
"update.go": true,
"build.go": true,
"test.go": true,
"README.md" + tplExt: true,
"LICENSE": true,
"driver/main.go" + tplExt: true,
"driver/sdk_test.go": true,
"driver/normalizer/transforms.go" + tplExt: true,
}
type updater struct {
root string
opt UpdateOptions
context map[string]interface{}
changes int
}
// PrintfFunc is a logging function type similar to log.Printf.
type PrintfFunc func(format string, args ...interface{}) (int, error)
func (f PrintfFunc) printf(msg string, args ...interface{}) {
if f == nil {
return
}
_, _ = f(msg, args...)
}
func mustAssetInfo(name string) os.FileInfo {
fi, err := skeleton.AssetInfo(name)
if err != nil {
panic(fmt.Errorf("missing asset info for %q: %v", name, err))
}
return fi
}
// UpdateOptions is a set of options available for
type UpdateOptions struct {
DryRun bool
Debug PrintfFunc
Notice PrintfFunc
Warning PrintfFunc
}
// ErrChangesRequired is returned by UpdateSDK in DryRun mode when changes are required.
var ErrChangesRequired = errors.New("changes are required")
// GenerateManifest writes the manifest file to a root driver directory.
func GenerateManifest(root, language string) error {
tpl := string(skeleton.MustAsset(manifestTpl))
t, err := template.New(manifestTpl).Funcs(funcs).Parse(tpl)
if err != nil {
return err
}
name := fixGitFolder(manifestTpl)
file := filepath.Join(root, manifest.Filename)
info := mustAssetInfo(name)
f, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_EXCL, info.Mode())
if err != nil {
return err
}
if err := t.Execute(f, map[string]string{
"Language": language,
}); err != nil {
_ = f.Close()
return err
}
return f.Close()
}
// UpdateSDK updates SDK-managed files for the driver located at root.
//
// If DryRun option is set, the function would not update any files, and instead will
// return ErrChangesRequired if there are any changes required.
func UpdateSDK(root string, opt *UpdateOptions) error {
if opt == nil {
opt = &UpdateOptions{}
}
m, err := manifest.Load(filepath.Join(root, manifest.Filename))
if err != nil {
return err
}
c := &updater{
root: root, opt: *opt,
context: map[string]interface{}{
"Manifest": m,
},
}
for _, file := range skeleton.AssetNames() {
if err := c.processAsset(file); err != nil {
return err
}
}
d, err := NewDriver(root)
if err != nil {
return err
}
var changed bool
if c.opt.DryRun {
changed, err = d.ScriptChanged()
} else {
changed, err = d.Prepare()
}
if err != nil {
return err
} else if changed {
c.notifyChangedFile(ScriptName)
}
if c.opt.DryRun && c.changes > 0 {
return ErrChangesRequired
}
return nil
}
func (c *updater) processAsset(name string) error {
if name == manifestTpl {
// manifest is always managed by the driver developer
// the template is only for the driver init
return nil
}
overwrite := managedFiles[name] && !overwriteManagedFiles
if strings.HasSuffix(name, tplExt) {
return c.processTemplateAsset(name, c.context, overwrite)
}
return c.processFileAsset(name, overwrite)
}
func (c *updater) processFileAsset(name string, overwrite bool) error {
content := skeleton.MustAsset(name)
info := mustAssetInfo(name)
name = fixGitFolder(name)
return c.writeIfChanged(filepath.Join(c.root, name), content, info.Mode(), overwrite)
}
var funcs = map[string]interface{}{
"escape_shield": escapeShield,
"expName": toExportedName,
}
func (c *updater) processTemplateAsset(name string, v interface{}, overwrite bool) error {
tpl := string(skeleton.MustAsset(name))
t, err := template.New(name).Funcs(funcs).Parse(tpl)
if err != nil {
return err
}
name = fixGitFolder(name)
file := filepath.Join(c.root, name[:len(name)-len(tplExt)])
buf := bytes.NewBuffer(nil)
if err := t.Execute(buf, v); err != nil {
return err
}
info := mustAssetInfo(name)
return c.writeIfChanged(file, buf.Bytes(), info.Mode(), overwrite)
}
func (c *updater) writeIfChanged(file string, content []byte, m os.FileMode, overwrite bool) error {
f, err := os.Open(file)
if os.IsNotExist(err) {
c.notifyMissingFile(file)
return c.writeFile(file, content, m)
} else if err != nil {
return err
} else if !overwrite {
return nil
}
original, err := ioutil.ReadAll(f)
if err != nil {
return err
}
if bytes.Compare(original, content) == 0 {
return nil
}
c.notifyChangedFile(file)
return c.writeFile(file, content, m)
}
func (c *updater) writeFile(file string, content []byte, m os.FileMode) error {
if c.opt.DryRun {
return nil
}
if err := os.MkdirAll(filepath.Dir(file), 0755); err != nil {
return err
}
err := ioutil.WriteFile(file, content, m.Perm())
if err != nil {
return err
}
if c.opt.Debug != nil {
c.opt.Debug.printf("file %q has been written\n", file)
}
rel, err := filepath.Rel(c.root, file)
if err != nil {
return err
}
if !strings.HasPrefix(rel, ".git"+string(filepath.Separator)) {
git := exec.Command("git", "add", rel)
git.Dir = c.root
if out, err := git.CombinedOutput(); err != nil {
c.opt.Warning.printf("cannot add a file to git: %v\n%s", err, string(out))
}
}
return nil
}
func (c *updater) notifyMissingFile(file string) {
if isDotGit(file) {
return
}
if !c.opt.DryRun {
c.opt.Notice.printf("creating file %q\n", file)
return
}
c.changes++
c.opt.Warning.printf("missing file %q\n", file)
}
func (c *updater) notifyChangedFile(file string) {
if !c.opt.DryRun {
c.opt.Warning.printf("managed file %q has changed, overriding changes\n", file)
return
}
c.changes++
c.opt.Warning.printf("managed file changed %q\n", file)
}
func escapeShield(text interface{}) string {
return strings.Replace(fmt.Sprintf("%s", text), "-", "--", -1)
}
func toExportedName(s string) string {
r, n := utf8.DecodeRuneInString(s)
if n == 0 {
return s
}
return string(unicode.ToUpper(r)) + s[n:]
}
func fixGitFolder(path string) string {
return strings.Replace(path, "git/", ".git/", 1)
}
func isDotGit(path string) bool {
return strings.Contains(path, ".git/")
}