Skip to content

Commit

Permalink
Merge pull request #323 from r4um/fix_exclude
Browse files Browse the repository at this point in the history
Remove empty directories only if they match exclude pattern.
  • Loading branch information
jordansissel committed Mar 21, 2013
2 parents 3054027 + d5f710d commit 2ac7700
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 35 deletions.
46 changes: 12 additions & 34 deletions lib/fpm/package.rb
@@ -1,6 +1,7 @@
require "fpm/namespace" # local
require "fpm/util" # local
require "pathname" # stdlib
require "find"
require "tmpdir" # stdlib
require "backports" # gem 'backports'
require "socket" # stdlib, for Socket.gethostname
Expand Down Expand Up @@ -361,43 +362,20 @@ def exclude
installdir = staging_path
end

exclude_path = proc do |file|
FileUtils.remove_entry_secure(staging_path(file))
Pathname.new(staging_path(file)).parent.ascend do |d|
if (::Dir.entries(d) - %w[ . .. ]).empty?
::Dir.rmdir(d)
@logger.info("Deleting empty directory left by removing exluded file", :path => d)
else
Find.find(staging_path) do |path|
match_path = path.sub("#{staging_path}/", '')

attributes[:excludes].each do |wildcard|
@logger.debug("Checking path against wildcard", :path => path, :wildcard => wildcard)

if File.fnmatch(wildcard, match_path)
@logger.info("Removing excluded path", :path => path, :matches => wildcard)
FileUtils.remove_entry_secure(path)
Find.prune
break
end
end
end # exclude_path

attributes[:excludes].each do |wildcard|
@logger.debug("Checking for things to exclude", :wildcard => wildcard)
files.each do |file|
@logger.debug("Checking path against wildcard", :path => file, :wildcard => wildcard)
if File.fnmatch(wildcard, file)
@logger.info("Removing excluded file", :path => file, :matches => wildcard)
exclude_path.call(file)
next
end

@logger.debug("Checking if path is a child of an excluded directory",
:path => file, :wildcard => wildcard,
:directory? => File.directory?(staging_path(wildcard)))
if File.directory?(staging_path(wildcard))
# issue #248, if the excludes entry is a directory, ignore that
# directory and anything inside it.
exclude_re = Regexp.new("^#{Regexp.escape(wildcard)}($|/)")
if exclude_re.match(file)
@logger.info("Removing excluded file which has a parent excluded directory",
:path => file, :excluded => wildcard)
exclude_path.call(file)
end
end
end
end # files.each
end
end # def exclude


Expand Down
12 changes: 11 additions & 1 deletion spec/fpm/package_spec.rb
Expand Up @@ -141,14 +141,24 @@
insist { subject.files } == ["hello"]
end

it "should obey attributes[:excludes] for directories" do
Dir.mkdir(subject.staging_path("example"))
Dir.mkdir(subject.staging_path("example/foo"))
File.write(subject.staging_path("example/foo/delete_me"), "Hello!")
File.write(subject.staging_path("keeper"), "Hello!")
subject.attributes[:excludes] = [ "example" ]
subject.instance_eval { exclude }
insist { subject.files } == [ "keeper" ]
end

it "should obey attributes[:excludes] for child directories" do
Dir.mkdir(subject.staging_path("example"))
Dir.mkdir(subject.staging_path("example/foo"))
File.write(subject.staging_path("example/foo/delete_me"), "Hello!")
File.write(subject.staging_path("keeper"), "Hello!")
subject.attributes[:excludes] = [ "example/foo" ]
subject.instance_eval { exclude }
insist { subject.files } == [ "keeper" ]
insist { subject.files.sort } == [ "example", "keeper" ]
end
end

Expand Down

0 comments on commit 2ac7700

Please sign in to comment.