forked from wellington/wellington
/
build.go
96 lines (86 loc) · 2.16 KB
/
build.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
package wellington
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
libsass "github.com/wellington/go-libsass"
)
var inputFileTypes = []string{".scss", ".sass"}
// LoadAndBuild kicks off parser and compiling
// TODO: make this function testable
func LoadAndBuild(sassFile string, gba *BuildArgs, partialMap *SafePartialMap) error {
if gba == nil {
return fmt.Errorf("build args are nil")
}
// If no imagedir specified, assume relative to the input file
if gba.Dir == "" {
gba.Dir = filepath.Dir(sassFile)
}
var (
out io.WriteCloser
fout string
)
if gba.BuildDir != "" {
// Build output file based off build directory and input filename
rel, _ := filepath.Rel(gba.Includes, filepath.Dir(sassFile))
filename := updateFileOutputType(filepath.Base(sassFile))
fout = filepath.Join(gba.BuildDir, rel, filename)
} else {
out = os.Stdout
}
ctx := libsass.Context{
Sprites: gba.Sprites,
Imgs: gba.Imgs,
OutputStyle: gba.Style,
ImageDir: gba.Dir,
FontDir: gba.Font,
// Assumption that output is a file
BuildDir: filepath.Dir(fout),
GenImgDir: gba.Gen,
MainFile: sassFile,
Comments: gba.Comments,
IncludePaths: []string{filepath.Dir(sassFile)},
}
ctx.Imports.Init()
if gba.Includes != "" {
ctx.IncludePaths = append(ctx.IncludePaths,
strings.Split(gba.Includes, ",")...)
}
fRead, err := os.Open(sassFile)
if err != nil {
return err
}
defer fRead.Close()
if fout != "" {
dir := filepath.Dir(fout)
err := os.MkdirAll(dir, 0755)
if err != nil {
return fmt.Errorf("Failed to create directory: %s", dir)
}
out, err = os.Create(fout)
defer out.Close()
if err != nil {
return fmt.Errorf("Failed to create file: %s", sassFile)
}
// log.Println("Created:", fout)
}
err = ctx.FileCompile(sassFile, out)
if err != nil {
log.Println(sassFile)
return err
}
for _, inc := range ctx.ResolvedImports {
partialMap.AddRelation(ctx.MainFile, inc)
}
fmt.Printf("Rebuilt: %s\n", sassFile)
return nil
}
func updateFileOutputType(filename string) string {
for _, filetype := range inputFileTypes {
filename = strings.Replace(filename, filetype, ".css", 1)
}
return filename
}