Render executes the file section templates and writes the resulting bytes to an output file. The path of the output file is computed by appending the file path to dir. If a file already exists with the computed path then Render happens the smallest integer value greater than 1 to make it unique. Ren
(dir string)
| 70 | // happens the smallest integer value greater than 1 to make it unique. Renders |
| 71 | // returns the computed path. |
| 72 | func (f *File) Render(dir string) (string, error) { |
| 73 | base, err := filepath.Abs(dir) |
| 74 | if err != nil { |
| 75 | return "", err |
| 76 | } |
| 77 | path := filepath.Join(base, f.Path) |
| 78 | if f.SkipExist { |
| 79 | if _, err = os.Stat(path); err == nil { |
| 80 | return "", nil |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if err := os.MkdirAll(filepath.Dir(path), 0750); err != nil { |
| 85 | return "", err |
| 86 | } |
| 87 | |
| 88 | // Render all sections to a buffer instead of directly to file |
| 89 | var buf bytes.Buffer |
| 90 | for _, s := range f.SectionTemplates { |
| 91 | if err := s.Write(&buf); err != nil { |
| 92 | return "", err |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // For Go files, process everything in memory |
| 97 | content := buf.Bytes() |
| 98 | if filepath.Ext(path) == ".go" { |
| 99 | content, err = finalizeGoSource(path, content) |
| 100 | if err != nil { |
| 101 | return "", err |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Write the final content exactly once |
| 106 | if err := os.WriteFile(path, content, 0644); err != nil { |
| 107 | return "", err |
| 108 | } |
| 109 | |
| 110 | // Run finalizer if any |
| 111 | if f.FinalizeFunc != nil { |
| 112 | if err := f.FinalizeFunc(path); err != nil { |
| 113 | return "", err |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return path, nil |
| 118 | } |
| 119 | |
| 120 | // Write writes the section to the given writer. |
| 121 | func (s *SectionTemplate) Write(w io.Writer) error { |