Skip to content

Commit 4e2d5b9

Browse files
committed
Added a workaround for issue #4, that will check if the package id is shortened and then tries to use the package name instead for all actions; Changed the properties of the WinGetPackage and WinGetSource classes
1 parent b0e5b1e commit 4e2d5b9

8 files changed

Lines changed: 547 additions & 273 deletions

File tree

src/WGet.NET/Components/WinGetPackageManager.cs

Lines changed: 57 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ namespace WGetNET
1717
public class WinGetPackageManager : WinGetInfo
1818
{
1919
private const string _listCmd = "list";
20-
private const string _searchInstalledCmd = "list {0}";
21-
private const string _searchInstalledBySourceCmd = "list {0} --source {1}";
22-
private const string _searchCmd = "search {0} --accept-source-agreements";
23-
private const string _searchBySourceCmd = "search {0} --source {1} --accept-source-agreements";
24-
private const string _installCmd = "install {0}";
25-
private const string _upgradeCmd = "upgrade {0}";
20+
private const string _searchInstalledCmd = "list \"{0}\"";
21+
private const string _searchInstalledBySourceCmd = "list \"{0}\" --source {1}";
22+
private const string _searchCmd = "search \"{0}\" --accept-source-agreements";
23+
private const string _searchBySourceCmd = "search \"{0}\" --source {1} --accept-source-agreements";
24+
private const string _installCmd = "install \"{0}\"";
25+
private const string _upgradeCmd = "upgrade \"{0}\"";
2626
private const string _upgradeAllCmd = "upgrade --all";
2727
private const string _getUpgradeableCmd = "upgrade";
2828
private const string _includeUnknown = "--include-unknown";
29-
private const string _uninstallCmd = "uninstall {0}";
29+
private const string _uninstallCmd = "uninstall \"{0}\"";
3030
private const string _exportCmd = "export -o {0}";
3131
private const string _importCmd = "import -i {0} --ignore-unavailable";
3232
private const string _hashCmd = "hash {0}";
@@ -473,7 +473,12 @@ public bool InstallPackage(WinGetPackage package)
473473
return false;
474474
}
475475

476-
return InstallPackage(package.PackageId);
476+
if (package.HasShortenedId)
477+
{
478+
return InstallPackage(package.Name);
479+
}
480+
481+
return InstallPackage(package.Id);
477482
}
478483

479484
/// <summary>
@@ -543,7 +548,12 @@ public async Task<bool> InstallPackageAsync(WinGetPackage package)
543548
return false;
544549
}
545550

546-
return await InstallPackageAsync(package.PackageId);
551+
if (package.HasShortenedId)
552+
{
553+
return await InstallPackageAsync(package.Name);
554+
}
555+
556+
return await InstallPackageAsync(package.Id);
547557
}
548558
//---------------------------------------------------------------------------------------------
549559

@@ -613,7 +623,12 @@ public bool UninstallPackage(WinGetPackage package)
613623
return false;
614624
}
615625

616-
return UninstallPackage(package.PackageId);
626+
if (package.HasShortenedId)
627+
{
628+
return UninstallPackage(package.Name);
629+
}
630+
631+
return UninstallPackage(package.Id);
617632
}
618633

619634
/// <summary>
@@ -683,7 +698,12 @@ public async Task<bool> UninstallPackageAsync(WinGetPackage package)
683698
return false;
684699
}
685700

686-
return await UninstallPackageAsync(package.PackageId);
701+
if (package.HasShortenedId)
702+
{
703+
return await UninstallPackageAsync(package.Name);
704+
}
705+
706+
return await UninstallPackageAsync(package.Id);
687707
}
688708
//---------------------------------------------------------------------------------------------
689709

@@ -822,7 +842,12 @@ public bool UpgradePackage(WinGetPackage package)
822842
return false;
823843
}
824844

