|
| 1 | +package plugin |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path" |
| 8 | + "text/template" |
| 9 | + |
| 10 | + pluginTpl "github.com/merico-dev/stream/internal/pkg/develop/plugin/template" |
| 11 | + "github.com/merico-dev/stream/pkg/util/log" |
| 12 | +) |
| 13 | + |
| 14 | +type Plugin struct { |
| 15 | + Name string |
| 16 | +} |
| 17 | + |
| 18 | +func NewPlugin(name string) *Plugin { |
| 19 | + return &Plugin{ |
| 20 | + Name: name, |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +// RenderTplFiles takes specified data that the templates needed, |
| 25 | +// then render TplFiles to "Files" and return it as []File. |
| 26 | +func (p *Plugin) RenderTplFiles() ([]pluginTpl.File, error) { |
| 27 | + retFiles := make([]pluginTpl.File, 0) |
| 28 | + count := len(pluginTpl.TplFiles) |
| 29 | + log.Debugf("Template files count: %d.", count) |
| 30 | + |
| 31 | + for i, tplFile := range pluginTpl.TplFiles { |
| 32 | + log.Debugf("Render process: %d/%d.", i+1, count) |
| 33 | + file, err := p.renderTplFile(&tplFile) |
| 34 | + if err != nil { |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + log.Debugf("File: %v.", *file) |
| 38 | + retFiles = append(retFiles, *file) |
| 39 | + } |
| 40 | + |
| 41 | + return retFiles, nil |
| 42 | +} |
| 43 | + |
| 44 | +// RenderTplFile takes specified data that the template needed, |
| 45 | +// then render one TplFile to File and return it. |
| 46 | +func (p *Plugin) renderTplFile(tplFile *pluginTpl.TplFile) (*pluginTpl.File, error) { |
| 47 | + name, err := p.renderTplString(tplFile.NameTpl) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + dir, err := p.renderTplString(tplFile.DirTpl) |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + content, err := p.renderTplString(tplFile.ContentTpl) |
| 56 | + if err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + |
| 60 | + return &pluginTpl.File{ |
| 61 | + Name: name, |
| 62 | + Dir: dir, |
| 63 | + Content: content, |
| 64 | + }, nil |
| 65 | +} |
| 66 | + |
| 67 | +// renderTplString get the template string and the data object, |
| 68 | +// then render it and return the rendered string. |
| 69 | +func (p *Plugin) renderTplString(tplStr string) (string, error) { |
| 70 | + if tplStr == "" { |
| 71 | + return "", nil |
| 72 | + } |
| 73 | + |
| 74 | + t, err := template.New("default").Parse(tplStr) |
| 75 | + if err != nil { |
| 76 | + log.Debugf("Template parse failed: %s.", err) |
| 77 | + log.Debugf("Template content: %s.", tplStr) |
| 78 | + return "", err |
| 79 | + } |
| 80 | + |
| 81 | + var buf bytes.Buffer |
| 82 | + err = t.Execute(&buf, *p) |
| 83 | + if err != nil { |
| 84 | + log.Debugf("Template execute failed: %s.", err) |
| 85 | + log.Debugf("Template content: %s.", tplStr) |
| 86 | + log.Debugf("Data object: %v.", *p) |
| 87 | + return "", err |
| 88 | + } |
| 89 | + |
| 90 | + return buf.String(), nil |
| 91 | +} |
| 92 | + |
| 93 | +// PersistFiles gets the []pluginTpl.File, for each File: |
| 94 | +// call the persistFile() method to deal with. |
| 95 | +func (p *Plugin) PersistFiles(files []pluginTpl.File) error { |
| 96 | + fileCount := len(files) |
| 97 | + log.Debugf("There are %d files wait to persist.", fileCount) |
| 98 | + for i, file := range files { |
| 99 | + log.Debugf("Persist process: %d/%d.", i+1, fileCount) |
| 100 | + if err := p.persistFile(&file); err != nil { |
| 101 | + log.Errorf("Failed to persist: %s/%s.", file.Dir, file.Name) |
| 102 | + return err |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +// persistFile gets the *pluginTpl.File, then do the following: |
| 110 | +// 1. mkdir the File.Dir |
| 111 | +// 2. create the File.Name file |
| 112 | +// 3. write the File.Content into the File.Name file |
| 113 | +func (p *Plugin) persistFile(file *pluginTpl.File) error { |
| 114 | + // mkdir the File.Dir |
| 115 | + if err := os.MkdirAll(file.Dir, 0755); err != nil { |
| 116 | + log.Debugf("Failed to create directory: %s.", file.Dir) |
| 117 | + } |
| 118 | + log.Debugf("Directory created: %s.", file.Dir) |
| 119 | + |
| 120 | + // create the File.Name file and write the File.Content into the File.Name file |
| 121 | + filePath := path.Join(file.Dir, file.Name) |
| 122 | + if err := os.WriteFile(filePath, []byte(file.Content), 0644); err != nil { |
| 123 | + log.Debugf("Failed to write content to the file: %s.", err) |
| 124 | + } |
| 125 | + log.Debugf("File %s has been created.", filePath) |
| 126 | + |
| 127 | + return nil |
| 128 | +} |
| 129 | + |
| 130 | +func (p *Plugin) PrintHelpInfo() { |
| 131 | + help := ` |
| 132 | +The DevStream PMC (project management committee) sincerely thank you for your devotion and enthusiasm in creating new plugins! |
| 133 | +
|
| 134 | +To make the process easy as a breeze, DevStream(dtm) has generated some templated source code files for you to flatten the learning curve and reduce manual copy-paste. |
| 135 | +In the generated templates, dtm has left some special marks in the format of "TODO(dtm)". |
| 136 | +Please look for these TODOs by global search. Once you find them, you will know what to do with them. Also, please remember to check our documentation on creating a new plugin: |
| 137 | +
|
| 138 | +**README_when_create_plugin.md** |
| 139 | +
|
| 140 | +Source code files created. |
| 141 | +
|
| 142 | +Happy hacking, buddy! |
| 143 | +Please give us feedback through GitHub issues if you encounter any difficulties. We guarantee that you will receive unrivaled help from our passionate community! |
| 144 | +` |
| 145 | + fmt.Println(help) |
| 146 | +} |
0 commit comments