Skip to content

Commit 4f82661

Browse files
committed
Added workaround to correctly read the pin list output for #18 ; Updated the xml documentation
1 parent ef131e3 commit 4f82661

7 files changed

Lines changed: 131 additions & 23 deletions

File tree

src/WGet.NET/Enums/PinType.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,31 @@
44
//--------------------------------------------------//
55
namespace WGetNET
66
{
7+
/// <summary>
8+
/// Enum of winget pin types.
9+
/// </summary>
710
public enum PinType
811
{
12+
/// <summary>
13+
/// The package is pinned.
14+
/// </summary>
15+
/// <remarks>
16+
/// Package can't be updatet automatically.
17+
/// </remarks>
918
Pinning,
19+
/// <summary>
20+
/// The package is blocked.
21+
/// </summary>
22+
/// <remarks>
23+
/// Package can't be updatet automatically and manually.
24+
/// </remarks>
1025
Blocking,
26+
/// <summary>
27+
/// The package is gated.
28+
/// </summary>
29+
/// <remarks>
30+
/// Package can't be updated to versions, that are not contained in the provided pinned version.
31+
/// </remarks>
1132
Gating
1233
}
1334
}

src/WGet.NET/HelperClasses/ProcessOutputReader.cs

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public static List<WinGetPinnedPackage> ToPinnedPackageList(string[] output)
203203
//that will be thrown later, will be catched in the calling method.
204204
int labelLine = ArrayManager.GetEntryContains(output, "------") - 1;
205205

206-
int[] columnList = GetColumnList(output[labelLine]);
206+
int[] columnList = GetColumnList(output[labelLine], true);
207207

208208
//Remove unneeded output Lines
209209
output = ArrayManager.RemoveRange(output, 0, labelLine + 2);
@@ -247,39 +247,61 @@ private static List<WinGetPinnedPackage> CreatePinnedPackageListFromOutput(strin
247247
}
248248
#endif
249249

250+
string pinType = string.Empty;
251+
string pinnedVersion = string.Empty;
252+
250253
#if NETCOREAPP3_1_OR_GREATER
251254
string packageName = output[i][columnList[0]..columnList[1]].Trim();
252255
string packageId = output[i][columnList[1]..columnList[2]].Trim();
253256
string packageVersion = output[i][columnList[2]..columnList[3]].Trim();
254257
string packageSource = output[i][columnList[3]..columnList[4]].Trim();
255-
string pinType = string.Empty;
256-
string pinnedVersion = string.Empty;
257-
if (columnList.Length > 5)
258+
259+
#elif NETSTANDARD2_0
260+
string packageName = output[i].Substring(columnList[0], (columnList[1] - columnList[0])).Trim();
261+
string packageId = output[i].Substring(columnList[1], (columnList[2] - columnList[1])).Trim();
262+
string packageVersion = output[i].Substring(columnList[2], (columnList[3] - columnList[2])).Trim();
263+
string packageSource = output[i].Substring(columnList[3], (columnList[4] - columnList[3])).Trim();
264+
#endif
265+
266+
// Workaround for getting the pin data from the output.
267+
// The normal method will not work, because "Pin type" and "Pinned version" contain spaces.
268+
#if NETCOREAPP3_1_OR_GREATER
269+
string pinInfoString = output[i][columnList[4]..].TrimStart();
270+
#elif NETSTANDARD2_0
271+
string pinInfoString = output[i].Substring(columnList[4]).TrimStart();
272+
#endif
273+
int endOfTypeIndex = -1;
274+
for (int j = 0; j < pinInfoString.Length; j++)
258275
{
259-
pinType = output[i][columnList[4]..columnList[5]].Trim();
260-
pinnedVersion = output[i][columnList[5]..].Trim();
276+
if (pinInfoString[j] == (char)32)
277+
{
278+
endOfTypeIndex = j;
279+
break;
280+
}
281+
}
282+
283+
#if NETCOREAPP3_1_OR_GREATER
284+
if (endOfTypeIndex == -1)
285+
{
286+
pinType = pinInfoString.Trim();
261287
}
262288
else
263289
{
264-
pinType = output[i][columnList[4]..].Trim();
290+
pinType = pinInfoString[0..endOfTypeIndex].Trim();
291+
pinnedVersion = pinInfoString[endOfTypeIndex..].Trim();
265292
}
266293
#elif NETSTANDARD2_0
267-
string packageName = output[i].Substring(columnList[0], (columnList[1] - columnList[0])).Trim();
268-
string packageId = output[i].Substring(columnList[1], (columnList[2] - columnList[1])).Trim();
269-
string packageVersion = output[i].Substring(columnList[2], (columnList[3] - columnList[2])).Trim();
270-
string packageSource = output[i].Substring(columnList[3], (columnList[4] - columnList[3])).Trim();
271-
string pinType = string.Empty;
272-
string pinnedVersion = string.Empty;
273-
if (columnList.Length > 5)
294+
if (endOfTypeIndex == -1)
274295
{
275-
pinType = output[i].Substring(columnList[4], (columnList[5] - columnList[4])).Trim();
276-
pinnedVersion = output[i].Substring(columnList[5]).Trim();
296+
pinType = pinInfoString.Trim();
277297
}
278298
else
279299
{
280-
pinType = output[i].Substring(columnList[4]).Trim();
300+
pinType = pinInfoString.Substring(0, endOfTypeIndex).Trim();
301+
pinnedVersion = pinInfoString.Substring(endOfTypeIndex);
281302
}
282303
#endif
304+
// End of workaround
283305

