Created
November 1, 2014 23:23
-
-
Save maxattack/13fc38f0e296107cfa68 to your computer and use it in GitHub Desktop.
Unity: Prepare Files for Version Control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using UnityEditor; | |
using UnityEngine; | |
public static class Misc { | |
[MenuItem("Misc/Prepare Commit")] | |
static void PrepareCommit() { | |
Debug.Log ("Preparing Commit"); | |
// make sure all of unity's changes are saved to disk first | |
EditorApplication.SaveScene(); | |
AssetDatabase.SaveAssets(); | |
// go through all scenes (ignoring samples), sorting YAML records | |
var allScenes = Directory.GetFiles("Assets", "*.unity", SearchOption.AllDirectories); | |
foreach(var path in allScenes) { | |
SortYAMLRecords("Scene", path); | |
} | |
var allPrefabs = Directory.GetFiles("Assets", "*.prefab", SearchOption.AllDirectories); | |
foreach(var path in allPrefabs) { | |
SortYAMLRecords("Prefab", path); | |
} | |
} | |
static void SortYAMLRecords(string typeName, string path) { | |
Debug.Log (string.Format ("Sorting {0}: {1}", typeName, path)); | |
// read in header and records | |
var header = ""; | |
var records = new List<String>(); | |
using(var reader = File.OpenText(path)) { | |
// first read header | |
var builder = new StringBuilder(); | |
string line; | |
while((line = reader.ReadLine()) != null && !BeginningOfRecord(line)) { | |
builder.AppendLine(line); | |
} | |
header = builder.ToString(); | |
builder.Length = 0; | |
// then read records | |
builder.AppendLine(line); | |
while((line = reader.ReadLine()) != null) { | |
if (BeginningOfRecord(line)) { | |
records.Add(builder.ToString()); | |
builder.Length = 0; | |
} | |
builder.AppendLine(line); | |
} | |
if (builder.Length > 0) { | |
records.Add(builder.ToString()); | |
} | |
} | |
// write out header and sorted-records | |
records.Sort(); | |
using(var writer = new StreamWriter(path)) { | |
writer.Write (header); | |
foreach(var record in records) { | |
writer.Write(record); | |
} | |
} | |
} | |
static bool BeginningOfRecord(string s) { | |
return s.StartsWith("--- "); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment