|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"embed"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/fs"
|
|
|
|
|
"flag"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"text/template"
|
|
|
|
|
"strings"
|
|
|
|
|
"os"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//go:embed templates
|
|
|
|
|
var templates embed.FS
|
|
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
|
Name string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func WriteTemplate(config Config, from string, to string) error {
|
|
|
|
|
source, err := templates.ReadFile(from)
|
|
|
|
|
if err != nil { return err }
|
|
|
|
|
|
|
|
|
|
t := template.Must(template.New(from).Parse(string(source)))
|
|
|
|
|
|
|
|
|
|
out, err := os.Create(to)
|
|
|
|
|
if err != nil { return err }
|
|
|
|
|
|
|
|
|
|
err = t.Execute(out, config)
|
|
|
|
|
if err != nil { panic(err) }
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
var config Config
|
|
|
|
|
|
|
|
|
|
flag.StringVar(&config.Name, "name", "", "name of the feature to generate")
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
|
|
if config.Name == "" {
|
|
|
|
|
fmt.Println("USAGE: fgen -name name")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
feature_dir := filepath.Join("features", config.Name)
|
|
|
|
|
view_dir := filepath.Join("views", config.Name)
|
|
|
|
|
tests_dir := filepath.Join("tests", config.Name)
|
|
|
|
|
|
|
|
|
|
err := os.MkdirAll(feature_dir, 0755)
|
|
|
|
|
if err != nil { panic(err) }
|
|
|
|
|
|
|
|
|
|
err = os.MkdirAll(view_dir, 0755)
|
|
|
|
|
if err != nil { panic(err) }
|
|
|
|
|
|
|
|
|
|
err = os.MkdirAll(tests_dir, 0755)
|
|
|
|
|
if err != nil { panic(err) }
|
|
|
|
|
|
|
|
|
|
err = fs.WalkDir(templates, "templates",
|
|
|
|
|
func(path string, d fs.DirEntry, err error) error {
|
|
|
|
|
if err != nil { return err }
|
|
|
|
|
target := filepath.Base(path)
|
|
|
|
|
|
|
|
|
|
if !d.IsDir() {
|
|
|
|
|
switch {
|
|
|
|
|
case strings.HasPrefix(path, "templates/views"):
|
|
|
|
|
out_path := filepath.Join("views", config.Name, target)
|
|
|
|
|
err := WriteTemplate(config, path, out_path)
|
|
|
|
|
if err != nil { panic(err) }
|
|
|
|
|
case strings.HasPrefix(path, "templates/feature"):
|
|
|
|
|
out_path := filepath.Join("features", config.Name, target)
|
|
|
|
|
err := WriteTemplate(config, path, out_path)
|
|
|
|
|
if err != nil { panic(err) }
|
|
|
|
|
case strings.HasPrefix(path, "templates/tests"):
|
|
|
|
|
out_path := filepath.Join("tests", config.Name, target)
|
|
|
|
|
err := WriteTemplate(config, path, out_path)
|
|
|
|
|
if err != nil { panic(err) }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if err != nil { panic(err) }
|
|
|
|
|
}
|