Skip to content

Instantly share code, notes, and snippets.

@capyvara
Last active February 12, 2016 18:15
Show Gist options
  • Save capyvara/9090942 to your computer and use it in GitHub Desktop.
Save capyvara/9090942 to your computer and use it in GitHub Desktop.
PostProcessor for MonoDevelop project files in Unity
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
public class CSharpProjectProcessor : AssetPostprocessor
{
[MenuItem("Assets/Clean MonoDevelop Files")]
static void CleanMonoDevelopFiles()
{
foreach (var file in Directory.GetFiles(Directory.GetCurrentDirectory(), "*.*"))
{
var extension = Path.GetExtension(file);
if (extension == ".sln" ||
extension == ".csproj" ||
extension == ".unityproj" ||
extension == ".userprefs" ||
extension == ".pidb")
{
File.Delete(file);
}
}
}
// Undocumented, called when CS project changes
static void OnGeneratedCSProjectFiles()
{
if (Application.platform == RuntimePlatform.OSXEditor)
{
RemoveVisualStudioFiles();
ApplyFrameworkPolicy();
}
}
static void RemoveVisualStudioFiles()
{
foreach (var file in Directory.GetFiles(Directory.GetCurrentDirectory(), "*.*"))
{
if (file.Contains("-vs"))
File.Delete(file);
if (file.Contains("-csharp.sln"))
File.Delete(file);
}
}
static void ApplyFrameworkPolicy()
{
string replaceText = ReplaceTextMonoDevelop;
string editorPath = UnityEditorInternal.InternalEditorUtility.GetExternalScriptEditor();
if (string.IsNullOrEmpty(editorPath))
editorPath = Path.Combine(Path.GetDirectoryName(EditorApplication.applicationPath), "MonoDevelop.app");
string infoPListPath = Path.Combine(Path.Combine(editorPath, "Contents"), "Info.plist");
try
{
var info = new InfoPList(infoPListPath);
if (info.BundleIdentifier.Contains("monodevelop"))
{
var versionSplit = info.BundleVersion.Split('.');
int majorVersion = int.Parse(versionSplit[0]);
if (majorVersion >= 4)
replaceText = ReplaceTextMonoDevelop4;
Console.WriteLine("Found monodevelop " + majorVersion);
}
}
catch { }
Console.WriteLine("Appying framework policy");
foreach (var file in Directory.GetFiles(Directory.GetCurrentDirectory(), "*.sln"))
{
var content = File.ReadAllText(file);
File.WriteAllText(file, content.Replace(FindText, replaceText));
}
}
static readonly string FindText = string.Join("\r\n", new[] {
"\tGlobalSection(MonoDevelopProperties) = preSolution",
"\t\tStartupItem = Assembly-CSharp.csproj",
"\t\tPolicies = $0",
"\t\t$0.TextStylePolicy = $1",
"\t\t$1.inheritsSet = null",
"\t\t$1.scope = text/x-csharp",
"\t\t$0.CSharpFormattingPolicy = $2",
"\t\t$2.inheritsSet = Mono",
"\t\t$2.inheritsScope = text/x-csharp",
"\t\t$2.scope = text/x-csharp",
"\t\t$0.TextStylePolicy = $3",
"\t\t$3.FileWidth = 120",
"\t\t$3.TabWidth = 4",
"\t\t$3.EolMarker = Unix",
"\t\t$3.inheritsSet = Mono",
"\t\t$3.inheritsScope = text/plain",
"\t\t$3.scope = text/plain",
"\tEndGlobalSection",
});
// Matches Tools/MonoDevelop.mdpolicy
static readonly string ReplaceTextMonoDevelop = string.Join("\r\n", new[] {
"\tGlobalSection(MonoDevelopProperties) = preSolution",
"\t\tStartupItem = Assembly-CSharp.csproj",
"\t\tPolicies = $0",
"\t\t$0.TextStylePolicy = $1",
"\t\t$1.FileWidth = 120",
"\t\t$1.NoTabsAfterNonTabs = True",
"\t\t$1.RemoveTrailingWhitespace = True",
"\t\t$1.inheritsSet = VisualStudio",
"\t\t$1.inheritsScope = text/plain",
"\t\t$1.scope = text/x-csharp",
"\t\t$0.CSharpFormattingPolicy = $2",
"\t\t$2.IndentSwitchBody = True",
"\t\t$2.AnonymousMethodBraceStyle = NextLine",
"\t\t$2.PropertyBraceStyle = NextLine",
"\t\t$2.PropertyGetBraceStyle = NextLine",
"\t\t$2.PropertySetBraceStyle = NextLine",
"\t\t$2.EventBraceStyle = NextLine",
"\t\t$2.EventAddBraceStyle = NextLine",
"\t\t$2.EventRemoveBraceStyle = NextLine",
"\t\t$2.StatementBraceStyle = NextLine",
"\t\t$2.PlaceElseOnNewLine = True",
"\t\t$2.PlaceCatchOnNewLine = True",
"\t\t$2.PlaceFinallyOnNewLine = True",
"\t\t$2.PlaceWhileOnNewLine = True",
"\t\t$2.BeforeMethodDeclarationParentheses = False",
"\t\t$2.BeforeMethodCallParentheses = False",
"\t\t$2.BeforeConstructorDeclarationParentheses = False",
"\t\t$2.BeforeIndexerDeclarationBracket = False",
"\t\t$2.BeforeDelegateDeclarationParentheses = False",
"\t\t$2.NewParentheses = False",
"\t\t$2.SpacesBeforeBrackets = False",
"\t\t$2.inheritsSet = Mono",
"\t\t$2.inheritsScope = text/x-csharp",
"\t\t$2.scope = text/x-csharp",
"\t\t$0.TextStylePolicy = $3",
"\t\t$3.FileWidth = 120",
"\t\t$3.NoTabsAfterNonTabs = True",
"\t\t$3.RemoveTrailingWhitespace = True",
"\t\t$3.inheritsSet = VisualStudio",
"\t\t$3.inheritsScope = text/plain",
"\t\t$3.scope = text/plain",
"\t\t$0.TextStylePolicy = $4",
"\t\t$4.FileWidth = 120",
"\t\t$4.NoTabsAfterNonTabs = True",
"\t\t$4.RemoveTrailingWhitespace = True",
"\t\t$4.inheritsSet = VisualStudio",
"\t\t$4.inheritsScope = text/plain",
"\t\t$4.scope = application/xml",
"\t\t$0.XmlFormattingPolicy = $5",
"\t\t$5.inheritsSet = Mono",
"\t\t$5.inheritsScope = application/xml",
"\t\t$5.scope = application/xml",
"\tEndGlobalSection",
});
// Matches Tools/MonoDevelop4.mdpolicy
static readonly string ReplaceTextMonoDevelop4 = string.Join("\r\n", new[] {
"\tGlobalSection(MonoDevelopProperties) = preSolution",
"\t\tStartupItem = Assembly-CSharp.csproj",
"\t\tPolicies = $0",
"\t\t$0.TextStylePolicy = $1",
"\t\t$1.FileWidth = 120",
"\t\t$1.NoTabsAfterNonTabs = True",
"\t\t$1.inheritsSet = VisualStudio",
"\t\t$1.inheritsScope = text/plain",
"\t\t$1.scope = text/x-csharp",
"\t\t$0.CSharpFormattingPolicy = $2",
"\t\t$2.IndentSwitchBody = True",
"\t\t$2.AnonymousMethodBraceStyle = NextLine",
"\t\t$2.PropertyBraceStyle = NextLine",
"\t\t$2.PropertyGetBraceStyle = NextLine",
"\t\t$2.PropertySetBraceStyle = NextLine",
"\t\t$2.EventBraceStyle = NextLine",
"\t\t$2.EventAddBraceStyle = NextLine",
"\t\t$2.EventRemoveBraceStyle = NextLine",
"\t\t$2.StatementBraceStyle = NextLine",
"\t\t$2.ElseNewLinePlacement = NewLine",
"\t\t$2.ElseIfNewLinePlacement = SameLine",
"\t\t$2.CatchNewLinePlacement = NewLine",
"\t\t$2.FinallyNewLinePlacement = NewLine",
"\t\t$2.WhileNewLinePlacement = NewLine",
"\t\t$2.BeforeMethodDeclarationParentheses = False",
"\t\t$2.BeforeMethodCallParentheses = False",
"\t\t$2.BeforeConstructorDeclarationParentheses = False",
"\t\t$2.BeforeIndexerDeclarationBracket = False",
"\t\t$2.BeforeDelegateDeclarationParentheses = False",
"\t\t$2.NewParentheses = False",
"\t\t$2.SpacesBeforeBrackets = False",
"\t\t$2.MethodCallArgumentWrapping = WrapIfTooLong",
"\t\t$2.MethodDeclarationParameterWrapping = WrapIfTooLong",
"\t\t$2.IndexerDeclarationParameterWrapping = WrapIfTooLong",
"\t\t$2.IndexerArgumentWrapping = WrapIfTooLong",
"\t\t$2.inheritsSet = Mono",
"\t\t$2.inheritsScope = text/x-csharp",
"\t\t$2.scope = text/x-csharp",
"\t\t$0.TextStylePolicy = $3",
"\t\t$3.FileWidth = 120",
"\t\t$3.NoTabsAfterNonTabs = True",
"\t\t$3.inheritsSet = VisualStudio",
"\t\t$3.inheritsScope = text/plain",
"\t\t$3.scope = text/plain",
"\t\t$0.StandardHeader = $4",
"\t\t$4.Text = ",
"\t\t$4.IncludeInNewFiles = True",
"\t\t$0.NameConventionPolicy = $5",
"\t\t$5.Rules = $6",
"\t\t$6.NamingRule = $7",
"\t\t$7.Name = Namespaces",
"\t\t$7.AffectedEntity = Namespace",
"\t\t$7.VisibilityMask = VisibilityMask",
"\t\t$7.NamingStyle = PascalCase",
"\t\t$7.IncludeInstanceMembers = True",
"\t\t$7.IncludeStaticEntities = True",
"\t\t$6.NamingRule = $8",
"\t\t$8.Name = Types",
"\t\t$8.AffectedEntity = Class, Struct, Enum, Delegate",
"\t\t$8.VisibilityMask = VisibilityMask",
"\t\t$8.NamingStyle = PascalCase",
"\t\t$8.IncludeInstanceMembers = True",
"\t\t$8.IncludeStaticEntities = True",
"\t\t$6.NamingRule = $9",
"\t\t$9.Name = Interfaces",
"\t\t$9.RequiredPrefixes = $10",
"\t\t$10.String = I",
"\t\t$9.AffectedEntity = Interface",
"\t\t$9.VisibilityMask = VisibilityMask",
"\t\t$9.NamingStyle = PascalCase",
"\t\t$9.IncludeInstanceMembers = True",
"\t\t$9.IncludeStaticEntities = True",
"\t\t$6.NamingRule = $11",
"\t\t$11.Name = Attributes",
"\t\t$11.RequiredSuffixes = $12",
"\t\t$12.String = Attribute",
"\t\t$11.AffectedEntity = CustomAttributes",
"\t\t$11.VisibilityMask = VisibilityMask",
"\t\t$11.NamingStyle = PascalCase",
"\t\t$11.IncludeInstanceMembers = True",
"\t\t$11.IncludeStaticEntities = True",
"\t\t$6.NamingRule = $13",
"\t\t$13.Name = Event Arguments",
"\t\t$13.RequiredSuffixes = $14",
"\t\t$14.String = EventArgs",
"\t\t$13.AffectedEntity = CustomEventArgs",
"\t\t$13.VisibilityMask = VisibilityMask",
"\t\t$13.NamingStyle = PascalCase",
"\t\t$13.IncludeInstanceMembers = True",
"\t\t$13.IncludeStaticEntities = True",
"\t\t$6.NamingRule = $15",
"\t\t$15.Name = Exceptions",
"\t\t$15.RequiredSuffixes = $16",
"\t\t$16.String = Exception",
"\t\t$15.AffectedEntity = CustomExceptions",
"\t\t$15.VisibilityMask = VisibilityMask",
"\t\t$15.NamingStyle = PascalCase",
"\t\t$15.IncludeInstanceMembers = True",
"\t\t$15.IncludeStaticEntities = True",
"\t\t$6.NamingRule = $17",
"\t\t$17.Name = Methods",
"\t\t$17.AffectedEntity = Methods",
"\t\t$17.VisibilityMask = VisibilityMask",
"\t\t$17.NamingStyle = PascalCase",
"\t\t$17.IncludeInstanceMembers = True",
"\t\t$17.IncludeStaticEntities = True",
"\t\t$6.NamingRule = $18",
"\t\t$18.Name = Static Readonly Fields",
"\t\t$18.AffectedEntity = ReadonlyField",
"\t\t$18.VisibilityMask = Internal, Protected, Public",
"\t\t$18.NamingStyle = PascalCase",
"\t\t$18.IncludeInstanceMembers = False",
"\t\t$18.IncludeStaticEntities = True",
"\t\t$6.NamingRule = $19",
"\t\t$19.Name = Fields (Non Private)",
"\t\t$19.AffectedEntity = Field",
"\t\t$19.VisibilityMask = Internal, Protected, Public",
"\t\t$19.NamingStyle = PascalCase",
"\t\t$19.IncludeInstanceMembers = True",
"\t\t$19.IncludeStaticEntities = True",
"\t\t$6.NamingRule = $20",
"\t\t$20.Name = ReadOnly Fields (Non Private)",
"\t\t$20.AffectedEntity = ReadonlyField",
"\t\t$20.VisibilityMask = Internal, Protected, Public",
"\t\t$20.NamingStyle = PascalCase",
"\t\t$20.IncludeInstanceMembers = True",
"\t\t$20.IncludeStaticEntities = False",
"\t\t$6.NamingRule = $21",
"\t\t$21.Name = Fields (Private)",
"\t\t$21.RequiredPrefixes = $22",
"\t\t$22.String = _",
"\t\t$21.AffectedEntity = Field, ReadonlyField",
"\t\t$21.VisibilityMask = Private",
"\t\t$21.NamingStyle = CamelCase",
"\t\t$21.IncludeInstanceMembers = True",
"\t\t$21.IncludeStaticEntities = False",
"\t\t$6.NamingRule = $23",
"\t\t$23.Name = Static Fields (Private)",
"\t\t$23.RequiredPrefixes = $24",
"\t\t$24.String = _",
"\t\t$23.AffectedEntity = Field",
"\t\t$23.VisibilityMask = Private",
"\t\t$23.NamingStyle = CamelCase",
"\t\t$23.IncludeInstanceMembers = False",
"\t\t$23.IncludeStaticEntities = True",
"\t\t$6.NamingRule = $25",
"\t\t$25.Name = ReadOnly Fields (Private)",
"\t\t$25.RequiredPrefixes = $26",
"\t\t$26.String = _",
"\t\t$25.AffectedEntity = ReadonlyField",
"\t\t$25.VisibilityMask = Private",
"\t\t$25.NamingStyle = CamelCase",
"\t\t$25.IncludeInstanceMembers = True",
"\t\t$25.IncludeStaticEntities = False",
"\t\t$6.NamingRule = $27",
"\t\t$27.Name = Constant Fields",
"\t\t$27.AffectedEntity = ConstantField",
"\t\t$27.VisibilityMask = VisibilityMask",
"\t\t$27.NamingStyle = PascalCase",
"\t\t$27.IncludeInstanceMembers = True",
"\t\t$27.IncludeStaticEntities = True",
"\t\t$6.NamingRule = $28",
"\t\t$28.Name = Properties",
"\t\t$28.AffectedEntity = Property",
"\t\t$28.VisibilityMask = VisibilityMask",
"\t\t$28.NamingStyle = PascalCase",
"\t\t$28.IncludeInstanceMembers = True",
"\t\t$28.IncludeStaticEntities = True",
"\t\t$6.NamingRule = $29",
"\t\t$29.Name = Events",
"\t\t$29.AffectedEntity = Event",
"\t\t$29.VisibilityMask = VisibilityMask",
"\t\t$29.NamingStyle = PascalCase",
"\t\t$29.IncludeInstanceMembers = True",
"\t\t$29.IncludeStaticEntities = True",
"\t\t$6.NamingRule = $30",
"\t\t$30.Name = Enum Members",
"\t\t$30.AffectedEntity = EnumMember",
"\t\t$30.VisibilityMask = VisibilityMask",
"\t\t$30.NamingStyle = PascalCase",
"\t\t$30.IncludeInstanceMembers = True",
"\t\t$30.IncludeStaticEntities = True",
"\t\t$6.NamingRule = $31",
"\t\t$31.Name = Parameters",
"\t\t$31.AffectedEntity = Parameter",
"\t\t$31.VisibilityMask = VisibilityMask",
"\t\t$31.NamingStyle = CamelCase",
"\t\t$31.IncludeInstanceMembers = True",
"\t\t$31.IncludeStaticEntities = True",
"\t\t$6.NamingRule = $32",
"\t\t$32.Name = Type Parameters",
"\t\t$32.RequiredPrefixes = $33",
"\t\t$33.String = T",
"\t\t$32.AffectedEntity = TypeParameter",
"\t\t$32.VisibilityMask = VisibilityMask",
"\t\t$32.NamingStyle = PascalCase",
"\t\t$32.IncludeInstanceMembers = True",
"\t\t$32.IncludeStaticEntities = True",
"\tEndGlobalSection",
});
}
@reapazor
Copy link

Heads up ... looks like your using a library not natively supported ...

"Assets/Editor/CSharpProjectProcessor.cs(52,64): error CS0246: The type or namespace name `InfoPList' could not be found. Are you missing a using directive or an assembly reference?"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment