-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdateRepo.cs
109 lines (97 loc) · 4.67 KB
/
UpdateRepo.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;
namespace Abuksigun.UnityGitUI
{
public static class UpdateRepo
{
[MenuItem("Assets/Git Package/Update", true)]
public static bool PullCheck() => Utils.GetSelectedModules().Any(x => x.IsGitPackage || x.IsGitRepo.GetResultOrDefault());
[MenuItem("Assets/Git Package/Update", priority = 110)]
public static void UpdatePackage()
{
_ = ShowUpdateRepoWindow();
}
[MenuItem("Assets/Git Package/Refresh", true)]
public static bool Check() => Utils.GetSelectedModules().Any();
[MenuItem("Assets/Git Package/Refresh", priority = 110)]
public static void Invoke() => Utils.ResetModules(Utils.GetSelectedModules());
public static async Task ShowUpdateRepoWindow()
{
var scrollPosition = Vector2.zero;
var tasks = new Dictionary<string, Task<CommandResult>>();
var processIds = new Dictionary<string, int[]>();
string currentLogGuid = null;
Queue<Module> packageUpdateQueue = new();
Task<CommandResult> currentPackageUpdate = null;
using CancellationTokenSource ctSource = new();
int spinCounter = 0;
async Task<CommandResult> Update(Module module)
{
if (!await module.IsUpdateAvailable)
return null;
if (module.IsGitPackage)
{
packageUpdateQueue.Enqueue(module);
while (currentPackageUpdate != null && !currentPackageUpdate.IsCompleted)
{
await currentPackageUpdate;
if (ctSource.Token.IsCancellationRequested)
return null;
}
return await (currentPackageUpdate = packageUpdateQueue.Dequeue().UpdateGitPackage());
}
else if (await module.IsGitRepo)
{
return await module.Pull();
}
else
{
return null;
}
}
var modules = Utils.GetSelectedModules().ToArray();
tasks = modules.ToDictionary(x => x.Guid, x => Update(x));
await GUIUtils.ShowModalWindow("Update", new Vector2Int(600, 400), (window) => {
var width = GUILayout.Width(window.position.width);
var height = GUILayout.Height(window.position.height);
window.Repaint();
using (var scroll = new GUILayout.ScrollViewScope(scrollPosition, false, false, width, height))
{
foreach (var module in modules)
{
var task = tasks.GetValueOrDefault(module.Guid);
using (new GUILayout.HorizontalScope())
{
GUILayout.Label($"{module.DisplayName} [{module.CurrentBranch.GetResultOrDefault()}]", GUILayout.Width(400));
if (task != null)
{
string status =
task.IsCompletedSuccessfully && task.Result == null ? "<color=orange><b>Nothing to update</b></color>"
: task.IsCompletedSuccessfully && task.Result.ExitCode == 0 ? "<color=green><b>Done</b></color>"
: module.IsGitPackage && packageUpdateQueue.Contains(module) ? "<b>In queue</b>"
: !task.IsCompleted ? null
: "<color=red><b>Failed</b></color>";
if (status == null)
GUIUtils.DrawSpin(ref spinCounter, EditorGUILayout.GetControlRect(GUILayout.Width(17), GUILayout.Height(17)));
else
GUILayout.Label(status, Style.RichTextLabel.Value, GUILayout.Width(150));
}
}
if (task.GetResultOrDefault() is { } result && result.ExitCode != 0)
EditorGUILayout.HelpBox(task.Result.Output, MessageType.Error);
}
scrollPosition = scroll.scrollPosition;
}
});
ctSource.Cancel();
await Task.WhenAll(tasks.Values);
foreach (var module in modules)
module?.RefreshRemoteStatus();
}
}
}