forked from gobuffalo/packr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pack.go
106 lines (91 loc) · 1.92 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
package jam
import (
"context"
"encoding/json"
"io"
"os"
"os/exec"
"time"
"github.com/gobuffalo/packr/v2/jam/parser"
"github.com/gobuffalo/packr/v2/jam/store"
"github.com/gobuffalo/packr/v2/plog"
)
// PackOptions ...
type PackOptions struct {
IgnoreImports bool
Legacy bool
StoreCmd string
Roots []string
}
// Pack the roots given + PWD
func Pack(opts PackOptions) error {
pwd, err := os.Getwd()
if err != nil {
return err
}
opts.Roots = append(opts.Roots, pwd)
if err := Clean(opts.Roots...); err != nil {
return err
}
p, err := parser.NewFromRoots(opts.Roots, &parser.RootsOptions{
IgnoreImports: opts.IgnoreImports,
})
if err != nil {
return err
}
boxes, err := p.Run()
if err != nil {
return err
}
// reduce boxes - remove ones we don't want
// MB: current assumption is we want all these
// boxes, just adding a comment suggesting they're
// might be a reason to exclude some
plog.Logger.Debugf("found %d boxes", len(boxes))
if len(opts.StoreCmd) != 0 {
return ShellPack(opts, boxes)
}
var st store.Store = store.NewDisk("", "")
if opts.Legacy {
st = store.NewLegacy()
}
for _, b := range boxes {
if b.Name == store.DISK_GLOBAL_KEY {
continue
}
if err := st.Pack(b); err != nil {
return err
}
}
if cl, ok := st.(io.Closer); ok {
return cl.Close()
}
return nil
}
// ShellPack ...
func ShellPack(opts PackOptions, boxes parser.Boxes) error {
b, err := json.Marshal(boxes)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
c := exec.CommandContext(ctx, opts.StoreCmd, string(b))
c.Stdout = os.Stdout
c.Stderr = os.Stderr
return c.Run()
}
// Clean ...
func Clean(args ...string) error {
pwd, err := os.Getwd()
if err != nil {
return err
}
args = append(args, pwd)
for _, root := range args {
if err := store.Clean(root); err != nil {
return err
}
}
return nil
}