-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodebasegen.go
More file actions
163 lines (132 loc) · 4.25 KB
/
codebasegen.go
File metadata and controls
163 lines (132 loc) · 4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package resrap
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
)
type file_type int
const (
DIR file_type = iota
FILE
)
// node describes either a directory or file entry.
type node struct {
Name string
Type file_type
Pattern string // E.g., "code_*.c"
Count int // Number of files (e.g., 10)
TokenCount int // Tokens per file (e.g., 20)
FileType string // Language/type identifier (e.g., "C", "sql")
Children []*node
}
// ParseDSL parses the directory DSL into a tree.
func ParseDSL(input string) (*node, error) {
lines := strings.Split(input, "\n")
var root = &node{Name: "", Type: DIR}
stack := []*node{root}
indentStack := []int{-1}
for lineNum, line := range lines {
if strings.TrimSpace(line) == "" {
continue
}
indent := len(line) - len(strings.TrimLeft(line, " "))
name := strings.TrimSpace(line)
var n node
// Check if it's a file specification: name[count x tokens pattern]
if i := strings.Index(name, "["); i != -1 && strings.HasSuffix(name, "]") {
n.Type = FILE
n.FileType = name[:i] // e.g., "C", "sql"
rest := name[i+1 : len(name)-1] // e.g., "10x20 code_*.c"
parts := strings.Fields(rest)
if len(parts) < 2 {
return nil, fmt.Errorf("line %d: invalid file spec format, expected 'type[countXtokens pattern]'", lineNum+1)
}
// Parse "10x20" format
xparts := strings.Split(parts[0], "x")
if len(xparts) != 2 {
return nil, fmt.Errorf("line %d: invalid count format '%s', expected 'NUMxNUM'", lineNum+1, parts[0])
}
var err error
n.Count, err = strconv.Atoi(xparts[0])
if err != nil {
return nil, fmt.Errorf("line %d: invalid file count '%s': %w", lineNum+1, xparts[0], err)
}
n.TokenCount, err = strconv.Atoi(xparts[1])
if err != nil {
return nil, fmt.Errorf("line %d: invalid token count '%s': %w", lineNum+1, xparts[1], err)
}
n.Pattern = parts[1] // e.g., "code_*.c"
if n.Count <= 0 || n.TokenCount <= 0 {
return nil, fmt.Errorf("line %d: count and token count must be positive numbers", lineNum+1)
}
} else {
// It's a directory
n.Type = DIR
n.Name = name
}
// Pop from stack until correct indentation level
for len(indentStack) > 0 && indentStack[len(indentStack)-1] >= indent {
stack = stack[:len(stack)-1]
indentStack = indentStack[:len(indentStack)-1]
}
if len(stack) == 0 {
return nil, fmt.Errorf("line %d: indentation error", lineNum+1)
}
node := &n
parent := stack[len(stack)-1]
parent.Children = append(parent.Children, node)
// Only directories can have children
if n.Type == DIR {
stack = append(stack, node)
indentStack = append(indentStack, indent)
}
}
return root, nil
}
// GenerateStructure creates the directory structure and files
func (n *node) GenerateStructure(r *Resrap, root string) error {
return n.generate_node(r, root)
}
// Recursive function to create directories and generate files
func (n *node) generate_node(r *Resrap, root string) error {
loc := root
if n.Name != "" {
loc = filepath.Join(root, n.Name)
}
if n.Type == DIR {
// Create directory and parents if needed
if err := os.MkdirAll(loc, 0755); err != nil {
return fmt.Errorf("failed to create directory '%s': %w", loc, err)
}
// Recurse for children
for _, child := range n.Children {
if err := child.generate_node(r, loc); err != nil {
return err
}
}
} else {
// FILE type - generate multiple files based on count
if n.Count <= 0 {
return fmt.Errorf("invalid file count %d for pattern '%s'", n.Count, n.Pattern)
}
// Ensure parent directory exists
if err := os.MkdirAll(root, 0755); err != nil {
return fmt.Errorf("failed to create parent directory '%s': %w", root, err)
}
for i := 1; i <= n.Count; i++ {
// Replace * with the file number
fileName := strings.ReplaceAll(n.Pattern, "*", strconv.Itoa(i))
fullPath := filepath.Join(root, fileName)
// Generate file content using the FileType (e.g., "C", "sql") and TokenCount
content := r.GenerateRandom(n.FileType, "program", n.TokenCount)
// Write to file
if err := os.WriteFile(fullPath, []byte(content), 0644); err != nil {
return fmt.Errorf("failed to write file '%s': %w", fullPath, err)
}
fmt.Printf("Generated: %s (%d tokens)\n", fullPath, n.TokenCount)
}
}
return nil
}