-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
331 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
|
||
libsass "github.com/wellington/go-libsass" | ||
) | ||
|
||
var ( | ||
configFile = flag.String("config", "config.yml", "Path to configuration file.") | ||
|
||
cfg *config | ||
) | ||
|
||
func compileSass(sassConfig sass) error { | ||
for _, compile := range sassConfig.Compile { | ||
fi, err := os.Open(compile.Input) | ||
if err != nil { | ||
return err | ||
} | ||
defer fi.Close() | ||
|
||
fo, err := os.Create(compile.Output) | ||
if err != nil { | ||
return err | ||
} | ||
defer fo.Close() | ||
|
||
includePaths, err := sassConfig.getIncludePaths() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
p := libsass.IncludePaths(includePaths) | ||
s := libsass.OutputStyle(libsass.COMPRESSED_STYLE) | ||
|
||
fmt.Printf("Compiling %s: `%s` into `%s`\n", compile.Name, compile.Input, compile.Output) | ||
|
||
comp, err := libsass.New(fo, fi, p, s) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := comp.Run(); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func copyFiles(list []copy) error { | ||
for _, entry := range list { | ||
err := entry.copy() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func prepareContent(query string) error { | ||
files, err := filepath.Glob(query) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, filename := range files { | ||
p, err := newPage(filename) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Building %s from %s...\n", p.Path, filename) | ||
|
||
p.withConfig(cfg) | ||
err = p.compile() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func main() { | ||
var err error | ||
|
||
flag.Parse() | ||
|
||
cfg, err = loadConfig(*configFile) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
|
||
err = run(cfg) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
} | ||
|
||
func run(cfg *config) error { | ||
var err error | ||
|
||
err = copyFiles(cfg.Copy) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = compileSass(cfg.SASS) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = prepareContent(cfg.Content) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func cleanup() { | ||
_ = os.Remove("test/public/test.html") | ||
_ = os.Remove("test/test.fail") | ||
_ = os.Remove("test/test.html") | ||
_ = os.Remove("test/test.txt") | ||
_ = os.Remove("test/tmp/1.txt") | ||
_ = os.Remove("test/tmp/3.txt") | ||
_ = os.Remove("test/tmp/test.css") | ||
} | ||
|
||
var _ = Describe("Main", func() { | ||
AfterEach(cleanup) | ||
|
||
It("should prepareContent()", func() { | ||
err := prepareContent("test/content/test.yml") | ||
|
||
Expect(err).ShouldNot(HaveOccurred()) | ||
|
||
_, err = os.Stat("test/public/test.html") | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
}) | ||
|
||
It("should fail to prepareContent() due to bad pattern", func() { | ||
err := prepareContent("[]a]") | ||
|
||
Expect(err).Should(HaveOccurred()) | ||
|
||
_, err = os.Stat("test/public/test.html") | ||
Expect(err).Should(HaveOccurred()) | ||
}) | ||
|
||
It("should fail to prepareContent() due to incorrect syntax", func() { | ||
err := prepareContent("test/fail.yml") | ||
|
||
Expect(err).Should(HaveOccurred()) | ||
|
||
_, err = os.Stat("test/public/test.html") | ||
Expect(err).Should(HaveOccurred()) | ||
}) | ||
|
||
It("should fail to prepareContent() due to incorrect file", func() { | ||
err := prepareContent("test/content/missing-template.yml") | ||
|
||
Expect(err).Should(HaveOccurred()) | ||
|
||
_, err = os.Stat("test/public/test.html") | ||
Expect(err).Should(HaveOccurred()) | ||
}) | ||
|
||
Context("copy functionality", func() { | ||
AfterEach(cleanup) | ||
|
||
It("should copyFiles() successfully", func() { | ||
c := []copy{ | ||
copy{Source: "test/copy/*.txt", Destination: "test/tmp"}, | ||
} | ||
|
||
err := copyFiles(c) | ||
|
||
Expect(err).ShouldNot(HaveOccurred()) | ||
}) | ||
|
||
It("should fail to copyFiles() due to syntax error in source", func() { | ||
c := []copy{ | ||
copy{Source: "[]", Destination: "test/tmp"}, | ||
} | ||
|
||
err := copyFiles(c) | ||
|
||
Expect(err).Should(HaveOccurred()) | ||
}) | ||
}) | ||
|
||
Context("sass functionality", func() { | ||
var s sass | ||
var c sassCompile | ||
|
||
BeforeEach(func() { | ||
c = sassCompile{ | ||
Name: "test", | ||
Input: "test/scss/test.scss", | ||
Output: "test/tmp/test.css", | ||
} | ||
|
||
s = sass{ | ||
Compile: []sassCompile{c}, | ||
} | ||
}) | ||
|
||
AfterEach(cleanup) | ||
|
||
It("should compileSass() successfully", func() { | ||
err := compileSass(s) | ||
|
||
Expect(err).ShouldNot(HaveOccurred()) | ||
|
||
By("compiling successfully, we'd like to be sure it kept unicode string") | ||
|
||
css, err := loadFile(s.Compile[0].Output) | ||
|
||
Expect(err).ShouldNot(HaveOccurred()) | ||
Expect(string(css)).To(ContainSubstring("p span")) | ||
Expect(string(css)).To(ContainSubstring("\\f00c")) | ||
}) | ||
|
||
It("should fail to compileSass() due to invalid input file", func() { | ||
s.Compile[0].Input = "/etc/not_gonna_happen.ever" | ||
err := compileSass(s) | ||
|
||
Expect(err).Should(HaveOccurred()) | ||
}) | ||
|
||
It("should fail to compileSass() due to lack of permissions", func() { | ||
s.Compile[0].Output = "/etc/asda" | ||
err := compileSass(s) | ||
|
||
Expect(err).Should(HaveOccurred()) | ||
}) | ||
|
||
It("should fail to compileSass() due to wrong syntax", func() { | ||
s.Compile[0].Input = "test/copy/1.txt" | ||
err := compileSass(s) | ||
|
||
Expect(err).Should(HaveOccurred()) | ||
}) | ||
}) | ||
|
||
Describe("run", func() { | ||
var cfg config | ||
|
||
BeforeEach(func() { | ||
cfg = config{ | ||
Author: "John Smith", | ||
Copyright: "John Smith 1971", | ||
Content: "test/content/test.yml", | ||
Description: "Little compiler.", | ||
Keywords: "all, the, key, words", | ||
Name: "This should be title, but whatever...", | ||
Version: "1.2.3", | ||
Inline: map[string]string{ | ||
"json": "test/copy/2.json", | ||
}, | ||
SASS: sass{ | ||
IncludePaths: []string{}, | ||
Compile: []sassCompile{ | ||
sassCompile{ | ||
Name: "test", | ||
Input: "test/scss/test.scss", | ||
Output: "test/tmp/test.css", | ||
}, | ||
}, | ||
}, | ||
Copy: []copy{ | ||
copy{ | ||
Name: "test", | ||
Source: "test/copy/*.txt", | ||
Destination: "test/tmp", | ||
Override: false, | ||
}, | ||
}, | ||
} | ||
}) | ||
|
||
AfterEach(cleanup) | ||
|
||
It("should run() successfully", func() { | ||
err := run(&cfg) | ||
|
||
Expect(err).ShouldNot(HaveOccurred()) | ||
}) | ||
|
||
It("should fail to run() due to invalid copy syntax", func() { | ||
cfg.Copy[0].Source = "[]" | ||
|
||
err := run(&cfg) | ||
|
||
Expect(err).Should(HaveOccurred()) | ||
}) | ||
|
||
It("should fail to run() due to invalid sass syntax", func() { | ||
cfg.SASS.Compile[0].Input = "test/copy/1.txt" | ||
|
||
err := run(&cfg) | ||
|
||
Expect(err).Should(HaveOccurred()) | ||
}) | ||
|
||
It("should fail to run() due to invalid content syntax", func() { | ||
cfg.Content = "[]" | ||
|
||
err := run(&cfg) | ||
|
||
Expect(err).Should(HaveOccurred()) | ||
}) | ||
}) | ||
}) |