Skip to content

Commit

Permalink
[LocalPod] Fix for over aggressive #clean.
Browse files Browse the repository at this point in the history
Paths that contain or are contained by an used path are now preserved.
The new logic works well with used path that are directories.

Closes #300
  • Loading branch information
fabiopelosin committed Jun 6, 2012
1 parent 3c3daf1 commit b333671
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
46 changes: 26 additions & 20 deletions lib/cocoapods/local_pod.rb
Expand Up @@ -64,21 +64,36 @@ def implode
root.rmtree if exists?
end

# It deletes all the files identified by clean_files, then it removes
# all the empty folders or symlinks.
### Cleaning

# Public: Deletes any path that is not used by the pod.
def clean
clean_files.each { |path| FileUtils.rm_rf(path) }
clean_paths.each { |path| FileUtils.rm_rf(path) }
end

# Get all the directories. Then sort them from the longest
# to the shortest, so a directory will be empty if its
# subdirs where empty. We need to delete the symlinks because
# it might prevent a bundle from being deleted
dirs = Dir.glob(root + "**/*", File::FNM_DOTMATCH)
dirs = dirs.reject { |d| d.end_with?('.', '..') || !File.directory?(d) }.sort_by(&:length).reverse
dirs.each { |d| FileUtils.rm_rf(d) if File.symlink?(d) || (Dir.entries(d) == %w[ . .. ]) }
# Public: Finds the absolute paths, including hidden ones, of the files
# that are not used by the pod and can be safely deleted.
#
# Returns an Array of Strings containing the absolute paths.
def clean_paths
cached_used_paths = used_paths.map{ |path| path.to_s }
files = Dir.glob(root + "**/*", File::FNM_DOTMATCH)
files.reject! do |candidate|
candidate.end_with?('.', '..') ||
cached_used_paths.any? { |path| path.include?(candidate) || candidate.include?(path) }
end
files
end

# File attributes
# Public: Finds all the absolute paths used by pod.
#
# Returns an Array of Pathnames containing the absolute paths.
def used_paths
files = source_files(false) + resources(false) + preserve_paths + [ readme_file, license_file, prefix_header_file ]
files.compact
end

### File attributes

def prefix_header_file
root + top_specification.prefix_header_file if top_specification.prefix_header_file
Expand All @@ -96,15 +111,6 @@ def resources(relative = true)
chained_expanded_paths(:resources, :relative_to_sandbox => relative)
end

def clean_files
all_files = Dir.glob(root + "**/*", File::FNM_DOTMATCH).map { |f| root + f }.reject { |p| p.directory? }
all_files - used_files
end

def used_files
source_files(false) + resources(false) + preserve_paths + [ readme_file, license_file, prefix_header_file ]
end

# TODO: implement case insensitive search
def preserve_paths
chained_expanded_paths(:preserve_paths) + expanded_paths(%w[ *.podspec notice* NOTICE* CREDITS* ])
Expand Down
9 changes: 5 additions & 4 deletions spec/unit/local_pod_spec.rb
Expand Up @@ -38,10 +38,11 @@
end

it 'returns an expanded list the files to clean' do
clean_files = @pod.clean_files.map { |p| p.to_s }
clean_files.should.include "#{@sandbox.root}/BananaLib/.git/config"
clean_files_without_hidden = clean_files.reject { |p| p.to_s.include?('/.') }
clean_files_without_hidden.should == ["#{@sandbox.root}/BananaLib/sub-dir/sub-dir-2/somefile.txt"]
clean_paths = @pod.clean_paths.map { |p| p.to_s.gsub(/.*Pods\/BananaLib/,'') }
clean_paths.should.include "/.git/config"
# There are some hidden files on Travis
clean_files_without_hidden = clean_paths.reject { |p| p.to_s.include?('/.') }
clean_files_without_hidden.should == %W[ /sub-dir /sub-dir/sub-dir-2 /sub-dir/sub-dir-2/somefile.txt ]
end

it 'returns an expanded list of resources, relative to the sandbox root' do
Expand Down

0 comments on commit b333671

Please sign in to comment.