-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeContext
executable file
·154 lines (133 loc) · 4.93 KB
/
makeContext
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
#!python3
import os
import sys
import time
import re
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
# Mapping of file extensions to Markdown code syntax specifiers
syntax_highlighting = {
'.swift': 'swift',
'.py': 'python',
'.js': 'javascript',
'.jsx': 'jsx',
'.ts': 'typescript',
'.tsx': 'tsx',
'.html': 'html',
'.css': 'css',
'.scss': 'scss',
'.json': 'json',
'.xml': 'xml',
'.yaml': 'yaml',
'.md': 'markdown',
'.markdown': 'markdown',
'.sh': 'bash',
'.rs': 'rust',
'.toml': 'toml',
}
# List of regexes for prioritizing specific files
priority_files = [
r"STARTHERE",
r"WIP",
]
# List of regexes for omitting specific files
omit_files = [
r"^OMIT-.*",
r"^.*/target/.*",
r"^.*\.xcworkspace/.*",
r".*\.(png|jpg|jpeg|gif|svg|pdf|resolved|lock|webloc|xlogic|pyc)",
]
kickoff_file = "STARTHERE.md"
kickoff = "Perform the task in `STARTHERE.md`. This is the entire task: stop once you have completed it."
class ChangeHandler(FileSystemEventHandler):
def __init__(self, directory, output_file, process_func):
self.directory = directory
self.output_file = output_file
self.process_func = process_func
def on_any_event(self, event):
# Ignoring changes to the output file itself
if self.output_file and os.path.abspath(event.src_path) == os.path.abspath(self.output_file):
return
self.process_func(self.directory, self.output_file)
def list_files_and_contents(directory, output_file=None):
top_level_directory = os.path.basename(directory.rstrip(os.sep))
regular_paths = []
priority_paths = []
kickoff_present = False
for root, dirs, files in os.walk(directory, topdown=True):
# Check for kickoff_file only at the top level
if root == directory:
kickoff_present = kickoff_file in files
# Check for OMIT file in the current directory
if any(f.startswith('OMIT') for f in files):
dirs[:] = [] # Clear dirs to skip subdirectories
continue
# Remove directories and files that start with '.'
dirs[:] = [d for d in dirs if not d.startswith('.')]
files = [f for f in files if not f.startswith('.')]
for filename in files:
full_path = os.path.join(root, filename)
if any(re.search(pattern, filename) for pattern in priority_files):
priority_paths.append(full_path)
else:
regular_paths.append(full_path)
# Combine lists, ensuring priority files are at the beginning
combined_paths = sorted(priority_paths) + sorted(regular_paths)
output_lines = []
if top_level_directory != '.':
output_lines.append(f"# {top_level_directory}\n\n")
for path in combined_paths:
relative_path = os.path.relpath(path, directory)
file_extension = os.path.splitext(path)[1]
syntax_specifier = syntax_highlighting.get(file_extension, '')
if os.path.basename(path) == output_file:
continue
elif any(re.search(pattern, path) for pattern in omit_files):
# output_lines.append(f"## {relative_path}\n\n*OMITTED*\n\n")
continue
else:
try:
with open(path, 'r') as file:
content = file.read()
# Omit if the first line contains `OMIT` anywhere within it
if 'OMIT' in content.split('\n', maxsplit=1)[0]:
output_lines.append(f"## {relative_path}\n\n*OMITTED*\n\n")
continue
output_lines.append(f"## {relative_path}\n\n````{syntax_specifier}\n")
output_lines.append(content.rstrip() + '\n````\n\n')
except Exception as e:
output_lines.append(f"Error reading file {relative_path}: {e}\n")
if kickoff_present:
output_lines.append(kickoff)
if output_file:
with open(output_file, 'w') as f:
f.writelines(output_lines)
else:
print(''.join(output_lines))
def main():
output_file = None
watch = False
try:
directory = sys.argv[1]
if '-o' in sys.argv:
o_index = sys.argv.index('-o')
output_file = sys.argv[o_index + 1]
if '-w' in sys.argv:
watch = True
except (IndexError, ValueError):
print("Usage: script.py <directory_path> [-o output_file_path] [-w]")
sys.exit(1)
list_files_and_contents(directory, output_file)
if watch:
event_handler = ChangeHandler(directory, output_file, list_files_and_contents)
observer = Observer()
observer.schedule(event_handler, directory, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == "__main__":
main()