Merge remote-tracking branch 'nzoschke/bios'

This commit is contained in:
Mark McGranaghan 2018-02-01 15:55:06 -05:00
commit bd1f86d96c
2 changed files with 28 additions and 14 deletions

13
bios.sh Executable file
View File

@ -0,0 +1,13 @@
#!/bin/bash
set -ex
mkdir -p src/$PKG && cd src/$PKG
run -s "Cloning" git clone $URL --branch $REF --single-branch .
git reset --hard $SHA
go get github.com/russross/blackfriday
PKGS=$(go list $PKG/... | grep -v examples)
run -s "Linting" golint -set_exit_status $PKGS
run -s "Vetting" go vet -x $PKGS
run -s "Building" tools/build

View File

@ -106,7 +106,6 @@ func whichLexer(path string) string {
return "console" return "console"
} }
panic("No lexer for " + path) panic("No lexer for " + path)
return ""
} }
func debug(msg string) { func debug(msg string) {
@ -118,15 +117,17 @@ func debug(msg string) {
var docsPat = regexp.MustCompile("^\\s*(\\/\\/|#)\\s") var docsPat = regexp.MustCompile("^\\s*(\\/\\/|#)\\s")
var dashPat = regexp.MustCompile("\\-+") var dashPat = regexp.MustCompile("\\-+")
// Seg is a segment of an example
type Seg struct { type Seg struct {
Docs, DocsRendered string Docs, DocsRendered string
Code, CodeRendered string Code, CodeRendered string
CodeEmpty, CodeLeading, CodeRun bool CodeEmpty, CodeLeading, CodeRun bool
} }
// Example is info extracted from an example file
type Example struct { type Example struct {
Id, Name string ID, Name string
GoCode, GoCodeHash, UrlHash string GoCode, GoCodeHash, URLHash string
Segs [][]*Seg Segs [][]*Seg
NextExample *Example NextExample *Example
} }
@ -136,7 +137,7 @@ func parseHashFile(sourcePath string) (string, string) {
return lines[0], lines[1] return lines[0], lines[1]
} }
func resetUrlHashFile(codehash, code, sourcePath string) string { func resetURLHashFile(codehash, code, sourcePath string) string {
payload := strings.NewReader(code) payload := strings.NewReader(code)
resp, err := http.Post("https://play.golang.org/share", "text/plain", payload) resp, err := http.Post("https://play.golang.org/share", "text/plain", payload)
if err != nil { if err != nil {
@ -220,17 +221,17 @@ func parseExamples() []*Example {
for _, exampleName := range exampleNames { for _, exampleName := range exampleNames {
if (exampleName != "") && !strings.HasPrefix(exampleName, "#") { if (exampleName != "") && !strings.HasPrefix(exampleName, "#") {
example := Example{Name: exampleName} example := Example{Name: exampleName}
exampleId := strings.ToLower(exampleName) exampleID := strings.ToLower(exampleName)
exampleId = strings.Replace(exampleId, " ", "-", -1) exampleID = strings.Replace(exampleID, " ", "-", -1)
exampleId = strings.Replace(exampleId, "/", "-", -1) exampleID = strings.Replace(exampleID, "/", "-", -1)
exampleId = strings.Replace(exampleId, "'", "", -1) exampleID = strings.Replace(exampleID, "'", "", -1)
exampleId = dashPat.ReplaceAllString(exampleId, "-") exampleID = dashPat.ReplaceAllString(exampleID, "-")
example.Id = exampleId example.ID = exampleID
example.Segs = make([][]*Seg, 0) example.Segs = make([][]*Seg, 0)
sourcePaths := mustGlob("examples/" + exampleId + "/*") sourcePaths := mustGlob("examples/" + exampleID + "/*")
for _, sourcePath := range sourcePaths { for _, sourcePath := range sourcePaths {
if strings.HasSuffix(sourcePath, ".hash") { if strings.HasSuffix(sourcePath, ".hash") {
example.GoCodeHash, example.UrlHash = parseHashFile(sourcePath) example.GoCodeHash, example.URLHash = parseHashFile(sourcePath)
} else { } else {
sourceSegs, filecontents := parseAndRenderSegs(sourcePath) sourceSegs, filecontents := parseAndRenderSegs(sourcePath)
if filecontents != "" { if filecontents != "" {
@ -241,7 +242,7 @@ func parseExamples() []*Example {
} }
newCodeHash := sha1Sum(example.GoCode) newCodeHash := sha1Sum(example.GoCode)
if example.GoCodeHash != newCodeHash { if example.GoCodeHash != newCodeHash {
example.UrlHash = resetUrlHashFile(newCodeHash, example.GoCode, "examples/"+example.Id+"/"+example.Id+".hash") example.URLHash = resetURLHashFile(newCodeHash, example.GoCode, "examples/"+example.ID+"/"+example.ID+".hash")
} }
examples = append(examples, &example) examples = append(examples, &example)
} }
@ -268,7 +269,7 @@ func renderExamples(examples []*Example) {
_, err := exampleTmpl.Parse(mustReadFile("templates/example.tmpl")) _, err := exampleTmpl.Parse(mustReadFile("templates/example.tmpl"))
check(err) check(err)
for _, example := range examples { for _, example := range examples {
exampleF, err := os.Create(siteDir + "/" + example.Id) exampleF, err := os.Create(siteDir + "/" + example.ID)
check(err) check(err)
exampleTmpl.Execute(exampleF, example) exampleTmpl.Execute(exampleF, example)
} }