-
Notifications
You must be signed in to change notification settings - Fork 749
Expand file tree
/
Copy pathPowerShell.cs
More file actions
151 lines (135 loc) · 5.57 KB
/
PowerShell.cs
File metadata and controls
151 lines (135 loc) · 5.57 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
using System.Diagnostics;
using System.Text;
using System.Text.RegularExpressions;
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,
SupportsCustomSources = true,
SupportsPreRelease = true,
SupportsCustomPackageIcons = true,
Sources = new SourceCapabilities
{
KnowsPackageCount = false,
KnowsUpdateDate = false,
}
};
Properties = new ManagerProperties
{
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",
ExecutableCallArgs = " -NoProfile -Command",
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()
{
Process p = new()
{
StartInfo = new ProcessStartInfo
{
FileName = Status.ExecutablePath,
Arguments = Properties.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<Package> Packages = [];
bool DashesPassed = false;
while ((line = p.StandardOutput.ReadLine()) is not null)
{
logger.AddToStdOut(line);
if (!DashesPassed)
{
if (line.Contains("-----"))
{
DashesPassed = true;
}
}
else
{
string[] elements = Regex.Replace(line, " {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],
SourcesHelper.Factory.GetSourceOrDefault(elements[2]), this));
}
}
logger.AddToStdErr(p.StandardError.ReadToEnd());
p.WaitForExit();
logger.Close(p.ExitCode);
return Packages;
}
protected override ManagerStatus LoadManager()
{
ManagerStatus status = new()
{
ExecutablePath = Path.Join(Environment.SystemDirectory, "windowspowershell\\v1.0\\powershell.exe")
};
status.Found = File.Exists(status.ExecutablePath);
if (!status.Found)
{
return status;
}
Process process = new()
{
StartInfo = new ProcessStartInfo
{
FileName = status.ExecutablePath,
Arguments = Properties.ExecutableCallArgs + " \"echo $PSVersionTable\"",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
StandardOutputEncoding = Encoding.UTF8
}
};
process.Start();
status.Version = process.StandardOutput.ReadToEnd().Trim();
return status;
}
}
}