class PDD::Sources

Code base abstraction

Public Class Methods

new(dir, ptns = []) click to toggle source

Ctor.

dir

Directory with source code files

# File lib/pdd/sources.rb, line 30
def initialize(dir, ptns = [])
  @dir = File.absolute_path(dir)
  @exclude = ptns + ['.git/**/*']
  @include = ptns + ['.git/**/*']
end

Public Instance Methods

exclude(ptn) click to toggle source
# File lib/pdd/sources.rb, line 63
def exclude(ptn)
  Sources.new(@dir, @exclude.push(ptn))
end
fetch() click to toggle source

Fetch all sources.

# File lib/pdd/sources.rb, line 37
def fetch
  files = Dir.glob(
    File.join(@dir, '**/*'), File::FNM_DOTMATCH
  ).reject { |f| File.directory?(f) }
  included = 0
  @include.each do |ptn|
    Dir.glob(File.join(@dir, ptn), File::FNM_DOTMATCH) do |f|
      files.keep_if { |i| i != f }
      included += 1
    end
  end
  PDD.log.info "#{files.size} file(s) found, #{included} files included"
  excluded = 0
  @exclude.each do |ptn|
    Dir.glob(File.join(@dir, ptn), File::FNM_DOTMATCH) do |f|
      files.delete_if { |i| i == f }
      excluded += 1
    end
  end
  PDD.log.info "#{files.size} file(s) found, #{excluded} excluded"
  files.reject { |f| binary?(f) }.map do |file|
    path = file[@dir.length + 1, file.length]
    VerboseSource.new(path, Source.new(file, path))
  end
end
include(ptn) click to toggle source
# File lib/pdd/sources.rb, line 67
def include(ptn)
  Sources.new(@dir, @include.push(ptn))
end

Private Instance Methods

binary?(file) click to toggle source

@todo #98:30min Change the implementation of this method

to also work in Windows machines. Investigate the possibility
of use a gem for this. After that, remove the skip of the test
`test_ignores_binary_files` in `test_sources.rb`.
# File lib/pdd/sources.rb, line 77
def binary?(file)
  return false if Gem.win_platform?
  `grep -qI '.' #{Shellwords.escape(file)}`
  if $CHILD_STATUS.success?
    false
  else
    PDD.log.info "#{file} is a binary file (#{File.size(file)} bytes)"
    true
  end
end