71 lines
1.1 KiB
Go
71 lines
1.1 KiB
Go
|
package template
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"embed"
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
//go:embed built-in/*.scaffold
|
||
|
var tmpl embed.FS
|
||
|
|
||
|
type File struct {
|
||
|
Path string
|
||
|
Content []byte
|
||
|
}
|
||
|
|
||
|
type Template struct {
|
||
|
Language string
|
||
|
Variables interface{}
|
||
|
}
|
||
|
|
||
|
func NewTemplate(language string, variables interface{}) Template {
|
||
|
return Template{
|
||
|
Language: language,
|
||
|
Variables: variables,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (t *Template) Generate() error {
|
||
|
file, err := tmpl.Open(fmt.Sprintf("built-in/%s.scaffold", t.Language))
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
defer file.Close()
|
||
|
|
||
|
files := []File{}
|
||
|
scanner := bufio.NewScanner(file)
|
||
|
|
||
|
var current File
|
||
|
|
||
|
for scanner.Scan() {
|
||
|
line := scanner.Text()
|
||
|
|
||
|
if strings.HasPrefix(line, "#file:") {
|
||
|
if current.Path != "" {
|
||
|
files = append(files, current)
|
||
|
}
|
||
|
|
||
|
parts := strings.Split(line, ":")
|
||
|
current = File{Path: parts[1], Content: []byte{}}
|
||
|
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
current.Content = append(current.Content, []byte(line+"\n")...)
|
||
|
}
|
||
|
|
||
|
if current.Path != "" {
|
||
|
files = append(files, current)
|
||
|
}
|
||
|
|
||
|
for _, f := range files {
|
||
|
fmt.Printf(">>> %s\n", f.Path)
|
||
|
fmt.Printf("%s---\n", f.Content)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|