825-
return UpgradePackage(package.PackageId);
845+
if (package.HasShortenedId)
846+
{
847+
return UpgradePackage(package.Name);
848+
}
849+
850+
return UpgradePackage(package.Id);
826851
}
827852

828853
/// <summary>
@@ -892,7 +917,12 @@ public async Task<bool> UpgradePackageAsync(WinGetPackage package)
892917
return false;
893918
}
894919

895-
return await UpgradePackageAsync(package.PackageId);
920+
if (package.HasShortenedId)
921+
{
922+
return await UpgradePackageAsync(package.Name);
923+
}
924+
925+
return await UpgradePackageAsync(package.Id);
896926
}
897927

898928
/// <summary>
@@ -1398,32 +1428,7 @@ public bool Download(string packageId, string directory)
13981428
/// </exception>
13991429
public bool Download(string packageId, DirectoryInfo directory)
14001430
{
1401-
if (!WinGetVersionIsMatchOrAbove(1, 6))
1402-
{
1403-
throw new WinGetFeatureNotSupportedException("1.6");
1404-
}
1405-
1406-
try
1407-
{
1408-
ProcessResult result =
1409-
_processManager.ExecuteWingetProcess(
1410-
string.Format(_downloadCmd, packageId, directory.FullName));
1411-
1412-
if (!result.Success)
1413-
{
1414-
return false;
1415-
}
1416-
1417-
return true;
1418-
}
1419-
catch (Win32Exception)
1420-
{
1421-
throw new WinGetNotInstalledException();
1422-
}
1423-
catch (Exception e)
1424-
{
1425-
throw new WinGetActionFailedException("Download failed.", e);
1426-
}
1431+
return Download(packageId, directory.FullName);
14271432
}
14281433

14291434
/// <summary>
@@ -1446,32 +1451,12 @@ public bool Download(string packageId, DirectoryInfo directory)
14461451
/// </exception>
14471452
public bool Download(WinGetPackage package, string directory)
14481453
{
1449-
if (!WinGetVersionIsMatchOrAbove(1, 6))
1454+
if (package.HasShortenedId)
14501455
{
1451-
throw new WinGetFeatureNotSupportedException("1.6");
1456+
return Download(package.Name, directory);
14521457
}
14531458

1454-
try
1455-
{
1456-
ProcessResult result =
1457-
_processManager.ExecuteWingetProcess(
1458-
string.Format(_downloadCmd, package.PackageId, directory));
1459-
1460-
if (!result.Success)
1461-
{
1462-
return false;
1463-
}
1464-
1465-
return true;
1466-
}
1467-
catch (Win32Exception)
1468-
{
1469-
throw new WinGetNotInstalledException();
1470-
}
1471-
catch (Exception e)
1472-
{
1473-
throw new WinGetActionFailedException("Download failed.", e);
1474-
}
1459+
return Download(package.Id, directory);
14751460
}
14761461

14771462
/// <summary>
@@ -1494,32 +1479,12 @@ public bool Download(WinGetPackage package, string directory)
14941479
/// </exception>
14951480
public bool Download(WinGetPackage package, DirectoryInfo directory)
14961481
{
1497-
if (!WinGetVersionIsMatchOrAbove(1, 6))
1482+
if (package.HasShortenedId)
14981483
{
1499-
throw new WinGetFeatureNotSupportedException("1.6");
1484+
return Download(package.Name, directory.FullName);
15001485
}
15011486

1502-
try
1503-
{
1504-
ProcessResult result =
1505-
_processManager.ExecuteWingetProcess(
1506-
string.Format(_downloadCmd, package.PackageId, directory.FullName));
1507-
1508-
if (!result.Success)
1509-
{
1510-
return false;
1511-
}
1512-
1513-
return true;
1514-
}
1515-
catch (Win32Exception)
1516-
{
1517-
throw new WinGetNotInstalledException();
1518-
}
1519-
catch (Exception e)
1520-
{
1521-
throw new WinGetActionFailedException("Download failed.", e);
1522-
}
1487+
return Download(package.Id, directory.FullName);
15231488
}
15241489

