-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
215 lines (165 loc) · 7.57 KB
/
Program.cs
File metadata and controls
215 lines (165 loc) · 7.57 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using LibGit2Sharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using UnityXRefMap.Yaml;
using YamlDotNet.RepresentationModel;
using YamlDotNet.Serialization;
namespace UnityXRefMap
{
internal class Program
{
private static readonly string UnityCsReferenceRepositoryUrl = "https://github.com/Unity-Technologies/UnityCsReference";
private static readonly string UnityCsReferenceLocalPath = Path.Join(Environment.CurrentDirectory, "UnityCsReference");
private static readonly string GeneratedMetadataPath = Path.Join(Environment.CurrentDirectory, "ScriptReference");
private static readonly string OutputFolder = Path.Join(Environment.CurrentDirectory, "out");
private static void Main(string[] args)
{
if (!Directory.Exists(UnityCsReferenceLocalPath))
{
Repository.Clone(UnityCsReferenceRepositoryUrl, UnityCsReferenceLocalPath);
}
var files = new List<string>();
using (var repo = new Repository(UnityCsReferenceLocalPath))
{
Regex branchRegex = new Regex(@"^origin/(\d{4}\.\d+)$");
foreach (Branch branch in repo.Branches.OrderByDescending(b => b.FriendlyName))
{
Match match = branchRegex.Match(branch.FriendlyName);
if (!match.Success) continue;
string version = match.Groups[1].Value;
if (args.Length > 0 && Array.IndexOf(args, version) == -1)
{
Logger.Warning($"Skipping '{branch.FriendlyName}'");
continue;
}
Logger.Info($"Checking out '{branch.FriendlyName}'");
Commands.Checkout(repo, branch);
repo.Reset(ResetMode.Hard);
int exitCode = RunDocFx();
if (exitCode != 0)
{
Logger.Error($"DocFX exited with code {exitCode}");
continue;
}
files.Add(GenerateMap(version));
}
}
using (var writer = new StreamWriter(Path.Join(OutputFolder, "index.html")))
{
Logger.Info("Writing index.html");
writer.WriteLine("<html>\n<body>\n<ul>");
foreach (string file in files)
{
writer.WriteLine($"<li><a href=\"{file}\">{file}</a></li>");
}
writer.WriteLine("</ul>\n</body>\n</html>");
}
}
private static int RunDocFx()
{
Logger.Info("Running DocFX");
var process = new Process
{
StartInfo = new ProcessStartInfo
{
CreateNoWindow = true,
FileName = "docfx",
Arguments = "metadata",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false
}
};
process.OutputDataReceived += (sender, args) => Logger.Trace("[DocFX]" + args.Data, 1);
process.ErrorDataReceived += (sender, args) =>
{
if (string.IsNullOrEmpty(args.Data)) return;
Logger.Error("[DocFX]" + args.Data);
};
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
return process.ExitCode;
}
private static string GenerateMap(string version)
{
Logger.Info($"Generating XRef map for Unity {version}");
var serializer = new Serializer();
var deserializer = new Deserializer();
var references = new List<XRefMapReference>();
foreach (var file in Directory.GetFiles(GeneratedMetadataPath, "*.yml"))
{
Logger.Trace($"Reading '{file}'", 1);
using (TextReader reader = new StreamReader(file))
{
if (reader.ReadLine() != "### YamlMime:ManagedReference") continue;
YamlMappingNode reference = deserializer.Deserialize<YamlMappingNode>(reader);
foreach (YamlMappingNode item in (YamlSequenceNode)reference.Children["items"])
{
string fullName = Normalize(item.GetScalarValue("fullName"));
string name = Normalize(item.GetScalarValue("name"));
string type = item.GetScalarValue("type");
string parent = item.GetScalarValue("parent");
string documentationFileName;
switch (type)
{
case "Property":
case "Field":
if (char.IsLower(name[0]))
{
documentationFileName = parent + "-" + name;
}
else
{
documentationFileName = parent + "." + name;
}
break;
default:
documentationFileName = fullName;
break;
}
if (documentationFileName.StartsWith("UnityEngine.") || documentationFileName.StartsWith("UnityEditor."))
{
documentationFileName = documentationFileName.Substring(12);
}
string url = $"https://docs.unity3d.com/Documentation/ScriptReference/{documentationFileName}.html";
Logger.Trace($"Adding reference to '{fullName}'", 2);
references.Add(new XRefMapReference
{
Uid = item.GetScalarValue("uid"),
Name = name,
Href = url,
CommentId = item.GetScalarValue("commentId"),
FullName = fullName,
NameWithType = item.GetScalarValue("nameWithType")
});
}
}
}
var serializedMap = serializer.Serialize(new XRefMap
{
Sorted = true,
References = references.OrderBy(r => r.Uid).ToArray()
});
string relativeOutputFilePath = Path.Join(version, "xrefmap.yml");
string outputFilePath = Path.Join(OutputFolder, relativeOutputFilePath);
Logger.Info($"Saving XRef map to '{outputFilePath}'");
Directory.CreateDirectory(Path.GetDirectoryName(outputFilePath));
File.WriteAllText(outputFilePath, "### YamlMime:XRefMap\n" + serializedMap);
return relativeOutputFilePath;
}
private static string Normalize(string text)
{
if (text.Contains('(')) text = text.Remove(text.IndexOf('('));
if (text.Contains('<')) text = text.Remove(text.IndexOf('<'));
text = text.Replace('`', '_');
text = text.Replace("#ctor", "ctor");
return text;
}
}
}