Skip to content

Instantly share code, notes, and snippets.

@robertwahler
Last active Aug 1, 2016
Embed
What would you like to do?
Empty folders, #git, & #unity3d don't mix. Git doesn't track empty folders, why does @Unity3D?
module BasicUnity
class Code < Thor
namespace :code
include Thor::Actions
desc "prune", "Remove empty folders and Unity meta files that point to empty folders"
method_option "dry-run", :type => :boolean, :desc => "Show what will happen but don't 'rmdir' or 'rm'"
def prune(folder=nil)
# bash version, requires GNU find
#
# dry-run
# cmd = "find ./Assets -type d -empty -exec echo '{}' \; -exec echo '{}.meta' \;"
# run(cmd)
#
# for real
# cmd = "find ./Assets -type d -empty -exec rmdir '{}' \; -exec rm '{}.meta' \;"
# run(cmd)
#
folder = ASSETS_FOLDER unless folder
Dir.glob(File.join(folder, "**", "*")).select do |d|
File.directory?(d)
end.reverse_each do |d|
if ((Dir.entries(d) - %w[ . .. ]).empty?)
puts d
Dir.rmdir(d) unless options["dry-run"]
meta = "#{d}.meta"
if File.exist?(meta)
puts meta
File.delete(meta) unless options["dry-run"]
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment