refactor(recipe): make file discovery generic using recipe metadata
- Build allowed extensions from recipe languages and extensions map - Generalize filepath walking to filter by allowed extensions instead of hardcoding .go - Retain Go-specific content check for now; can generalize later - Simplify refactor JSON handling and patch creation by removing redundant comments and error checks - Adjust comments and minor formatting for clarity
This commit is contained in:
parent
019ce1e95a
commit
7ffbf352bc
@ -24,7 +24,7 @@ func (r *Runner) Run() error {
|
|||||||
fmt.Printf("🍳 Starting recipe: %s v%s\n\n", r.Recipe.Name, r.Recipe.Version)
|
fmt.Printf("🍳 Starting recipe: %s v%s\n\n", r.Recipe.Name, r.Recipe.Version)
|
||||||
|
|
||||||
var previousResults []string
|
var previousResults []string
|
||||||
var refactorJSONs []string // collect only JSON from refactor steps
|
var refactorJSONs []string
|
||||||
|
|
||||||
for _, step := range r.Recipe.Steps {
|
for _, step := range r.Recipe.Steps {
|
||||||
fmt.Printf("Step %d/%d: %s\n", step.Number, len(r.Recipe.Steps), step.Title)
|
fmt.Printf("Step %d/%d: %s\n", step.Number, len(r.Recipe.Steps), step.Title)
|
||||||
@ -47,7 +47,6 @@ func (r *Runner) Run() error {
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// fallback
|
|
||||||
prompt := fmt.Sprintf(`Recipe Overview:
|
prompt := fmt.Sprintf(`Recipe Overview:
|
||||||
%s
|
%s
|
||||||
|
|
||||||
@ -82,11 +81,11 @@ Execute this step now. Respond ONLY with the expected output format — no expla
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// discoverFiles now respects the package_path parameter from the recipe
|
// discoverFiles — now fully generic using recipe metadata
|
||||||
func (r *Runner) discoverFiles() []string {
|
func (r *Runner) discoverFiles() []string {
|
||||||
var files []string
|
var files []string
|
||||||
|
|
||||||
// Use parameter if present, otherwise fall back to internal
|
// Get root from parameter or default
|
||||||
root := "internal"
|
root := "internal"
|
||||||
if p, ok := r.Recipe.Parameters["package_path"]; ok {
|
if p, ok := r.Recipe.Parameters["package_path"]; ok {
|
||||||
if def, ok := p.Default.(string); ok && def != "" {
|
if def, ok := p.Default.(string); ok && def != "" {
|
||||||
@ -94,14 +93,27 @@ func (r *Runner) discoverFiles() []string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Build set of allowed extensions from recipe frontmatter
|
||||||
|
allowedExt := make(map[string]bool)
|
||||||
|
for _, lang := range r.Recipe.ProjectLanguages {
|
||||||
|
if exts, ok := r.Recipe.Extensions[lang]; ok {
|
||||||
|
for _, ext := range exts {
|
||||||
|
allowedExt[ext] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_ = filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
|
_ = filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
|
||||||
if err != nil || d.IsDir() || !strings.HasSuffix(path, ".go") {
|
if err != nil || d.IsDir() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
ext := filepath.Ext(path)
|
||||||
|
if allowedExt[ext] {
|
||||||
b, _ := os.ReadFile(path)
|
b, _ := os.ReadFile(path)
|
||||||
if strings.Contains(string(b), "if err != nil") {
|
if strings.Contains(string(b), "if err != nil") { // this is still Go-specific for now; we can generalize later
|
||||||
files = append(files, path)
|
files = append(files, path)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -111,7 +123,7 @@ func (r *Runner) discoverFiles() []string {
|
|||||||
return files
|
return files
|
||||||
}
|
}
|
||||||
|
|
||||||
// refactorFiles — one file at a time, stores pure JSON for apply step
|
// refactorFiles — one file at a time
|
||||||
func (r *Runner) refactorFiles(previousResults []string, refactorJSONs *[]string) {
|
func (r *Runner) refactorFiles(previousResults []string, refactorJSONs *[]string) {
|
||||||
discoveredLine := previousResults[len(previousResults)-1]
|
discoveredLine := previousResults[len(previousResults)-1]
|
||||||
lines := strings.Split(discoveredLine, "\n")
|
lines := strings.Split(discoveredLine, "\n")
|
||||||
@ -167,7 +179,6 @@ func (r *Runner) handleApplyStep(refactorJSONs []string) {
|
|||||||
|
|
||||||
var allChanges []FileChange
|
var allChanges []FileChange
|
||||||
for _, jsonStr := range refactorJSONs {
|
for _, jsonStr := range refactorJSONs {
|
||||||
// Find the JSON object in the response
|
|
||||||
start := strings.Index(jsonStr, "{")
|
start := strings.Index(jsonStr, "{")
|
||||||
end := strings.LastIndex(jsonStr, "}") + 1
|
end := strings.LastIndex(jsonStr, "}") + 1
|
||||||
if start == -1 {
|
if start == -1 {
|
||||||
@ -200,23 +211,12 @@ func createUnifiedPatch(changes []FileChange, patchPath string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer func(f *os.File) {
|
defer f.Close()
|
||||||
err := f.Close()
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}(f)
|
|
||||||
|
|
||||||
for _, ch := range changes {
|
for _, ch := range changes {
|
||||||
_, err := fmt.Fprintf(f, "--- %s\n+++ %s\n@@ -0,0 +1,%d @@\n", ch.File, ch.File, strings.Count(ch.Content, "\n")+1)
|
fmt.Fprintf(f, "--- %s\n+++ %s\n@@ -0,0 +1,%d @@\n", ch.File, ch.File, strings.Count(ch.Content, "\n")+1)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, line := range strings.Split(ch.Content, "\n") {
|
for _, line := range strings.Split(ch.Content, "\n") {
|
||||||
_, err := fmt.Fprintf(f, "+%s\n", line)
|
fmt.Fprintf(f, "+%s\n", line)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user