Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WinGet COM API to fetch installed packages and available updates #2451

Merged
merged 6 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.0" />
<PackageReference Include="xunit.abstractions" Version="2.0.3" />
<PackageReference Include="xunit.analyzers" Version="1.13.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.0">
<PackageReference Include="xunit.analyzers" Version="1.14.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
9 changes: 6 additions & 3 deletions src/UniGetUI.Core.Data.Tests/UniGetUI.Core.Data.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.0" />
<PackageReference Include="xunit.analyzers" Version="1.13.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.0">
<PackageReference Include="xunit.analyzers" Version="1.14.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.0" />
<PackageReference Include="xunit.analyzers" Version="1.13.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.0">
<PackageReference Include="xunit.analyzers" Version="1.14.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.0" />
<PackageReference Include="xunit.analyzers" Version="1.13.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.0">
<PackageReference Include="xunit.analyzers" Version="1.14.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.0" />
<PackageReference Include="xunit.analyzers" Version="1.13.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.0">
<PackageReference Include="xunit.analyzers" Version="1.14.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.8.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
196 changes: 8 additions & 188 deletions src/UniGetUI.PackageEngine.Managers.WinGet/WinGet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,201 +113,20 @@ public WinGet() : base()

protected override async Task<Package[]> FindPackages_UnSafe(string query)
{
return await WinGetHelper.Instance.FindPackages_UnSafe(this, query);
return await Task.Run(() => WinGetHelper.Instance.FindPackages_UnSafe(this, query).GetAwaiter().GetResult());
}

protected override async Task<Package[]> GetAvailableUpdates_UnSafe()
{
if (Settings.Get("ForceLegacyBundledWinGet"))
return await BundledWinGetLegacyMethods.GetAvailableUpdates_UnSafe(this);

List<Package> Packages = [];

Process p = new()
{
StartInfo = new ProcessStartInfo()
{
FileName = "cmd.exe",
Arguments = "/C " + PowerShellPath + " " + PowerShellPromptArgs,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
CreateNoWindow = true,
StandardOutputEncoding = CodePagesEncodingProvider.Instance.GetEncoding(CoreData.CODE_PAGE),
StandardInputEncoding = new UTF8Encoding(false),
StandardErrorEncoding = CodePagesEncodingProvider.Instance.GetEncoding(CoreData.CODE_PAGE),
}
};

ManagerClasses.Classes.ProcessTaskLogger logger = TaskLogger.CreateNew(LoggableTaskType.ListUpdates, p);

p.Start();

string command = """
Write-Output (Get-Module -Name Microsoft.WinGet.Client).Version
Import-Module Microsoft.WinGet.Client
function Print-WinGetPackage {
param (
[Parameter(Mandatory,ValueFromPipelineByPropertyName)] [string] $Name,
[Parameter(Mandatory,ValueFromPipelineByPropertyName)] [string] $Id,
[Parameter(Mandatory,ValueFromPipelineByPropertyName)] [string] $InstalledVersion,
[Parameter(ValueFromPipelineByPropertyName)] [string[]] $AvailableVersions,
[Parameter(Mandatory,ValueFromPipelineByPropertyName)] [bool] $IsUpdateAvailable,
[Parameter(ValueFromPipelineByPropertyName)] [string] $Source
)
process {
if($IsUpdateAvailable)
{
Write-Output("#" + $Name + "`t" + $Id + "`t" + $InstalledVersion + "`t" + $AvailableVersions[0] + "`t" + $Source)
}
}
}

Get-WinGetPackage | Print-WinGetPackage

exit

""";

await p.StandardInput.WriteAsync(command);
p.StandardInput.Close();
logger.AddToStdIn(command);

string? line;
while ((line = await p.StandardOutput.ReadLineAsync()) != null)
{
logger.AddToStdOut(line);
if (!line.StartsWith("#"))
{
continue; // The PowerShell script appends a '#' to the beginning of each line to identify the output
}

string[] elements = line.Split('\t');
if (elements.Length < 5)
{
continue;
}

ManagerSource source = GetSourceOrDefault(elements[4]);

Packages.Add(new Package(elements[0][1..], elements[1], elements[2], elements[3], source, this));
}

logger.AddToStdErr(await p.StandardError.ReadToEndAsync());
await p.WaitForExitAsync();
logger.Close(p.ExitCode);

if (Packages.Count() > 0)
{
return Packages.ToArray();
}
else
{
Logger.Warn("WinGet updates returned zero packages, attempting legacy...");
return await BundledWinGetLegacyMethods.GetAvailableUpdates_UnSafe(this);
}
return await Task.Run(() => WinGetHelper.Instance.GetAvailableUpdates_UnSafe(this).GetAwaiter().GetResult());
}

