-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathfull_code.txt
More file actions
405 lines (313 loc) · 12.5 KB
/
Copy pathfull_code.txt
File metadata and controls
405 lines (313 loc) · 12.5 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
Directory Tree:
all_code/
│ ├── README.md
│ ├── pyproject.toml
│ ├── full_code.txt
│ ├── .gitignore
│ ├── test_all_code.py
│ ├── .git/ [EXCLUDED]
│ ├── .venv/ [EXCLUDED]
│ ├── __pycache__/ [EXCLUDED]
# ======================
# File: README.md
# ======================
# All Code Aggregator
## Overview
The **All Code Aggregator** script consolidates all programming-related code files in the current directory into a single `full_code.txt` file. This is especially useful for providing your entire codebase to AI models for analysis or assistance.
## Features
- **Directory Tree Generation**: Creates an ASCII representation of the directory structure, excluding specified directories.
- **Code Aggregation**: Combines contents of programming-related files into a master file.
- **Customizable Inclusions/Exclusions**: Easily specify which files or directories to include or exclude.
## Setup
1. **From Github**
```shell
pip install git+https://github.com/foxalabs/all_code@0.4.0
```
2. **From local directory**
To install from a local directory, use edit mode so your code changes are reflected immediately. This is useful for development/debugging mode.
```shell
git clone https://github.com/foxalabs/all_code.git
cd all_code
pip install -e .
```
## Usage
**Basic usage**
```bash
all-code
```
**Specify a Directory**
```bash
all-code -d /path/to/start/directory
```
**Copy Aggregated Content to Clipboard Instead of Writing to File**
```bash
all-code -c
```
**Combine Both Arguments: Specify Directory and Copy to Clipboard**
```bash
all-code -d /path/to/start/directory -c
```
**Exclude Specific File Extensions**
```bash
all-code -X .json,.md,.html
```
**Combine Multiple Options: Specify Directory, Copy to Clipboard, and Exclude Extensions**
```bash
all-code -d /path/to/start/directory -c -X .json,.md,.html
```
**More options**
```bash
all-code --help
usage: all_code.py [-h] [-c] [-d DIRECTORY] [-o OUTPUT_FILE] [-i INCLUDE_FILES] [-x EXTENSIONS] [-e EXCLUDE_DIRS]
Aggregate code files into a master file with a directory tree.
options:
-h, --help show this help message and exit
-c, --clipboard Copy the aggregated content to the clipboard instead of writing to a file.
-d, --directory DIRECTORY
Specify the directory to start aggregation from. Defaults to the current working directory.
-o, --output-file OUTPUT_FILE
Name of the output file. Defaults to full_code.txt.
-i, --include-files INCLUDE_FILES
Comma-separated list of files to include. If not provided, all files are included.
-x, --extensions EXTENSIONS
Comma-separated list of programming extensions to use. Replaces the default set if provided.
-e, --exclude-dirs EXCLUDE_DIRS
Comma-separated list of directories to exclude. Replaces the default set if provided.
-X, --exclude-extensions EXCLUDE_EXTENSIONS
Comma-separated list of file extensions to exclude from aggregation.
```
## Exclusions
The **All Code Aggregator** script automatically excludes specific directories and files to streamline the aggregation process and avoid including unnecessary or sensitive information.
### Excluded Directories
The following directories are excluded by default:
- `venv`
- `.venv`
- `node_modules`
- `__pycache__`
- `.git`
- `dist`
- `build`
- `temp`
- `old_files`
- `flask_session`
**Purpose**: These directories are typically used for virtual environments, dependencies, build artifacts, version control, or temporary files that are not part of the core codebase.
### Excluded Extensions
Users can now exclude specific file extensions using the `-X` or `--exclude-extensions` argument.
**Example:**
```bash
all-code -X .json,.md,.html
```
This will exclude all .json, .md, and .html files from aggregation.
## Example Output to full_code.txt
```
Directory Tree:
all_code/
│ ├── full_code.txt
│ ├── README.md
│ ├── test.py
│ ├── .git/ [EXCLUDED]
│ ├── demo_folder/
│ │ ├── another_file.js
# ======================
# File: test.py
# ======================
def greet(name):
return f"Hello, {name}!"
if __name__ == "__main__":
print(greet("World"))
def farewell(name):
return f"Goodbye, {name}!"
print(farewell("World"))
# ======================
# File: demo_folder\another_file.js
# ======================
// another_file.js
// Function to add two numbers
function add(a, b) {
return a + b;
}
// Function to subtract two numbers
function subtract(a, b) {
return a - b;
}
// Function to multiply two numbers
function multiply(a, b) {
return a * b;
}
// Function to divide two numbers
function divide(a, b) {
if (b === 0) {
throw new Error("Division by zero is not allowed.");
}
return a / b;
}
// Example usage
console.log("Add: " + add(5, 3)); // Output: Add: 8
console.log("Subtract: " + subtract(5, 3)); // Output: Subtract: 2
console.log("Multiply: " + multiply(5, 3)); // Output: Multiply: 15
console.log("Divide: " + divide(5, 3)); // Output: Divide: 1.6666666666666667
```
## Testing the script
If you want to test the script, run the following command.
```bash
python test_all_code.py
```
As of the current commit, only the command line args and their effects were tested, but more tests can be added in the future.
# ======================
# File: pyproject.toml
# ======================
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "all-code"
version = "0.4.0"
description = "Aggregate code files into a single file with a directory tree."
readme = "README.md"
requires-python = ">=3.7"
license = { text = "MIT" }
authors = [
{ name = "Spencer Bentley" }
]
dependencies = []
[project.scripts]
all-code = "all_code:main"
[tool.hatch.build.targets.sdist]
include = [
"all_code.py",
"README.md"
]
[tool.hatch.build.targets.wheel]
# ======================
# File: test_all_code.py
# ======================
"""
Test script for all_code.py command-line arguments.
This script creates temporary directories and files, then uses the built-in
subprocess module to run all_code.py with different options:
- Default behavior (creates 'full_code.txt')
- Overriding the output file name with -o/--output-file
- Including only specific files with -i/--include-files
- Overriding programming extensions with -x/--extensions
- Excluding directories with -e/--exclude-dirs
No external libraries are required.
"""
import os
import shutil
import subprocess
import tempfile
SCRIPT_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "all_code.py")
def run_script(args, cwd):
"""
Run all_code.py with the provided command-line arguments in directory cwd.
"""
cmd = ["python", SCRIPT_PATH] + args
result = subprocess.run(cmd, cwd=cwd, capture_output=True, text=True)
return result
def test_default_arguments():
# Create a temporary directory to serve as the source for aggregation.
with tempfile.TemporaryDirectory() as tmp_dir:
# Copy all_code.py to tmp directory and run it without any args
TMP_SCRIPT_PATH = shutil.copy(SCRIPT_PATH, tmp_dir)
cmd = ["python", TMP_SCRIPT_PATH]
subprocess.run(cmd, cwd=tmp_dir, capture_output=True, text=True)
assert os.path.exists(os.path.join(tmp_dir, "full_code.txt")), "full_code.txt not created by default"
def test_directory_argument():
with tempfile.TemporaryDirectory() as source_dir:
# Create a dummy Python file in the source directory.
dummy_file = os.path.join(source_dir, "dummy.py")
with open(dummy_file, "w") as f:
f.write("print('Aggregating from source directory')")
# Use a separate temporary directory as the working directory for the script.
with tempfile.TemporaryDirectory() as tmp_working:
custom_output = "test_output.txt"
# Run the script with -d pointing to the source directory.
run_script(["-d", source_dir, "-o", custom_output], cwd=tmp_working)
output_file = os.path.join(tmp_working, custom_output)
assert os.path.exists(output_file), "Output file not created when using --directory"
with open(output_file, "r") as f:
content = f.read()
assert "dummy.py" in content, "Aggregated content does not reflect the --directory argument"
print("test_directory_argument passed.")
def test_default_output():
with tempfile.TemporaryDirectory() as tmpdir:
# Create a dummy Python file so that there is something to include.
dummy_file = os.path.join(tmpdir, "dummy.py")
with open(dummy_file, "w") as f:
f.write("print('Hello World')")
# Run the script with default output (should create full_code.txt)
run_script(["-d", tmpdir], cwd=tmpdir)
output_file = os.path.join(tmpdir, "full_code.txt")
assert os.path.exists(output_file), "Default output file not created"
print("test_default_output passed.")
def test_override_output_file():
with tempfile.TemporaryDirectory() as tmpdir:
dummy_file = os.path.join(tmpdir, "dummy.py")
with open(dummy_file, "w") as f:
f.write("print('Hello World')")
custom_output = "custom_output.txt"
run_script(["-d", tmpdir, "-o", custom_output], cwd=tmpdir)
output_file = os.path.join(tmpdir, custom_output)
assert os.path.exists(output_file), "Overridden output file not created"
print("test_override_output_file passed.")
def test_include_files():
with tempfile.TemporaryDirectory() as tmpdir:
# Create two Python files.
file1 = os.path.join(tmpdir, "dummy.py")
with open(file1, "w") as f:
f.write("print('Hello from dummy')")
file2 = os.path.join(tmpdir, "extra.py")
with open(file2, "w") as f:
f.write("print('Hello from extra')")
# Specify only dummy.py to be included.
run_script(["-d", tmpdir, "-i", "dummy.py"], cwd=tmpdir)
output_file = os.path.join(tmpdir, "full_code.txt")
with open(output_file, "r") as f:
content = f.read()
assert "dummy.py" in content, "dummy.py should be included"
assert "print('Hello from extra')" not in content, "extra.py should not be included"
print("test_include_files passed.")
def test_extensions():
with tempfile.TemporaryDirectory() as tmpdir:
# Create one .py file and one .txt file.
py_file = os.path.join(tmpdir, "dummy.py")
with open(py_file, "w") as f:
f.write("print('Python file')")
txt_file = os.path.join(tmpdir, "dummy.txt")
with open(txt_file, "w") as f:
f.write("This is a text file.")
# Override extensions to only include .py files.
run_script(["-d", tmpdir, "-x", ".py"], cwd=tmpdir)
output_file = os.path.join(tmpdir, "full_code.txt")
with open(output_file, "r") as f:
content = f.read()
assert "dummy.py" in content, "dummy.py should be included with .py extension"
assert "This is a text file." not in content, "dummy.txt should be excluded when only .py is allowed"
print("test_extensions passed.")
def test_exclude_dirs():
with tempfile.TemporaryDirectory() as tmpdir:
# Create a subdirectory that will be excluded.
exclude_dir = os.path.join(tmpdir, "exclude_me")
os.mkdir(exclude_dir)
file_in_excluded = os.path.join(exclude_dir, "dummy.py")
with open(file_in_excluded, "w") as f:
f.write("print('This file is in an excluded directory')")
# Run the script with exclude_dirs set to 'exclude_me'
run_script(["-d", tmpdir, "-e", "exclude_me"], cwd=tmpdir)
output_file = os.path.join(tmpdir, "full_code.txt")
with open(output_file, "r") as f:
content = f.read()
# The directory tree should mark exclude_me as excluded.
assert "exclude_me/ [EXCLUDED]" in content, "exclude_me directory should be marked as excluded"
# The file inside should not be aggregated.
assert "dummy.py" not in content, "File inside excluded directory should not be included"
print("test_exclude_dirs passed.")
if __name__ == "__main__":
test_default_arguments()
test_directory_argument()
test_default_output()
test_override_output_file()
test_include_files()
test_extensions()
test_exclude_dirs()
print("All tests passed!")