feat(09-03): scaffold Winter admin controller layout
- config_form.yaml and config_list.yaml point at models/<name>/fields.yaml and columns.yaml - Duplicate model or controller assets fail before any new file is written
This commit is contained in:
@@ -27,6 +27,8 @@ type artifactData struct {
|
||||
Worker string
|
||||
Kind string
|
||||
ConfigDir string
|
||||
PluginPath string
|
||||
Snake string
|
||||
MigrationID string
|
||||
UpSQL string
|
||||
DownSQL string
|
||||
@@ -246,20 +248,33 @@ func MakeAdminController(ctx context.Context, startDir, pluginID, name string) (
|
||||
return ArtifactResult{}, err
|
||||
}
|
||||
configDir := filepath.ToSlash(filepath.Join("controllers", snake))
|
||||
fieldsRel := filepath.Join("controllers", snake, "fields.yaml")
|
||||
columnsRel := filepath.Join("controllers", snake, "columns.yaml")
|
||||
for _, yamlRel := range []string{fieldsRel, columnsRel} {
|
||||
formRel := filepath.Join("controllers", snake, "config_form.yaml")
|
||||
listRel := filepath.Join("controllers", snake, "config_list.yaml")
|
||||
fieldsRel := filepath.Join("models", snake, "fields.yaml")
|
||||
columnsRel := filepath.Join("models", snake, "columns.yaml")
|
||||
for _, yamlRel := range []string{formRel, listRel, fieldsRel, columnsRel} {
|
||||
if err := rejectDuplicateFile(plugin.Dir, yamlRel); err != nil {
|
||||
return ArtifactResult{}, err
|
||||
}
|
||||
}
|
||||
src, err := renderStub("admin_controller.go", artifactData{
|
||||
Ident: name,
|
||||
Func: fn,
|
||||
Worker: unexportedIdent(name) + "Admin",
|
||||
Kind: plugin.ID + "." + snake,
|
||||
ConfigDir: configDir,
|
||||
})
|
||||
data := artifactData{
|
||||
Ident: name,
|
||||
Func: fn,
|
||||
Worker: unexportedIdent(name) + "Admin",
|
||||
Kind: plugin.ID + "." + snake,
|
||||
ConfigDir: configDir,
|
||||
PluginPath: strings.ReplaceAll(plugin.ID, ".", "/"),
|
||||
Snake: snake,
|
||||
}
|
||||
src, err := renderStub("admin_controller.go", data)
|
||||
if err != nil {
|
||||
return ArtifactResult{}, err
|
||||
}
|
||||
formSrc, err := renderStub("config_form.yaml", data)
|
||||
if err != nil {
|
||||
return ArtifactResult{}, err
|
||||
}
|
||||
listSrc, err := renderStub("config_list.yaml", data)
|
||||
if err != nil {
|
||||
return ArtifactResult{}, err
|
||||
}
|
||||
@@ -286,16 +301,21 @@ func MakeAdminController(ctx context.Context, startDir, pluginID, name string) (
|
||||
return ArtifactResult{}, err
|
||||
}
|
||||
created = append(created, goPath)
|
||||
fieldsPath := filepath.Join(plugin.Dir, fieldsRel)
|
||||
if err := writeFile(fieldsPath, fieldsSrc); err != nil {
|
||||
return ArtifactResult{}, err
|
||||
for _, item := range []struct {
|
||||
rel string
|
||||
body []byte
|
||||
}{
|
||||
{formRel, formSrc},
|
||||
{listRel, listSrc},
|
||||
{fieldsRel, fieldsSrc},
|
||||
{columnsRel, columnsSrc},
|
||||
} {
|
||||
path := filepath.Join(plugin.Dir, item.rel)
|
||||
if err := writeFile(path, item.body); err != nil {
|
||||
return ArtifactResult{}, err
|
||||
}
|
||||
created = append(created, path)
|
||||
}
|
||||
created = append(created, fieldsPath)
|
||||
columnsPath := filepath.Join(plugin.Dir, columnsRel)
|
||||
if err := writeFile(columnsPath, columnsSrc); err != nil {
|
||||
return ArtifactResult{}, err
|
||||
}
|
||||
created = append(created, columnsPath)
|
||||
if err := finishArtifact(ctx, plugin); err != nil {
|
||||
return ArtifactResult{}, err
|
||||
}
|
||||
|
||||
@@ -714,10 +714,12 @@ func TestScaffoldAllArtifacts(t *testing.T) {
|
||||
}
|
||||
wantAdmin := []string{
|
||||
filepath.Join(pluginDir, "controllers", "albums.go"),
|
||||
filepath.Join(pluginDir, "controllers", "albums", "fields.yaml"),
|
||||
filepath.Join(pluginDir, "controllers", "albums", "columns.yaml"),
|
||||
filepath.Join(pluginDir, "controllers", "albums", "config_form.yaml"),
|
||||
filepath.Join(pluginDir, "controllers", "albums", "config_list.yaml"),
|
||||
filepath.Join(pluginDir, "models", "albums", "fields.yaml"),
|
||||
filepath.Join(pluginDir, "models", "albums", "columns.yaml"),
|
||||
}
|
||||
if len(admin.Files) != 3 {
|
||||
if len(admin.Files) != len(wantAdmin) {
|
||||
t.Fatalf("make:admin-controller files = %v", admin.Files)
|
||||
}
|
||||
for i, want := range wantAdmin {
|
||||
@@ -739,8 +741,8 @@ func TestScaffoldAllArtifacts(t *testing.T) {
|
||||
}
|
||||
}
|
||||
for _, rel := range []string{
|
||||
filepath.Join("controllers", "albums", "fields.yaml"),
|
||||
filepath.Join("controllers", "albums", "columns.yaml"),
|
||||
filepath.Join("models", "albums", "fields.yaml"),
|
||||
filepath.Join("models", "albums", "columns.yaml"),
|
||||
} {
|
||||
body, err := os.ReadFile(filepath.Join(pluginDir, rel))
|
||||
if err != nil {
|
||||
|
||||
@@ -105,6 +105,27 @@ func ({{.Worker}}) ConfigDir() string { return {{printf "%q" .ConfigDir}} }
|
||||
func {{.Func}}() pact.AdminController { return {{.Worker}}{} }
|
||||
{{end}}
|
||||
|
||||
{{define "config_form.yaml"}}name: {{.Ident}}
|
||||
form: ~/plugins/{{.PluginPath}}/models/{{.Snake}}/fields.yaml
|
||||
modelClass: {{.Ident}}
|
||||
defaultRedirect: {{.PluginPath}}/{{.Snake}}
|
||||
|
||||
create:
|
||||
redirect: {{.PluginPath}}/{{.Snake}}/update/:id
|
||||
redirectClose: {{.PluginPath}}/{{.Snake}}
|
||||
|
||||
update:
|
||||
redirect: {{.PluginPath}}/{{.Snake}}
|
||||
redirectClose: {{.PluginPath}}/{{.Snake}}
|
||||
{{end}}
|
||||
|
||||
{{define "config_list.yaml"}}list: ~/plugins/{{.PluginPath}}/models/{{.Snake}}/columns.yaml
|
||||
modelClass: {{.Ident}}
|
||||
title: {{.Ident}}
|
||||
recordsPerPage: 20
|
||||
showSearch: true
|
||||
{{end}}
|
||||
|
||||
{{define "fields.yaml"}}fields:
|
||||
id:
|
||||
label: ID
|
||||
|
||||
Reference in New Issue
Block a user