forked from gobuild/gopack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pack.go
211 lines (190 loc) · 4.66 KB
/
pack.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
package main
import (
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"github.com/codegangsta/cli"
sh "github.com/codeskyblue/go-sh"
"github.com/gobuild/log"
)
func shExecString(sess *sh.Session, command string) error {
if runtime.GOOS == "windows" {
return sess.Command("cmd", "/c", command).Run()
}
return sess.Command("bash", "-c", command).Run()
}
func findFiles(path string, depth int, skips []*regexp.Regexp) ([]string, error) {
baseNumSeps := strings.Count(path, string(os.PathSeparator))
var files []string
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Debugf("filewalk: %s", err)
return nil
}
if info.IsDir() {
pathDepth := strings.Count(path, string(os.PathSeparator)) - baseNumSeps
//log.Println("Skip", info, pathDepth, depth)
if pathDepth > depth {
return filepath.SkipDir
}
}
name := info.Name()
isSkip := false
for _, skip := range skips {
if skip.MatchString(name) || skip.MatchString(path) {
isSkip = true
break
}
}
if isSkip {
log.Debug("skip:", path)
}
if !isSkip {
files = append(files, path)
}
if isSkip && info.IsDir() {
return filepath.SkipDir
}
return nil
})
return files, err
}
func actionPack(c *cli.Context) {
var goos, goarch = c.String("os"), c.String("arch")
//var depth = c.Int("depth")
var output = c.String("output")
var gom = c.String("gom")
var nobuild = c.Bool("nobuild")
var adds = c.StringSlice("add")
var rmflag = c.Bool("rm")
if c.GlobalBool("debug") {
log.SetOutputLevel(log.Ldebug)
}
if c.Bool("quiet") {
log.SetOutputLevel(log.Lwarn)
}
log.Debugf("os: %s, arch: %s", goos, goarch)
var err error
defer func() {
if err != nil {
log.Fatal(err)
}
}()
sess := sh.NewSession()
sess.SetEnv("GOOS", goos)
sess.SetEnv("GOARCH", goarch)
sess.ShowCMD = !c.Bool("quiet")
gomarr := strings.Fields(gom)
if len(gomarr) >= 1 {
sess.Alias("go", gomarr[0], gomarr[1:]...)
}
// parse yaml
pcfg := DefaultPcfg
log.Debug("config:", pcfg)
var skips []*regexp.Regexp
for _, str := range pcfg.Excludes {
skips = append(skips, regexp.MustCompile("^"+str+"$"))
}
var needs []*regexp.Regexp
for _, str := range pcfg.Includes {
needs = append(needs, regexp.MustCompile("^"+str+"$"))
}
os.MkdirAll(filepath.Dir(output), 0755)
var z Archiver
hasExt := func(ext string) bool { return strings.HasSuffix(output, ext) }
switch {
case hasExt(".zip"):
log.Println("zip format")
z, err = CreateZip(output)
case hasExt(".tar"):
log.Println("tar format")
z, err = CreateTar(output)
case hasExt(".tgz"):
fallthrough
case hasExt(".tar.gz"):
log.Println("tar.gz format")
z, err = CreateTgz(output)
default:
log.Println("unsupport file archive format")
os.Exit(1)
}
if err != nil {
return
}
var files = []string{}
var buildFiles = pcfg.Settings.Outfiles
// build source
if !nobuild {
targetDir := sanitizedName(pcfg.Settings.TargetDir)
if !sh.Test("dir", targetDir) {
os.MkdirAll(targetDir, 0755)
}
//gobin := filepath.Join(pwd, sanitizedName(pcfg.Settings.TargetDir))
//sess.SetEnv("GOBIN", gobin)
//log.Debugf("set env GOBIN=%s", gobin)
/*
symdir := filepath.Join(targetDir, goos+"_"+goarch)
if err = os.Symlink(gobin, symdir); err != nil {
log.Fatalf("os symlink error: %s", err)
}
*/
//defer os.Remove(symdir)
// opts := []string{"install", "-v"}
// opts = append(opts, strings.Fields(pcfg.Settings.Addopts)...) // TODO: here need to use shell args parse lib
//if pcfg.Settings.Build == "" {
//pcfg.Settings.Build = DEFAULT_BUILD
//}
for _, command := range pcfg.Script {
if err = shExecString(sess, command); err != nil {
return
}
}
//os.Remove(symdir) // I have to do it twice
if len(buildFiles) == 0 {
cwd, _ := os.Getwd()
program := filepath.Base(cwd)
buildFiles = append(buildFiles, program)
}
for _, filename := range buildFiles {
if goos == "windows" {
filename += ".exe"
}
files = append(files, filename)
}
}
log.Debug("archive files")
depth := pcfg.Depth
for _, filename := range pcfg.Includes {
fs, err := findFiles(filename, depth, skips)
if err != nil {
return
}
files = append(files, fs...)
}
// adds - parse by cli
files = append(files, adds...)
uniqset := make(map[string]bool, len(files))
for _, file := range files {
file = sanitizedName(file)
if uniqset[file] {
continue
}
uniqset[file] = true
log.Infof("zip add file: %v", file)
if err = z.Add(file); err != nil {
return
}
}
log.Info("finish archive file")
err = z.Close()
if rmflag {
for _, filename := range buildFiles {
if goos == "windows" {
filename += ".exe"
}
os.Remove(filename)
}
}
}