Skip to content
Kward Search API index

Module: Kward::ProjectFiles

Defined in:
lib/kward/project_files.rb

Overview

Discovers project files for prompt UI features.

Class Method Summary collapse

Class Method Details

.git_paths(root) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/kward/project_files.rb', line 17

def git_paths(root)
  output, status = Open3.capture2("git", "ls-files", "--cached", "--others", "--exclude-standard", chdir: root)
  return [] unless status.success?

  output.lines.map(&:chomp).reject(&:empty?)
rescue StandardError
  []
end

.ignored_directory?(relative) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/kward/project_files.rb', line 43

def ignored_directory?(relative)
  ignored_directories = %w[.git .yardoc _yardoc node_modules rdoc tmp vendor/bundle]
  ignored_directories.include?(relative) || relative.start_with?(".git/")
end

.ignored_file?(relative) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/kward/project_files.rb', line 48

def ignored_file?(relative)
  relative.start_with?(".git/")
end

.list(root: Dir.pwd) ⇒ Object



11
12
13
14
15
# File 'lib/kward/project_files.rb', line 11

def list(root: Dir.pwd)
  paths = git_paths(root)
  paths = scanned_paths(root) if paths.empty?
  paths.reject { |path| path.empty? || path.end_with?("/") }.uniq.sort
end

.scanned_paths(root) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/kward/project_files.rb', line 26

def scanned_paths(root)
  root_path = Pathname.new(root)
  paths = []
  Find.find(root_path.to_s) do |path|
    relative = Pathname.new(path).relative_path_from(root_path).to_s
    if File.directory?(path)
      Find.prune if ignored_directory?(relative)
      next
    end

    paths << relative unless ignored_file?(relative)
  end
  paths
rescue StandardError
  []
end