Skip to content

Instantly share code, notes, and snippets.

@lacostej
Created March 29, 2012 09:15
Show Gist options
  • Save lacostej/2235307 to your computer and use it in GitHub Desktop.
Save lacostej/2235307 to your computer and use it in GitHub Desktop.
Unity3d / Editor build with pre/post process operations, including code generation using antlr string template
public static IEnumerable<string> GetFiles(string path) {
Queue<string> queue = new Queue<string>();
queue.Enqueue(path);
while (queue.Count > 0) {
path = queue.Dequeue();
foreach (string subDir in Directory.GetDirectories(path)) {
queue.Enqueue(subDir);
}
string[] files = null;
files = Directory.GetFiles(path);
if (files != null) {
for(int i = 0 ; i < files.Length ; i++) {
yield return files[i];
}
}
}
}
public static void EnsureParentExists(string target_dir) {
DirectoryInfo parent = Directory.GetParent(target_dir);
if (!parent.Exists) {
Directory.CreateDirectory(parent.FullName);
}
}
public static void SaveTxtInFile(string _Txt,string _FileName) {
using (StreamWriter sw = new StreamWriter(_FileName))
{
sw.Write(_Txt);
}
}
public static string ReadTxtInFile(string _FileName) {
using (StreamReader sr = new StreamReader(_FileName))
{
return sr.ReadToEnd();
}
}
static void GenericBuild(string[] scenes, string target_dir, BuildTarget build_target, BuildOptions build_options, bool override_options)
{
FileSystemUtil.EnsureParentExists(target_dir);
if (EditorUserBuildSettings.activeBuildTarget != build_target) {
EditorUserBuildSettings.SwitchActiveBuildTarget(build_target);
}
if (override_options) {
build_options = OverrideBuildOptionsWithUserSettings(build_options);
}
Debug.Log("==> BuildOptions: " + build_options);
string dir = System.IO.Path.GetDirectoryName(target_dir);
PreProcess(dir);
string res = BuildPipeline.BuildPlayer(scenes,target_dir,build_target,build_options);
if (res.Length > 0) {
throw new Exception("BuildPlayer failure: " + res);
}
PostProcess(dir);
}
static void PreProcess(string target_dir) {
Debug.Log("==> PreProcess " + target_dir);
foreach (DictionaryEntry var in Environment.GetEnvironmentVariables())
Console.WriteLine("{0}={1}", var.Key, var.Value);
Dictionary<string, string> templateInput = new Dictionary<string, string>();
string buildNumber = Environment.GetEnvironmentVariable("BUILD_NUMBER");
string buildId = Environment.GetEnvironmentVariable("BUILD_ID");
string user = Environment.GetEnvironmentVariable("USER");
string buildName = buildNumber;
if (user != "jenkins") {
buildName += "_" + user;
}
templateInput.Add("BUILD_NUMBER", buildNumber);
templateInput.Add("BUILD_NAME", buildName);
templateInput.Add("BUILD_ID", buildId);
string templateDir = "EditorTemplates";
foreach(string relativePath in FileSystemUtil.GetFiles(templateDir)) {
if (relativePath.Contains(".svn")) continue;
GenerateFileFromTemplate(templateDir, relativePath, templateInput);
}
}
static void GenerateFileFromTemplate(string templateDir, string path, Dictionary<string,string> dictionary) {
string targetPath = path.Substring(templateDir.Length + 1);
StringTemplate ST = new StringTemplate(FileSystemUtil.ReadTxtInFile(path));
foreach(string key in dictionary.Keys) {
ST.SetAttribute(key, dictionary[key]);
}
FileSystemUtil.EnsureParentExists(targetPath);
Console.WriteLine("Generating file {0} from {1}.", targetPath, templateDir);
FileSystemUtil.SaveTxtInFile(ST.ToString(), targetPath);
}
// a bit redundant with PostProcessingPlayer...
static void PostProcess(string target_dir) {
Debug.Log("==> PostProcess " + target_dir);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment