-
Notifications
You must be signed in to change notification settings - Fork 821
Expand file tree
/
Copy pathPowerShell.cs
More file actions
203 lines (185 loc) · 6.92 KB
/
PowerShell.cs
File metadata and controls
203 lines (185 loc) · 6.92 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
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
using UniGetUI.Core.Data;
using UniGetUI.Core.Tools;
using UniGetUI.Interface.Enums;
using UniGetUI.PackageEngine.Classes.Manager;
using UniGetUI.PackageEngine.Classes.Manager.ManagerHelpers;
using UniGetUI.PackageEngine.Enums;
using UniGetUI.PackageEngine.ManagerClasses.Classes;
using UniGetUI.PackageEngine.ManagerClasses.Manager;
using UniGetUI.PackageEngine.Managers.Chocolatey;
using UniGetUI.PackageEngine.PackageClasses;
namespace UniGetUI.PackageEngine.Managers.PowerShellManager
{
public class PowerShell : BaseNuGet
{
public PowerShell()
{
Capabilities = new ManagerCapabilities
{
CanRunAsAdmin = true,
CanSkipIntegrityChecks = true,
SupportsCustomVersions = true,
CanDownloadInstaller = true,
SupportsCustomScopes = true,
CanListDependencies = true,
SupportsCustomSources = true,
SupportsPreRelease = true,
SupportsCustomPackageIcons = true,
Sources = new SourceCapabilities
{
KnowsPackageCount = false,
KnowsUpdateDate = false,
},
SupportsProxy = ProxySupport.Partially,
SupportsProxyAuth = true,
KnowsPackageReleaseDate = PackageReleaseDateSupport.Yes,
};
Properties = new ManagerProperties
{
Id = "winps",
Name = "PowerShell",
DisplayName = "PowerShell 5.x",
Description = CoreTools.Translate(
"PowerShell's package manager. Find libraries and scripts to expand PowerShell capabilities<br>Contains: <b>Modules, Scripts, Cmdlets</b>"
),
IconId = IconType.PowerShell,
ColorIconId = "powershell_color",
ExecutableFriendlyName = "powershell.exe",
InstallVerb = "Install-Module",
UninstallVerb = "Uninstall-Module",
UpdateVerb = "Update-Module",
KnownSources =
[
new ManagerSource(
this,
"PSGallery",
new Uri("https://www.powershellgallery.com/api/v2")
),
new ManagerSource(
this,
"PoshTestGallery",
new Uri("https://www.poshtestgallery.com/api/v2")
),
],
DefaultSource = new ManagerSource(
this,
"PSGallery",
new Uri("https://www.powershellgallery.com/api/v2")
),
};
DetailsHelper = new PowerShellDetailsHelper(this);
SourcesHelper = new PowerShellSourceHelper(this);
OperationHelper = new PowerShellPkgOperationHelper(this);
}
protected override IReadOnlyList<Package> _getInstalledPackages_UnSafe()
{
using Process p = new()
{
StartInfo = new ProcessStartInfo
{
FileName = Status.ExecutablePath,
Arguments = Status.ExecutableCallArgs + " Get-InstalledModule",
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = true,
StandardOutputEncoding = Encoding.UTF8,
},
};
IProcessTaskLogger logger = TaskLogger.CreateNew(
LoggableTaskType.ListInstalledPackages,
p
);
p.Start();
string? line;
List<string> outputLines = [];
while ((line = p.StandardOutput.ReadLine()) is not null)
{
logger.AddToStdOut(line);
outputLines.Add(line);
}
logger.AddToStdErr(p.StandardError.ReadToEnd());
p.WaitForExit();
logger.Close(p.ExitCode);
return ParseInstalledPackages(outputLines, this);
}
public override List<string> FindCandidateExecutableFiles()
{
var candidates = CoreTools.WhichMultiple("powershell.exe");
if (candidates.Count is 0)
candidates.Add(CoreData.PowerShell5);
return candidates;
}
protected override void _loadManagerExecutableFile(
out bool found,
out string path,
out string callArguments
)
{
var (_found, _path) = GetExecutableFile();
found = _found;
path = _path;
callArguments = " -NoProfile -Command";
}
protected override void _loadManagerVersion(out string version)
{
Process process = new()
{
StartInfo = new ProcessStartInfo
{
FileName = Status.ExecutablePath,
Arguments = Status.ExecutableCallArgs + " \"echo $PSVersionTable\"",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
StandardOutputEncoding = Encoding.UTF8,
},
};
process.Start();
version = process.StandardOutput.ReadToEnd().Trim();
}
internal static IReadOnlyList<Package> ParseInstalledPackages(
IEnumerable<string> outputLines,
PowerShell manager
)
{
List<Package> packages = [];
bool dashesPassed = false;
foreach (string rawLine in outputLines)
{
if (!dashesPassed)
{
if (rawLine.Contains("-----"))
{
dashesPassed = true;
}
continue;
}
string[] elements = Regex.Replace(rawLine, " {2,}", " ").Split(' ');
if (elements.Length < 3)
{
continue;
}
for (int i = 0; i < elements.Length; i++)
{
elements[i] = elements[i].Trim();
}
packages.Add(
new Package(
CoreTools.FormatAsName(elements[1]),
elements[1],
elements[0],
manager.SourcesHelper.Factory.GetSourceOrDefault(elements[2]),
manager
)
);
}
return packages;
}
}
}