-
Notifications
You must be signed in to change notification settings - Fork 0
/
scss.go
78 lines (61 loc) · 1.57 KB
/
scss.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
package scss
import (
"bytes"
"errors"
"io/ioutil"
"path/filepath"
"strings"
sass "github.com/go-gonzo/scss/internal/libsass"
"github.com/omeid/gonzo"
"github.com/omeid/gonzo/context"
)
// Config doesn't exist. Code over Configuration please.
type Config struct {
}
func Compile() gonzo.Stage {
return func(ctx context.Context, in <-chan gonzo.File, out chan<- gonzo.File) error {
for {
select {
case file, ok := <-in:
if !ok {
return nil
}
source, err := ioutil.ReadAll(file)
if err != nil {
return err
}
dc := sass.NewDataContext(string(source))
defer dc.Destroy()
opts := dc.Context().Options()
base := filepath.Join(
file.FileInfo().Base(),
filepath.Dir(file.FileInfo().Name()),
)
opts.SetIncludePath(base)
compiler := dc.Compiler()
defer compiler.Destroy()
err = compiler.Parse()
if err != nil {
ctx.Error(err)
//ctx.Error(dc.Context().Error().Error())
return errors.New(dc.Context().Error().Error())
}
err = compiler.Execute()
if err != nil {
ctx.Error(err)
return errors.New(dc.Context().Error().Error())
}
output := dc.Context().OutputString()
buff := bytes.NewBufferString(output)
name := strings.TrimSuffix(file.FileInfo().Name(), ".scss") + ".css"
ctx.Infof("Compiling %s to %s", file.FileInfo().Name(), name)
file = gonzo.NewFile(ioutil.NopCloser(buff), file.FileInfo())
file.FileInfo().SetSize(int64(buff.Len()))
file.FileInfo().SetName(name)
out <- file
case <-ctx.Done():
return nil
}
}
}
}