Commit 1f62fbd2 authored by Eloy Durán's avatar Eloy Durán

[PathList] Cleanup a bit and remove unused copy of strings.

parent 2d8bebdd
...@@ -44,12 +44,10 @@ module Pod ...@@ -44,12 +44,10 @@ module Pod
# #
def read_file_system def read_file_system
root_length = root.to_s.length+1 root_length = root.to_s.length+1
paths = Dir.glob(root + "**/*", File::FNM_DOTMATCH) paths = Dir.glob(root + "**/*", File::FNM_DOTMATCH)
paths = paths.map { |p| p[root_length..-1] } paths = paths.map { |p| p[root_length..-1] }
paths = paths.reject do |p| paths = paths.reject { |p| p == '.' || p == '..' }
p == '.' || p == '..' dirs_entries = paths.select { |path| path.end_with?('/.', '/..') }
end
dirs_entries = paths.select { |path| path.end_with?('/.', '/..') }
@files = paths - dirs_entries @files = paths - dirs_entries
@dirs = dirs_entries.map { |d| d.gsub(/\/\.\.?$/,'') }.uniq @dirs = dirs_entries.map { |d| d.gsub(/\/\.\.?$/,'') }.uniq
end end
...@@ -61,19 +59,21 @@ module Pod ...@@ -61,19 +59,21 @@ module Pod
relative_glob(patterns, dir_pattern, exclude_patterns).map {|p| root + p } relative_glob(patterns, dir_pattern, exclude_patterns).map {|p| root + p }
end end
# @return [Array<Pathname>] The list of the relative paths that are # @return [Array<Pathname>] The list of relative paths that are case
# case insensitively matched by a given pattern. This method emulates # insensitively matched by a given pattern. This method emulates
# {Dir#glob} with the {File::FNM_CASEFOLD} option. # {Dir#glob} with the {File::FNM_CASEFOLD} option.
# #
# @param [Array<String>] patterns A {Dir#glob} like pattern. # @param [String,Array<String>] patterns A signle {Dir#glob} like
# pattern, or a list of patterns.
# #
# @param [String] dir_pattern An optional pattern to append to # @param [String] dir_pattern An optional pattern to append to a
# pattern, if this one is the path of a # pattern, if it is the path to a
# directory. # directory.
# #
def relative_glob(patterns, dir_pattern = nil, exclude_patterns = nil) def relative_glob(patterns, dir_pattern = nil, exclude_patterns = nil)
return [] if patterns.empty? return [] if patterns.empty?
patterns = [ patterns ] if patterns.is_a? String patterns = [ patterns ] if patterns.is_a? String
list = patterns.map do |pattern| list = patterns.map do |pattern|
if pattern.is_a?(String) if pattern.is_a?(String)
pattern += '/' + dir_pattern if directory?(pattern) && dir_pattern pattern += '/' + dir_pattern if directory?(pattern) && dir_pattern
...@@ -84,14 +84,12 @@ module Pod ...@@ -84,14 +84,12 @@ module Pod
end end
end end
else else
pattern.to_s.cyan
files.select { |path| path.match(pattern) } files.select { |path| path.match(pattern) }
end end
end.flatten end.flatten
if exclude_patterns
excluded = relative_glob(exclude_patterns) list -= relative_glob(exclude_patterns) if exclude_patterns
list = exclude_patterns ? list - excluded : list
end
list.map { |path| Pathname.new(path) } list.map { |path| Pathname.new(path) }
end end
...@@ -101,29 +99,24 @@ module Pod ...@@ -101,29 +99,24 @@ module Pod
# @param [String, Pathname] sub_path The path that could be a directory. # @param [String, Pathname] sub_path The path that could be a directory.
# #
def directory?(sub_path) def directory?(sub_path)
sub_path = sub_path.to_s.downcase.gsub(/\/$/, '') sub_path = sub_path.to_s.downcase.sub(/\/$/, '')
dirs.any? { |dir| dir.downcase == sub_path } dirs.any? { |dir| dir.downcase == sub_path }
end end
# @return [Array<String>] An array containing the list of patterns for # @return [Array<String>] An array of patterns converted from a
# necessary to emulate {Dir#glob} with #{File.fnmatch}. If # {Dir.glob} pattern to patterns that {File.fnmatch} can handle. This
# #{File.fnmatch} invoked with the File::FNM_PATHNAME matches any of # is used by the {#relative_glob} method to emulate {Dir.glob}.
# the returned patterns {Dir#glob} would have matched the original
# pattern.
# #
# The expansion provides support for: # The expansion provides support for:
# #
# - Literals # - Literals
# #
# expand_pattern_literals('{file1,file2}.{h,m}') # dir_glob_equivalent_patterns('{file1,file2}.{h,m}')
# => ["file1.h", "file1.m", "file2.h", "file2.m"] # => ["file1.h", "file1.m", "file2.h", "file2.m"]
# #
# expand_pattern_literals('file*.*')
# => ["file*.*"]
#
# - Matching the direct children of a directory with `**` # - Matching the direct children of a directory with `**`
# #
# expand_pattern_literals('Classes/**/file.m') # dir_glob_equivalent_patterns('Classes/**/file.m')
# => ["Classes/**/file.m", "Classes/file.m"] # => ["Classes/**/file.m", "Classes/file.m"]
# #
# @param [String] pattern A {Dir#glob} like pattern. # @param [String] pattern A {Dir#glob} like pattern.
......
...@@ -65,7 +65,7 @@ describe Pod::LocalPod::PathList do ...@@ -65,7 +65,7 @@ describe Pod::LocalPod::PathList do
patterns.sort.should == %w| file1.h file1.m file2.h file2.m | patterns.sort.should == %w| file1.h file1.m file2.h file2.m |
end end
it "returns the original patter if there are no Dir#glob expansions" do it "returns the original pattern if there are no Dir#glob expansions" do
patterns = @path_list.dir_glob_equivalent_patterns('file*.*') patterns = @path_list.dir_glob_equivalent_patterns('file*.*')
patterns.sort.should == %w| file*.* | patterns.sort.should == %w| file*.* |
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment