This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
assembly.go
115 lines (105 loc) · 2.09 KB
/
assembly.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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/Unknwon/cae/zip"
"github.com/codeskyblue/gobuild/utils"
"github.com/codeskyblue/goyaml"
"github.com/qiniu/log"
)
type FileSet struct {
Includes []string `yaml:"includes"`
Excludes []string `yaml:"excludes"`
}
type Assembly struct {
Framework string `yaml:"framework"`
FileSet `yaml:"filesets"`
}
// basic regrex match
func match(bre string, str string) bool {
if bre == str { // FIXME: use re
return true
}
return false
}
func pkgZip(root string, files []string) (path string, err error) {
log.Info("package to zip:", path)
tmpFile, err := utils.TempFile("files", "tmp-", "-"+filepath.Base(root)+".zip")
if err != nil {
return
}
z, err := zip.Create(tmpFile)
if err != nil {
return
}
for _, f := range files {
var save string
if f == "" {
continue
}
// binary file use abspath
//fmt.Println(root, f)
if strings.HasSuffix(f, root) {
save = f[len(root):]
} else {
save = filepath.Base(f)
}
info, er := os.Stat(f)
if er != nil {
continue
}
log.Debug("add", save, f)
if info.IsDir() {
if err = z.AddDir(save, f); err != nil {
return
}
} else {
if err = z.AddFile(save, f); err != nil {
return
}
}
}
if err = z.Close(); err != nil {
log.Error(err)
return
}
return tmpFile, nil
}
func (b *Builder) pack(bins []string, rcfile string) (path string, err error) {
log.Debug(bins)
log.Debug(rcfile)
data, err := ioutil.ReadFile(rcfile)
if err != nil {
log.Debug("use default rc")
data, err = ioutil.ReadFile("public/gobuildrc")
if err != nil {
log.Error(err)
}
}
ass := new(Assembly)
err = goyaml.Unmarshal(data, ass)
if err != nil {
return
}
dir := filepath.Dir(rcfile)
fs, err := ioutil.ReadDir(dir)
if err != nil {
return
}
var includes = bins // this may change slice bins
for _, f := range fs {
var ok = false
for _, patten := range ass.Includes {
if match(patten, f.Name()) {
ok = true
break
}
}
if ok {
includes = append(includes, filepath.Join(dir, f.Name()))
}
}
return pkgZip(dir, includes)
}