Search Unity

Say goodbye to empty folders (Editor Tool)

Discussion in 'Assets and Asset Store' started by kbabilinski, Sep 20, 2019.

  1. kbabilinski

    kbabilinski

    Joined:
    Jul 12, 2012
    Posts:
    19
    Hi everyone! I'm one of the developers as BlackBox Realities and I’ve decided to start blogging about the solutions to some of the problems that the BlackBox team faced. I’ll try to post frequently, but don’t hold me to it, because of my crazy work schedule. Without further ado, let's start by talking about source control.

    Empty Folders that won’t go away.

    If you’ve used git to source control your project, you’re probably familiar with your project loading a bunch of empty folders that you swore you threw away. You might have just accepted the fact that empty folder will always exist in your project unless you manually delete the meta file in the Windows Explorer. However, I refused to surrender and created a few tools that prevent your project having empty directories.


    Batch delete empty folders and associated .meta files.


    Screenshot from ALTPROG’s Clean Empty Directories


    Realizing that the issue is caused due to the .meta files, I developed a brute force method of iterating through all of the directories in the project and finding the ones that are empty and then deleting the empty folder and the associated .meta file. I’ve attached a simple script below and a link to the git repository a bottom of this post. The tool from the git repository is more user friendly but the script below illustrates that logic behind the tool.

    Git Repo: https://github.com/muscly/UnityCleanEmptyDirectories

    C# example
    Code (CSharp):
    1. using System.IO;
    2. using System.Linq;
    3. using UnityEditor;
    4. using UnityEngine;
    5.  
    6. namespace Common.Utilities
    7. {
    8.     public class CleanEmptyFoldersEditorUtility : EditorWindow
    9.     {
    10.         private static string deletedFolders;
    11.  
    12.         [MenuItem("Tools/Clean Empty Folders")]
    13.         private static void Cleanup()
    14.         {
    15.             deletedFolders = string.Empty;
    16.  
    17.             var directoryInfo = new DirectoryInfo(Application.dataPath);
    18.             foreach (var subDirectory in directoryInfo.GetDirectories("*.*", SearchOption.AllDirectories))
    19.             {
    20.                 if (subDirectory.Exists)
    21.                 {
    22.                     ScanDirectory(subDirectory);
    23.                 }
    24.             }
    25.  
    26.             Debug.Log("Deleted Folders:\n" + (deletedFolders.Length > 0 ? deletedFolders : "NONE"));
    27.         }
    28.  
    29.         private static string ScanDirectory(DirectoryInfo subDirectory)
    30.         {
    31.             Debug.Log("Scanning Directory: " + subDirectory.FullName);
    32.  
    33.             var filesInSubDirectory = subDirectory.GetFiles("*.*", SearchOption.AllDirectories);
    34.  
    35.             if (filesInSubDirectory.Length == 0 || !filesInSubDirectory.Any(t => t.FullName.EndsWith(".meta") == false))
    36.             {
    37.                 deletedFolders += subDirectory.FullName + "\n";
    38.                 subDirectory.Delete(true);
    39.             }
    40.  
    41.             return deletedFolders;
    42.         }
    43.     }
    44. }
    Git-Hooks to the rescue
    While the brute force method works, you can solve the issue at their core and prevent them from appearing in the first place by using git-hooks. Git hooks are scripts that git executes before or after events like committing and pushing. That means that you can tell git to ignore empty directories when you commit files, making sure that you never push and empty directory ever again. A unity developer on github has created a repo with git hooks to solve this.


    Doitian
    https://github.com/doitian/unity-git-hooks


    Automated Git Hook (Unity Editor Tool)
    But there’s a catch, git hooks do not transfer. This means that anytime you pull down a new directory you have to re-add the git hooks to your project or submodule. When I was working with a team of people who are unfamiliar with sourcetree, this was less than ideal. So I made a unity editor tool that adds the githooks to our project and to any submodule linked to it. I’ve linked the git repo to the project below.

    https://github.com/Babilinski/git-hook-utility


    Screenshot of the GitHook Utility ​


    Links

    Git Hook source:

    https://github.com/doitian/unity-git-hooks


    Batch Delete Empty Directories:

    https://github.com/muscly/UnityCleanEmptyDirectories


    Githooks Utility Unity Project:

    https://github.com/Babilinski/git-hook-utility
     
    Scott-Steffes, quabug and ReaktorDave like this.
  2. ChanzDog

    ChanzDog

    Joined:
    Sep 28, 2017
    Posts:
    43
    Thanks for sharing :)
     
  3. ReaktorDave

    ReaktorDave

    Joined:
    May 8, 2014
    Posts:
    139
    This looks super useful!