3 second build

This commit is contained in:
Mark McGranaghan 2012-10-01 18:15:22 -07:00
parent 0d8b61377c
commit 6be58e9f1d
2 changed files with 24 additions and 3 deletions

View File

@ -1,3 +1,4 @@
#!/bin/bash #!/bin/bash
mkdir -p /tmp/gbe-book-cache
ls src/0*/*.{go,sh} | xargs tool/build-html-inner > build/go-by-example.html ls src/0*/*.{go,sh} | xargs tool/build-html-inner > build/go-by-example.html

View File

@ -1,6 +1,8 @@
package main package main
import ( import (
"crypto/sha1"
"encoding/hex"
"fmt" "fmt"
"github.com/russross/blackfriday" "github.com/russross/blackfriday"
"io/ioutil" "io/ioutil"
@ -16,7 +18,7 @@ func check(err error) {
} }
} }
func render(bin string, arg []string, src string) string { func render(bin string, arg []string, src string) []byte {
cmd := exec.Command(bin, arg...) cmd := exec.Command(bin, arg...)
in, _ := cmd.StdinPipe() in, _ := cmd.StdinPipe()
out, _ := cmd.StdoutPipe() out, _ := cmd.StdoutPipe()
@ -26,7 +28,25 @@ func render(bin string, arg []string, src string) string {
bytes, _ := ioutil.ReadAll(out) bytes, _ := ioutil.ReadAll(out)
err := cmd.Wait() err := cmd.Wait()
check(err) check(err)
return string(bytes) return bytes
}
func sha1Sum(s string) string {
h := sha1.New()
h.Write([]byte(s))
b := h.Sum(nil)
return hex.EncodeToString(b)
}
func cachedRender(bin string, arg []string, src string) string {
cachePath := "/tmp/gbe-book-cache/pygmentize" + "-" + strings.Join(arg, "-") + "-" + sha1Sum(src)
cacheBytes, cacheErr := ioutil.ReadFile(cachePath)
if cacheErr == nil {
return string(cacheBytes)
}
renderBytes := render(bin, arg, src)
writeErr := ioutil.WriteFile(cachePath, renderBytes, 0600)
check(writeErr)
return string(renderBytes)
} }
func readLines(path string) []string { func readLines(path string) []string {
@ -148,7 +168,7 @@ func main() {
seg.docsRendered = string(blackfriday.MarkdownCommon([]byte(seg.docs))) seg.docsRendered = string(blackfriday.MarkdownCommon([]byte(seg.docs)))
} }
if seg.code != "" { if seg.code != "" {
seg.codeRendered = render("/usr/local/bin/pygmentize", []string{"-l", lexer, "-f", "html"}, seg.code) seg.codeRendered = cachedRender("/usr/local/bin/pygmentize", []string{"-l", lexer, "-f", "html"}, seg.code)
} }
} }