284306
// Check if the id is shortened
285307
bool isShortenedId = CheckShortenedId(packageId);
@@ -411,16 +433,23 @@ private static List<WinGetSource> CreateSourceListFromOutput(string[] output, in
411433
/// <param name="line">
412434
/// A <see cref="System.String"/> containing the header, for column calculation.
413435
/// </param>
436+
/// <param name="isPinnedPackageTable">Activate workaround for the pinned package list.</param>
414437
/// <returns>
415438
/// A <see cref="System.Int32"/> <see langword="array"/> containing the column start indexes.
416439
/// </returns>
417-
private static int[] GetColumnList(string line)
440+
private static int[] GetColumnList(string line, bool isPinnedPackageTable = false)
418441
{
419442
int[] columns = new int[0];
420443

421444
bool checkForColumn = true;
422445
for (int i = 0; i < line.Length; i++)
423446
{
447+
if (isPinnedPackageTable && columns.Length >= 5)
448+
{
449+
// Workaround for the pinned package table
450+
break;
451+
}
452+
424453
if (line[i] != ((char)32) && checkForColumn)
425454
{
426455
columns = ArrayManager.Add(columns, i);

src/WGet.NET/Models/WinGetPackage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ public WinGetPackage()
276276
/// <summary>
277277
/// Initializes a new instance of the <see cref="WGetNET.WinGetPackage"/> class.
278278
/// </summary>
279-
/// <param name="hasShortenedId">Sets if the id is shortened or not</param>
279+
/// <param name="hasShortenedId">Sets if the id is shortened or not.</param>
280280
internal WinGetPackage(bool hasShortenedId)
281281
{
282282
_hasShortenedId = hasShortenedId;

src/WGet.NET/Models/WinGetPinnedPackage.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ public PinType PinType
4949
/// <summary>
5050
/// Initializes a new instance of the <see cref="WGetNET.WinGetPinnedPackage"/> class.
5151
/// </summary>
52-
/// <param name="hasShortenedId">Sets if the id is shortened or not</param>
52+
/// <param name="pinType">Name of the winget pin type for the package.</param>
53+
/// <param name="pinnedVersion"><see cref="System.String"/> containing the pinned version for the package.</param>
54+
/// <param name="hasShortenedId">Sets if the id is shortened or not.</param>
5355
internal WinGetPinnedPackage(string pinType, string pinnedVersion, bool hasShortenedId): base(hasShortenedId)
5456
{
5557
_pinTypeString = pinType;

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

Lines changed: 53 additions & 3 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ private void Run()
6868

6969
Console.WriteLine(connector.PinAdd("7zip.7zip", true));
7070
List<WinGetPinnedPackage> pinnedList1 = connector.GetPinnedPackages();
71+
Console.WriteLine(pinnedList1[0].Name + ": " + pinnedList1[0].PinTypeString);
7172
Console.WriteLine(connector.PinRemove("7zip.7zip"));
7273
Console.WriteLine(connector.PinAdd("7zip.7zip", "23.*"));
7374
List<WinGetPinnedPackage> pinnedList2 = connector.GetPinnedPackages();
75+
Console.WriteLine(pinnedList2[0].Name + ": " + pinnedList2[0].PinTypeString);
7476
Console.WriteLine(connector.PinRemove("7zip.7zip"));
7577

7678
Console.WriteLine(connector.PinAddInstalled("7zip.7zip", true));

src/WGetTestLegacySupport/Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,12 @@ private void Run()
7373
bool downloadResult = connector.Download("7zip.7zip", "C:\\Test");
7474

7575
Console.WriteLine(connector.PinAdd("7zip.7zip", true));
76+
List<WinGetPinnedPackage> pinnedList1 = connector.GetPinnedPackages();
77+
Console.WriteLine(pinnedList1[0].Name + ": " + pinnedList1[0].PinTypeString);
7678
Console.WriteLine(connector.PinRemove("7zip.7zip"));
7779
Console.WriteLine(connector.PinAdd("7zip.7zip", "23.*"));
80+
List<WinGetPinnedPackage> pinnedList2 = connector.GetPinnedPackages();
81+
Console.WriteLine(pinnedList2[0].Name + ": " + pinnedList2[0].PinTypeString);
7882
Console.WriteLine(connector.PinRemove("7zip.7zip"));
7983

8084
Console.WriteLine(connector.PinAddInstalled("7zip.7zip", true));

0 commit comments

Comments
 (0)