-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refac: generate Go code to render themes (#39)
Fixes #38
- Loading branch information
Showing
12 changed files
with
465 additions
and
240 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
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 |
---|---|---|
@@ -1,3 +1,6 @@ | ||
/gocov-html | ||
/generator | ||
*.swp | ||
/*.css | ||
// Generated Go files | ||
*_gen.go |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
v 1.3 | ||
- refac: generate Go code to render themes. #38 | ||
- doc: fix semver in README. #36 | ||
|
||
v 1.2 | ||
|
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
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
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,158 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"go/ast" | ||
"go/parser" | ||
"go/token" | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"path" | ||
"strings" | ||
"text/template" | ||
|
||
"github.com/matm/gocov-html/pkg/types" | ||
) | ||
|
||
const tmpl = `// Code generated by "go run generator.go". DO NOT EDIT. | ||
package themes | ||
import ( | ||
"text/template" | ||
"time" | ||
"github.com/matm/gocov-html/pkg/types" | ||
) | ||
func (t defaultTheme) Data() *types.TemplateData { | ||
td:= &types.TemplateData{ | ||
When: time.Now().Format(time.RFC822Z), | ||
ProjectURL: types.ProjectURL, | ||
} | ||
{{if .Style}} | ||
td.Style = {{.Style}} | ||
{{end}} | ||
{{if .Script}} | ||
td.Script = {{.Script}} | ||
{{end}} | ||
return td | ||
} | ||
func (t defaultTheme) Template() *template.Template { | ||
tmpl := {{.Template}} | ||
p := template.Must(template.New("theme").Parse(tmpl)) | ||
return p | ||
} | ||
` | ||
|
||
func inspect(name string, theme *string, assets *types.StaticAssets) error { | ||
fset := token.NewFileSet() | ||
token, err := parser.ParseFile(fset, name, nil, parser.ParseComments) | ||
if err != nil { | ||
return err | ||
} | ||
ast.Inspect(token, func(n ast.Node) bool { | ||
fn, ok := n.(*ast.FuncDecl) | ||
if ok { | ||
switch fn.Name.Name { | ||
case "Name": | ||
*theme = fn.Body.List[0].(*ast.ReturnStmt).Results[0].(*ast.BasicLit).Value | ||
*theme = strings.Replace(*theme, `"`, "", -1) | ||
case "Assets": | ||
es := fn.Body.List[0].(*ast.ReturnStmt).Results[0].(*ast.CompositeLit).Elts | ||
for _, e := range es { | ||
kv := e.(*ast.KeyValueExpr) | ||
id := kv.Key.(*ast.Ident) | ||
switch id.Name { | ||
case "Stylesheets": | ||
elems := kv.Value.(*ast.CompositeLit).Elts | ||
for _, elem := range elems { | ||
sheet := elem.(*ast.BasicLit).Value | ||
assets.Stylesheets = append(assets.Stylesheets, strings.Replace(sheet, `"`, "", -1)) | ||
} | ||
case "Index": | ||
tmplName := kv.Value.(*ast.BasicLit).Value | ||
assets.Index = strings.Replace(tmplName, `"`, "", -1) | ||
case "Scripts": | ||
elems := kv.Value.(*ast.CompositeLit).Elts | ||
for _, elem := range elems { | ||
script := elem.(*ast.BasicLit).Value | ||
assets.Scripts = append(assets.Scripts, strings.Replace(script, `"`, "", -1)) | ||
} | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
return true | ||
}) | ||
return nil | ||
} | ||
|
||
func render(name, theme string, assets types.StaticAssets) error { | ||
baseThemeDir := path.Join("..", "..", "themes", theme) | ||
out := strings.Replace(name, ".go", "_gen.go", 1) | ||
outFile, err := os.Create(out) | ||
if err != nil { | ||
return err | ||
} | ||
defer outFile.Close() | ||
index, err := ioutil.ReadFile(path.Join(baseThemeDir, assets.Index)) | ||
if err != nil { | ||
return err | ||
} | ||
// Contains all stylesheets' data. | ||
var allStyles bytes.Buffer | ||
for _, css := range assets.Stylesheets { | ||
style, err := ioutil.ReadFile(path.Join(baseThemeDir, css)) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Fprintf(&allStyles, "`%s`", style) | ||
} | ||
|
||
// Contains all scripts' data. | ||
var allScripts bytes.Buffer | ||
for _, script := range assets.Scripts { | ||
js, err := ioutil.ReadFile(path.Join(baseThemeDir, script)) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Fprintf(&allScripts, "`%s`", js) | ||
} | ||
t, err := template.New("").Parse(tmpl) | ||
if err != nil { | ||
return err | ||
} | ||
type data struct { | ||
Script string | ||
Style string | ||
Template string | ||
} | ||
err = t.Execute(outFile, &data{ | ||
Script: allScripts.String(), | ||
Style: allStyles.String(), | ||
Template: "`" + string(index) + "`"}, | ||
) | ||
return err | ||
} | ||
|
||
func main() { | ||
name := os.Getenv("GOFILE") | ||
if name == "" { | ||
fmt.Println("Must be run by the \"go generate\" tool, like \"go generate ./...\"") | ||
os.Exit(1) | ||
} | ||
assets := new(types.StaticAssets) | ||
theme := new(string) | ||
err := inspect(name, theme, assets) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if err := render(name, *theme, *assets); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
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
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
Oops, something went wrong.