lib/pdd/sources.rb in pdd-0.20.7 vs lib/pdd/sources.rb in pdd-0.20.8
- old
+ new
@@ -16,67 +16,75 @@
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
+require 'rainbow'
require 'shellwords'
require 'English'
require_relative 'source'
+require_relative '../../utils/glob'
module PDD
# Code base abstraction
class Sources
# Ctor.
# +dir+:: Directory with source code files
- def initialize(dir, ptns = [])
+ def initialize(dir)
@dir = File.absolute_path(dir)
- @exclude = ptns + ['.git/**/*']
- @include = ptns + ['.git/**/*']
+ @exclude = ['.git/**/*']
+ @include = []
end
# Fetch all sources.
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 { |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
+ ).reject do |f|
+ File.directory?(f) || exclude_paths.any? { |ptn| f.match(ptn) }
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 += 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
- def exclude(ptn)
- Sources.new(@dir, @exclude.push(ptn))
+ 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
- def include(ptn)
- Sources.new(@dir, @include.push(ptn))
+ 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
private
# @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`.
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)"