protected override async Task<Package[]> GetInstalledPackages_UnSafe()
{
if (Settings.Get("ForceLegacyBundledWinGet"))
return await BundledWinGetLegacyMethods.GetInstalledPackages_UnSafe(this);

List<Package> Packages = [];
Process p = new()
{
StartInfo = new ProcessStartInfo()
{
FileName = "cmd.exe",
Arguments = "/C " + PowerShellPath + " " + PowerShellPromptArgs,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
CreateNoWindow = true,
StandardOutputEncoding = CodePagesEncodingProvider.Instance.GetEncoding(CoreData.CODE_PAGE),
StandardInputEncoding = new UTF8Encoding(false),
StandardErrorEncoding = CodePagesEncodingProvider.Instance.GetEncoding(CoreData.CODE_PAGE),
}
};

ManagerClasses.Classes.ProcessTaskLogger logger = TaskLogger.CreateNew(LoggableTaskType.ListInstalledPackages, p);
p.Start();

string command = """
Write-Output (Get-Module -Name Microsoft.WinGet.Client).Version
Import-Module Microsoft.WinGet.Client
function Print-WinGetPackage {
param (
[Parameter(Mandatory,ValueFromPipelineByPropertyName)] [string] $Name,
[Parameter(Mandatory,ValueFromPipelineByPropertyName)] [string] $Id,
[Parameter(Mandatory,ValueFromPipelineByPropertyName)] [string] $InstalledVersion,
[Parameter(ValueFromPipelineByPropertyName)] [string[]] $AvailableVersions,
[Parameter(Mandatory,ValueFromPipelineByPropertyName)] [bool] $IsUpdateAvailable,
[Parameter(ValueFromPipelineByPropertyName)] [string] $Source
)
process {
Write-Output("#" + $Name + "`t" + $Id + "`t" + $InstalledVersion + "`t" + $Source)
}
}

Get-WinGetPackage | Print-WinGetPackage


exit

""";

await p.StandardInput.WriteAsync(command);
p.StandardInput.Close();
logger.AddToStdIn(command);


string? line;
while ((line = await p.StandardOutput.ReadLineAsync()) != null)
{
logger.AddToStdOut(line);
if (!line.StartsWith("#"))
{
continue; // The PowerShell script appends a '#' to the beginning of each line to identify the output
}

string[] elements = line.Split('\t');
if (elements.Length < 4)
{
continue;
}

ManagerSource source;
if (elements[3] != "")
{
source = GetSourceOrDefault(elements[3]);
}
else
{
source = GetLocalSource(elements[1]);
}

Packages.Add(new Package(elements[0][1..], elements[1], elements[2], source, this));
}

logger.AddToStdErr(await p.StandardError.ReadToEndAsync());
await p.WaitForExitAsync();
logger.Close(p.ExitCode);

if (Packages.Count() > 0)
{
return Packages.ToArray();
}
else
{
Logger.Warn("WinGet installed packages returned zero packages, attempting legacy...");
return await BundledWinGetLegacyMethods.GetInstalledPackages_UnSafe(this);
}
return await Task.Run(() => WinGetHelper.Instance.GetInstalledPackages_UnSafe(this).GetAwaiter().GetResult());
}

private ManagerSource GetLocalSource(string id)
public ManagerSource GetLocalSource(string id)
{
try
{
Expand Down Expand Up @@ -361,6 +180,7 @@ private ManagerSource GetLocalSource(string id)
return LocalPcSource;
}
}


public override string[] GetInstallParameters(Package package, InstallationOptions options)
{
Expand Down
Loading