Last active
August 29, 2015 14:24
-
-
Save mprivat/63e42aef38aa2a08d123 to your computer and use it in GitHub Desktop.
Unity editor script which compiles itself on command, including OS X when PATH doesn't include the target's folder.
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 UnityEngine; | |
using UnityEditor; | |
using System.Diagnostics; | |
using Debug = UnityEngine.Debug; | |
using System.IO; | |
public class CompileTest : ScriptableObject | |
{ | |
[MenuItem ("Assets/Run compiler test")] | |
static void Test () | |
{ | |
CompileTest instance = CreateInstance<CompileTest> (); | |
string path = Application.dataPath + AssetDatabase.GetAssetPath (MonoScript.FromScriptableObject (instance)).Substring ("Assets".Length); | |
DestroyImmediate (instance); | |
string arguments = "-target:library "; | |
arguments += path; | |
arguments += " -r:\"" + Path.Combine (UnityAssemblyPath, "UnityEngine.dll") + "\""; | |
arguments += " -r:\"" + Path.Combine (UnityAssemblyPath, "UnityEditor.dll") + "\""; | |
Process process = null; | |
try | |
{ | |
process = Process.Start (CreateCompilerStartInfo (arguments)); | |
process.WaitForExit (); | |
} | |
catch (System.Exception e) | |
{ | |
string errorMessage = e.GetType ().Name + ": "; | |
if (e is System.ComponentModel.Win32Exception) | |
{ | |
errorMessage += "Win32 error code " + ((System.ComponentModel.Win32Exception)e).NativeErrorCode + ": "; | |
} | |
errorMessage += e.Message; | |
Debug.LogError ("Process exception (" + e.GetType ().Name + "): " + errorMessage); | |
return; | |
} | |
if (!process.StandardError.EndOfStream) | |
{ | |
string error = process.StandardError.ReadToEnd (); | |
Debug.LogError ("Process error: " + error); | |
return; | |
} | |
Debug.Log ("Success"); | |
AssetDatabase.Refresh (); | |
} | |
public static bool OsIsX | |
// Assuming we are never executing on non-OS X Unix | |
{ | |
get | |
{ | |
int p = (int)System.Environment.OSVersion.Platform; | |
return (p == 4 || p == 6 || p == 128); | |
} | |
} | |
public static string CompilerPath | |
{ | |
get | |
{ | |
return EditorApplication.applicationContentsPath + | |
(OsIsX ? "/Frameworks/Mono/bin/gmcs" : "/Mono/bin/gmcs.bat"); | |
} | |
} | |
public static string UnityAssemblyPath | |
{ | |
get | |
{ | |
return (Application.platform == RuntimePlatform.WindowsEditor) ? | |
Path.Combine ( | |
EditorApplication.applicationContentsPath, | |
"Managed" | |
) | |
: | |
Path.Combine ( | |
Path.Combine ( | |
EditorApplication.applicationContentsPath, | |
"Frameworks" | |
), | |
"Managed" | |
); | |
} | |
} | |
static string ProperPath (string path) | |
{ | |
return path.Replace (Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); | |
} | |
public static ProcessStartInfo CreateCompilerStartInfo (string arguments) | |
{ | |
ProcessStartInfo startInfo; | |
if (OsIsX) | |
{ | |
startInfo = new ProcessStartInfo () | |
{ | |
WorkingDirectory = Path.GetDirectoryName (CompilerPath), | |
FileName = Path.GetFileName (CompilerPath) | |
}; | |
string currentPath = System.Environment.GetEnvironmentVariable("PATH"); | |
if(currentPath == null || !currentPath.Contains(Path.GetDirectoryName (CompilerPath))) { | |
System.Environment.SetEnvironmentVariable("PATH", (currentPath != null ? currentPath+":" : "")+Path.GetDirectoryName (CompilerPath)); | |
} | |
} | |
else | |
{ | |
startInfo = new ProcessStartInfo () | |
{ | |
FileName = ProperPath (CompilerPath) | |
}; | |
} | |
startInfo.Arguments = arguments; | |
startInfo.UseShellExecute = false; | |
startInfo.RedirectStandardError = true; | |
startInfo.CreateNoWindow = true; | |
return startInfo; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment