Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/js/devtools/bindgen/output.go
2070 views
1
package generator
2
3
import (
4
"bytes"
5
"fmt"
6
"os"
7
"os/exec"
8
"path"
9
"strings"
10
"text/template"
11
12
"github.com/pkg/errors"
13
)
14
15
// markdownIndexes is a map of markdown modules to their filename index
16
//
17
// It is used to generate the index.md file for the documentation
18
var markdownIndexes = make(map[string]string)
19
20
// WriteGoTemplate writes the go template to the output file
21
func (d *TemplateData) WriteGoTemplate(outputDirectory string, pkgName string) error {
22
_ = os.MkdirAll(outputDirectory, os.ModePerm)
23
24
var err error
25
tmpl := template.New("go_class")
26
tmpl = tmpl.Funcs(templateFuncs())
27
tmpl, err = tmpl.Parse(goClassFile)
28
if err != nil {
29
return errors.Wrap(err, "could not parse go class template")
30
}
31
32
filename := path.Join(outputDirectory, fmt.Sprintf("%s.go", pkgName))
33
output, err := os.Create(filename)
34
if err != nil {
35
return errors.Wrap(err, "could not create go class template")
36
}
37
38
if err := tmpl.Execute(output, d); err != nil {
39
_ = output.Close()
40
return errors.Wrap(err, "could not execute go class template")
41
}
42
_ = output.Close()
43
44
cmd := exec.Command("gofmt", "-w", filename)
45
cmd.Stderr = os.Stderr
46
cmd.Stdout = os.Stdout
47
if err := cmd.Run(); err != nil {
48
return errors.Wrap(err, "could not format go class template")
49
}
50
return nil
51
}
52
53
// WriteJSTemplate writes the js template to the output file
54
func (d *TemplateData) WriteJSTemplate(outputDirectory string, pkgName string) error {
55
_ = os.MkdirAll(outputDirectory, os.ModePerm)
56
57
var err error
58
tmpl := template.New("js_class")
59
tmpl, err = tmpl.Parse(jsClassFile)
60
if err != nil {
61
return errors.Wrap(err, "could not parse js class template")
62
}
63
64
filename := path.Join(outputDirectory, fmt.Sprintf("%s.js", pkgName))
65
output, err := os.Create(filename)
66
if err != nil {
67
return errors.Wrap(err, "could not create js class template")
68
}
69
70
if err := tmpl.Execute(output, d); err != nil {
71
_ = output.Close()
72
return errors.Wrap(err, "could not execute js class template")
73
}
74
_ = output.Close()
75
76
cmd := exec.Command("js-beautify", "-r", filename)
77
cmd.Stderr = os.Stderr
78
cmd.Stdout = os.Stdout
79
if err := cmd.Run(); err != nil {
80
return err
81
}
82
return nil
83
}
84
85
// WriteMarkdownIndexTemplate writes the markdown documentation to the output file
86
func (d *TemplateData) WriteMarkdownIndexTemplate(outputDirectory string) error {
87
_ = os.MkdirAll(outputDirectory, os.ModePerm)
88
89
filename := path.Join(outputDirectory, "index.md")
90
output, err := os.Create(filename)
91
if err != nil {
92
return errors.Wrap(err, "could not create markdown index template")
93
}
94
defer func() {
95
_ = output.Close()
96
}()
97
98
buffer := &bytes.Buffer{}
99
_, _ = buffer.WriteString("# Index\n\n")
100
for _, v := range markdownIndexes {
101
_, _ = fmt.Fprintf(buffer, "* %s\n", v)
102
}
103
_, _ = buffer.WriteString("\n\n")
104
105
_, _ = buffer.WriteString("# Scripts\n\n")
106
for _, v := range d.NativeScripts {
107
_, _ = fmt.Fprintf(buffer, "* `%s`\n", v)
108
}
109
if _, err := output.Write(buffer.Bytes()); err != nil {
110
return errors.Wrap(err, "could not write markdown index template")
111
}
112
return nil
113
}
114
115
// WriteMarkdownLibraryDocumentation writes the markdown documentation for a js library
116
// to the output file
117
func (d *TemplateData) WriteMarkdownLibraryDocumentation(outputDirectory string, pkgName string) error {
118
var err error
119
_ = os.MkdirAll(outputDirectory, os.ModePerm)
120
121
tmpl := template.New("markdown_class")
122
tmpl = tmpl.Funcs(templateFuncs())
123
tmpl, err = tmpl.Parse(markdownClassFile)
124
if err != nil {
125
return errors.Wrap(err, "could not parse markdown class template")
126
}
127
128
filename := path.Join(outputDirectory, fmt.Sprintf("%s.md", pkgName))
129
output, err := os.Create(filename)
130
if err != nil {
131
return errors.Wrap(err, "could not create markdown class template")
132
}
133
134
markdownIndexes[pkgName] = fmt.Sprintf("[%s](%s.md)", pkgName, pkgName)
135
if err := tmpl.Execute(output, d); err != nil {
136
_ = output.Close()
137
return err
138
}
139
_ = output.Close()
140
141
return nil
142
}
143
144
// templateFuncs returns the template functions for the generator
145
func templateFuncs() map[string]interface{} {
146
return map[string]interface{}{
147
"exist": func(v map[string]string, key string) bool {
148
_, exist := v[key]
149
return exist
150
},
151
"toTitle": func(v string) string {
152
if len(v) == 0 {
153
return v
154
}
155
156
return strings.ToUpper(string(v[0])) + v[1:]
157
},
158
"uncomment": func(v string) string {
159
return strings.ReplaceAll(strings.ReplaceAll(v, "// ", " "), "\n", " ")
160
},
161
}
162
}
163
164