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
|
Worker string
|
||||||
Kind string
|
Kind string
|
||||||
ConfigDir string
|
ConfigDir string
|
||||||
|
PluginPath string
|
||||||
|
Snake string
|
||||||
MigrationID string
|
MigrationID string
|
||||||
UpSQL string
|
UpSQL string
|
||||||
DownSQL string
|
DownSQL string
|
||||||
@@ -246,20 +248,33 @@ func MakeAdminController(ctx context.Context, startDir, pluginID, name string) (
|
|||||||
return ArtifactResult{}, err
|
return ArtifactResult{}, err
|
||||||
}
|
}
|
||||||
configDir := filepath.ToSlash(filepath.Join("controllers", snake))
|
configDir := filepath.ToSlash(filepath.Join("controllers", snake))
|
||||||
fieldsRel := filepath.Join("controllers", snake, "fields.yaml")
|
formRel := filepath.Join("controllers", snake, "config_form.yaml")
|
||||||
columnsRel := filepath.Join("controllers", snake, "columns.yaml")
|
listRel := filepath.Join("controllers", snake, "config_list.yaml")
|
||||||
for _, yamlRel := range []string{fieldsRel, columnsRel} {
|
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 {
|
if err := rejectDuplicateFile(plugin.Dir, yamlRel); err != nil {
|
||||||
return ArtifactResult{}, err
|
return ArtifactResult{}, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
src, err := renderStub("admin_controller.go", artifactData{
|
data := artifactData{
|
||||||
Ident: name,
|
Ident: name,
|
||||||
Func: fn,
|
Func: fn,
|
||||||
Worker: unexportedIdent(name) + "Admin",
|
Worker: unexportedIdent(name) + "Admin",
|
||||||
Kind: plugin.ID + "." + snake,
|
Kind: plugin.ID + "." + snake,
|
||||||
ConfigDir: configDir,
|
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 {
|
if err != nil {
|
||||||
return ArtifactResult{}, err
|
return ArtifactResult{}, err
|
||||||
}
|
}
|
||||||
@@ -286,16 +301,21 @@ func MakeAdminController(ctx context.Context, startDir, pluginID, name string) (
|
|||||||
return ArtifactResult{}, err
|
return ArtifactResult{}, err
|
||||||
}
|
}
|
||||||
created = append(created, goPath)
|
created = append(created, goPath)
|
||||||
fieldsPath := filepath.Join(plugin.Dir, fieldsRel)
|
for _, item := range []struct {
|
||||||
if err := writeFile(fieldsPath, fieldsSrc); err != nil {
|
rel string
|
||||||
return ArtifactResult{}, err
|
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 {
|
if err := finishArtifact(ctx, plugin); err != nil {
|
||||||
return ArtifactResult{}, err
|
return ArtifactResult{}, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -714,10 +714,12 @@ func TestScaffoldAllArtifacts(t *testing.T) {
|
|||||||
}
|
}
|
||||||
wantAdmin := []string{
|
wantAdmin := []string{
|
||||||
filepath.Join(pluginDir, "controllers", "albums.go"),
|
filepath.Join(pluginDir, "controllers", "albums.go"),
|
||||||
filepath.Join(pluginDir, "controllers", "albums", "fields.yaml"),
|
filepath.Join(pluginDir, "controllers", "albums", "config_form.yaml"),
|
||||||
filepath.Join(pluginDir, "controllers", "albums", "columns.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)
|
t.Fatalf("make:admin-controller files = %v", admin.Files)
|
||||||
}
|
}
|
||||||
for i, want := range wantAdmin {
|
for i, want := range wantAdmin {
|
||||||
@@ -739,8 +741,8 @@ func TestScaffoldAllArtifacts(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, rel := range []string{
|
for _, rel := range []string{
|
||||||
filepath.Join("controllers", "albums", "fields.yaml"),
|
filepath.Join("models", "albums", "fields.yaml"),
|
||||||
filepath.Join("controllers", "albums", "columns.yaml"),
|
filepath.Join("models", "albums", "columns.yaml"),
|
||||||
} {
|
} {
|
||||||
body, err := os.ReadFile(filepath.Join(pluginDir, rel))
|
body, err := os.ReadFile(filepath.Join(pluginDir, rel))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -105,6 +105,27 @@ func ({{.Worker}}) ConfigDir() string { return {{printf "%q" .ConfigDir}} }
|
|||||||
func {{.Func}}() pact.AdminController { return {{.Worker}}{} }
|
func {{.Func}}() pact.AdminController { return {{.Worker}}{} }
|
||||||
{{end}}
|
{{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:
|
{{define "fields.yaml"}}fields:
|
||||||
id:
|
id:
|
||||||
label: ID
|
label: ID
|
||||||
|
|||||||
Reference in New Issue
Block a user