-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources.go
74 lines (61 loc) · 1.55 KB
/
resources.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
package resources
import (
"bytes"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"github.com/omeid/gonzo/context"
"github.com/omeid/go-resources"
"github.com/omeid/gonzo"
)
type Config resources.Config
var FilenameFormat = "%s_resource.go"
// A build stage creates a new Package and adds all the files coming through the channel to
// the package and returns the result of build as a File on the output channel.
func Build(config Config) gonzo.Stage {
return func(ctx context.Context, files <-chan gonzo.File, out chan<- gonzo.File) error {
ctx, cancel := context.WithCancel(ctx)
res := resources.New()
res.Config = resources.Config(config)
var err error
buff := &bytes.Buffer{}
for {
select {
case file, ok := <-files:
if !ok {
goto BUILD
}
if file.FileInfo().IsDir() {
continue
}
path, _ := filepath.Rel(file.FileInfo().Base(), file.FileInfo().Name())
res.Add(filepath.ToSlash(path), file)
ctx.Infof("Adding %s", path)
defer func(path string) {
ctx.Debug("Closing %s", path)
file.Close() //Close files AFTER we have build our package.
}(path)
case <-ctx.Done():
err = ctx.Err()
goto BUILD
}
}
BUILD:
if err != nil {
return err
}
ctx.Debug("Runnig build...")
err = res.Build(buff)
if err != nil {
cancel()
return err
}
path := fmt.Sprintf(FilenameFormat, strings.ToLower(config.Var))
sf := gonzo.NewFile(ioutil.NopCloser(buff), gonzo.NewFileInfo())
sf.FileInfo().SetName(path)
sf.FileInfo().SetSize(int64(buff.Len()))
out <- sf
return nil
}
}