develop.codewalker #
CodeWalker Module
The CodeWalker module provides functionality to walk through directories and create a map of files with their content. It's particularly useful for processing code directories while respecting gitignore patterns.
Features
- Walk through directories recursively
- Respect gitignore patterns to exclude files
- Store file content in memory
- Export files back to a directory structure
Usage
import incubaid.herolib.lib.lang.codewalker
mut cw := codewalker.new('/tmp/adir')!
// Get content of a specific file
content := cw.filemap.get('path/to/file.txt')!
// return output again
cw.filemap.content()
// Export all files to a destination directory
cw.filemap.export('/tmp/exported_files')!
format of filemap
full files
text before will be ignored
===FILE:filename===
code
===FILE:filename===
code
===END===
text behind will be ignored
files with changes
text before will be ignored
===FILECHANGE:filename===
code
===FILECHANGE:filename===
code
===END===
text behind will be ignored
FILECHANGE and FILE can be mixed, in FILE it means we have full content otherwise only changed content e.g. a method or s struct and then we need to use morph to change it
fn build_file_tree_fs #
fn build_file_tree_fs(roots []string, prefix string) string
build_file_tree_fs builds a file system tree for given root directories
fn build_file_tree_selected #
fn build_file_tree_selected(files []string, base_root string) string
build_file_tree_selected builds a minimal tree that contains only the selected files. The tree is rendered relative to base_root when provided.
fn build_selected_tree #
fn build_selected_tree(files []string, base_root string) string
build_selected_tree renders a minimal tree of the given file paths.- files: absolute or relative file paths
- base_root: if provided and files are absolute, the tree is rendered relative to this rootThe output marks files with a trailing " *" like the existing map convention.
fn gitignore_matcher_new #
fn gitignore_matcher_new() IgnoreMatcher
fn list_directory #
fn list_directory(base_path string, rel_path string) ![]DirItem
list_directory lists the contents of a directory.- base_path: workspace base path
- rel_path: relative path from base (or absolute path)Returns a list of DirItem with name and type (file/directory).
fn list_directory_filtered #
fn list_directory_filtered(base_path string, rel_path string, ignore_matcher &IgnoreMatcher) ![]DirItem
list_directory_filtered lists the contents of a directory with ignore filtering applied.- base_path: workspace base path
- rel_path: relative path from base (or absolute path)
- ignore_matcher: IgnoreMatcher to filter out ignored files/directoriesReturns a list of DirItem with name and type (file/directory), filtered by ignore patterns.
fn list_files_recursive #
fn list_files_recursive(root string) []string
list_files_recursive recursively lists all files in a directory
fn new #
fn new(args CodeWalkerArgs) !CodeWalker
fn resolve_path #
fn resolve_path(base_path string, rel_path string) string
resolve_path resolves a relative path against a base path. If rel_path is absolute, returns it as-is. If rel_path is empty, returns base_path.
struct CWError #
struct CWError {
pub:
message string
linenr int
category string
}
struct CodeWalker #
struct CodeWalker {
pub mut:
ignorematcher IgnoreMatcher
errors []CWError
}
fn (CodeWalker) parse #
fn (mut cw CodeWalker) parse(content string) !FileMap
Public factory to parse the filemap-text format directly
fn (CodeWalker) filemap_get #
fn (mut cw CodeWalker) filemap_get(args FileMapArgs) !FileMap
struct CodeWalkerArgs #
struct CodeWalkerArgs {
// No fields required for now; kept for API stability
}
struct DirItem #
struct DirItem {
pub:
name string
typ string
}
struct FMError #
struct FMError {
pub:
message string
linenr int // is optional
category string
filename string
}
struct FileMap #
struct FileMap {
pub mut:
source string
content map[string]string
content_change map[string]string
errors []FMError
}
fn (FileMap) content #
fn (mut fm FileMap) content() string
fn (FileMap) export #
fn (mut fm FileMap) export(path string) !
write in new location, all will be overwritten, will only work with full files, not changes
fn (FileMap) write #
fn (mut fm FileMap) write(path string) !
update the files as found in the folder and update them or create
fn (FileMap) get #
fn (fm FileMap) get(relpath string) !string
fn (FileMap) set #
fn (mut fm FileMap) set(relpath string, content string)
fn (FileMap) delete #
fn (mut fm FileMap) delete(relpath string)
fn (FileMap) find #
fn (fm FileMap) find(path string) []string
struct FileMapArgs #
struct FileMapArgs {
pub mut:
path string
content string
content_read bool = true // if we start from path, and this is on false then we don't read the content
}
struct IgnoreMatcher #
struct IgnoreMatcher {
pub mut:
rules []IgnoreRule
}
fn (IgnoreMatcher) add_content #
fn (mut m IgnoreMatcher) add_content(content string)
Add raw .gitignore-style content as global (root-scoped) rules
fn (IgnoreMatcher) add_content_with_base #
fn (mut m IgnoreMatcher) add_content_with_base(base_rel string, content string)
Add raw .gitignore/.heroignore-style content scoped to base_rel
fn (IgnoreMatcher) is_ignored #
fn (m IgnoreMatcher) is_ignored(relpath string) bool
Very simple glob/substring-based matching with directory scoping
struct WriteParams #
struct WriteParams {
path string
v_test bool = true
v_format bool = true
python_test bool
}
- README
- fn build_file_tree_fs
- fn build_file_tree_selected
- fn build_selected_tree
- fn gitignore_matcher_new
- fn list_directory
- fn list_directory_filtered
- fn list_files_recursive
- fn new
- fn resolve_path
- struct CWError
- struct CodeWalker
- struct CodeWalkerArgs
- struct DirItem
- struct FMError
- struct FileMap
- struct FileMapArgs
- struct IgnoreMatcher
- struct WriteParams