/
bundle.go
367 lines (303 loc) · 9.74 KB
/
bundle.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package syntax
import (
"archive/zip"
"bytes"
"context"
"errors"
"fmt"
"io"
"path"
"path/filepath"
"regexp"
"runtime"
"strings"
"github.com/arr-ai/arrai/pkg/ctxfs"
"github.com/arr-ai/arrai/rel"
"github.com/anz-bank/pkg/mod"
"github.com/spf13/afero"
"github.com/spf13/afero/zipfs"
)
const windows = "windows"
type bundleConfig struct {
mainRoot, mainFile string
// absRootPath is only meant for bundling, to trim absolute filepaths.
// It is not stored to the config file or used during running bundled
// scripts.
absRootPath string
}
// module that is currently being imported by a script
type moduleData struct {
// moduleName is the name of the go module e.h. github.com/arr-ai/arrai
moduleName string
// modulePath is the location of the module. Modules are cached by go mod
// e.g. /go/pkg/mod/github.com/arr-ai/arrai@v0.200.0/
modulePath string
}
type bundleKey int
const (
// ModuleDir contains all the module files in a bundled scripts.
ModuleDir = "/module"
// NoModuleDir contains all the module files without root in a bundled scripts.
NoModuleDir = "/unnamed"
// BundleConfig contains configurations to run a bundled scripts.
BundleConfig = "/config.arrai"
arraiExt = ".arrai"
// random name for scripts without modules. This value is used if a script with
// no module is compiled into binary.
unnamedModule = "unnamed.com/unnamed/unnamed"
bundleFsKey bundleKey = iota
bundleConfKey
runBundleMode
currentModule
)
var (
rootModuleRE = regexp.MustCompile("^module ([^\n]+)\n")
errSentinelHasNoModule = errors.New("sentinel does not show module path")
)
func (b bundleConfig) String() string {
return fmt.Sprintf("(main_root: %q, main_file: %q)", b.mainRoot, b.mainFile)
}
func createConfig(ctx context.Context) error {
return ctxfs.ZipCreate(
ctx, bundleFsKey,
BundleConfig, []byte(fromBundleConfig(ctx).String()),
)
}
// WithBundleRun adds necessary values to the context to allow running bundled arrai scripts.
func WithBundleRun(ctx context.Context, buf []byte) (context.Context, error) {
r := bytes.NewReader(buf)
z, err := zip.NewReader(r, r.Size())
if err != nil {
return ctx, err
}
return context.WithValue(ctxfs.SourceFsOnto(ctx, zipfs.New(z)), runBundleMode, true), nil
}
// GetMainBundleSource gets the path for the main arrai script in the bundled arrai scripts.
func GetMainBundleSource(ctx context.Context) (context.Context, []byte, string) {
if !isRunningBundle(ctx) {
return ctx, nil, ""
}
ctx = withBundledConfig(ctx)
mainFile := bundleToValidPath(ctx, fromBundleConfig(ctx).mainFile)
fs := ctxfs.SourceFsFrom(ctx)
buf, err := afero.ReadFile(fs, mainFile)
if err != nil {
panic(fmt.Errorf("not bundled properly, main file not accessible: %s", err))
}
return ctx, buf, mainFile
}
// GetModuleFromBundle fetches the module path of the bundle from the bundle's buffer.
func GetModuleFromBundle(ctx context.Context, buf []byte) (context.Context, string, error) {
ctx, err := WithBundleRun(ctx, buf)
if err != nil {
return ctx, "", err
}
ctx = withBundledConfig(ctx)
return ctx, fromBundleConfig(ctx).mainRoot, nil
}
// OutputArraiz writes the zip binary to the provided writer.
func OutputArraiz(ctx context.Context, w io.Writer) error {
if !isBundling(ctx) {
return errors.New("cannot output bundled arrai because it is not bundling")
}
return ctxfs.OutputZip(ctx, bundleFsKey, w)
}
// withBundledConfig is used add bundled scripts configuration to the context.
func withBundledConfig(ctx context.Context) context.Context {
if !isRunningBundle(ctx) {
//FIXME: return error?
return ctx
}
buf, err := afero.ReadFile(ctxfs.SourceFsFrom(ctx), BundleConfig)
if err != nil {
// config file not generated
panic(err)
}
expr, err := Compile(ctx, BundleConfig, string(buf))
if err != nil {
// config file not generated properly
panic(err)
}
val, err := expr.Eval(ctx, rel.EmptyScope)
if err != nil {
panic(err)
}
t := val.(rel.Tuple)
root := t.MustGet("main_root").String()
if root == "{}" {
root = unnamedModule
}
root = ctxfs.ToUnixPath(root)
return context.WithValue(ctx, bundleConfKey, bundleConfig{
mainRoot: root,
mainFile: t.MustGet("main_file").String(),
})
}
func isRunningBundle(ctx context.Context) bool {
return ctx.Value(runBundleMode) != nil
}
func withBundleConfig(ctx context.Context, b bundleConfig) context.Context {
return context.WithValue(ctx, bundleConfKey, b)
}
// fromBundleConfig is meant to be used for fetching configurations during bundling.
func fromBundleConfig(ctx context.Context) bundleConfig {
return ctx.Value(bundleConfKey).(bundleConfig)
}
func isBundling(ctx context.Context) bool {
return ctx.Value(bundleFsKey) != nil
}
// SetupBundle adds necessary values to the context for bundling arrai scripts.
func SetupBundle(ctx context.Context, filePath string, source []byte) (_ context.Context, err error) {
ctx = ctxfs.WithZipFs(ctx, bundleFsKey)
filePath, err = filepath.Abs(filePath)
if err != nil {
return ctx, err
}
root, err := findRootFromModule(ctx, filepath.Dir(filePath))
if err != nil {
if err != errModuleNotExist {
return ctx, err
}
mainFile := path.Join(NoModuleDir, filepath.Base(filePath))
if err = ctxfs.ZipCreate(ctx, bundleFsKey, mainFile, source); err != nil {
return ctx, err
}
ctx = withBundleConfig(ctx, bundleConfig{mainFile: mainFile, absRootPath: filepath.Dir(filePath)})
return ctx, createConfig(ctx)
}
buf, err := afero.ReadFile(ctxfs.SourceFsFrom(ctx), filepath.Join(root, ModuleRootSentinel))
if err != nil {
return ctx, err
}
moduleRoot := rootModuleRE.FindStringSubmatch(string(buf))
if moduleRoot == nil {
//TODO: maybe treat it as no module?
return ctx, errSentinelHasNoModule
}
err = ctxfs.ZipCreate(ctx, bundleFsKey, path.Join(ModuleDir, moduleRoot[1], ModuleRootSentinel), buf)
if err != nil {
return ctx, err
}
filePathRelativeToSentinel := ctxfs.ToUnixPath(strings.TrimPrefix(filePath, root))
mainPath := path.Join(ModuleDir, moduleRoot[1], filePathRelativeToSentinel)
if err = ctxfs.ZipCreate(ctx, bundleFsKey, mainPath, source); err != nil {
return ctx, err
}
ctx = withBundleConfig(ctx, bundleConfig{moduleRoot[1], mainPath, root})
return ctx, createConfig(ctx)
}
func addModuleSentinel(ctx context.Context, rootPath string) (err error) {
if !isBundling(ctx) {
return nil
}
rootPath, err = filepath.Abs(rootPath)
if err != nil {
return err
}
rootPath = filepath.Join(rootPath, ModuleRootSentinel)
buf, err := afero.ReadFile(ctxfs.SourceFsFrom(ctx), rootPath)
if err != nil {
return err
}
var sentinelLocation string
if isImportModule(ctx) {
sentinelLocation, err = createModulePath(ctx, rootPath)
if err != nil {
return err
}
} else {
rootPath = strings.TrimPrefix(rootPath, fromBundleConfig(ctx).absRootPath)
sentinelLocation = path.Join(fromBundleConfig(ctx).mainRoot, rootPath)
}
pathInBundle := path.Join(ModuleDir, sentinelLocation)
if exists, err := ctxfs.FileExists(ctx, bundleFsKey, pathInBundle); err != nil {
return err
} else if exists {
return nil
}
return ctxfs.ZipCreate(ctx, bundleFsKey, pathInBundle, buf)
}
func createModulePath(ctx context.Context, filePath string) (modulePath string, err error) {
if !filepath.IsAbs(filePath) {
return "", fmt.Errorf("filepath is not absolute: %s", filePath)
}
filePath = ctxfs.ToUnixPath(filePath)
currModule := getCurrentModule(ctx)
relPath, err := filepath.Rel(currModule.modulePath, filePath)
if err != nil {
return "", err
}
return path.Join(currModule.moduleName, ctxfs.ToUnixPath(relPath)), nil
}
func bundleLocalFile(ctx context.Context, filePath string) (err error) {
if !isBundling(ctx) {
return nil
}
filePath, err = filepath.Abs(filePath)
if err != nil {
return err
}
//FIXME: not very clean
if filepath.Ext(filePath) == "" {
filePath += arraiExt
}
source, err := afero.ReadFile(ctxfs.SourceFsFrom(ctx), filePath)
if err != nil {
return err
}
var dir string
if isImportModule(ctx) {
dir = ModuleDir
filePath, err = createModulePath(ctx, filePath)
if err != nil {
return err
}
} else {
config := fromBundleConfig(ctx)
if config.mainRoot != "" {
dir = ModuleDir
filePath = path.Join(config.mainRoot, ctxfs.ToUnixPath(strings.TrimPrefix(filePath, config.absRootPath)))
} else {
dir = NoModuleDir
filePath = ctxfs.ToUnixPath(strings.TrimPrefix(filePath, config.absRootPath))
}
}
return ctxfs.ZipCreate(ctx, bundleFsKey, path.Join(dir, filePath), source)
}
func bundleModule(ctx context.Context, relImportPath string, m *mod.Module) (context.Context, error) {
if !isBundling(ctx) {
return ctx, nil
}
if filepath.Ext(relImportPath) == "" {
relImportPath += arraiExt
}
source, err := afero.ReadFile(ctxfs.SourceFsFrom(ctx), filepath.Join(m.Dir, relImportPath))
if err != nil {
return ctx, err
}
ctx = context.WithValue(ctx, currentModule, moduleData{ctxfs.ToUnixPath(m.Name), ctxfs.ToUnixPath(m.Dir)})
return ctx, ctxfs.ZipCreate(ctx, bundleFsKey, path.Join(ModuleDir, m.Name, relImportPath), source)
}
func isImportModule(ctx context.Context) bool {
return ctx.Value(currentModule) != nil
}
func getCurrentModule(ctx context.Context) moduleData {
return ctx.Value(currentModule).(moduleData)
}
func bundleRemoteFile(ctx context.Context, url string, source []byte) error {
if !isBundling(ctx) {
return nil
}
//TODO: test with deep imports
url = strings.TrimPrefix(strings.TrimPrefix(url, "https://"), "http://")
return ctxfs.ZipCreate(ctx, bundleFsKey, path.Join(ModuleDir, url), source)
}
// since config file uses UNIX path, they need to be converted to windows path
// (without volume) to work with afero zipfs.
func bundleToValidPath(ctx context.Context, p string) string {
if runtime.GOOS == windows && isRunningBundle(ctx) {
p = strings.TrimPrefix(p, filepath.VolumeName(p))
return strings.ReplaceAll(p, "/", "\\")
}
return p
}