Skip to content

Commit 882c3fc

Browse files
committed
Initial commit.
0 parents  commit 882c3fc

File tree

9 files changed

+365
-0
lines changed

9 files changed

+365
-0
lines changed

.gitignore

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# poetry
98+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99+
# This is especially recommended for binary packages to ensure reproducibility, and is more
100+
# commonly ignored for libraries.
101+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102+
#poetry.lock
103+
104+
# pdm
105+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106+
#pdm.lock
107+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108+
# in version control.
109+
# https://pdm-project.org/#use-with-ide
110+
.pdm.toml
111+
.pdm-python
112+
.pdm-build/
113+
114+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
115+
__pypackages__/
116+
117+
# Celery stuff
118+
celerybeat-schedule
119+
celerybeat.pid
120+
121+
# SageMath parsed files
122+
*.sage.py
123+
124+
# Environments
125+
.env
126+
.venv
127+
env/
128+
venv/
129+
ENV/
130+
env.bak/
131+
venv.bak/
132+
133+
# Spyder project settings
134+
.spyderproject
135+
.spyproject
136+
137+
# Rope project settings
138+
.ropeproject
139+
140+
# mkdocs documentation
141+
/site
142+
143+
# mypy
144+
.mypy_cache/
145+
.dmypy.json
146+
dmypy.json
147+
148+
# Pyre type checker
149+
.pyre/
150+
151+
# pytype static type analyzer
152+
.pytype/
153+
154+
# Cython debug symbols
155+
cython_debug/
156+
157+
# PyCharm
158+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
159+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
160+
# and can be added to the global gitignore or merged into this file. For a more nuclear
161+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
162+
#.idea/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 B.T. Franklin
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Coderoller
2+
3+
`Coderoller` is a Python utility that flattens a source code repository into a single markdown file. This tool collects all relevant source and configuration files, including Python, JavaScript, TypeScript, HTML, CSS, JSON, and more, and compiles them into a markdown document. The flattened file provides an organized overview of the repository's contents, making it easy to use in conjunction with LLMs. Simply copy the contents of the flattened file and paste it into your LLM chat context. The flattened form is also great for API-based uses of LLMs in automated workflows.
4+
5+
## Features
6+
7+
- **Flattens source code repositories** into a single markdown file.
8+
- **Supports multiple file types** including `.py`, `.js`, `.jsx`, `.ts`, `.tsx`, `.swift`, `.go`, `.java`, `.c`, `.cpp`, `.h`, `.hpp`, `.cs`, `.lua`, `.rb`, `.php`, `.pl`, `.html`, `.css`, `.json`, `.toml`, `.md`, `.yaml`, `.yml`, `.conf`, `.ini`, and `.sh`.
9+
- **Automatically includes README** files if present, placing it at the start of the flattened file.
10+
- **Excludes hidden files and directories** (those starting with a dot).
11+
12+
## Installation
13+
14+
Coderoller requires Python 3.12 or later and PDM for package management.
15+
16+
1. Clone the repository:
17+
18+
```bash
19+
git clone https://github.com/yourusername/coderoller.git
20+
cd coderoller
21+
```
22+
23+
2. Install dependencies using PDM:
24+
25+
```bash
26+
pdm install
27+
```
28+
29+
## Usage
30+
31+
To flatten a source repository, use the `coderoller-flatten-repo` script.
32+
33+
```bash
34+
pdm run coderoller-flatten-repo /path/to/root/folder
35+
```
36+
37+
This command will create a markdown file named `reponame.flat.md` in the current working directory, containing the flattened contents of the repository.
38+
39+
## License
40+
41+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

pdm.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[project]
2+
name = "coderoller"
3+
version = "0.1.0"
4+
description = "A collection of tools to flatten and process code repositories"
5+
authors = [
6+
{name = "B.T. Franklin", email = "brandon.franklin@gmail.com"},
7+
]
8+
dependencies = []
9+
requires-python = ">=3.12"
10+
readme = "README.md"
11+
license = {text = "MIT"}
12+
13+
[build-system]
14+
requires = ["pdm-backend"]
15+
build-backend = "pdm.backend"
16+
17+
18+
[tool.pdm]
19+
distribution = true
20+
21+
[project.scripts]
22+
coderoller-flatten-repo = "coderoller.flatten_repo:main"