15251490
/// <summary>
@@ -1590,32 +1555,7 @@ await _processManager.ExecuteWingetProcessAsync(
15901555
/// </exception>
15911556
public async Task<bool> DownloadAsync(string packageId, DirectoryInfo directory)
15921557
{
1593-
if (!WinGetVersionIsMatchOrAbove(1, 6))
1594-
{
1595-
throw new WinGetFeatureNotSupportedException("1.6");
1596-
}
1597-
1598-
try
1599-
{
1600-
ProcessResult result =
1601-
await _processManager.ExecuteWingetProcessAsync(
1602-
string.Format(_downloadCmd, packageId, directory.FullName));
1603-
1604-
if (!result.Success)
1605-
{
1606-
return false;
1607-
}
1608-
1609-
return true;
1610-
}
1611-
catch (Win32Exception)
1612-
{
1613-
throw new WinGetNotInstalledException();
1614-
}
1615-
catch (Exception e)
1616-
{
1617-
throw new WinGetActionFailedException("Download failed.", e);
1618-
}
1558+
return await DownloadAsync(packageId, directory.FullName);
16191559
}
16201560

16211561
/// <summary>
@@ -1638,32 +1578,12 @@ await _processManager.ExecuteWingetProcessAsync(
16381578
/// </exception>
16391579
public async Task<bool> DownloadAsync(WinGetPackage package, string directory)
16401580
{
1641-
if (!WinGetVersionIsMatchOrAbove(1, 6))
1581+
if (package.HasShortenedId)
16421582
{
1643-
throw new WinGetFeatureNotSupportedException("1.6");
1583+
return await DownloadAsync(package.Name, directory);
16441584
}
16451585

1646-
try
1647-
{
1648-
ProcessResult result =
1649-
await _processManager.ExecuteWingetProcessAsync(
1650-
string.Format(_downloadCmd, package.PackageId, directory));
1651-
1652-
if (!result.Success)
1653-
{
1654-
return false;
1655-
}
1656-
1657-
return true;
1658-
}
1659-
catch (Win32Exception)
1660-
{
1661-
throw new WinGetNotInstalledException();
1662-
}
1663-
catch (Exception e)
1664-
{
1665-
throw new WinGetActionFailedException("Download failed.", e);
1666-
}
1586+
return await DownloadAsync(package.Id, directory);
16671587
}
16681588

16691589
/// <summary>
@@ -1686,32 +1606,12 @@ await _processManager.ExecuteWingetProcessAsync(
16861606
/// </exception>
16871607
public async Task<bool> DownloadAsync(WinGetPackage package, DirectoryInfo directory)
16881608
{
1689-
if (!WinGetVersionIsMatchOrAbove(1, 6))
1609+
if (package.HasShortenedId)
16901610
{
1691-
throw new WinGetFeatureNotSupportedException("1.6");
1611+
return await DownloadAsync(package.Name, directory.FullName);
16921612
}
16931613

1694-
try
1695-
{
1696-
ProcessResult result =
1697-
await _processManager.ExecuteWingetProcessAsync(
1698-
string.Format(_downloadCmd, package.PackageId, directory.FullName));
1699-
1700-
if (!result.Success)
1701-
{
1702-
return false;
1703-
}
1704-
1705-
return true;
1706-
}
1707-
catch (Win32Exception)
1708-
{
1709-
throw new WinGetNotInstalledException();
1710-
}
1711-
catch (Exception e)
1712-
{
1713-
throw new WinGetActionFailedException("Download failed.", e);
1714-
}
1614+
return await DownloadAsync(package.Id, directory.FullName);
17151615
}
17161616
//---------------------------------------------------------------------------------------------
17171617
}

0 commit comments

Comments
 (0)