forked from gobuffalo/buffalo
/
transform.go
44 lines (40 loc) · 927 Bytes
/
transform.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
package build
import (
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
func (b *Builder) transform(path string, fn func([]byte, io.Writer) error) error {
logrus.Debugf("transforming %s", path)
path = filepath.Join(b.Root, path)
s, err := ioutil.ReadFile(path)
if err != nil {
return errors.WithStack(err)
}
if _, ok := b.originals[path]; ok {
return errors.Errorf("%s was already transformed and can't be altered twice", path)
}
b.originals[path] = s
f, err := os.Create(path)
if err != nil {
return errors.WithStack(err)
}
defer f.Close()
b.cleanups = append(b.cleanups, func() error {
logrus.Debugf("restoring %s", path)
f, err := os.Create(path)
if err != nil {
return errors.WithStack(err)
}
defer f.Close()
_, err = f.Write(b.originals[path])
if err != nil {
return errors.WithStack(err)
}
return nil
})
return fn(s, f)
}