forked from JohannesDeml/UnityWebGL-LoadingTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnityPackageScripts.cs
260 lines (235 loc) · 7.1 KB
/
UnityPackageScripts.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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UnityPackageScripts.cs">
// Copyright (c) 2023 Johannes Deml. All rights reserved.
// </copyright>
// <author>
// Johannes Deml
// </author>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEditor.PackageManager;
using UnityEditor.PackageManager.Requests;
using UnityEngine;
namespace UnityBuilderAction
{
/// <summary>
/// Editor script to automatically update all packages in the project to the newest version
/// Can be used in continuous integration (CI) or through menu items in the editor
/// Uses non-blocking tasks, so you can continue working until the packages recompile
/// </summary>
public static class UnityPackageScripts
{
private static int ProgressId;
private static ListRequest ListRequest;
#if UNITY_2021_2_OR_NEWER
private static AddAndRemoveRequest AddAndRemoveRequest;
#else
private static AddRequest AddRequest;
private static Queue<string> PackagesToAdd;
#endif
private static bool IncludePrereleases;
[MenuItem("Tools/Packages/Update to verified version")]
public static void UpgradeAllPackagesToVerifiedVersion()
{
IncludePrereleases = false;
StartPackageListUpdate();
}
[MenuItem("Tools/Packages/Update to latest pre-release")]
public static void UpgradeAllPackagesToLatestCompatibleVersion()
{
IncludePrereleases = true;
StartPackageListUpdate();
}
private static void StartPackageListUpdate()
{
#if UNITY_2020_1_OR_NEWER
ProgressId = Progress.Start("Update Packages",
$"Update all packages to latest version (Include Pre-Releases: {IncludePrereleases})");
#endif
ListRequest = Client.List();
EditorApplication.update += OnWaitForPackageList;
}
private static void OnWaitForPackageList()
{
if (!ListRequest.IsCompleted)
{
#if UNITY_2020_1_OR_NEWER
Progress.Report(ProgressId, 0.1f, "Update package list");
#endif
return;
}
EditorApplication.update -= OnWaitForPackageList;
switch (ListRequest.Status)
{
case StatusCode.Success:
Console.WriteLine("Package list updated, checking for newer package versions");
UpdatePackages();
break;
case StatusCode.Failure:
Console.WriteLine($"Retrieving package list failed! {ListRequest.Error}");
EndUpdate(101);
break;
case StatusCode.InProgress:
Console.WriteLine("Retrieving package list is still in progress!");
EndUpdate(102);
break;
default:
Console.WriteLine($"Unsupported status {ListRequest.Status}!");
EndUpdate(103);
break;
}
}
private static void UpdatePackages()
{
List<string> toAdd = new List<string>();
List<string> toRemove = new List<string>();
foreach (var package in ListRequest.Result)
{
if (package.source == PackageSource.Embedded ||
package.source == PackageSource.BuiltIn ||
package.source == PackageSource.Local)
{
continue;
}
string latestVersion = IncludePrereleases ?
package.versions.latestCompatible :
#if UNITY_2022_3_OR_NEWER
package.versions.recommended;
#elif UNITY_2019_3_OR_NEWER
package.versions.verified;
#else
package.versions.recommended;
#endif
if (package.version == latestVersion || string.IsNullOrEmpty(latestVersion))
{
continue;
}
Debug.Log($"Update {package.name} from {package.version} to {latestVersion}");
toAdd.Add($"{package.name}@{latestVersion}");
}
if (toRemove.Count == 0 && toAdd.Count == 0)
{
Console.WriteLine("All packages up to date");
EndUpdate(0);
return;
}
#if UNITY_2020_1_OR_NEWER
Progress.Report(ProgressId, 0.5f, $"Updating {toAdd.Count} packages: {string.Join(", ", toAdd)}");
#endif
#if UNITY_2021_2_OR_NEWER
AddAndRemoveRequest = Client.AddAndRemove(toAdd.ToArray(), toRemove.ToArray());
EditorApplication.update += OnWaitForPackageUpdates;
#else
PackagesToAdd = new Queue<string>(toAdd);
AddRequest = Client.Add(PackagesToAdd.Dequeue());
EditorApplication.update += OnWaitForSinglePackageUpdate;
#endif
}
#if UNITY_2021_2_OR_NEWER
private static void OnWaitForPackageUpdates()
{
#if UNITY_2020_1_OR_NEWER
Progress.Report(ProgressId, 0.5f, null);
#endif
if (!AddAndRemoveRequest.IsCompleted)
{
return;
}
EditorApplication.update -= OnWaitForPackageUpdates;
switch (AddAndRemoveRequest.Status)
{
case StatusCode.Success:
Console.WriteLine($"Packages updated successful: {AddAndRemoveRequest.Result}");
EndUpdate(0);
break;
case StatusCode.Failure:
Console.WriteLine($"Updating package list failed! {AddAndRemoveRequest.Error}");
EndUpdate(201);
break;
case StatusCode.InProgress:
Console.WriteLine("Retrieving package list is still in progress!");
EndUpdate(202);
break;
default:
Console.WriteLine($"Unsupported status {AddAndRemoveRequest.Status}!");
EndUpdate(203);
break;
}
}
#else
private static void OnWaitForSinglePackageUpdate()
{
#if UNITY_2020_1_OR_NEWER
Progress.Report(ProgressId, 0.5f, null);
#endif
if (!AddRequest.IsCompleted)
{
return;
}
if (AddRequest.Status == StatusCode.Success)
{
if (PackagesToAdd.Count > 0)
{
// Update the next package
AddRequest = Client.Add(PackagesToAdd.Dequeue());
return;
}
}
EditorApplication.update -= OnWaitForSinglePackageUpdate;
switch (AddRequest.Status)
{
case StatusCode.Success:
Console.WriteLine($"Packages updated successful: {AddRequest.Result}");
EndUpdate(0);
break;
case StatusCode.Failure:
Console.WriteLine($"Updating package list failed! {AddRequest.Error}");
EndUpdate(201);
break;
case StatusCode.InProgress:
Console.WriteLine("Retrieving package list is still in progress!");
EndUpdate(202);
break;
default:
Console.WriteLine($"Unsupported status {AddRequest.Status}!");
EndUpdate(203);
break;
}
}
#endif
private static void EndUpdate(int returnValue)
{
#if UNITY_2020_1_OR_NEWER
Progress.Finish(ProgressId, returnValue == 0 ? Progress.Status.Succeeded : Progress.Status.Failed);
#endif
Debug.Log($"Updating unity packages finished with exit code {returnValue}");
if (Application.isBatchMode)
{
// Hack for unity-builder v3, since it is expecting a build result output
Console.WriteLine(
$"{Environment.NewLine}" +
$"###########################{Environment.NewLine}" +
$"# Build results #{Environment.NewLine}" +
$"###########################{Environment.NewLine}" +
$"{Environment.NewLine}" +
$"Duration: 0{Environment.NewLine}" +
$"Warnings: 0{Environment.NewLine}" +
$"Errors: 0{Environment.NewLine}" +
$"Size: 0 bytes{Environment.NewLine}" +
$"{Environment.NewLine}"
);
EditorApplication.Exit(returnValue);
}
else
{
if (returnValue != 0)
{
throw new Exception($"BuildScript ended with non-zero exitCode: {returnValue}");
}
}
}
}
}