Last active
June 26, 2017 18:54
-
-
Save tcchau/10d92d83a78eda61906ee16936c52395 to your computer and use it in GitHub Desktop.
Event handler for Unity Visual Studio 2015 to allow StyleCopAnalyzers to work properly. Credit: jwvanderbeck @ https://forum.unity3d.com/threads/unity-strips-out-nuget-properties-on-csproj-files.473016/. See also https://github.com/jizc/WitchWing/blob/master/WitchWing/Assets/Scripts/Editor/ProjectFileHook.cs
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
#if UNITY_EDITOR_WIN | |
using System.IO; | |
using System.Text; | |
using System.Xml.Linq; | |
using SyntaxTree.VisualStudio.Unity.Bridge; | |
using UnityEditor; | |
/// <summary> | |
/// Provide a hook into Unity's Project File Generation so that StyleCop gets re-added each time | |
/// </summary> | |
[InitializeOnLoad] | |
public class ProjectFileHook | |
{ | |
static ProjectFileHook() | |
{ | |
ProjectFilesGenerator.ProjectFileGeneration += (string name, string content) => | |
{ | |
// parse the document and make some changes | |
var document = XDocument.Parse(content); | |
XNamespace xmlns = "http://schemas.microsoft.com/developer/msbuild/2003"; | |
XElement itemGroup = new XElement(xmlns + "ItemGroup"); | |
itemGroup.Add(new XElement(xmlns + "Analyzer", new XAttribute("Include", "packages\\StyleCop.Analyzers.1.0.2\\analyzers\\dotnet\\cs\\StyleCop.Analyzers.CodeFixes.dll"))); | |
itemGroup.Add(new XElement(xmlns + "Analyzer", new XAttribute("Include", "packages\\StyleCop.Analyzers.1.0.2\\analyzers\\dotnet\\cs\\StyleCop.Analyzers.dll"))); | |
document.Root.Add(itemGroup); | |
document.Root.Add(new XElement(xmlns + "ItemGroup", new XElement(xmlns + "AdditionalFiles", new XAttribute("Include", "stylecop.json")))); | |
// save the changes using the Utf8StringWriter | |
var str = new Utf8StringWriter(); | |
document.Save(str); | |
return str.ToString(); | |
}; | |
} | |
// necessary for XLinq to save the xml project file in utf8 | |
private class Utf8StringWriter : StringWriter | |
{ | |
public override Encoding Encoding | |
{ | |
get { return Encoding.UTF8; } | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment