Skip to content

Instantly share code, notes, and snippets.

@chrisk
Created January 4, 2010 16:46
Show Gist options
  • Save chrisk/268645 to your computer and use it in GitHub Desktop.
Save chrisk/268645 to your computer and use it in GitHub Desktop.
Rails initializers that clean up git-submodule droppings
module EmptyDirectoryHelper
def self.dir_contains_files?(dir)
entries = Dir.entries(dir).reject { |entry| entry == '.' || entry == '..' }
entries.any? { |entry| !File.directory?(File.join(dir, entry)) || dir_contains_files?(File.join(dir, entry)) }
end
def self.empty_vendor_dirs
Dir.glob(Rails.root.join("vendor/{gems,plugins}/*")).reject { |dir| dir_contains_files?(dir) }
end
end
if %w(development test cucumber).include?(RAILS_ENV) # Only do this in local environments
empty = EmptyDirectoryHelper.empty_vendor_dirs
if empty.any?
puts
empty.each do |dir|
puts "Automatically deleting #{dir.sub(Rails.root.to_s + '/', '')}, since it only contains empty directories"
FileUtils.rm_rf dir
end
puts "You can probably ignore any 'config.gem: Unpacked gem' errors above.\n\n"
end
end
paths = %w(vendor/gems/first_old_gem-0.1.0
vendor/gems/second_old_gem-0.2.0)
paths.each do |path|
dir = Rails.root.join(path)
if File.directory?(dir)
puts "Automatically deleting #{dir.sub(Rails.root.to_s + '/', '')}, since it's an old submodule that we removed"
FileUtils.rm_rf dir
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment