This repository has been archived by the owner on Feb 24, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 575
/
archived_assets.go
104 lines (83 loc) · 2.02 KB
/
archived_assets.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
package build
import (
"archive/zip"
"bytes"
"io"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
func (b *Builder) buildExtractedAssets() error {
err := b.buildAssetsArchive()
if err != nil {
return errors.WithStack(err)
}
return b.disableAssetsHandling()
}
func (b *Builder) disableAssetsHandling() error {
logrus.Debug("disable asset handling in binary")
b.transform("actions/app.go", func(body []byte, w io.Writer) error {
body = bytes.Replace(body, []byte("app.ServeFiles(\"/assets\""), []byte("//app.ServeFiles(\"/assets\""), 1)
_, err := w.Write(body)
if err != nil {
return errors.WithStack(err)
}
return nil
})
return nil
}
func (b *Builder) buildAssetsArchive() error {
outputDir := filepath.Dir(filepath.Join(b.Root, b.Bin))
target := filepath.Join(outputDir, "assets.zip")
source := filepath.Join(b.Root, "public", "assets")
logrus.Debugf("building assets archive to %s", target)
zipfile, err := os.Create(target)
if err != nil {
return errors.WithStack(err)
}
defer zipfile.Close()
archive := zip.NewWriter(zipfile)
defer archive.Close()
info, err := os.Stat(source)
if err != nil {
return errors.WithStack(err)
}
var baseDir string
if info.IsDir() {
baseDir = filepath.Base(source)
}
filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
if err != nil {
return errors.WithStack(err)
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return errors.WithStack(err)
}
if baseDir != "" {
header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, source))
}
if info.IsDir() {
header.Name += "/"
} else {
header.Method = zip.Deflate
}
writer, err := archive.CreateHeader(header)
if err != nil {
return errors.WithStack(err)
}
if info.IsDir() {
return nil
}
file, err := os.Open(path)
if err != nil {
return errors.WithStack(err)
}
defer file.Close()
_, err = io.Copy(writer, file)
return errors.WithStack(err)
})
return errors.WithStack(err)
}