Repository navigation
Expand file tree
/
Copy pathAnalyzeUnityApiCommand.cs
More file actions
114 lines (97 loc) · 3.43 KB
/
Copy pathAnalyzeUnityApiCommand.cs
File metadata and controls
114 lines (97 loc) · 3.43 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
using System.Text;
using ConsoleAppFramework;
using Microsoft.Extensions.Logging;
using ZLogger;
namespace UnityApiAnalyzer;
public sealed class AnalyzeUnityApiCommand(
ILogger<AnalyzeUnityApiCommand> logger,
ApiAnalyzer analyzer)
{
private const int SuccessExitCode = 0;
private const int FailureExitCode = 1;
/// <summary>
/// Analyze Unity API.
/// </summary>
/// <param name="a">Target unity version.</param>
/// <param name="b">Target unity version.</param>
/// <param name="outputDir">Output path.</param>
/// <param name="token">cancellation token</param>
[Command("")]
public async Task<int> Analyze(
string a,
string b,
string output,
CancellationToken token)
{
if (!UnityVersion.TryParse(a, out var parsedUnityVersionA))
{
logger.ZLogCritical($"Failed to parse unity version \"{a}\".");
return FailureExitCode;
}
if (!UnityVersion.TryParse(b, out var parsedUnityVersionB))
{
logger.ZLogCritical($"Failed to parse unity version \"{b}\".");
return FailureExitCode;
}
if (parsedUnityVersionA.Major <= 2021 ||
parsedUnityVersionB.Major <= 2021)
{
logger.ZLogCritical($"Unity version 2021 and earlier are not supported.");
return FailureExitCode;
}
var outputDirectory = Directory.Exists(output) ? new DirectoryInfo(output) : Directory.CreateDirectory(output);
try
{
var resultA = await analyzer.RunAsync(parsedUnityVersionA, token);
var resultB = await analyzer.RunAsync(parsedUnityVersionB, token);
await OutputDiff(outputDirectory, "UnityEngine", resultA, resultB);
await OutputDiff(outputDirectory, "UnityEditor", resultA, resultB);
}
catch (Exception e)
{
logger.LogCritical($"{e.Message}");
return FailureExitCode;
}
return SuccessExitCode;
}
private async Task OutputDiff(
DirectoryInfo outputDirectory,
string projectName,
ApiAnalyzer.Result resultA,
ApiAnalyzer.Result resultB)
{
var projectA = resultA.Projects.FirstOrDefault(x => x.Name == projectName);
var projectB = resultB.Projects.FirstOrDefault(x => x.Name == projectName);
if (projectA == null || projectB == null)
{
logger.ZLogCritical($"Failed to find project {projectName}.");
return;
}
var membersA = projectA.Types
.SelectMany(x => x.Members)
.Select(x => x.GetDocumentationCommentId())
.Distinct()
.ToArray();
var membersB = projectB.Types
.SelectMany(x => x.Members)
.Select(x => x.GetDocumentationCommentId())
.Distinct()
.ToArray();
var sb = new StringBuilder();
foreach (var member in membersA.Except(membersB))
{
sb.AppendLine(member);
}
await File.WriteAllTextAsync(
Path.Combine(outputDirectory.FullName, $"{projectName}_added.txt"),
sb.ToString());
sb.Clear();
foreach (var member in membersB.Except(membersA))
{
sb.AppendLine(member);
}
await File.WriteAllTextAsync(
Path.Combine(outputDirectory.FullName, $"{projectName}_removed.txt"),
sb.ToString());
}
}