Skip to content
Kward Search API index

Module: Kward::ProjectFiles

Defined in:
lib/kward/workspace/files.rb

Overview

Discovers project files for prompt UI features.

Class Method Summary collapse

Class Method Details

.git_paths(root, include_ignored: false, literal_names: false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kward/workspace/files.rb', line 17

def git_paths(root, include_ignored: false, literal_names: false)
  command = ["git", "ls-files"]
  command << "-z" if literal_names
  output, status = Open3.capture2e(*command, "--cached", "--others", "--exclude-standard", chdir: root)
  return [] unless status.success?

  paths = split_git_paths(output, literal_names: literal_names)
  return paths unless include_ignored

  ignored_output, ignored_status = Open3.capture2e(*command, "--others", "--ignored", "--exclude-standard", chdir: root)
  return paths unless ignored_status.success?

  paths + split_git_paths(ignored_output, literal_names: literal_names)
rescue StandardError
  []
end

.git_repository?(root) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
# File 'lib/kward/workspace/files.rb', line 38

def git_repository?(root)
  _output, status = Open3.capture2e("git", "rev-parse", "--is-inside-work-tree", chdir: root)
  status.success?
rescue StandardError
  false
end

.ignored_directory?(relative) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
# File 'lib/kward/workspace/files.rb', line 62

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)


67
68
69
# File 'lib/kward/workspace/files.rb', line 67

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

.list(root: Dir.pwd, include_ignored: false, literal_names: false) ⇒ Object



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

def list(root: Dir.pwd, include_ignored: false, literal_names: false)
  paths = git_paths(root, include_ignored: include_ignored, literal_names: literal_names)
  paths = scanned_paths(root) if paths.empty? && !git_repository?(root)
  paths.reject { |path| path.empty? || path.end_with?("/") }.uniq.sort
end

.scanned_paths(root) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/kward/workspace/files.rb', line 45

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

.split_git_paths(output, literal_names:) ⇒ Object



34
35
36
# File 'lib/kward/workspace/files.rb', line 34

def split_git_paths(output, literal_names:)
  literal_names ? output.split("\0") : output.lines.map(&:chomp).reject(&:empty?)
end