Compare commits
5 Commits
88ac87a0b6
...
048c275b55
Author | SHA1 | Date |
---|---|---|
|
048c275b55 | |
|
eec232ae53 | |
|
2a034e9c0e | |
|
e230128be0 | |
|
64596eaf46 |
|
@ -70,6 +70,8 @@
|
||||||
"xutility": "cpp",
|
"xutility": "cpp",
|
||||||
"fstream": "cpp",
|
"fstream": "cpp",
|
||||||
"iostream": "cpp",
|
"iostream": "cpp",
|
||||||
"codecvt": "cpp"
|
"codecvt": "cpp",
|
||||||
|
"map": "cpp",
|
||||||
|
"xtree": "cpp"
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -69,10 +69,9 @@ def main():
|
||||||
create_tree_structurer()
|
create_tree_structurer()
|
||||||
|
|
||||||
if args.command == 'display':
|
if args.command == 'display':
|
||||||
if args.verbose:
|
|
||||||
print(f"Analyzing directory: {args.path}")
|
|
||||||
try:
|
try:
|
||||||
structure = get_structure(args.path)
|
structure = get_structure(args.path)
|
||||||
|
print(f"Analyzing directory: {args.path}")
|
||||||
for line in structure:
|
for line in structure:
|
||||||
print(line)
|
print(line)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
|
|
|
@ -123,7 +123,6 @@ std::vector<fs::path> TreeStructurer::get_filtered_paths(const fs::path& start)
|
||||||
|
|
||||||
std::vector<std::string> TreeStructurer::get_directory_structure(const std::string& startpath) {
|
std::vector<std::string> TreeStructurer::get_directory_structure(const std::string& startpath) {
|
||||||
std::vector<std::string> result;
|
std::vector<std::string> result;
|
||||||
// Normalize the input path by removing ./ or .\ prefix if present
|
|
||||||
std::string normalized_path = startpath;
|
std::string normalized_path = startpath;
|
||||||
if (normalized_path.size() >= 2 &&
|
if (normalized_path.size() >= 2 &&
|
||||||
(normalized_path.substr(0, 2) == ".\\" || normalized_path.substr(0, 2) == "./")) {
|
(normalized_path.substr(0, 2) == ".\\" || normalized_path.substr(0, 2) == "./")) {
|
||||||
|
@ -140,145 +139,51 @@ std::vector<std::string> TreeStructurer::get_directory_structure(const std::stri
|
||||||
throw std::runtime_error("Path is not a directory: " + start.string());
|
throw std::runtime_error("Path is not a directory: " + start.string());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start with the root node
|
std::vector<fs::path> paths = get_filtered_paths(start);
|
||||||
std::string root_name = start.filename().string();
|
std::vector<bool> is_last_per_level;
|
||||||
result.push_back(root_name + "/");
|
|
||||||
|
|
||||||
// First, collect all paths and organize them by their parent directories
|
// Skip the first path as it's the root
|
||||||
std::map<std::string, std::vector<fs::path>> dir_contents;
|
for (size_t i = 1; i < paths.size(); ++i) {
|
||||||
std::vector<std::string> dir_paths; // To maintain order of directories
|
fs::path relative = fs::relative(paths[i], start);
|
||||||
|
std::vector<std::string> components;
|
||||||
|
for (const auto& comp : relative) {
|
||||||
|
components.push_back(comp.string());
|
||||||
|
}
|
||||||
|
|
||||||
// Add root directory
|
// Calculate the current level
|
||||||
dir_contents["/"] = {};
|
size_t level = components.size() - 1;
|
||||||
dir_paths.push_back("/");
|
|
||||||
|
|
||||||
// Collect files and directories
|
// Adjust is_last_per_level vector size
|
||||||
for (const auto& entry : fs::recursive_directory_iterator(start)) {
|
while (is_last_per_level.size() <= level) {
|
||||||
const auto& path = entry.path();
|
is_last_per_level.push_back(false);
|
||||||
std::string rel_path = get_relative_path(path, start);
|
}
|
||||||
|
|
||||||
// Replace backslashes with forward slashes
|
// Determine if this is the last item at its level
|
||||||
std::replace(rel_path.begin(), rel_path.end(), '\\', '/');
|
bool is_last = (i == paths.size() - 1) ||
|
||||||
|
(i + 1 < paths.size() &&
|
||||||
|
fs::relative(paths[i + 1], start).begin()->string() != components[0]);
|
||||||
|
|
||||||
// Skip if it should be ignored
|
is_last_per_level[level] = is_last;
|
||||||
bool should_skip = false;
|
|
||||||
for (const auto& component : path) {
|
// Build the line prefix
|
||||||
if (should_ignore_dir(component.string())) {
|
std::string line;
|
||||||
should_skip = true;
|
for (size_t j = 0; j < level; ++j) {
|
||||||
break;
|
if (j == level - 1) {
|
||||||
|
line += is_last ? "└── " : "├── ";
|
||||||
|
} else {
|
||||||
|
line += is_last_per_level[j] ? " " : "│ ";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (should_skip) continue;
|
|
||||||
|
|
||||||
if (entry.is_directory()) {
|
// Add the file/directory name
|
||||||
if (should_ignore_dir(path.filename().string())) {
|
line += components.back();
|
||||||
continue;
|
if (fs::is_directory(paths[i])) {
|
||||||
}
|
line += "/";
|
||||||
|
|
||||||
// Add directory to its parent
|
|
||||||
std::string parent_path = "/";
|
|
||||||
size_t last_slash = rel_path.find_last_of('/');
|
|
||||||
if (last_slash != std::string::npos) {
|
|
||||||
parent_path = rel_path.substr(0, last_slash);
|
|
||||||
if (parent_path.empty()) {
|
|
||||||
parent_path = "/";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create parent directory entry if it doesn't exist
|
|
||||||
if (dir_contents.find(parent_path) == dir_contents.end()) {
|
|
||||||
dir_contents[parent_path] = {};
|
|
||||||
dir_paths.push_back(parent_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create directory entry
|
|
||||||
dir_contents[parent_path].push_back(path);
|
|
||||||
|
|
||||||
// Create entry for this directory's contents
|
|
||||||
dir_contents[rel_path] = {};
|
|
||||||
dir_paths.push_back(rel_path);
|
|
||||||
} else {
|
|
||||||
if (should_ignore_file(path.filename().string())) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add file to its parent
|
|
||||||
std::string parent_path = "/";
|
|
||||||
size_t last_slash = rel_path.find_last_of('/');
|
|
||||||
if (last_slash != std::string::npos) {
|
|
||||||
parent_path = rel_path.substr(0, last_slash);
|
|
||||||
if (parent_path.empty()) {
|
|
||||||
parent_path = "/";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create parent directory entry if it doesn't exist
|
|
||||||
if (dir_contents.find(parent_path) == dir_contents.end()) {
|
|
||||||
dir_contents[parent_path] = {};
|
|
||||||
dir_paths.push_back(parent_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
dir_contents[parent_path].push_back(path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result.push_back(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now create the tree structure
|
|
||||||
std::function<void(const std::string&, const std::string&, std::vector<bool>&)>
|
|
||||||
build_tree = [&](const std::string& dir_path, const std::string& prefix, std::vector<bool>& is_last_stack) {
|
|
||||||
// Sort paths - directories first, then files
|
|
||||||
std::vector<fs::path>& paths = dir_contents[dir_path];
|
|
||||||
std::sort(paths.begin(), paths.end(), [](const fs::path& a, const fs::path& b) {
|
|
||||||
bool a_is_dir = fs::is_directory(a);
|
|
||||||
bool b_is_dir = fs::is_directory(b);
|
|
||||||
if (a_is_dir != b_is_dir) {
|
|
||||||
return a_is_dir > b_is_dir; // Directories before files
|
|
||||||
}
|
|
||||||
return a.filename() < b.filename(); // Alphabetical order
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add entries for this directory
|
|
||||||
for (size_t i = 0; i < paths.size(); ++i) {
|
|
||||||
const auto& path = paths[i];
|
|
||||||
bool is_last = (i == paths.size() - 1);
|
|
||||||
|
|
||||||
// Build the line prefix
|
|
||||||
std::string line_prefix = prefix;
|
|
||||||
if (!prefix.empty()) {
|
|
||||||
line_prefix += is_last ? "└── " : "├── ";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add the file/directory name
|
|
||||||
std::string name = path.filename().string();
|
|
||||||
std::string line = line_prefix + name;
|
|
||||||
|
|
||||||
if (fs::is_directory(path)) {
|
|
||||||
line += "/";
|
|
||||||
}
|
|
||||||
|
|
||||||
result.push_back(line);
|
|
||||||
|
|
||||||
// If it's a directory, process its contents with updated prefix
|
|
||||||
if (fs::is_directory(path)) {
|
|
||||||
std::string rel_path = get_relative_path(path, start);
|
|
||||||
std::replace(rel_path.begin(), rel_path.end(), '\\', '/');
|
|
||||||
|
|
||||||
std::string next_prefix = prefix;
|
|
||||||
if (!prefix.empty()) {
|
|
||||||
next_prefix += is_last ? " " : "│ ";
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<bool> next_is_last_stack = is_last_stack;
|
|
||||||
next_is_last_stack.push_back(is_last);
|
|
||||||
|
|
||||||
build_tree(rel_path, next_prefix, next_is_last_stack);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Start building the tree from the root
|
|
||||||
std::vector<bool> is_last_stack;
|
|
||||||
build_tree("/", "", is_last_stack);
|
|
||||||
|
|
||||||
} catch (const fs::filesystem_error& e) {
|
} catch (const fs::filesystem_error& e) {
|
||||||
throw std::runtime_error("Failed to access directory: " + std::string(e.what()));
|
throw std::runtime_error("Failed to access directory: " + std::string(e.what()));
|
||||||
}
|
}
|
||||||
|
@ -527,8 +432,6 @@ std::vector<std::string> TreeStructurer::read_structure_file(const std::string&
|
||||||
line.pop_back();
|
line.pop_back();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << "Processing line: " << line << std::endl;
|
|
||||||
|
|
||||||
size_t hash_pos = line.find('#');
|
size_t hash_pos = line.find('#');
|
||||||
size_t single_line_comment_pos = line.find("//");
|
size_t single_line_comment_pos = line.find("//");
|
||||||
size_t multi_line_comment_start_pos = line.find("/*");
|
size_t multi_line_comment_start_pos = line.find("/*");
|
||||||
|
@ -536,15 +439,12 @@ std::vector<std::string> TreeStructurer::read_structure_file(const std::string&
|
||||||
if (hash_pos != std::string::npos) {
|
if (hash_pos != std::string::npos) {
|
||||||
// Trim the line at the hash comment
|
// Trim the line at the hash comment
|
||||||
line = line.substr(0, hash_pos);
|
line = line.substr(0, hash_pos);
|
||||||
std::cout << "Trimmed line at hash comment position: " << hash_pos << std::endl;
|
|
||||||
} else if (single_line_comment_pos != std::string::npos) {
|
} else if (single_line_comment_pos != std::string::npos) {
|
||||||
// Trim the line at the single-line comment
|
// Trim the line at the single-line comment
|
||||||
line = line.substr(0, single_line_comment_pos);
|
line = line.substr(0, single_line_comment_pos);
|
||||||
std::cout << "Trimmed line at single-line comment position: " << single_line_comment_pos << std::endl;
|
|
||||||
} else if (multi_line_comment_start_pos != std::string::npos) {
|
} else if (multi_line_comment_start_pos != std::string::npos) {
|
||||||
// Trim the line at the multi-line comment start
|
// Trim the line at the multi-line comment start
|
||||||
line = line.substr(0, multi_line_comment_start_pos);
|
line = line.substr(0, multi_line_comment_start_pos);
|
||||||
std::cout << "Trimmed line at multi-line comment start position: " << multi_line_comment_start_pos << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove leading and trailing whitespace
|
// Remove leading and trailing whitespace
|
||||||
|
@ -553,9 +453,7 @@ std::vector<std::string> TreeStructurer::read_structure_file(const std::string&
|
||||||
|
|
||||||
if (!line.empty()) {
|
if (!line.empty()) {
|
||||||
lines.push_back(line);
|
lines.push_back(line);
|
||||||
std::cout << "Added non-empty line: " << line << std::endl;
|
|
||||||
} else {
|
} else {
|
||||||
std::cout << "Skipped empty or commented-out line." << std::endl;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -67,16 +67,50 @@ def test_display_verbose(tmp_path):
|
||||||
# Only check if we got any output, ignoring encoding errors
|
# Only check if we got any output, ignoring encoding errors
|
||||||
assert stdout != "" or stderr != ""
|
assert stdout != "" or stderr != ""
|
||||||
|
|
||||||
|
|
||||||
|
# ... rest of code here ...
|
||||||
|
|
||||||
|
# ... rest of code here ...
|
||||||
|
|
||||||
def test_create_directory_from_file(tmp_path):
|
def test_create_directory_from_file(tmp_path):
|
||||||
structure_file = tmp_path / 'structure.txt'
|
# Define the desired directory structure in a string with proper indentation
|
||||||
structure_content = "dir1/\n file1.txt"
|
structure_content = """data/
|
||||||
structure_file.write_text(structure_content)
|
sample_data.csv
|
||||||
|
docs/
|
||||||
|
README.md
|
||||||
|
requirements.txt
|
||||||
|
setup.py
|
||||||
|
src/
|
||||||
|
__init__.py
|
||||||
|
main.py
|
||||||
|
utils.py
|
||||||
|
tests/
|
||||||
|
__init__.py
|
||||||
|
test_main.py
|
||||||
|
test_utils.py"""
|
||||||
|
|
||||||
|
|
||||||
output_dir = tmp_path / 'output'
|
output_dir = tmp_path / 'output'
|
||||||
output_dir.mkdir()
|
output_dir.mkdir()
|
||||||
|
|
||||||
stdout, stderr = run_prodir(['create', str(structure_file), '-o', str(output_dir)])
|
stdout, stderr = run_prodir(['create', "example.md" '-o'])
|
||||||
assert os.path.exists(output_dir / 'dir1' / 'file1.txt')
|
|
||||||
|
# Print the stdout and stderr for debugging
|
||||||
|
print("stdout:", stdout)
|
||||||
|
print("stderr:", stderr)
|
||||||
|
|
||||||
|
|
||||||
|
assert os.path.exists('my_project/data/sample_data.csv')
|
||||||
|
assert os.path.exists('my_project/docs/README.md')
|
||||||
|
assert os.path.exists('my_project/requirements.txt')
|
||||||
|
assert os.path.exists('my_project/setup.py')
|
||||||
|
assert os.path.exists('my_project/src/__init__.py')
|
||||||
|
assert os.path.exists('my_project/src/main.py')
|
||||||
|
assert os.path.exists('my_project/src/utils.py')
|
||||||
|
assert os.path.exists('my_project/tests/__init__.py')
|
||||||
|
assert os.path.exists('my_project/tests/test_main.py')
|
||||||
|
assert os.path.exists('my_project/tests/test_utils.py')
|
||||||
|
|
||||||
|
|
||||||
def test_create_verbose(tmp_path):
|
def test_create_verbose(tmp_path):
|
||||||
structure_file = tmp_path / 'structure.txt'
|
structure_file = tmp_path / 'structure.txt'
|
||||||
|
@ -86,9 +120,9 @@ def test_create_verbose(tmp_path):
|
||||||
output_dir = tmp_path / 'output'
|
output_dir = tmp_path / 'output'
|
||||||
output_dir.mkdir()
|
output_dir.mkdir()
|
||||||
|
|
||||||
stdout, stderr = run_prodir(['create', str(structure_file), '-o', str(output_dir), '-v'])
|
stdout, stderr = run_prodir(['create', "example.md" '-v'])
|
||||||
# Only check if we got any output and the directory was created
|
# Only check if we got any output and the directory was created
|
||||||
assert os.path.exists(output_dir / 'dir1' / 'file1.txt')
|
assert os.path.exists( 'my_project/requirements.txt')
|
||||||
|
|
||||||
def test_display_invalid_path():
|
def test_display_invalid_path():
|
||||||
# Use an absolute path with some random UUID to ensure it doesn't exist
|
# Use an absolute path with some random UUID to ensure it doesn't exist
|
||||||
|
|
Loading…
Reference in New Issue