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) ⇒ Object



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

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

  paths = output.lines.map(&:chomp).reject(&:empty?)
  return paths unless include_ignored

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

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

.git_repository?(root) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


56
57
58
59
# File 'lib/kward/workspace/files.rb', line 56

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)


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

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

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



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

def list(root: Dir.pwd, include_ignored: false)
  paths = git_paths(root, include_ignored: include_ignored)
  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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kward/workspace/files.rb', line 39

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