mirror of
https://github.com/simonw/files-to-prompt.git
synced 2025-10-23 00:02:47 +03:00
Handle multiple files and paths, closes #2
This commit is contained in:
@@ -23,11 +23,48 @@ def read_gitignore(path):
|
||||
return []
|
||||
|
||||
|
||||
def process_path(path, include_hidden, ignore_gitignore, gitignore_rules):
|
||||
if os.path.isfile(path):
|
||||
with open(path, "r") as f:
|
||||
file_contents = f.read()
|
||||
click.echo(path)
|
||||
click.echo("---")
|
||||
click.echo(file_contents)
|
||||
click.echo()
|
||||
click.echo("---")
|
||||
elif os.path.isdir(path):
|
||||
for root, dirs, files in os.walk(path):
|
||||
if not include_hidden:
|
||||
dirs[:] = [d for d in dirs if not d.startswith(".")]
|
||||
files = [f for f in files if not f.startswith(".")]
|
||||
|
||||
if not ignore_gitignore:
|
||||
gitignore_rules.extend(read_gitignore(root))
|
||||
dirs[:] = [
|
||||
d
|
||||
for d in dirs
|
||||
if not should_ignore(os.path.join(root, d), gitignore_rules)
|
||||
]
|
||||
files = [
|
||||
f
|
||||
for f in files
|
||||
if not should_ignore(os.path.join(root, f), gitignore_rules)
|
||||
]
|
||||
|
||||
for file in files:
|
||||
file_path = os.path.join(root, file)
|
||||
with open(file_path, "r") as f:
|
||||
file_contents = f.read()
|
||||
|
||||
click.echo(file_path)
|
||||
click.echo("---")
|
||||
click.echo(file_contents)
|
||||
click.echo()
|
||||
click.echo("---")
|
||||
|
||||
|
||||
@click.command()
|
||||
@click.argument(
|
||||
"path",
|
||||
type=click.Path(exists=True, file_okay=False, dir_okay=True, readable=True),
|
||||
)
|
||||
@click.argument("paths", nargs=-1, type=click.Path(exists=True))
|
||||
@click.option(
|
||||
"--include-hidden",
|
||||
is_flag=True,
|
||||
@@ -39,9 +76,9 @@ def read_gitignore(path):
|
||||
help="Ignore .gitignore files and include all files",
|
||||
)
|
||||
@click.version_option()
|
||||
def cli(path, include_hidden, ignore_gitignore):
|
||||
def cli(paths, include_hidden, ignore_gitignore):
|
||||
"""
|
||||
Takes a path to a folder and outputs every file in that folder,
|
||||
Takes one or more paths to files or directories and outputs every file,
|
||||
recursively, each one preceded with its filename like this:
|
||||
|
||||
path/to/file.py
|
||||
@@ -53,33 +90,10 @@ def cli(path, include_hidden, ignore_gitignore):
|
||||
---
|
||||
...
|
||||
"""
|
||||
gitignore_rules = [] if ignore_gitignore else read_gitignore(path)
|
||||
|
||||
for root, dirs, files in os.walk(path):
|
||||
if not include_hidden:
|
||||
dirs[:] = [d for d in dirs if not d.startswith(".")]
|
||||
files = [f for f in files if not f.startswith(".")]
|
||||
|
||||
gitignore_rules = []
|
||||
for path in paths:
|
||||
if not os.path.exists(path):
|
||||
raise click.BadArgumentUsage(f"Path does not exist: {path}")
|
||||
if not ignore_gitignore:
|
||||
gitignore_rules.extend(read_gitignore(root))
|
||||
dirs[:] = [
|
||||
d
|
||||
for d in dirs
|
||||
if not should_ignore(os.path.join(root, d), gitignore_rules)
|
||||
]
|
||||
files = [
|
||||
f
|
||||
for f in files
|
||||
if not should_ignore(os.path.join(root, f), gitignore_rules)
|
||||
]
|
||||
|
||||
for file in files:
|
||||
file_path = os.path.join(root, file)
|
||||
with open(file_path, "r") as f:
|
||||
file_contents = f.read()
|
||||
|
||||
click.echo(file_path)
|
||||
click.echo("---")
|
||||
click.echo(file_contents)
|
||||
click.echo()
|
||||
click.echo("---")
|
||||
gitignore_rules.extend(read_gitignore(os.path.dirname(path)))
|
||||
process_path(path, include_hidden, ignore_gitignore, gitignore_rules)
|
||||
|
||||
@@ -58,3 +58,72 @@ def test_ignore_gitignore(tmpdir):
|
||||
assert "test_dir/ignored.txt" in result.output
|
||||
assert "This file should be ignored" in result.output
|
||||
assert "test_dir/included.txt" in result.output
|
||||
|
||||
|
||||
def test_multiple_paths(tmpdir):
|
||||
runner = CliRunner()
|
||||
with tmpdir.as_cwd():
|
||||
os.makedirs("test_dir1")
|
||||
with open("test_dir1/file1.txt", "w") as f:
|
||||
f.write("Contents of file1")
|
||||
os.makedirs("test_dir2")
|
||||
with open("test_dir2/file2.txt", "w") as f:
|
||||
f.write("Contents of file2")
|
||||
with open("single_file.txt", "w") as f:
|
||||
f.write("Contents of single file")
|
||||
|
||||
result = runner.invoke(cli, ["test_dir1", "test_dir2", "single_file.txt"])
|
||||
assert result.exit_code == 0
|
||||
assert "test_dir1/file1.txt" in result.output
|
||||
assert "Contents of file1" in result.output
|
||||
assert "test_dir2/file2.txt" in result.output
|
||||
assert "Contents of file2" in result.output
|
||||
assert "single_file.txt" in result.output
|
||||
assert "Contents of single file" in result.output
|
||||
|
||||
|
||||
def test_mixed_paths_with_options(tmpdir):
|
||||
runner = CliRunner()
|
||||
with tmpdir.as_cwd():
|
||||
os.makedirs("test_dir")
|
||||
with open("test_dir/.gitignore", "w") as f:
|
||||
f.write("ignored.txt\n.hidden_ignored.txt")
|
||||
with open("test_dir/ignored.txt", "w") as f:
|
||||
f.write("This file should be ignored")
|
||||
with open("test_dir/.hidden_ignored.txt", "w") as f:
|
||||
f.write("This hidden file should be ignored")
|
||||
with open("test_dir/included.txt", "w") as f:
|
||||
f.write("This file should be included")
|
||||
with open("test_dir/.hidden_included.txt", "w") as f:
|
||||
f.write("This hidden file should be included")
|
||||
with open("single_file.txt", "w") as f:
|
||||
f.write("Contents of single file")
|
||||
|
||||
result = runner.invoke(cli, ["test_dir", "single_file.txt"])
|
||||
assert result.exit_code == 0
|
||||
assert "test_dir/ignored.txt" not in result.output
|
||||
assert "test_dir/.hidden_ignored.txt" not in result.output
|
||||
assert "test_dir/included.txt" in result.output
|
||||
assert "test_dir/.hidden_included.txt" not in result.output
|
||||
assert "single_file.txt" in result.output
|
||||
assert "Contents of single file" in result.output
|
||||
|
||||
result = runner.invoke(cli, ["test_dir", "single_file.txt", "--include-hidden"])
|
||||
assert result.exit_code == 0
|
||||
assert "test_dir/ignored.txt" not in result.output
|
||||
assert "test_dir/.hidden_ignored.txt" not in result.output
|
||||
assert "test_dir/included.txt" in result.output
|
||||
assert "test_dir/.hidden_included.txt" in result.output
|
||||
assert "single_file.txt" in result.output
|
||||
assert "Contents of single file" in result.output
|
||||
|
||||
result = runner.invoke(
|
||||
cli, ["test_dir", "single_file.txt", "--ignore-gitignore"]
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
assert "test_dir/ignored.txt" in result.output
|
||||
assert "test_dir/.hidden_ignored.txt" not in result.output
|
||||
assert "test_dir/included.txt" in result.output
|
||||
assert "test_dir/.hidden_included.txt" not in result.output
|
||||
assert "single_file.txt" in result.output
|
||||
assert "Contents of single file" in result.output
|
||||
|
||||
Reference in New Issue
Block a user