From 7c1cd3a78384fef888cc782603c3ba9c8630f639 Mon Sep 17 00:00:00 2001 From: matm Date: Tue, 7 Mar 2023 15:25:34 +0100 Subject: [PATCH] Add support for javascript files also Fixes #38 --- .gitignore | 1 + Makefile | 3 +++ build.mk | 3 ++- cmd/generator/main.go | 32 +++++++++++++++++++++++++++----- cmd/gocov-html/main.go | 2 +- pkg/cov/report.go | 4 ++-- pkg/types/theme.go | 6 ++++-- themes/golang/index.html | 11 ++++++++++- 8 files changed, 50 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 5acd6a9..0ff6f2d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /gocov-html +/generator *.swp /*.css // Generated Go files diff --git a/Makefile b/Makefile index ff030f1..6c18c7d 100644 --- a/Makefile +++ b/Makefile @@ -4,5 +4,8 @@ BIN=gocov-html MAIN_CMD=github.com/matm/${BIN}/cmd/${BIN} +GENERATOR_BIN=generator +GENERATOR_CMD=github.com/matm/${BIN}/cmd/${GENERATOR_BIN} + include version.mk include build.mk diff --git a/build.mk b/build.mk index 1d8fa50..7c571e2 100644 --- a/build.mk +++ b/build.mk @@ -71,6 +71,7 @@ cleardist: @rm -rf ${DISTDIR} && mkdir -p ${BINDIR} && mkdir -p ${BUILDDIR} build: + @go build ${GENERATOR_CMD} @go generate ./... @go build -ldflags "all=$(GO_LDFLAGS)" ${MAIN_CMD} @@ -79,4 +80,4 @@ test: clean: @find pkg -name \*_gen.go -delete - @rm -rf ${BIN} ${BUILDDIR} ${DISTDIR} + @rm -rf ${BIN} ${GENERATOR_BIN} ${BUILDDIR} ${DISTDIR} diff --git a/cmd/generator/main.go b/cmd/generator/main.go index b958cbf..a0ac54e 100644 --- a/cmd/generator/main.go +++ b/cmd/generator/main.go @@ -28,12 +28,17 @@ import ( ) func (t defaultTheme) Data() *types.TemplateData { - css := {{.Style}} - return &types.TemplateData{ - CSS: css, + 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 { @@ -71,7 +76,12 @@ func inspect(name string, theme *string, assets *types.StaticAssets) error { case "Index": tmplName := kv.Value.(*ast.BasicLit).Value assets.Index = strings.Replace(tmplName, `"`, "", -1) - // TODO: case for "Scripts". + 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)) + } } } } @@ -101,17 +111,29 @@ func render(name, theme string, assets types.StaticAssets) error { if err != nil { return err } - fmt.Fprintf(&allStyles, "`%s`\n", style) + 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) + "`"}, ) diff --git a/cmd/gocov-html/main.go b/cmd/gocov-html/main.go index f0aceaa..ea6f785 100644 --- a/cmd/gocov-html/main.go +++ b/cmd/gocov-html/main.go @@ -68,7 +68,7 @@ func main() { } if *showDefaultCSS { - fmt.Println(themes.Current().Data().CSS) + fmt.Println(themes.Current().Data().Style) return } diff --git a/pkg/cov/report.go b/pkg/cov/report.go index 15d9b26..32d1f77 100644 --- a/pkg/cov/report.go +++ b/pkg/cov/report.go @@ -106,7 +106,7 @@ func printReport(w io.Writer, r *report) error { theme := themes.Current() data := theme.Data() - css := data.CSS + css := data.Style if len(r.stylesheet) > 0 { // Inline CSS. f, err := os.Open(r.stylesheet) @@ -124,7 +124,7 @@ func printReport(w io.Writer, r *report) error { reportPackages[i] = buildReportPackage(pkg) } - data.CSS = css + data.Style = css data.Packages = reportPackages if len(reportPackages) > 1 { diff --git a/pkg/types/theme.go b/pkg/types/theme.go index 402e077..7a3d30a 100644 --- a/pkg/types/theme.go +++ b/pkg/types/theme.go @@ -19,8 +19,10 @@ type Beautifier interface { // TemplateData has all the fields needed by the the HTML template for rendering. type TemplateData struct { - // CSS is the stylesheet content that will be embedded in the HTML page. - CSS string + // Style is the stylesheet content that will be embedded in the HTML page. + Style string + // Script is the javascript content that will be embedded in the HTML page. + Script string // When is the date time of report generation. When string // Overview holds data used for an additional header in case of multiple Go packages diff --git a/themes/golang/index.html b/themes/golang/index.html index 689b79a..fb4bb83 100644 --- a/themes/golang/index.html +++ b/themes/golang/index.html @@ -3,7 +3,11 @@ Coverage Report - {{.CSS}} + {{if .Style}} + + {{end}}
Coverage Report
@@ -91,6 +95,11 @@ {{end}} {{/* if overview end */}} {{end}} {{/* range if end */}} + {{if .Script}} + + {{end}} {{end}} \ No newline at end of file