Class: PDD::Sources

Inherits:
Object
  • Object
show all
Defined in:
lib/pdd/sources.rb

Overview

Code base abstraction

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ Sources

Ctor.

dir

Directory with source code files



32
33
34
35
36
# File 'lib/pdd/sources.rb', line 32

def initialize(dir)
  @dir = File.absolute_path(dir)
  @exclude = ['.git/**/*']
  @include = []
end

Instance Method Details

#exclude(paths) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/pdd/sources.rb', line 58

def exclude(paths)
  paths = paths.nil? ? [] : paths
  paths = paths.is_a?(Array) ? paths : [paths]
  @exclude.push(*paths)
  paths&.each do |path|
    PDD.log.info "#{Rainbow('Excluding').orange} #{path}"
  end
  self
end

#fetchObject

Fetch all sources.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pdd/sources.rb', line 39

def fetch
  exclude_paths = @exclude.map do |ptn|
    Glob.new(File.join(@dir, ptn)).to_regexp
  end
  files = Dir.glob(
    File.join(@dir, '**/*'), File::FNM_DOTMATCH
  ).reject do |f|
    File.directory?(f) || exclude_paths.any? { |ptn| f.match(ptn) }
  end
  files += Dir.glob(
    @include.map { |ptn| File.join(@dir, ptn) }
  ).reject { |f| File.directory?(f) }
  files = files.uniq # remove duplicates
  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(paths) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/pdd/sources.rb', line 68

def include(paths)
  paths = paths.nil? ? [] : paths
  paths = paths.is_a?(Array) ? paths : [paths]
  @include.push(*paths)
  paths&.each do |path|
    PDD.log.info "#{Rainbow('Including').blue} #{path}"
  end
  self
end