Path: blob/dev/pkg/js/devtools/bindgen/output.go
2070 views
package generator12import (3"bytes"4"fmt"5"os"6"os/exec"7"path"8"strings"9"text/template"1011"github.com/pkg/errors"12)1314// markdownIndexes is a map of markdown modules to their filename index15//16// It is used to generate the index.md file for the documentation17var markdownIndexes = make(map[string]string)1819// WriteGoTemplate writes the go template to the output file20func (d *TemplateData) WriteGoTemplate(outputDirectory string, pkgName string) error {21_ = os.MkdirAll(outputDirectory, os.ModePerm)2223var err error24tmpl := template.New("go_class")25tmpl = tmpl.Funcs(templateFuncs())26tmpl, err = tmpl.Parse(goClassFile)27if err != nil {28return errors.Wrap(err, "could not parse go class template")29}3031filename := path.Join(outputDirectory, fmt.Sprintf("%s.go", pkgName))32output, err := os.Create(filename)33if err != nil {34return errors.Wrap(err, "could not create go class template")35}3637if err := tmpl.Execute(output, d); err != nil {38_ = output.Close()39return errors.Wrap(err, "could not execute go class template")40}41_ = output.Close()4243cmd := exec.Command("gofmt", "-w", filename)44cmd.Stderr = os.Stderr45cmd.Stdout = os.Stdout46if err := cmd.Run(); err != nil {47return errors.Wrap(err, "could not format go class template")48}49return nil50}5152// WriteJSTemplate writes the js template to the output file53func (d *TemplateData) WriteJSTemplate(outputDirectory string, pkgName string) error {54_ = os.MkdirAll(outputDirectory, os.ModePerm)5556var err error57tmpl := template.New("js_class")58tmpl, err = tmpl.Parse(jsClassFile)59if err != nil {60return errors.Wrap(err, "could not parse js class template")61}6263filename := path.Join(outputDirectory, fmt.Sprintf("%s.js", pkgName))64output, err := os.Create(filename)65if err != nil {66return errors.Wrap(err, "could not create js class template")67}6869if err := tmpl.Execute(output, d); err != nil {70_ = output.Close()71return errors.Wrap(err, "could not execute js class template")72}73_ = output.Close()7475cmd := exec.Command("js-beautify", "-r", filename)76cmd.Stderr = os.Stderr77cmd.Stdout = os.Stdout78if err := cmd.Run(); err != nil {79return err80}81return nil82}8384// WriteMarkdownIndexTemplate writes the markdown documentation to the output file85func (d *TemplateData) WriteMarkdownIndexTemplate(outputDirectory string) error {86_ = os.MkdirAll(outputDirectory, os.ModePerm)8788filename := path.Join(outputDirectory, "index.md")89output, err := os.Create(filename)90if err != nil {91return errors.Wrap(err, "could not create markdown index template")92}93defer func() {94_ = output.Close()95}()9697buffer := &bytes.Buffer{}98_, _ = buffer.WriteString("# Index\n\n")99for _, v := range markdownIndexes {100_, _ = fmt.Fprintf(buffer, "* %s\n", v)101}102_, _ = buffer.WriteString("\n\n")103104_, _ = buffer.WriteString("# Scripts\n\n")105for _, v := range d.NativeScripts {106_, _ = fmt.Fprintf(buffer, "* `%s`\n", v)107}108if _, err := output.Write(buffer.Bytes()); err != nil {109return errors.Wrap(err, "could not write markdown index template")110}111return nil112}113114// WriteMarkdownLibraryDocumentation writes the markdown documentation for a js library115// to the output file116func (d *TemplateData) WriteMarkdownLibraryDocumentation(outputDirectory string, pkgName string) error {117var err error118_ = os.MkdirAll(outputDirectory, os.ModePerm)119120tmpl := template.New("markdown_class")121tmpl = tmpl.Funcs(templateFuncs())122tmpl, err = tmpl.Parse(markdownClassFile)123if err != nil {124return errors.Wrap(err, "could not parse markdown class template")125}126127filename := path.Join(outputDirectory, fmt.Sprintf("%s.md", pkgName))128output, err := os.Create(filename)129if err != nil {130return errors.Wrap(err, "could not create markdown class template")131}132133markdownIndexes[pkgName] = fmt.Sprintf("[%s](%s.md)", pkgName, pkgName)134if err := tmpl.Execute(output, d); err != nil {135_ = output.Close()136return err137}138_ = output.Close()139140return nil141}142143// templateFuncs returns the template functions for the generator144func templateFuncs() map[string]interface{} {145return map[string]interface{}{146"exist": func(v map[string]string, key string) bool {147_, exist := v[key]148return exist149},150"toTitle": func(v string) string {151if len(v) == 0 {152return v153}154155return strings.ToUpper(string(v[0])) + v[1:]156},157"uncomment": func(v string) string {158return strings.ReplaceAll(strings.ReplaceAll(v, "// ", " "), "\n", " ")159},160}161}162163164