-
Notifications
You must be signed in to change notification settings - Fork 402
/
code.go
80 lines (67 loc) · 1.84 KB
/
code.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
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package asset
import (
"bytes"
"fmt"
)
// InmemoryCode generates a function closure []byte that can be assigned to a variable.
func (asset *Asset) InmemoryCode() []byte {
var source bytes.Buffer
fmt.Fprintf(&source, "func() *asset.InmemoryFileSystem {\n")
blob := []byte{}
blobMapping := map[*Asset][2]int{}
var writeBlob func(asset *Asset)
writeBlob = func(asset *Asset) {
if !asset.Mode.IsDir() {
start := len(blob)
blob = append(blob, asset.Data...)
finish := len(blob)
blobMapping[asset] = [2]int{start, finish}
return
}
for _, child := range asset.Children {
writeBlob(child)
}
}
writeBlob(asset)
fmt.Fprintf(&source, "const blob = ")
const lineLength = 120
for len(blob) > 0 {
if lineLength < len(blob) {
fmt.Fprintf(&source, "\t%q +\n", string(blob[:lineLength]))
blob = blob[lineLength:]
continue
}
fmt.Fprintf(&source, "\t%q\n", string(blob))
break
}
var writeAsset func(asset *Asset)
writeAsset = func(asset *Asset) {
fmt.Fprintf(&source, "{")
defer fmt.Fprintf(&source, "}")
if asset.Mode.IsDir() {
fmt.Fprintf(&source, "\n")
}
fmt.Fprintf(&source, "Name: %q,", asset.Name)
fmt.Fprintf(&source, "Mode: 0%o,", asset.Mode)
fmt.Fprintf(&source, "ModTime: time.Unix(%d, 0),", asset.ModTime.Unix())
if !asset.Mode.IsDir() {
r := blobMapping[asset]
fmt.Fprintf(&source, "Data: []byte(blob[%d:%d])", r[0], r[1])
return
}
fmt.Fprintf(&source, "\nChildren: []*asset.Asset{\n")
for _, child := range asset.Children {
writeAsset(child)
fmt.Fprintf(&source, ",\n")
}
fmt.Fprintf(&source, "},\n")
}
fmt.Fprintf(&source, "\n")
fmt.Fprintf(&source, "return asset.Inmemory(&asset.Asset")
writeAsset(asset)
fmt.Fprintf(&source, ")\n")
fmt.Fprintf(&source, "}()\n")
return source.Bytes()
}