Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Linq;
- using System.Xml.Linq;
- using JetBrains.Rider.Unity.Editor;
- using UnityEditor;
- using UnityEngine;
- namespace Editor
- {
- public class CsprojAssetPostprocessor : AssetPostprocessor
- {
- internal static string[] GetCsprojLinesInSln()
- {
- var projectDirectory = Directory.GetParent(Application.dataPath).FullName;
- var projectName = Path.GetFileName(projectDirectory);
- var slnFile = Path.GetFullPath(string.Format("{0}.sln", projectName));
- if (!File.Exists(slnFile))
- return new string[0];
- var slnAllText = File.ReadAllText(slnFile);
- var lines = slnAllText.Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
- .Where(a => a.StartsWith("Project(")).ToArray();
- return lines;
- }
- public static void OnGeneratedCSProjectFiles()
- {
- if (!PluginEntryPoint.Enabled)
- return;
- try
- {
- // get only csproj files, which are mentioned in sln
- var lines = GetCsprojLinesInSln();
- var currentDirectory = Directory.GetCurrentDirectory();
- var projectFiles = Directory.GetFiles(currentDirectory, "*.csproj")
- .Where(csprojFile => lines.Any(line => line.Contains("\"" + Path.GetFileName(csprojFile) + "\""))).ToArray();
- foreach (var file in projectFiles)
- {
- UpgradeProjectFile(file);
- }
- }
- catch (Exception e)
- {
- // unhandled exception kills editor
- Debug.LogError(e);
- }
- }
- private static void UpgradeProjectFile(string projectFile)
- {
- XDocument doc;
- try
- {
- doc = XDocument.Load(projectFile);
- }
- catch (Exception)
- {
- return;
- }
- var projectContentElement = doc.Root;
- XNamespace xmlns = projectContentElement.Name.NamespaceName; // do not use var
- SetBooDllReference(projectContentElement, xmlns);
- doc.Save(projectFile);
- }
- private static void SetBooDllReference(XElement projectContentElement, XNamespace xmlns)
- {
- var unityAppBaseFolder = Path.GetDirectoryName(EditorApplication.applicationPath);
- if (string.IsNullOrEmpty(unityAppBaseFolder))
- {
- return;
- }
- var name = "Boo.Lang.dll";
- var booDllPath = Path.Combine(unityAppBaseFolder, Path.Combine("Data/MonoBleedingEdge/lib/mono/2.0-api", name));
- if (!File.Exists(booDllPath))
- return;
- var itemGroup = new XElement(xmlns + "ItemGroup");
- var reference = new XElement(xmlns + "Reference");
- reference.Add(new XAttribute("Include", Path.GetFileNameWithoutExtension(booDllPath)));
- reference.Add(new XElement(xmlns + "HintPath", booDllPath));
- itemGroup.Add(reference);
- projectContentElement.Add(itemGroup);
- }
- private static int ourScriptingRuntimeCached = -1;
- private static string SlnFile;
- }
- }
Add Comment
Please, Sign In to add comment