-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnityCSProject.cs
158 lines (136 loc) · 5.98 KB
/
UnityCSProject.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Collections.Generic;
namespace Unity.CSProject
{
public class UnityCSProject
{
public PropertyGroup PropertyDebug;
public PropertyGroup PropertyRelease;
public string ProductVersion;
public string SchemaVersion;
public string AssemblyName;
public string TargetFrameworkVersion;
public string FileAlignment;
public string BaseDirectory;
public string OutputType;
public string ProjectGuid;
public List<CompileItem> CompileItems;
public List<ReferenceItem> ReferenceItems;
private UnityCSProject()
{
}
public static UnityCSProject Parse(string path)
{
if (string.IsNullOrEmpty(path)) return null;
if (!path.ToLower().EndsWith(".csproj")) return null;
if (!File.Exists(path)) return null;
XElement xproject = XElement.Load(path);
string xns = xproject.Name.NamespaceName;
UnityCSProject csproj = new UnityCSProject();
try
{
//Compile files
List<CompileItem> compileFiles = new List<CompileItem>();
List<ReferenceItem> referenceItems = new List<ReferenceItem>();
var itemgroups = xproject.Elements(XName.Get("ItemGroup", xns));
foreach (var itemgroup in itemgroups)
{
var compiles = itemgroup.Elements(XName.Get("Compile", xns));
foreach (var compile in compiles)
{
var compilePath = compile.Attribute(XName.Get("Include"));
if (compilePath == null) continue;
compileFiles.Add(new CompileItem(compilePath.Value));
}
var references = itemgroup.Elements(XName.Get("Reference", xns));
foreach (var reference in references)
{
var attrName = reference.Attribute(XName.Get("Include"))?.Value;
var hintPath = reference.Element(XName.Get("HintPath", xns))?.Value;
referenceItems.Add(new ReferenceItem(attrName, hintPath));
}
}
csproj.CompileItems = compileFiles;
csproj.ReferenceItems = referenceItems;
//PropettyGroup
var propertyGroups = xproject.Elements(XName.Get("PropertyGroup", xns));
foreach (var propertyGroup in propertyGroups)
{
var condition = propertyGroup.Attribute(XName.Get("Condition"));
if (condition == null)
{
//parse main config
csproj.AssemblyName = propertyGroup.Element(XName.Get(nameof(AssemblyName), xns))?.Value;
csproj.BaseDirectory = propertyGroup.Element(XName.Get(nameof(BaseDirectory), xns))?.Value;
csproj.FileAlignment = propertyGroup.Element(XName.Get(nameof(FileAlignment), xns))?.Value;
csproj.OutputType = propertyGroup.Element(XName.Get(nameof(OutputType), xns))?.Value;
csproj.ProductVersion = propertyGroup.Element(XName.Get(nameof(ProductVersion), xns))?.Value;
csproj.ProjectGuid = propertyGroup.Element(XName.Get(nameof(ProjectGuid), xns))?.Value;
csproj.SchemaVersion = propertyGroup.Element(XName.Get(nameof(SchemaVersion), xns))?.Value;
csproj.TargetFrameworkVersion = propertyGroup.Element(XName.Get(nameof(TargetFrameworkVersion), xns))?.Value;
}
else
{
var conditionDesc = condition.Value;
if (conditionDesc.Contains("Debug"))
{
csproj.PropertyDebug = new PropertyGroup(propertyGroup, xns);
}
else if (conditionDesc.Contains("Release"))
{
csproj.PropertyRelease = new PropertyGroup(propertyGroup, xns);
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message + "\n" + e.StackTrace);
}
return csproj;
}
}
public class CompileItem
{
public string Path;
public CompileItem(string path)
{
this.Path = path;
}
}
public class PropertyGroup
{
public string DebugSymbols;
public string DebugType;
public string Optimize;
public string OutputPath;
public string DefineConstants;
public string ErrorReport;
public string WarningLevel;
public string AllowUnsafeBlocks;
public PropertyGroup(XElement property, string xns)
{
DebugSymbols = property.Element(XName.Get(nameof(DebugSymbols), xns))?.Value;
DebugType = property.Element(XName.Get(nameof(DebugType), xns))?.Value;
Optimize = property.Element(XName.Get(nameof(Optimize), xns))?.Value;
OutputPath = property.Element(XName.Get(nameof(OutputPath), xns))?.Value;
DefineConstants = property.Element(XName.Get(nameof(DefineConstants), xns))?.Value;
ErrorReport = property.Element(XName.Get(nameof(ErrorReport), xns))?.Value;
WarningLevel = property.Element(XName.Get(nameof(WarningLevel), xns))?.Value;
AllowUnsafeBlocks = property.Element(XName.Get(nameof(AllowUnsafeBlocks), xns))?.Value;
}
}
public class ReferenceItem
{
public string Name;
public string HintPath;
public ReferenceItem(string name, string hintpath)
{
this.Name = name;
this.HintPath = hintpath;
}
}
}