mirror of
https://github.com/dunglas/frankenphp.git
synced 2026-04-22 16:27:12 +08:00
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:
@@ -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
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user