fix(ext-gen): skip README.md generation in extension-init command if it exists (#2283)

Currently, the `extension-init` command automatically generates a
boilerplate `README.md`. While helpful for initial setups, this behavior
becomes destructive during the iterative development phase. If a
developer has customized their README and later runs `extension-init` to
update function signatures or add new functions, their custom
documentation is overwritten without warning.
This commit is contained in:
Indra Gunawan
2026-03-26 21:51:24 +08:00
committed by GitHub
parent 4dcf6aa9dd
commit ebbc4e2658
2 changed files with 42 additions and 0 deletions
+6
View File
@@ -3,6 +3,7 @@ package extgen
import (
"bytes"
_ "embed"
"os"
"path/filepath"
"text/template"
)
@@ -22,6 +23,11 @@ type DocTemplateData struct {
func (dg *DocumentationGenerator) generate() error {
filename := filepath.Join(dg.generator.BuildDir, "README.md")
if _, err := os.Stat(filename); err == nil {
return nil
}
content, err := dg.generateMarkdown()
if err != nil {
return err
+36
View File
@@ -383,3 +383,39 @@ func BenchmarkDocumentationGenerator_GenerateMarkdown(b *testing.B) {
assert.NoError(b, err)
}
}
func TestDocumentationGenerator_SkipExistingReadme(t *testing.T) {
tempDir := t.TempDir()
readmePath := filepath.Join(tempDir, "README.md")
err := os.WriteFile(readmePath, []byte("hello"), 0644)
require.NoError(t, err)
generator := &Generator{
BaseName: "testextension",
BuildDir: tempDir,
Functions: []phpFunction{
{
Name: "greet",
ReturnType: phpString,
Params: []phpParameter{
{Name: "name", PhpType: phpString},
},
Signature: "greet(string $name): string",
},
},
Classes: []phpClass{},
}
docGen := &DocumentationGenerator{
generator: generator,
}
err = docGen.generate()
assert.NoError(t, err, "generate() unexpected error")
content, err := os.ReadFile(readmePath)
require.NoError(t, err, "Failed to read generated README.md")
assert.Equal(t, string(content), "hello")
}