Module: OllamaChat::Utils::AnalyzeDirectory

Defined in:
lib/ollama_chat/utils/analyze_directory.rb

Overview

A module that provides functionality for analyzing directory structures and generating file listings.

The AnalyzeDirectory module offers methods to traverse directory hierarchies and create structured representations of file systems. It supports recursive directory traversal, filtering of hidden files and symbolic links, and generation of detailed file and directory information including paths, names, and metadata.

Class Method Summary collapse

Class Method Details

.generate_structure(path = ?., exclude: []) ⇒ Array<Hash>, ...

Note:

Hidden files and directories (starting with ‘.’) are skipped

Note:

Symbolic links are skipped

Note:

The method uses recursive calls to traverse subdirectories

Note:

If an error occurs during traversal, it returns a hash with error details

Generates a directory structure representation with files and subdirectories.

defaults to current directory

directories, or a hash with error information if an exception occurs

Examples:

Generate structure for current directory

generate_structure

Generate structure for a specific path

generate_structure('/path/to/directory')

Parameters:

  • path (String) (defaults to: ?.)

    the path to start generating the structure from,

Returns:

  • (Array<Hash>, Hash)

    an array of hashes representing files and

  • (Array)

    an empty array if the path is invalid or has no children

  • (Hash)

    a hash with error details if an exception is raised during processing



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
# File 'lib/ollama_chat/utils/analyze_directory.rb', line 33

def generate_structure(path = ?., exclude: [])
  exclude = Array(exclude).map { Pathname.new(_1).expand_path }
  path    = Pathname.new(path).expand_path
  entries = []
  path.children.sort.each do |child|
    # Skip hidden files/directories
    next if child.basename.to_s.start_with?('.')
    # Skip symlinks
    next if child.symlink?
    # Skip if excluded
    next if exclude.any? { child.fnmatch?(_1.to_s, File::FNM_PATHNAME) }

    if child.directory?
      entries << {
        type: 'directory',
        name: child.basename.to_s,
        path: child.expand_path.to_s,
        children: generate_structure(child)
      }
    elsif child.file?
      entries << {
        type: 'file',
        path: child.expand_path.to_s,
        name: child.basename.to_s
      }
    end
  end
  entries
rescue => e
  { error: e.class, message: e.message }
end