forked from Devolutions/UniGetUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCargo.cs
More file actions
222 lines (195 loc) · 8.3 KB
/
Cargo.cs
File metadata and controls
222 lines (195 loc) · 8.3 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
216
217
218
219
220
221
222
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using UniGetUI.Core.Classes;
using UniGetUI.Core.Data;
using UniGetUI.Core.Logging;
using UniGetUI.Core.Tools;
using UniGetUI.PackageEngine.Classes.Manager;
using UniGetUI.Interface.Enums;
using UniGetUI.PackageEngine.Classes.Manager.Classes;
using UniGetUI.PackageEngine.Classes.Manager.ManagerHelpers;
using UniGetUI.PackageEngine.Enums;
using UniGetUI.PackageEngine.ManagerClasses.Manager;
using UniGetUI.PackageEngine.PackageClasses;
using UniGetUI.PackageEngine.ManagerClasses.Classes;
namespace UniGetUI.PackageEngine.Managers.CargoManager;
public partial class Cargo : PackageManager
{
[GeneratedRegex(@"(\w+)\s=\s""(\d+\.\d+\.\d+)""\s*#\s(.*)")]
private static partial Regex SearchLineRegex();
[GeneratedRegex(@"(.+)v(\d+\.\d+\.\d+)\s*v(\d+\.\d+\.\d+)\s*(Yes|No)")]
private static partial Regex UpdateLineRegex();
public Cargo()
{
Dependencies = [
// cargo-update is required to check for installed and upgradable packages
new ManagerDependency(
"cargo-update",
CoreData.PowerShell5,
"-ExecutionPolicy Bypass -NoLogo -NoProfile -Command \"& {cargo install cargo-update; if ($error.count -ne 0){pause}}\"",
"cargo install cargo-update",
async () => (await CoreTools.WhichAsync("cargo-install-update.exe")).Item1),
// Cargo-binstall is required to install and update cargo binaries
new ManagerDependency(
"cargo-binstall",
CoreData.PowerShell5,
"-ExecutionPolicy Bypass -NoLogo -NoProfile -Command \"& {Set-ExecutionPolicy Unrestricted -Scope Process; iex (iwr \\\"https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.ps1\\\").Content; if ($error.count -ne 0){pause}}\"",
"Set-ExecutionPolicy Unrestricted -Scope Process; iex (iwr \"https://raw.githubusercontent.com/cargo-bins/cargo-binstall/main/install-from-binstall-release.ps1\").Content",
async () => (await CoreTools.WhichAsync("cargo-binstall.exe")).Item1)
];
Capabilities = new ManagerCapabilities
{
CanRunAsAdmin = true,
CanSkipIntegrityChecks = true,
SupportsCustomVersions = true,
SupportsCustomLocations = true,
SupportsProxy = ProxySupport.Partially,
SupportsProxyAuth = true
};
var cratesIo = new ManagerSource(this, "crates.io", new Uri("https://index.crates.io/"));
Properties = new ManagerProperties
{
Name = "Cargo",
Description = CoreTools.Translate("The Rust package manager.<br>Contains: <b>Rust libraries and programs written in Rust</b>"),
IconId = IconType.Rust,
ColorIconId = "cargo_color",
ExecutableFriendlyName = "cargo.exe",
InstallVerb = "binstall",
UninstallVerb = "uninstall",
UpdateVerb = "binstall",
DefaultSource = cratesIo,
KnownSources = [cratesIo]
};
DetailsHelper = new CargoPkgDetailsHelper(this);
OperationHelper = new CargoPkgOperationHelper(this);
}
protected override IReadOnlyList<Package> FindPackages_UnSafe(string query)
{
using Process p = GetProcess(Status.ExecutablePath, "search -q --color=never " + query);
IProcessTaskLogger logger = TaskLogger.CreateNew(LoggableTaskType.FindPackages, p);
p.Start();
string? line;
List<Package> Packages = [];
while ((line = p.StandardOutput.ReadLine()) is not null)
{
logger.AddToStdOut(line);
var match = SearchLineRegex().Match(line);
if (match.Success)
{
var id = match.Groups[1].Value;
var version = match.Groups[2].Value;
Packages.Add(new Package(CoreTools.FormatAsName(id), id, version, DefaultSource, this));
}
}
logger.AddToStdErr(p.StandardError.ReadToEnd());
p.WaitForExit();
List<Package> BinPackages = [];
for (int i = 0; i < Packages.Count; i++)
{
DateTime startTime = DateTime.Now;
var package = Packages[i];
try
{
var versionInfo = CratesIOClient.GetManifestVersion(package.Id, package.VersionString);
if (versionInfo.bin_names?.Length > 0)
{
BinPackages.Add(package);
}
}
catch (Exception ex)
{
logger.AddToStdErr($"{ex.Message}");
}
if (i + 1 == Packages.Count) break;
// Crates.io api requests that we send no more than one request per second
Task.Delay(Math.Max(0, 1000 - (int)((DateTime.Now - startTime).TotalMilliseconds))).GetAwaiter().GetResult();
}
logger.Close(p.ExitCode);
return [.. BinPackages];
}
protected override IReadOnlyList<Package> GetAvailableUpdates_UnSafe()
{
return GetPackages(LoggableTaskType.ListUpdates);
}
protected override IReadOnlyList<Package> GetInstalledPackages_UnSafe()
{
return GetPackages(LoggableTaskType.ListInstalledPackages);
}
public override IReadOnlyList<string> FindCandidateExecutableFiles()
{
return CoreTools.WhichMultiple("cargo.exe");
}
protected override ManagerStatus LoadManager()
{
var (found, executablePath) = GetExecutableFile();
if (!found)
{
return new(){ ExecutablePath = executablePath, Found = false, Version = ""};
}
using Process p = GetProcess(executablePath, "--version");
p.Start();
string version = p.StandardOutput.ReadToEnd().Trim();
string error = p.StandardError.ReadToEnd();
if (!string.IsNullOrEmpty(error))
{
Logger.Error("cargo version error: " + error);
}
return new() { ExecutablePath = executablePath, Found = found, Version = version, ExecutableCallArgs = ""};
}
private IReadOnlyList<Package> GetPackages(LoggableTaskType taskType)
{
List<Package> Packages = [];
foreach(var match in TaskRecycler<List<Match>>.RunOrAttach(GetInstalledCommandOutput, 15))
{
var id = match.Groups[1]?.Value?.Trim() ?? "";
var name = CoreTools.FormatAsName(id);
var oldVersion = match.Groups[2]?.Value?.Trim() ?? "";
var newVersion = match.Groups[3]?.Value?.Trim() ?? "";
if (taskType is LoggableTaskType.ListUpdates && oldVersion != newVersion)
Packages.Add(new Package(name, id, oldVersion, newVersion, DefaultSource, this));
else if (taskType is LoggableTaskType.ListInstalledPackages)
Packages.Add(new Package(name, id, oldVersion, DefaultSource, this));
}
return Packages;
}
private List<Match> GetInstalledCommandOutput()
{
List<Match> output = [];
using Process p = GetProcess(Status.ExecutablePath, "install-update --list");
IProcessTaskLogger logger = TaskLogger.CreateNew(LoggableTaskType.OtherTask, p);
logger.AddToStdOut("Other task: Call the install-update command");
p.Start();
string? line;
while ((line = p.StandardOutput.ReadLine()) is not null)
{
logger.AddToStdOut(line);
var match = UpdateLineRegex().Match(line);
if (match.Success)
{
output.Add(match);
}
}
logger.AddToStdErr(p.StandardError.ReadToEnd());
p.WaitForExit();
logger.Close(p.ExitCode);
return output;
}
private Process GetProcess(string fileName, string extraArguments)
{
return new()
{
StartInfo = new ProcessStartInfo
{
FileName = fileName,
Arguments = Status.ExecutableCallArgs + " " + extraArguments,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
StandardOutputEncoding = Encoding.UTF8,
StandardErrorEncoding = Encoding.UTF8,
}
};
}
}