Skip to content

Commit

Permalink
[cqfn#102] resolve merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
mbao01 committed Jan 3, 2022
2 parents a27de32 + bffbd33 commit 6a16c12
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 47 deletions.
3 changes: 0 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,5 @@ Style/MultilineBlockChain:
Enabled: false
Metrics/BlockLength:
Max: 50
# @todo #123:30m Needs to enable this check and fix all issues.
# For now, this is out of the scope of this issue.
# This issue appeared after update TargetRubyVersion to 2.3
Style/FrozenStringLiteralComment:
Enabled: false
22 changes: 2 additions & 20 deletions lib/pdd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,32 +86,14 @@ def initialize(opts)
PDD.log.info "Ruby version is #{RUBY_VERSION} at #{RUBY_PLATFORM}"
end

def include_files(sources)
@opts[:include]&.each do |path|
PDD.log.info "#{Rainbow('Including').blue} #{path}"
sources.include(path)
end
sources
end

def exclude_files(sources)
paths = (@opts[:exclude] || []) + (@opts['skip-gitignore'] || [])
paths&.each do |path|
PDD.log.info "#{Rainbow('Excluding').orange} #{path}"
sources.exclude(path)
end
sources
end

# Generate XML.
def xml
dir = @opts[:source] || Dir.pwd
PDD.log.info "Reading from root dir #{dir}"
require_relative 'pdd/sources'
sources = Sources.new(File.expand_path(dir))
sources = include_files sources
sources = exclude_files sources

sources.exclude((@opts[:exclude] || []) + (@opts['skip-gitignore'] || []))
sources.include(@opts[:include])
sanitize(
rules(
Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
Expand Down
48 changes: 24 additions & 24 deletions lib/pdd/sources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

require 'rainbow'
require 'English'
require 'filemagic'
require_relative 'source'
require_relative '../../utils/glob'

module PDD
# Code base abstraction
Expand All @@ -35,43 +37,41 @@ def initialize(dir)

# 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
excluded = 0
unless @include.empty?
@include.each do |ptn|
Dir.glob(File.join(@dir, ptn), File::FNM_DOTMATCH) do |f|
files.push(f)
included += 1
end
end
end
unless @exclude.empty?
@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
).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
PDD.log.info "#{files.size} file(s) found, "\
"#{included} files included, #{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

def exclude(ptn)
@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)
@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

Expand Down
67 changes: 67 additions & 0 deletions utils/glob.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright (c) 2014-2021 Yegor Bugayenko
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the 'Software'), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
# 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.

# Utility glob class
class Glob
NO_LEADING_DOT = '(?=[^\.])'.freeze

def initialize(glob_string)
@glob_string = glob_string
end

# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/MethodLength
def to_regexp
chars = @glob_string.gsub(%r{(\*\*\/\*)|(\*\*)}, '*').split('')
in_curlies = 0, escaping = false
chars.map do |char|
if escaping
escaping = false
return char
end
case char
when '*'
'.*'
when '?'
'.'
when '.'
'\\.'
when '{'
in_curlies += 1
'('
when '}'
if in_curlies.positive?
in_curlies -= 1
return ')'
end
return char
when ','
in_curlies.positive? ? '|' : char
when '\\'
escaping = true
'\\'
else
char
end
end.join
end
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/MethodLength
end

0 comments on commit 6a16c12

Please sign in to comment.