-
Notifications
You must be signed in to change notification settings - Fork 3
/
Program.cs
128 lines (110 loc) · 4.91 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using LibGit2Sharp;
namespace DocFxForUnity
{
/// <summary>
/// Generates the xref maps of the APIs of all the Unity versions.
///
/// Usage: Generate
///
/// </summary>
/// <remarks>
/// [.NET](https://dotnet.microsoft.com) >= 7.0 and [DocFX](https://dotnet.github.io/docfx/) must be installed
/// on your system.
/// </remarks>
partial class Program
{
/// <summary>
/// The path where the documentation of the Unity repository will be generated.
/// </summary>
private const string GeneratedDocsPath = $"{UnityRepoPath}/_site";
/// <summary>
/// The path of the xref map generated by DocFX.
/// </summary>
private static readonly string GeneratedXrefMapPath = Path.Combine(GeneratedDocsPath, XrefMapFileName);
/// <summary>
/// The path of the default xref map, pointing at <see cref="UnityApiUrl"/>.
/// </summary>
private static readonly string DefaultXrefMapPath = Path.Combine(XrefMapsPath, XrefMapFileName);
/// <summary>
/// Gets the URL of the online API documentation of Unity.
/// </summary>
private const string UnityApiUrl = "https://docs.unity3d.com/ScriptReference/";
[GeneratedRegex("\\d{4}\\.\\d")]
private static partial Regex UnityVersionRegex();
/// <summary>
/// The path of the Unity repository.
/// </summary>
private const string UnityRepoPath = "UnityCsReference";
/// <summary>
/// The URL of the Unity repository.
/// </summary>
private const string UnityRepoUrl = "https://github.com/Unity-Technologies/UnityCsReference.git";
/// <summary>
/// The xref map filename.
/// </summary>
private const string XrefMapFileName = "xrefmap.yml";
/// <summary>
/// The path where to copy the xref maps.
/// </summary>
private const string XrefMapsPath = "_site";
/// <summary>
/// Entry point of this program.
/// </summary>
public static void Main()
{
Console.WriteLine($"Sync the Unity repository in '{UnityRepoPath}'");
using var unityRepo = Git.GetSyncRepository(UnityRepoUrl, UnityRepoPath, branch: "master");
var versions = GetLatestVersions(unityRepo);
var latestVersion = versions
.OrderByDescending(version => version.name)
.First(version => version.release.Contains('f'));
foreach (var version in versions)
{
Console.WriteLine($"Generating Unity '{version.name}' xref map");
unityRepo.HardReset(version.release);
string xrefMapPath = Path.Combine(XrefMapsPath, version.name, XrefMapFileName); // ./<version>/xrefmap.yml
Console.WriteLine($"Running DocFX on '{version.release}'");
Utils.RunCommand("dotnet", "docfx", Console.WriteLine, Console.WriteLine);
if (!File.Exists(GeneratedXrefMapPath))
{
Console.WriteLine($"Error: '{GeneratedXrefMapPath}' for Unity '{version.name}' not generated");
Console.WriteLine("\n");
continue;
}
Console.WriteLine($"Fixing hrefs in '{xrefMapPath}'");
Utils.CopyFile(GeneratedXrefMapPath, xrefMapPath);
var xrefMap = XrefMap.Load(xrefMapPath);
xrefMap.FixHrefs(apiUrl: $"https://docs.unity3d.com/{version.name}/Documentation/ScriptReference/");
xrefMap.Save(xrefMapPath);
// Set the last version's xref map as the default one
if (version == latestVersion)
{
Console.WriteLine($"Fixing hrefs in '{DefaultXrefMapPath}'");
Utils.CopyFile(GeneratedXrefMapPath, DefaultXrefMapPath);
xrefMap = XrefMap.Load(DefaultXrefMapPath);
xrefMap.FixHrefs(UnityApiUrl);
xrefMap.Save(DefaultXrefMapPath);
}
Console.WriteLine("\n");
}
}
/// <summary>
/// Returns a collection of the latest versions of a specified repository of Unity.
/// </summary>
/// <param name="unityRepository">The repository of Unity to use.</param>
/// <returns>The latest versions.</returns>
private static IEnumerable<(string name, string release)> GetLatestVersions(Repository unityRepository)
{
return unityRepository
.GetTags()
.Select(release => (name: UnityVersionRegex().Match(release).Value, release))
.GroupBy(version => version.name)
.Select(version => version.First());
}
}
}