src/coderoller/__init__.py

Whitespace-only changes.

src/coderoller/flatten_repo.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import sys
2+
from coderoller.source_repo_flattener import flatten_repo
3+
4+
5+
def main():
6+
if len(sys.argv) != 2:
7+
print("Usage: coderoller-flatten-repo <root_folder>")
8+
sys.exit(1)
9+
10+
root_folder = sys.argv[1]
11+
flatten_repo(root_folder)
12+
13+
14+
if __name__ == "__main__":
15+
main()
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import os
2+
3+
# Dictionary mapping file extensions to their corresponding long form names
4+
FILE_TYPES = {
5+
".py": "python",
6+
".js": "javascript",
7+
".jsx": "jsx",
8+
".ts": "typescript",
9+
".tsx": "tsx",
10+
".swift": "swift",
11+
".go": "go",
12+
".java": "java",
13+
".c": "c",
14+
".cpp": "c++",
15+
".h": "c",
16+
".hpp": "c++",
17+
".cs": "csharp",
18+
".lua": "lua",
19+
".rb": "ruby",
20+
".php": "php",
21+
".pl": "perl",
22+
".html": "html",
23+
".css": "css",
24+
".json": "json",
25+
".toml": "toml",
26+
".md": "markdown",
27+
".yaml": "yaml",
28+
".yml": "yaml",
29+
".conf": "config",
30+
".ini": "ini",
31+
".sh": "shell",
32+
}
33+
34+
35+
def find_readme(root_folder: str) -> str:
36+
"""
37+
Find a README file in the root folder with any common README extension.
38+
39+
Args:
40+
root_folder (str): The root folder to search in.
41+
42+
Returns:
43+
str: The path to the README file if found, else an empty string.
44+
"""
45+
for filename in os.listdir(root_folder):
46+
if filename.lower().startswith("readme"):
47+
return os.path.join(root_folder, filename)
48+
return ""
49+
50+
51+
def flatten_repo(root_folder: str):
52+
"""
53+
Flatten the source repository into a single markdown file.
54+
55+
Args:
56+
root_folder (str): The root folder of the repository.
57+
"""
58+
repo_name = os.path.basename(os.path.normpath(root_folder))
59+
flattened_file_path = os.path.join(os.getcwd(), f"{repo_name}.flat.md")
60+
61+
readme_path = find_readme(root_folder)
62+
63+
with open(flattened_file_path, "w") as flat_file:
64+
flat_file.write(f"# Contents of {repo_name} source tree\n\n")
65+
66+
# Handle README file
67+
if readme_path:
68+
with open(readme_path, "r") as readme_file:
69+
readme_contents = readme_file.read()
70+
flat_file.write("## README\n\n")
71+
flat_file.write("```markdown\n")
72+
flat_file.write(readme_contents)
73+
flat_file.write("\n```\n\n")
74+
print(f"Included README file: {readme_path}")
75+
76+
# Recursively walk the repo and collect relevant files
77+
for dirpath, dirnames, filenames in os.walk(root_folder):
78+
# Exclude hidden directories
79+
dirnames[:] = [d for d in dirnames if not d.startswith(".")]
80+
81+
for filename in filenames:
82+
if filename.startswith("."):
83+
continue # Exclude hidden files
84+
extension = os.path.splitext(filename)[1]
85+
full_path = os.path.join(dirpath, filename)
86+
if extension in FILE_TYPES and full_path != readme_path:
87+
with open(full_path, "r") as file:
88+
file_contents = file.read()
89+
relative_path = os.path.relpath(full_path, root_folder)
90+
flat_file.write(f"## File: {relative_path}\n\n")
91+
flat_file.write(f"```{FILE_TYPES[extension]}\n")
92+
flat_file.write(file_contents)
93+
flat_file.write("\n```\n\n")
94+
print(f"Included file: {full_path}")
95+
96+
print(f"Flattening complete. Output saved to {flattened_file_path}")

tests/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)