Skip to content

Instantly share code, notes, and snippets.

@cesardeazevedo
Created March 15, 2016 12:49
Show Gist options
  • Save cesardeazevedo/810ffa667850a6f82ba3 to your computer and use it in GitHub Desktop.
Save cesardeazevedo/810ffa667850a6f82ba3 to your computer and use it in GitHub Desktop.
Unity3d F# AssetPostprocessor
using System.IO;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using System.Linq;
using System.Reflection;
/// <summary>
/// Rebuilds the Fsharp solution upon Fsharp source file changes.
/// Approach from IdrisUnityPlayground https://github.com/bamboo/IdrisUnityPlayground/blob/master/Assets/Editor/IdrisPostprocessor.cs
/// </summary>
public class FSharpPostprocessor : AssetPostprocessor {
static string xbuild = HomePath("/usr/local/bin/xbuild");
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) {
if(!importedAssets.Concat(deletedAssets).Concat(movedAssets).Concat(movedFromAssetPaths).Any(IsFsharpFile))
return;
Debug.Log("Starting FSharp build");
var packagePath = ProjectPath();
var processStartInfo = new System.Diagnostics.ProcessStartInfo {
FileName = xbuild,
Arguments = packagePath + "/Assembly-FSharp-Editor.sln",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
};
var fsharp = System.Diagnostics.Process.Start(processStartInfo);
fsharp.WaitForExit();
if(fsharp.ExitCode == 0) {
Debug.Log("Fsharp build successful.");
AssetDatabase.ImportAsset("Assets/UnityFSharp.dll");
} else
Debug.LogError("Fsharp build failed:\n" + fsharp.StandardOutput.ReadToEnd() + fsharp.StandardError.ReadToEnd());
}
static string HomePath(string path) {
return Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), path);
}
static string ProjectPath() {
return Path.GetFullPath(Path.Combine(Application.dataPath, @"../"));
}
static bool IsFsharpFile(string f) {
var ext = Path.GetExtension(f);
return ext.CompareTo(".fs") == 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment