Module: Moult::Discovery

Defined in:
lib/moult/discovery.rb

Overview

Finds the Ruby files to analyse under a root directory.

Inside a git repository we use git ls-files so .gitignore is respected for free (vendored and generated code is excluded as the repo intends). Otherwise we glob, explicitly skipping the usual non-source directories.

Constant Summary collapse

SKIP_DIRS =
%w[vendor tmp node_modules .git].freeze

Class Method Summary collapse

Class Method Details

.from_git(root) ⇒ Object



23
24
25
26
27
# File 'lib/moult/discovery.rb', line 23

def from_git(root)
  Git.listed_files(root)
    .select { |rel| rel.end_with?(".rb") }
    .map { |rel| File.join(root, rel) }
end

.from_glob(root) ⇒ Object



29
30
31
# File 'lib/moult/discovery.rb', line 29

def from_glob(root)
  Dir.glob(File.join(root, "**", "*.rb")).reject { |abs| skip?(abs, root) }
end

.ruby_files(root) ⇒ Array<String>

Returns absolute paths to .rb files, sorted.

Parameters:

  • root (String)

    absolute directory to search

Returns:

  • (Array<String>)

    absolute paths to .rb files, sorted



18
19
20
21
# File 'lib/moult/discovery.rb', line 18

def ruby_files(root)
  files = Git.repo?(root) ? from_git(root) : from_glob(root)
  files.sort
end

.skip?(abs, root) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/moult/discovery.rb', line 33

def skip?(abs, root)
  relative = abs.delete_prefix(root).delete_prefix(File::SEPARATOR)
  relative.split(File::SEPARATOR).any? { |segment| SKIP_DIRS.include?(segment) }
end