Skip to content

Commit 7931801

Browse files
committed
Added the winget package repair function
1 parent 3f2110c commit 7931801

4 files changed

Lines changed: 215 additions & 0 deletions

File tree

src/WGet.NET/Components/WinGetPackageManager.cs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class WinGetPackageManager : WinGet
4242
private const string _pinAddInstalledByVersionCmd = "pin add \"{0}\" --installed --version \"{1}\"";
4343
private const string _pinRemoveInstalledCmd = "pin remove \"{0}\" --installed";
4444
private const string _pinResetCmd = "pin reset --force";
45+
private const string _repairCmd = "repair \"{0}\"";
4546

4647
private readonly Version _downloadMinVersion = new(1, 6, 0);
4748
private readonly Version _pinMinVersion = new(1, 5, 0);
@@ -904,6 +905,130 @@ private string AddArgumentByVersion(string argument)
904905
}
905906
//---------------------------------------------------------------------------------------------
906907

908+
//---Repair------------------------------------------------------------------------------------
909+
/// <summary>
910+
/// Repairs a package using winget.
911+
/// </summary>
912+
/// <remarks>Limited to packages with an installer that supports this function.</remarks>
913+
/// <param name="packageId">The id or name of the package for the repair action.</param>
914+
/// <returns>
915+
/// <see langword="true"/> if the repair action was successful or <see langword="false"/> if it failed.
916+
/// </returns>
917+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
918+
/// WinGet is not installed or not found on the system.
919+
/// </exception>
920+
/// <exception cref="System.ArgumentException">
921+
/// A provided argument is empty.
922+
/// </exception>
923+
/// <exception cref="System.ArgumentNullException">
924+
/// A provided argument is null.
925+
/// </exception>
926+
public bool RepairPackage(string packageId)
927+
{
928+
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(packageId, "packageId");
929+
930+
string cmd = string.Format(_repairCmd, packageId);
931+
932+
ProcessResult result = Execute(cmd);
933+
934+
return result.Success;
935+
}
936+
937+
/// <summary>
938+
/// Repairs a package using winget.
939+
/// </summary>
940+
/// <remarks>Limited to packages with an installer that supports this function.</remarks>
941+
/// <param name="package">The <see cref="WGetNET.WinGetPackage"/> for the repair action.</param>
942+
/// <returns>
943+
/// <see langword="true"/> if the repair action was successful or <see langword="false"/> if it failed.
944+
/// </returns>
945+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
946+
/// WinGet is not installed or not found on the system.
947+
/// </exception>
948+
/// <exception cref="System.ArgumentException">
949+
/// A provided argument is empty.
950+
/// </exception>
951+
/// <exception cref="System.ArgumentNullException">
952+
/// A provided argument is null.
953+
/// </exception>
954+
public bool RepairPackage(WinGetPackage package)
955+
{
956+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
957+
958+
if (package.HasShortenedId || package.HasNoId)
959+
{
960+
return RepairPackage(package.Name);
961+
}
962+
963+
return RepairPackage(package.Id);
964+
}
965+
966+
/// <summary>
967+
/// Asynchronously repairs a package using winget.
968+
/// </summary>
969+
/// <remarks>Limited to packages with an installer that supports this function.</remarks>
970+
/// <param name="packageId">The id or name of the package for the repair action.</param>
971+
/// <param name="cancellationToken">
972+
/// The <see cref="System.Threading.CancellationToken"/> for the <see cref="System.Threading.Tasks.Task"/>.
973+
/// </param>
974+
/// <returns>
975+
/// A <see cref="System.Threading.Tasks.Task"/>, containing the result.
976+
/// The result is <see langword="true"/> if the repair action was successful or <see langword="false"/> if it failed.
977+
/// </returns>
978+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
979+
/// WinGet is not installed or not found on the system.
980+
/// </exception>
981+
/// <exception cref="System.ArgumentException">
982+
/// A provided argument is empty.
983+
/// </exception>
984+
/// <exception cref="System.ArgumentNullException">
985+
/// A provided argument is null.
986+
/// </exception>
987+
public async Task<bool> RepairPackageAsync(string packageId, CancellationToken cancellationToken = default)
988+
{
989+
ArgsHelper.ThrowIfStringIsNullOrWhiteSpace(packageId, "packageId");
990+
991+
string cmd = string.Format(_repairCmd, packageId);
992+
993+
ProcessResult result = await ExecuteAsync(cmd, false, cancellationToken);
994+
995+
return result.Success;
996+
}
997+
998+
/// <summary>
999+
/// Asynchronously repair a package using winget.
1000+
/// </summary>
1001+
/// <remarks>Limited to packages with an installer that supports this function.</remarks>
1002+
/// <param name="package">The <see cref="WGetNET.WinGetPackage"/> for the repair action.</param>
1003+
/// <param name="cancellationToken">
1004+
/// The <see cref="System.Threading.CancellationToken"/> for the <see cref="System.Threading.Tasks.Task"/>.
1005+
/// </param>
1006+
/// <returns>
1007+
/// A <see cref="System.Threading.Tasks.Task"/>, containing the result.
1008+
/// The result is <see langword="true"/> if the repair action was successful or <see langword="false"/> if it failed.
1009+
/// </returns>
1010+
/// <exception cref="WGetNET.Exceptions.WinGetNotInstalledException">
1011+
/// WinGet is not installed or not found on the system.
1012+
/// </exception>
1013+
/// <exception cref="System.ArgumentException">
1014+
/// A provided argument is empty.
1015+
/// </exception>
1016+
/// <exception cref="System.ArgumentNullException">
1017+
/// A provided argument is null.
1018+
/// </exception>
1019+
public async Task<bool> RepairPackageAsync(WinGetPackage package, CancellationToken cancellationToken = default)
1020+
{
1021+
ArgsHelper.ThrowIfWinGetObjectIsNullOrEmpty(package, "package");
1022+
1023+
if (package.HasShortenedId || package.HasNoId)
1024+
{
1025+
return await RepairPackageAsync(package.Name, cancellationToken);
1026+
}
1027+
1028+
return await RepairPackageAsync(package.Id, cancellationToken);
1029+
}
1030+
//---------------------------------------------------------------------------------------------
1031+
9071032
//---Export and Import-------------------------------------------------------------------------
9081033
/// <summary>
9091034
/// Exports a list of all installed winget packages as json to the given file.

src/WGet.NET/XmlDocumentation/WGet.NET.xml

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/WGetTest/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ private void Run()
5353
Console.WriteLine(test3[0].Name);
5454
Console.WriteLine(test3[0].Id);
5555

56+
/*bool repairResult = connector.RepairPackage("7zip.7zip");
57+
Console.WriteLine("Repair Test result: ", repairResult);*/
58+
5659
List<WinGetSource> sourceList = sourceManager.GetInstalledSources();
5760
bool sourceUpdateStatus = sourceManager.UpdateSources();
5861
//bool sourceResetStatus = sourceManager.ResetSources();

src/WGetTestLegacySupport/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ private void Run()
5353
Console.WriteLine(test3[0].Name);
5454
Console.WriteLine(test3[0].Id);
5555

56+
/*bool repairResult = connector.RepairPackage("7zip.7zip");
57+
Console.WriteLine("Repair Test result: ", repairResult);*/
58+
5659
List<WinGetSource> sourceList = sourceManager.GetInstalledSources();
5760
bool sourceUpdateStatus = sourceManager.UpdateSources();
5861
//bool sourceResetStatus = sourceManager.ResetSources();

0 commit comments

Comments
 (0)