feat(build): mark scaffolded files with //summer:make and pass the app to generated commands (quick-261028-pq8)
- model, migration, command, job and admin-controller stubs start with //summer:make <kind>; registry.gen.go keeps its generated-code header - the registry scan accepts the directive and the older header - command functions may take one *backpack.App; generatedCommands takes a variadic app, so plugin.go files calling generatedCommands() still compile - scaffolded plugins keep the app from Boot and return generatedCommands(p.app) - make:command notes the nil app for an older plugin.go - docs, help text and the blog example follow
This commit is contained in:
@@ -489,7 +489,8 @@ func TestScaffoldPluginSmoke(t *testing.T) {
|
||||
for _, want := range []string{
|
||||
"generatedModels()",
|
||||
"generatedMigrations()",
|
||||
"generatedCommands()",
|
||||
"generatedCommands(p.app)",
|
||||
"p.app = app",
|
||||
"generatedJobs()",
|
||||
"generatedAdminControllers()",
|
||||
"//go:embed config",
|
||||
@@ -503,7 +504,7 @@ func TestScaffoldPluginSmoke(t *testing.T) {
|
||||
for _, want := range []string{
|
||||
"func generatedModels()",
|
||||
"func generatedMigrations()",
|
||||
"func generatedCommands()",
|
||||
"func generatedCommands(app ...*backpack.App) []bonfire.Command",
|
||||
"func generatedJobs()",
|
||||
"func generatedAdminControllers()",
|
||||
} {
|
||||
@@ -655,7 +656,7 @@ func TestScaffoldCoreArtifacts(t *testing.T) {
|
||||
"&models.Widget{}",
|
||||
"updates.CreateAlbums()",
|
||||
"updates.AddWidgetIndex()",
|
||||
"console.PurgeCommand()",
|
||||
"console.PurgeCommand(bootApp)",
|
||||
} {
|
||||
if !bytes.Contains(reg1, []byte(want)) {
|
||||
t.Fatalf("registry.gen.go missing %s:\n%s", want, reg1)
|
||||
@@ -794,6 +795,22 @@ func TestScaffoldAllArtifacts(t *testing.T) {
|
||||
if len(command.Files) != 1 || command.Files[0] != wantCommand {
|
||||
t.Fatalf("make:command files = %v, want %s", command.Files, wantCommand)
|
||||
}
|
||||
if command.Hint != "" {
|
||||
t.Fatalf("new plugin printed a make:command hint: %s", command.Hint)
|
||||
}
|
||||
cmdSrc, err := os.ReadFile(wantCommand)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Contains(cmdSrc, []byte("func PurgeCommand(app *backpack.App) bonfire.Command")) {
|
||||
t.Fatalf("purge.go does not take the app:\n%s", cmdSrc)
|
||||
}
|
||||
// Report whether the app arrived, so the built binary proves the wiring.
|
||||
reported := bytes.Replace(cmdSrc, []byte(`out.Printf("demo:purge" + "\n")`), []byte(`out.Printf("demo:purge app=%t\n", app != nil)`), 1)
|
||||
if bytes.Equal(reported, cmdSrc) {
|
||||
t.Fatalf("could not patch purge.go:\n%s", cmdSrc)
|
||||
}
|
||||
write(t, wantCommand, string(reported))
|
||||
|
||||
job, err := MakeJob(ctx, dir, "golem15.demo", "Reindex")
|
||||
if err != nil {
|
||||
@@ -877,16 +894,38 @@ func TestScaffoldAllArtifacts(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
for path, kind := range map[string]string{
|
||||
wantModel: "model",
|
||||
model.Files[1]: "migration",
|
||||
wantCommand: "command",
|
||||
wantJob: "job",
|
||||
wantAdmin[0]: "admin-controller",
|
||||
} {
|
||||
body, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if want := "//summer:make " + kind + "\n\npackage "; !bytes.HasPrefix(body, []byte(want)) {
|
||||
t.Fatalf("%s does not start with %q:\n%s", path, want, body)
|
||||
}
|
||||
if bytes.Contains(body, []byte("DO NOT EDIT")) {
|
||||
t.Fatalf("%s still says DO NOT EDIT:\n%s", path, body)
|
||||
}
|
||||
}
|
||||
|
||||
reg1, err := os.ReadFile(filepath.Join(pluginDir, "registry.gen.go"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.HasPrefix(reg1, []byte("// Code generated by summer make. DO NOT EDIT.\n")) {
|
||||
t.Fatalf("registry.gen.go lost its generated-code header:\n%s", reg1)
|
||||
}
|
||||
for _, want := range []string{
|
||||
"&models.Album{}",
|
||||
"&models.Widget{}",
|
||||
"updates.CreateAlbums()",
|
||||
"updates.AddAlbumIndex()",
|
||||
"console.PurgeCommand()",
|
||||
"console.PurgeCommand(bootApp)",
|
||||
"jobs.ReindexJob()",
|
||||
"controllers.AlbumsController()",
|
||||
} {
|
||||
@@ -922,6 +961,11 @@ func TestScaffoldAllArtifacts(t *testing.T) {
|
||||
if err := App(ctx, dir, &buf); err != nil {
|
||||
t.Fatalf("app build: %v\n%s", err, buf.String())
|
||||
}
|
||||
run := exec.CommandContext(ctx, filepath.Join(dir, "bin", "hello"), "demo:purge")
|
||||
run.Dir = dir
|
||||
if out, err := run.CombinedOutput(); err != nil || !bytes.Contains(out, []byte("demo:purge app=true")) {
|
||||
t.Fatalf("built app demo:purge: %v\n%s", err, out)
|
||||
}
|
||||
if out, err := execGo(t, dir, "build", "./...").CombinedOutput(); err != nil {
|
||||
t.Fatalf("copied workspace go build: %v\n%s", err, out)
|
||||
}
|
||||
@@ -1518,3 +1562,137 @@ func TestLocatePluginLegacyLayout(t *testing.T) {
|
||||
t.Fatalf("locatePluginDir with both layouts = %q, %v; want %s", got, err, vendorDir)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRegistryScanMarkersAndAppCommands: registry.gen.go lists files that
|
||||
// carry the //summer:make directive or the older generated-code header, and
|
||||
// a command function may take exactly one *backpack.App.
|
||||
func TestRegistryScanMarkersAndAppCommands(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
const imports = "import (\n\t\"git.golem15.com/golem15/summercms/modules/backpack\"\n\t\"git.golem15.com/golem15/summercms/modules/bonfire\"\n\t\"gorm.io/gorm\"\n)\n\n" +
|
||||
"var (\n\t_ backpack.App\n\t_ gorm.DB\n)\n\n"
|
||||
files := map[string]string{
|
||||
"legacy.go": "// Code generated by summer make. DO NOT EDIT.\n\npackage console\n\n" + imports + "func LegacyCommand() bonfire.Command { return bonfire.Command{} }\n",
|
||||
"unmarked.go": "package console\n\n" + imports + "func UnmarkedCommand() bonfire.Command { return bonfire.Command{} }\n",
|
||||
"lookalike.go": "//summer:makefile command\n\npackage console\n\n" + imports + "func LookalikeCommand() bonfire.Command { return bonfire.Command{} }\n",
|
||||
"bare.go": "//summer:make\n\npackage console\n\n" + imports + "func BareCommand() bonfire.Command { return bonfire.Command{} }\n",
|
||||
"named.go": "//summer:make command\n\npackage console\n\n" + imports + "func NamedCommand(app *backpack.App) bonfire.Command { return bonfire.Command{} }\n",
|
||||
"unnamed.go": "//summer:make command\n\npackage console\n\n" + imports + "func UnnamedCommand(*backpack.App) bonfire.Command { return bonfire.Command{} }\n",
|
||||
"value.go": "//summer:make command\n\npackage console\n\n" + imports + "func ValueCommand(app backpack.App) bonfire.Command { return bonfire.Command{} }\n",
|
||||
"two.go": "//summer:make command\n\npackage console\n\n" + imports + "func TwoCommand(app *backpack.App, n int) bonfire.Command { return bonfire.Command{} }\n",
|
||||
"twonames.go": "//summer:make command\n\npackage console\n\n" + imports + "func TwoNamesCommand(a, b *backpack.App) bonfire.Command { return bonfire.Command{} }\n",
|
||||
"other.go": "//summer:make command\n\npackage console\n\n" + imports + "func OtherCommand(db *gorm.DB) bonfire.Command { return bonfire.Command{} }\n",
|
||||
}
|
||||
for name, body := range files {
|
||||
write(t, filepath.Join(dir, name), body)
|
||||
}
|
||||
refs, err := scanGeneratedIdents(dir, "console", scanCommandFuncs)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := map[string]bool{}
|
||||
for _, ref := range refs {
|
||||
got[ref.Ident] = ref.TakesApp
|
||||
}
|
||||
want := map[string]bool{
|
||||
"BareCommand": false,
|
||||
"LegacyCommand": false,
|
||||
"NamedCommand": true,
|
||||
"UnnamedCommand": true,
|
||||
}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("refs = %+v, want %v", refs, want)
|
||||
}
|
||||
for ident, takesApp := range want {
|
||||
if v, ok := got[ident]; !ok || v != takesApp {
|
||||
t.Fatalf("refs = %+v, want %s TakesApp=%t", refs, ident, takesApp)
|
||||
}
|
||||
}
|
||||
|
||||
// Other kinds still take no parameters at all.
|
||||
jobsDir := t.TempDir()
|
||||
write(t, filepath.Join(jobsDir, "job.go"), "//summer:make job\n\npackage jobs\n\nimport (\n\t\"git.golem15.com/golem15/summercms/modules/backpack\"\n\t\"git.golem15.com/golem15/summercms/modules/pact\"\n)\n\n"+
|
||||
"func AppJob(app *backpack.App) pact.Job { return nil }\n\nfunc PlainJob() pact.Job { return nil }\n")
|
||||
jobRefs, err := scanGeneratedIdents(jobsDir, "jobs", scanJobFuncs)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(jobRefs) != 1 || jobRefs[0].Ident != "PlainJob" {
|
||||
t.Fatalf("job refs = %+v, want only PlainJob", jobRefs)
|
||||
}
|
||||
|
||||
sortRefs(refs)
|
||||
data := registryData{Package: "demo", Module: "example.com/app/plugins/acme/demo", Commands: refs}
|
||||
data.prepare()
|
||||
src, err := renderStub("registry.go", data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, line := range []string{
|
||||
"func generatedCommands(app ...*backpack.App) []bonfire.Command {",
|
||||
"var bootApp *backpack.App",
|
||||
"console.BareCommand(),",
|
||||
"console.LegacyCommand(),",
|
||||
"console.NamedCommand(bootApp),",
|
||||
"console.UnnamedCommand(bootApp),",
|
||||
} {
|
||||
if !bytes.Contains(src, []byte(line)) {
|
||||
t.Fatalf("registry.gen.go missing %s:\n%s", line, src)
|
||||
}
|
||||
}
|
||||
|
||||
// Without app-taking commands the local is not declared.
|
||||
data = registryData{Package: "demo", Module: "example.com/app/plugins/acme/demo", Commands: []registryRef{{Package: "console", Ident: "LegacyCommand"}}}
|
||||
data.prepare()
|
||||
src, err = renderStub("registry.go", data)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if bytes.Contains(src, []byte("bootApp")) {
|
||||
t.Fatalf("registry.gen.go declares bootApp without app-taking commands:\n%s", src)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMakeCommandLegacyPluginGo: a plugin.go in the older shape (no app
|
||||
// kept, generatedCommands() without arguments) still compiles after
|
||||
// make:command, and the command prints how to pass the app.
|
||||
func TestMakeCommandLegacyPluginGo(t *testing.T) {
|
||||
dir := copyHelloApp(t)
|
||||
ctx := t.Context()
|
||||
pluginDir, err := MakePlugin(ctx, dir, "golem15.demo")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pluginGo := filepath.Join(pluginDir, "plugin.go")
|
||||
src, err := os.ReadFile(pluginGo)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
old := string(src)
|
||||
for _, r := range []struct{ from, to string }{
|
||||
{"type Plugin struct {\n\t// app is the application, kept from Boot for the generated commands.\n\tapp *backpack.App\n}", "type Plugin struct{}"},
|
||||
{"func (p *Plugin) Boot(app *backpack.App) error {\n\tp.app = app\n\treturn nil\n}", "func (p *Plugin) Boot(*backpack.App) error { return nil }"},
|
||||
{"generatedCommands(p.app)", "generatedCommands( )"},
|
||||
} {
|
||||
if !strings.Contains(old, r.from) {
|
||||
t.Fatalf("plugin.go lacks %q:\n%s", r.from, old)
|
||||
}
|
||||
old = strings.Replace(old, r.from, r.to, 1)
|
||||
}
|
||||
write(t, pluginGo, old)
|
||||
|
||||
res, err := MakeCommand(ctx, dir, "golem15.demo", "Purge")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, want := range []string{"console.PurgeCommand receives a nil *backpack.App", "generatedCommands(p.app)"} {
|
||||
if !strings.Contains(res.Hint, want) {
|
||||
t.Fatalf("hint %q lacks %q", res.Hint, want)
|
||||
}
|
||||
}
|
||||
if out, err := execGo(t, pluginDir, "build", "./...").CombinedOutput(); err != nil {
|
||||
t.Fatalf("legacy plugin.go does not build: %v\n%s", err, out)
|
||||
}
|
||||
if out, err := execGo(t, pluginDir, "vet", "./...").CombinedOutput(); err != nil {
|
||||
t.Fatalf("legacy plugin.go vet: %v\n%s", err, out)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user