diff --git a/gotime/smplt/Makefile b/gotime/smplt/Makefile deleted file mode 100644 index a8830cc..0000000 --- a/gotime/smplt/Makefile +++ /dev/null @@ -1,16 +0,0 @@ -TARGETS = \ - github.com/meatballhat/box-o-sand/gotime/smplt - -test: build - go test -x -v $(TARGETS) - -build: deps - go install -x $(TARGETS) - -deps: - go get -n -x $(TARGETS) - -clean: - go clean -x -i $(TARGETS) - -.PHONY: test build clean fmt diff --git a/gotime/smplt/canned_responses.go b/gotime/smplt/canned_responses.go deleted file mode 100644 index fb5bf56..0000000 --- a/gotime/smplt/canned_responses.go +++ /dev/null @@ -1,22 +0,0 @@ -package smplt - -var ( - HTTP_500_RESPONSE = []byte(` - - -
-Something go boom inside.
- - -`) -) diff --git a/gotime/smplt/simplate.go b/gotime/smplt/simplate.go deleted file mode 100644 index f155169..0000000 --- a/gotime/smplt/simplate.go +++ /dev/null @@ -1,166 +0,0 @@ -package smplt - -import ( - "io" - "mime" - "path" - "strings" - "text/template" -) - -const ( - SIMPLATE_TYPE_RENDERED = "rendered" - SIMPLATE_TYPE_STATIC = "static" - SIMPLATE_TYPE_NEGOTIATED = "negotiated" - SIMPLATE_TYPE_JSON = "json" -) - -var ( - SIMPLATE_TYPES = []string{ - SIMPLATE_TYPE_JSON, - SIMPLATE_TYPE_NEGOTIATED, - SIMPLATE_TYPE_RENDERED, - SIMPLATE_TYPE_STATIC, - } - simplateGenFileTmpl = template.Must(template.New("smpltgen").Parse(strings.Replace(` -/* GENERATED FILE - DO NOT EDIT */ -/* Rebuild with simplate filesystem parsing thingy! */ -package smpltgen - -import ( - "bytes" - "net/http" - "text/template" - - "github.com/meatballhat/box-o-sand/gotime/smplt" -) - -{{.InitPage.Body}} - -{{if .HasTemplatePage}} -const ( - SIMPLATE_TMPL_{{.ConstName}} = __BACKTICK__{{.TemplatePage.Body}}__BACKTICK__ -) - -var ( - simplateTmpl{{.FuncName}} = template.Must(template.New("{{.FuncName}}").Parse(SIMPLATE_TMPL_{{.ConstName}})) -) -{{end}} - -func SimplateHandlerFunc{{.FuncName}}(w http.ResponseWriter, req *http.Request) { - var err error - ctx := make(map[string]interface{}) - - {{range .LogicPages}} - {{.Body}} - {{end}} - - {{if .HasTemplatePage}} - var tmplBuf bytes.Buffer - err = simplateTmpl{{.FuncName}}.Execute(&tmplBuf, ctx) - if err != nil { - w.Header().Set("Content-Type", "text/html") - w.WriteHeader(http.StatusInternalServerError) - w.Write(smplt.HTTP_500_RESPONSE) - return - } - - w.Header().Set("Content-Type", "{{.ContentType}}") - w.WriteHeader(http.StatusOK) - w.Write(tmplBuf.Bytes()) - {{end}} -} -`, "__BACKTICK__", "`", -1))) -) - -type Simplate struct { - Filename string - Type string - ContentType string - InitPage *SimplatePage - LogicPages []*SimplatePage - TemplatePage *SimplatePage -} - -type SimplatePage struct { - Body string -} - -func NewSimplateFromString(filename, content string) *Simplate { - rawPages := strings.Split(content, "") - nbreaks := len(rawPages) - 1 - - s := &Simplate{ - Filename: filename, - Type: SIMPLATE_TYPE_STATIC, - ContentType: mime.TypeByExtension(path.Ext(filename)), - } - - if nbreaks == 1 || nbreaks == 2 { - s.InitPage = &SimplatePage{Body: rawPages[0]} - s.LogicPages = append(s.LogicPages, &SimplatePage{Body: rawPages[1]}) - - if s.ContentType == "application/json" { - s.Type = SIMPLATE_TYPE_JSON - } else { - s.Type = SIMPLATE_TYPE_RENDERED - s.TemplatePage = &SimplatePage{Body: rawPages[2]} - } - - return s - } - - if nbreaks > 2 { - s.Type = SIMPLATE_TYPE_NEGOTIATED - s.InitPage = &SimplatePage{Body: rawPages[0]} - - for _, rawPage := range rawPages { - s.LogicPages = append(s.LogicPages, &SimplatePage{Body: rawPage}) - } - - return s - } - - return s -} - -func (me *Simplate) Execute(wr io.Writer) error { - return simplateGenFileTmpl.Execute(wr, me) -} - -func (me *Simplate) escapedFilename() string { - lessDots := strings.Replace(me.Filename, ".", "-DOT-", -1) - lessSlashes := strings.Replace(lessDots, "/", "-SLASH-", -1) - return strings.Replace(lessSlashes, " ", "-SPACE-", -1) -} - -func (me *Simplate) OutputName() string { - if me.Type == SIMPLATE_TYPE_STATIC { - return me.Filename - } - - return me.escapedFilename() + ".go" -} - -func (me *Simplate) FuncName() string { - escaped := me.escapedFilename() - parts := strings.Split(escaped, "-") - for i, part := range parts { - var capitalized []string - capitalized = append(capitalized, strings.ToUpper(string(part[0]))) - capitalized = append(capitalized, strings.ToLower(part[1:])) - parts[i] = strings.Join(capitalized, "") - } - - return strings.Join(parts, "") -} - -func (me *Simplate) ConstName() string { - escaped := me.escapedFilename() - uppered := strings.ToUpper(escaped) - return strings.Replace(uppered, "-", "_", -1) -} - -func (me *Simplate) HasTemplatePage() bool { - return len(me.TemplatePage.Body) > 0 -} diff --git a/gotime/smplt/smplt_test.go b/gotime/smplt/smplt_test.go deleted file mode 100644 index a0d200b..0000000 --- a/gotime/smplt/smplt_test.go +++ /dev/null @@ -1,444 +0,0 @@ -package smplt_test - -import ( - "bytes" - "fmt" - "go/parser" - "go/token" - "log" - "mime" - "os" - "os/exec" - "path" - "sort" - "strings" - "testing" - "time" - - . "github.com/meatballhat/box-o-sand/gotime/smplt" -) - -const ( - BASIC_RENDERED_TXT_SIMPLATE = ` -import ( - "time" -) - -type Dance struct { - Who string - When time.Time -} - -ctx["D"] = &Dance{ - Who: "Everybody", - When: time.Now(), -} - -{{.D.Who}} Dance {{.D.When}}! -` - BASIC_STATIC_TXT_SIMPLATE = ` -Everybody Dance Now! -` - BASIC_JSON_SIMPLATE = ` -import ( - "time" -) - -type Dance struct { - Who string - When time.Time -} - -ctx["D"] = &Dance{ - Who: "Everybody", - When: time.Now(), -response.SetBody(ctx["D"]) -} -` - BASIC_NEGOTIATED_SIMPLATE = ` -import ( - "time" -) - -type Dance struct { - Who string - When time.Time -} - -ctx["D"] = &Dance{ - Who: "Everybody", - When: time.Now(), -} - text/plain -{{.D.Who}} Dance {{.D.When}}! - - application/json -{"who":"{{.D.Who}}","when":"{{.D.When}}"} -` -) - -var ( - tmpdir = path.Join(os.TempDir(), - fmt.Sprintf("smplt_test-%d", time.Now().UTC().UnixNano())) - smpltgenDir = path.Join(tmpdir, "src", "smpltgen") - testSiteRoot = path.Join(tmpdir, "test-site") - goCmd string - noCleanup bool - testSiteFiles = map[string]string{ - "hams/bone/derp": BASIC_NEGOTIATED_SIMPLATE, - "shill/cans.txt": BASIC_RENDERED_TXT_SIMPLATE, - "hat/v.json": BASIC_JSON_SIMPLATE, - "silmarillion.handlebar.mustache.moniker.html": "INVALID AS BUTT", - "Big CMS/Owns_UR Contents/flurb.txt": BASIC_STATIC_TXT_SIMPLATE, - } -) - -func init() { - err := os.Setenv("GOPATH", strings.Join([]string{tmpdir, os.Getenv("GOPATH")}, ":")) - if err != nil { - if noCleanup { - log.Fatal(err) - } else { - panic(err) - } - } - - cmd, err := exec.LookPath("go") - if err != nil { - if noCleanup { - log.Fatal(err) - } else { - panic(err) - } - } - - goCmdAddr := &goCmd - *goCmdAddr = cmd - - noCleanupAddr := &noCleanup - *noCleanupAddr = len(os.Getenv("SMPLT_TEST_NOCLEANUP")) > 0 -} - -func mkTmpDir() { - err := os.MkdirAll(smpltgenDir, os.ModeDir|os.ModePerm) - if err != nil { - panic(err) - } -} - -func rmTmpDir() { - err := os.RemoveAll(tmpdir) - if err != nil { - panic(err) - } -} - -func mkTestSite() string { - mkTmpDir() - - for filePath, content := range testSiteFiles { - fullPath := path.Join(testSiteRoot, filePath) - err := os.MkdirAll(path.Dir(fullPath), os.ModeDir|os.ModePerm) - if err != nil { - panic(err) - } - - f, err := os.Create(fullPath) - if err != nil { - panic(err) - } - - _, err = f.WriteString(content) - if err != nil { - panic(err) - } - - err = f.Close() - if err != nil { - panic(err) - } - } - - return testSiteRoot -} - -func writeRenderedTemplate() (string, error) { - s := NewSimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE) - outfileName := path.Join(smpltgenDir, s.OutputName()) - outf, err := os.Create(outfileName) - if err != nil { - return outfileName, err - } - - s.Execute(outf) - err = outf.Close() - if err != nil { - return outfileName, err - } - - return outfileName, nil -} - -func runGoCommandOnSmpltgen(command string) error { - cmd := exec.Command(goCmd, command, "smpltgen") - - var out bytes.Buffer - cmd.Stdout = &out - cmd.Stderr = &out - - err := cmd.Run() - if err != nil { - return err - } - - if noCleanup { - log.Println(out.String()) - } - return nil -} - -func formatRenderedTemplate() error { - return runGoCommandOnSmpltgen("fmt") -} - -func buildRenderedTemplate() error { - return runGoCommandOnSmpltgen("install") -} - -func TestSimplateKnowsItsFilename(t *testing.T) { - s := NewSimplateFromString("hasty-decisions.txt", "herpherpderpherp") - if s.Filename != "hasty-decisions.txt" { - t.Errorf("Simplate filename incorrectly assigned as %s instead of %s", - s.Filename, "hasty-decisions.txt") - } -} - -func TestSimplateKnowsItsContentType(t *testing.T) { - s := NewSimplateFromString("hasty-decisions.js", "function herp() { return 'derp'; }") - expected := mime.TypeByExtension(".js") - - if s.ContentType != expected { - t.Errorf("Simplate content type incorrectly assigned as %s instead of %s", - s.ContentType, expected) - } -} - -func TestStaticSimplateKnowsItsOutputName(t *testing.T) { - s := NewSimplateFromString("nothing.txt", "foo\nham\n") - if s.OutputName() != "nothing.txt" { - t.Errorf("Static simplate output name is wrong!: %v", s.OutputName()) - } -} - -func TestRenderedSimplateKnowsItsOutputName(t *testing.T) { - s := NewSimplateFromString("flip/dippy slippy/snork.d/basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE) - if s.OutputName() != "flip-SLASH-dippy-SPACE-slippy-SLASH-snork-DOT-d-SLASH-basic-rendered-DOT-txt.go" { - t.Errorf("Rendered simplate output name is wrong!: %v", s.OutputName()) - } -} - -func TestDetectsRenderedSimplate(t *testing.T) { - s := NewSimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE) - if s.Type != SIMPLATE_TYPE_RENDERED { - t.Errorf("Simplate detected as %s instead of %s", s.Type, SIMPLATE_TYPE_RENDERED) - } -} - -func TestDetectsStaticSimplate(t *testing.T) { - s := NewSimplateFromString("basic-static.txt", BASIC_STATIC_TXT_SIMPLATE) - if s.Type != SIMPLATE_TYPE_STATIC { - t.Errorf("Simplate detected as %s instead of %s", s.Type, SIMPLATE_TYPE_STATIC) - } -} - -func TestDetectsJSONSimplates(t *testing.T) { - s := NewSimplateFromString("basic.json", BASIC_JSON_SIMPLATE) - if s.Type != SIMPLATE_TYPE_JSON { - t.Errorf("Simplate detected as %s instead of %s", s.Type, SIMPLATE_TYPE_JSON) - } -} - -func TestDetectsNegotiatedSimplates(t *testing.T) { - s := NewSimplateFromString("hork", BASIC_NEGOTIATED_SIMPLATE) - if s.Type != SIMPLATE_TYPE_NEGOTIATED { - t.Errorf("Simplate detected as %s instead of %s", - s.Type, SIMPLATE_TYPE_NEGOTIATED) - } -} - -func TestAssignsNoGoPagesToStaticSimplates(t *testing.T) { - s := NewSimplateFromString("basic-static.txt", BASIC_STATIC_TXT_SIMPLATE) - if s.InitPage != nil { - t.Errorf("Static simplate had init page assigned!: %v", s.InitPage) - } - - if len(s.LogicPages) > 0 { - t.Errorf("Static simplate had logic pages assigned!: %v", s.LogicPages) - } -} - -func TestAssignsAnInitPageToRenderedSimplates(t *testing.T) { - s := NewSimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE) - if s.InitPage == nil { - t.Errorf("Rendered simplate had no init page assigned!: %v", s.InitPage) - } -} - -func TestAssignsOneLogicPageToRenderedSimplates(t *testing.T) { - s := NewSimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE) - if len(s.LogicPages) != 1 { - t.Errorf("Rendered simplate unexpected number "+ - "of logic pages assigned!: %v", len(s.LogicPages)) - } -} - -func TestAssignsOneTemplatePageToRenderedSimplates(t *testing.T) { - s := NewSimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE) - if s.TemplatePage == nil { - t.Errorf("Rendered simplate had no template page assigned!: %v", s.TemplatePage) - } -} - -func TestAssignsAnInitPageToJSONSimplates(t *testing.T) { - s := NewSimplateFromString("basic.json", BASIC_JSON_SIMPLATE) - if s.InitPage == nil { - t.Errorf("JSON simplate had no init page assigned!: %v", s.InitPage) - } -} - -func TestAssignsOneLogicPageToJSONSimplates(t *testing.T) { - s := NewSimplateFromString("basic.json", BASIC_JSON_SIMPLATE) - if len(s.LogicPages) != 1 { - t.Errorf("Rendered simplate unexpected number "+ - "of logic pages assigned!: %v", len(s.LogicPages)) - } -} - -func TestAssignsNoTemplatePageToJSONSimplates(t *testing.T) { - s := NewSimplateFromString("basic.json", BASIC_JSON_SIMPLATE) - if s.TemplatePage != nil { - t.Errorf("JSON simplate had a template page assigned!: %v", s.TemplatePage) - } -} - -func TestAssignsAnInitPageToNegotiatedSimplates(t *testing.T) { - s := NewSimplateFromString("basic-negotiated.txt", BASIC_NEGOTIATED_SIMPLATE) - if s.InitPage == nil { - t.Errorf("Negotiated simplate had no init page assigned!: %v", s.InitPage) - } -} - -func TestAssignsAtLeastOneLogicPageToNegotiatedSimplates(t *testing.T) { - s := NewSimplateFromString("basic-negotiated.txt", BASIC_NEGOTIATED_SIMPLATE) - if len(s.LogicPages) < 1 { - t.Errorf("Negotiated simplate unexpected number "+ - "of logic pages assigned!: %v", len(s.LogicPages)) - } -} - -func TestAssignsNoTemplatePageToNegotiatedSimplates(t *testing.T) { - s := NewSimplateFromString("basic-negotiated.txt", BASIC_NEGOTIATED_SIMPLATE) - if s.TemplatePage != nil { - t.Errorf("Negotiated simplate had a template page assigned!: %v", s.TemplatePage) - } -} - -func TestRenderedSimplateCanExecuteToWriter(t *testing.T) { - s := NewSimplateFromString("basic-rendered.txt", BASIC_RENDERED_TXT_SIMPLATE) - var out bytes.Buffer - err := s.Execute(&out) - if err != nil { - t.Error(err) - } -} - -func TestRenderedSimplateOutputIsValidGoSource(t *testing.T) { - mkTmpDir() - if noCleanup { - fmt.Println("tmpdir =", tmpdir) - } else { - defer rmTmpDir() - } - - outfileName, err := writeRenderedTemplate() - if err != nil { - t.Error(err) - return - } - - fset := token.NewFileSet() - _, err = parser.ParseFile(fset, outfileName, nil, parser.DeclarationErrors) - if err != nil { - t.Error(err) - return - } -} - -func TestRenderedSimplateCanBeCompiled(t *testing.T) { - mkTmpDir() - if noCleanup { - fmt.Println("tmpdir =", tmpdir) - } else { - defer rmTmpDir() - } - - _, err := writeRenderedTemplate() - if err != nil { - t.Error(err) - return - } - - err = formatRenderedTemplate() - if err != nil { - t.Error(err) - return - } - - err = buildRenderedTemplate() - if err != nil { - t.Error(err) - return - } -} - -func TestTreeWalkerRequiresValidDirectoryRoot(t *testing.T) { - _, err := NewTreeWalker("/dev/null") - if err == nil { - t.Errorf("New tree walker failed to reject invalid dir!") - return - } -} - -func TestTreeWalkerYieldsSimplates(t *testing.T) { - siteRoot := mkTestSite() - if noCleanup { - fmt.Println("tmpdir =", tmpdir) - } else { - defer rmTmpDir() - } - - tw, err := NewTreeWalker(siteRoot) - if err != nil { - t.Error(err) - return - } - - n := 0 - - simplates, err := tw.Simplates() - if err != nil { - t.Error(err) - } - - for simplate := range simplates { - if sort.SearchStrings(SIMPLATE_TYPES, simplate.Type) < 0 { - t.Errorf("Simplate yielded with invalid type: %v", simplate.Type) - return - } - n++ - } - - if n != 5 { - t.Errorf("Tree walking yielded unexpected number of files: %v", n) - } -} diff --git a/gotime/smplt/tree_walker.go b/gotime/smplt/tree_walker.go deleted file mode 100644 index 754921f..0000000 --- a/gotime/smplt/tree_walker.go +++ /dev/null @@ -1,66 +0,0 @@ -package smplt - -import ( - "errors" - "io/ioutil" - "log" - "os" - "path/filepath" -) - -var ( - InvalidTreeWalkerRoot = errors.New("Invalid tree climber root given") -) - -type TreeWalker struct { - Root string -} - -func NewTreeWalker(dirRoot string) (*TreeWalker, error) { - fi, err := os.Stat(dirRoot) - if err != nil { - return nil, err - } - - if !fi.IsDir() { - return nil, InvalidTreeWalkerRoot - } - - return &TreeWalker{Root: dirRoot}, nil -} - -func (me *TreeWalker) Simplates() (<-chan *Simplate, error) { - schan := make(chan *Simplate) - - go func() { - filepath.Walk(me.Root, - func(path string, info os.FileInfo, err error) error { - if err != nil { - log.Println("TreeWalker error:", err) - return nil - } - - if info.IsDir() { - return nil - } - - f, err := os.Open(path) - if err != nil { - return err - } - - defer f.Close() - - content, err := ioutil.ReadAll(f) - if err != nil { - return err - } - - schan <- NewSimplateFromString(path, string(content)) - return nil - }) - close(schan) - }() - - return (<-chan *Simplate)(schan), nil -}