-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsproj_references_for_wsl_fix.cs
52 lines (46 loc) · 1.83 KB
/
csproj_references_for_wsl_fix.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using UnityEditor;
public class UnityBinaryReferencesForNvimFix : AssetPostprocessor
{
private static string OnGeneratedCSProject(string path, string content)
{
bool enable = true;
var document = XDocument.Parse(content);
if (enable)
{
List<XElement> hintPaths = document.Root.Descendants().Where(x => x.Name.LocalName == "HintPath").ToList();
foreach (XElement hintPath in hintPaths)
{
string newPath = ConvertWindowsPathToWslPath(hintPath.Value);
hintPath.Value = newPath;
}
List<XElement> noneElements = document.Descendants().Where(x => x.Name.LocalName == "None").ToList();
foreach (XElement noneElement in noneElements)
{
XAttribute attribute = noneElement.Attribute("Include");
if (attribute == null) break;
string newPath = ConvertWindowsPathToWslPath(attribute.Value);
attribute.Value = newPath;
}
}
return document.Declaration + System.Environment.NewLine + document.Root;
}
public static string ConvertWindowsPathToWslPath(string windowsPath)
{
if (string.IsNullOrWhiteSpace(windowsPath)) return string.Empty;
if (windowsPath.Substring(0, 3).Contains(":\\"))
{
// Convert drive letter (e.g., C:) to /mnt/c
string driveLetter = windowsPath.Substring(0, 1).ToLower();
string pathWithoutDrive = windowsPath.Substring(2).Replace('\\', '/');
return $"/mnt/{driveLetter}{pathWithoutDrive}";
}
else
{
string pathWithoutDrive = windowsPath.Replace('\\', '/');
return $"{pathWithoutDrive}";
}
}
}