/// <summary> /// プロジェクト ファイル (.csproj) 内に C# 言語バージョン (<LangVersion>) を追加. /// </summary> public static void SetCSharpLangVersion(ProjectFileGenerationArgs args, int langVersion) { // UnityVersion が書いてあるノードを取得. var document = args.Content; XElement unityVersionNode = null; if (document != null) { unityVersionNode = document.Root.Nodes() .OfType <XElement>() .Where(x => x.Name.LocalName == "PropertyGroup") .SelectMany(x => x.Nodes()) .Where(x => x != null) .OfType <XElement>() .SingleOrDefault(x => x.Name.LocalName == "UnityVersion"); } // UnityVersion と同じところに (同じ親の子として) LangVersion を書き込む. if (unityVersionNode != null && unityVersionNode.Parent != null) { var root = document.Root != null ? document.Root.Name.Namespace : null; unityVersionNode.Parent.Add(new XElement(root + "LangVersion", langVersion)); } }
/// <summary> /// プロジェクト ファイル (.csproj) 内から 不要な.Lang への参照を削除するためのフック処理を実行. /// </summary> public static void ExcludeAnotherLanguageReference(ProjectFileGenerationArgs args) { var deleteTargets = new[] { "Boo.Lang", "UnityScript", "UnityScript.Lang", }; var document = args.Content; XElement[] targetChilds = null; if (document.Root != null) { targetChilds = document.Root.Nodes() .OfType <XElement>() .Where(x => x.Name.LocalName == "ItemGroup") .Where(x => x.FirstNode != null) .Select(x => x.FirstNode) .OfType <XElement>() .ToArray(); } if (targetChilds == null) { return; } if (targetChilds.Length == 0) { return; } foreach (var targetChild in targetChilds) { if (targetChild.Parent == null) { continue; } var targetNode = targetChild.Parent; var removeTargets = targetNode.Descendants() .Where(x => x.Name.LocalName == "Reference") .Where(x => x.Attribute("Include") != null) .Where(x => !string.IsNullOrEmpty(x.Attribute("Include").Value)) .Where(x => deleteTargets.Any(y => x.Attribute("Include").Value.ToLower().Contains(y.ToLower()))) .ToArray(); foreach (var removeTarget in removeTargets) { removeTarget.Remove(); } } }
/// <summary> /// VisualStudio用のAnalyzerを参照に追加. /// ※ analyzerIdにはNuGetのIDを指定. /// </summary> public static void IncludeAnalyzer(ProjectFileGenerationArgs args, string analyzerId) { // NuGetのpackages.configから取得. var currentDir = Directory.GetCurrentDirectory(); var packagePath = Path.Combine(currentDir, "packages.config"); if (!File.Exists(packagePath)) { return; } var packages = XDocument.Load(packagePath); var analyzerPackage = packages.Descendants("package") .FirstOrDefault(x => (string)x.Attribute("id") == analyzerId); if (analyzerPackage == null) { return; } var pathRoot = "packages\\" + (string)analyzerPackage.Attribute("id") + "." + (string)analyzerPackage.Attribute("version") + "\\analyzers\\dotnet\\cs"; var ns = args.Content.Root.Name.Namespace; var dirPath = Path.Combine(currentDir, pathRoot); if (!Directory.Exists(dirPath)) { return; } var analyzers = Directory.GetFiles(Path.Combine(currentDir, pathRoot)) .Select(x => new XElement(ns + "Analyzer", new XAttribute("Include", pathRoot + "\\" + Path.GetFileName(x)))) .ToArray(); if (analyzers.Length == 0) { return; } // 最後のItempGroupの下に追加. var lastGroup = args.Content.Descendants().Last(x => x.Name.LocalName == "ItemGroup"); lastGroup.AddAfterSelf(new XElement(ns + "ItemGroup", analyzers)); }
/// <summary> /// プロジェクト ファイル (.csproj) 内に既定の名前空間の設定を追加するためのフック処理を実行. /// </summary> public static void SetRootNamespace(ProjectFileGenerationArgs args, string @namespace) { if (!string.IsNullOrEmpty(@namespace) && args.Content.Root != null) { var node = args.Content.Root.Nodes() .OfType <XElement>() .Where(x => x.Name.LocalName == "PropertyGroup") .SelectMany(x => x.Nodes()) .Where(x => x != null) .OfType <XElement>() .SingleOrDefault(x => x.Name.LocalName == "RootNamespace"); if (node != null) { node.SetValue(@namespace); } } }