Each generator is now a standalone Go module with its own go.mod: TOOLS/ ├── yaml-generator/ ← service_spec_gen (API → YAML) ├── resource-generator/ ← gen_v2 (YAML → Go resources) ├── docs-generator/ ← docs_template_gen_v2 (YAML → Markdown) ├── ops-generator/ ← ops_docs_gen (ops documentation) └── bin/ ← pre-built binaries All scripts updated to use TOOLS/bin/* binaries. Removed old universal_rebuild/tools/ and universal_rebuild/bin/.
136 lines
4.7 KiB
Go
136 lines
4.7 KiB
Go
// Package writers — генерация Go-файлов ресурсов и registry.go.
|
|
package writers
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"text/template"
|
|
|
|
"resource-generator/helpers"
|
|
"resource-generator/templates"
|
|
"resource-generator/types"
|
|
)
|
|
|
|
// WriteInstanceResource генерирует nubes_{service} (CRUD инстанса).
|
|
func WriteInstanceResource(outDir string, svc types.GenResource) error {
|
|
fileName := fmt.Sprintf("%d_%s_resource.go", svc.ServiceID, svc.Name)
|
|
filePath := filepath.Join(outDir, fileName)
|
|
|
|
tpl, err := template.New("resource").Funcs(template.FuncMap{
|
|
"ToCamel": helpers.ToCamel,
|
|
"ToSnake": helpers.ToSnake,
|
|
"ParamType": helpers.ParamType,
|
|
"ParamDefaultExpr": helpers.ParamDefaultExpr,
|
|
"ParamFormat": helpers.ParamFormat,
|
|
"ParamParse": helpers.ParamParse,
|
|
"ParamDescription": helpers.ParamDescription,
|
|
"OutputType": helpers.OutputParamType,
|
|
"OutputIsMap": helpers.OutputParamIsMap,
|
|
"OutputIsList": helpers.OutputParamIsList,
|
|
"OutputSensitive": helpers.OutputParamSensitive,
|
|
"bt": func() string { return "`" },
|
|
}).Parse(templates.Instance)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
if err := tpl.Execute(&buf, svc); err != nil {
|
|
return err
|
|
}
|
|
|
|
formatted := helpers.FormatSourceOrWarn(filePath, buf.Bytes())
|
|
|
|
return os.WriteFile(filePath, formatted, 0644)
|
|
}
|
|
|
|
// WriteSubresource генерирует nubes_{service}_{sub} (пользователи, базы, ...).
|
|
func WriteSubresource(outDir string, sr types.GenSubresource) error {
|
|
name := fmt.Sprintf("%s_%s", sr.ServiceName, sr.SubName)
|
|
fileName := fmt.Sprintf("%d_%s_resource.go", sr.ServiceID, name)
|
|
filePath := filepath.Join(outDir, fileName)
|
|
|
|
tpl, err := template.New("subresource").Funcs(template.FuncMap{
|
|
"ToCamel": helpers.ToCamel,
|
|
"ToSnake": helpers.ToSnake,
|
|
"ParamType": helpers.ParamType,
|
|
"ParamDefaultExpr": helpers.ParamDefaultExpr,
|
|
"ParamFormat": helpers.ParamFormat,
|
|
"ParamParse": helpers.ParamParse,
|
|
"ParamDescription": helpers.ParamDescription,
|
|
"PlanModifierType": helpers.PlanModifierType,
|
|
"PlanModifierExpr": helpers.PlanModifierExpr,
|
|
"bt": func() string { return "`" },
|
|
}).Parse(templates.Subresource)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
if err := tpl.Execute(&buf, sr); err != nil {
|
|
return err
|
|
}
|
|
|
|
formatted := helpers.FormatSourceOrWarn(filePath, buf.Bytes())
|
|
|
|
return os.WriteFile(filePath, formatted, 0644)
|
|
}
|
|
|
|
// WriteActionResource генерирует action-ресурс (почти не используется).
|
|
func WriteActionResource(outDir string, act types.GenAction) error {
|
|
name := fmt.Sprintf("%s_%s", act.ServiceName, act.ActionName)
|
|
fileName := fmt.Sprintf("%d_%s_action.go", act.ServiceID, name)
|
|
filePath := filepath.Join(outDir, fileName)
|
|
|
|
tpl, err := template.New("action").Funcs(template.FuncMap{
|
|
"ToCamel": helpers.ToCamel,
|
|
"ToSnake": helpers.ToSnake,
|
|
"ParamType": helpers.ParamType,
|
|
"ParamDefaultExpr": helpers.ParamDefaultExpr,
|
|
"ParamFormat": helpers.ParamFormat,
|
|
"ParamParse": helpers.ParamParse,
|
|
"ParamDescription": helpers.ParamDescription,
|
|
"bt": func() string { return "`" },
|
|
}).Parse(templates.Action)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
if err := tpl.Execute(&buf, act); err != nil {
|
|
return err
|
|
}
|
|
|
|
formatted := helpers.FormatSourceOrWarn(filePath, buf.Bytes())
|
|
|
|
return os.WriteFile(filePath, formatted, 0644)
|
|
}
|
|
|
|
// WriteRegistry генерирует registry.go со списком всех ресурсов.
|
|
func WriteRegistry(outDir string, services []types.GenResource, subs []types.GenSubresource, actions []types.GenAction) error {
|
|
var buf bytes.Buffer
|
|
buf.WriteString("package resources_gen\n\n")
|
|
buf.WriteString("import \"github.com/hashicorp/terraform-plugin-framework/resource\"\n\n")
|
|
buf.WriteString("// Code generated by tools/gen_v2. DO NOT EDIT.\n")
|
|
buf.WriteString("func AllResources() []func() resource.Resource {\n")
|
|
buf.WriteString("\treturn []func() resource.Resource{\n")
|
|
for _, svc := range services {
|
|
buf.WriteString(fmt.Sprintf("\t\tNew%[1]sResource,\n", helpers.ToCamel(svc.Name)))
|
|
}
|
|
for _, sr := range subs {
|
|
buf.WriteString(fmt.Sprintf("\t\tNew%[1]sResource,\n", helpers.ToCamel(sr.ServiceName+"_"+sr.SubName)))
|
|
}
|
|
for _, act := range actions {
|
|
buf.WriteString(fmt.Sprintf("\t\tNew%[1]sResource,\n", helpers.ToCamel(act.ServiceName+"_"+act.ActionName)))
|
|
}
|
|
buf.WriteString("\t}\n")
|
|
buf.WriteString("}\n")
|
|
|
|
regPath := filepath.Join(outDir, "registry.go")
|
|
formatted := helpers.FormatSourceOrWarn(regPath, buf.Bytes())
|
|
|
|
return os.WriteFile(regPath, formatted, 0644)
|
|
}
|