Skip to content

Commit c72edeb

Browse files
committed
Removed deprecated properties; Added version object property to the WingetPackage; Added the VersionParser class that tries to parse a string to a version object as best as possible
1 parent 554da8e commit c72edeb

5 files changed

Lines changed: 203 additions & 356 deletions

File tree

src/WGet.NET/Components/WinGetInfo.cs

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -377,41 +377,16 @@ private Version GetVersionObject()
377377
versionString = versionString[1..].Trim();
378378

379379
}
380-
381-
//Remove text from the end of the version string.
382-
for (int i = 0; i < versionString.Length; i++)
383-
{
384-
if (versionString[i] == '-')
385-
{
386-
versionString = versionString[0..i];
387-
break;
388-
}
389-
}
390380
#elif NETSTANDARD2_0
391381
//Remove the first letter from the version string.
392382
if (versionString.StartsWith("v"))
393383
{
394384
versionString = versionString.Substring(1).Trim();
395385

396386
}
397-
398-
//Remove text from the end of the version string.
399-
for (int i = 0; i < versionString.Length; i++)
400-
{
401-
if (versionString[i] == '-')
402-
{
403-
versionString = versionString.Substring(0, i);
404-
break;
405-
}
406-
}
407387
#endif
408388

409-
if (!Version.TryParse(versionString, out Version? versionObject))
410-
{
411-
versionObject = Version.Parse("0.0.0");
412-
}
413-
414-
return versionObject;
389+
return VersionParser.Parse(versionString);
415390
}
416391
}
417392
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
//--------------------------------------------------//
2+
// Created by basicx-StrgV //
3+
// https://github.com/basicx-StrgV/ //
4+
//--------------------------------------------------//
5+
using System;
6+
using System.Text;
7+
8+
namespace WGetNET.HelperClasses
9+
{
10+
internal static class VersionParser
11+
{
12+
public static Version Parse(string input)
13+
{
14+
if (Version.TryParse(input, out Version? result))
15+
{
16+
return result;
17+
}
18+
19+
return CleanParse(input);
20+
}
21+
22+
private static Version CleanParse(string input)
23+
{
24+
string[] versionParts = input.Split('.');
25+
26+
int major = 0;
27+
int minor = 0;
28+
int build = -1;
29+
int revision = -1;
30+
31+
if (versionParts.Length >= 1)
32+
{
33+
major = ParseToInt(versionParts[0]);
34+
}
35+
36+
if (versionParts.Length >= 2)
37+
{
38+
minor = ParseToInt(versionParts[1]);
39+
}
40+
41+
if (versionParts.Length >= 3)
42+
{
43+
build = ParseToInt(versionParts[2], -1);
44+
}
45+
46+
if (versionParts.Length >= 4)
47+
{
48+
revision = ParseToInt(versionParts[3], -1);
49+
}
50+
51+
if (build < 0)
52+
{
53+
return new Version(major, minor);
54+
}
55+
else if (revision < 0)
56+
{
57+
return new Version(major, minor, build);
58+
}
59+
else
60+
{
61+
return new Version(major, minor, build, revision);
62+
}
63+
}
64+
65+
private static int ParseToInt(string input, int defaultValue = 0)
66+
{
67+
if(int.TryParse(input, out int result))
68+
{
69+
return result;
70+
}
71+
72+
return CleanParseToInt(input, defaultValue);
73+
}
74+
75+
private static int CleanParseToInt(string input, int defaultValue = 0)
76+
{
77+
string cleanInput = CleanupNumberString(input);
78+
79+
if (int.TryParse(cleanInput, out int result))
80+
{
81+
return result;
82+
}
83+
84+
return defaultValue;
85+
}
86+
87+
private static string CleanupNumberString(string input)
88+
{
89+
// Remove appendix (e.g. 123456-preview1) becaue it could contain a mix of numbers and letters,
90+
// which is not considert by the next cleanup.
91+
input = RemoveAppendix(input);
92+
93+
char[] parts = input.ToCharArray();
94+
95+
StringBuilder cleanString = new();
96+
bool lastCharWasALetter = false;
97+
for (int i = 0; i < parts.Length; i++)
98+
{
99+
if (char.IsDigit(parts[i]) && !lastCharWasALetter)
100+
{
101+
cleanString.Append(parts[i]);
102+
}
103+
else if (char.IsDigit(parts[i]) && lastCharWasALetter)
104+
{
105+
// Dont clenup a string that is a mix of numbers and letters (e.g. 123ABC456),
106+
// only strings that have letters at the end (e.g. 123456ABC).
107+
return string.Empty;
108+
}
109+
else
110+
{
111+
lastCharWasALetter = true;
112+
}
113+
}
114+
115+
return cleanString.ToString();
116+
}
117+
118+
private static string RemoveAppendix(string input)
119+
{
120+
string[] parts = input.Split('-');
121+
122+
if (parts.Length >= 1)
123+
{
124+
return parts[0];
125+
}
126+
127+
return input;
128+
}
129+
}
130+
}

src/WGet.NET/Models/WinGetPackage.cs

Lines changed: 7 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// https://github.com/basicx-StrgV/ //
44
//--------------------------------------------------//
55
using System;
6+
using WGetNET.HelperClasses;
67

78
namespace WGetNET
89
{
@@ -11,28 +12,6 @@ namespace WGetNET
1112
/// </summary>
1213
public class WinGetPackage
1314
{
14-
/// <summary>
15-
/// Gets or sets the name of the package.
16-
/// </summary>
17-
[Obsolete("The property \"PackageName\" is deprecated, please use \"Name\" instead.", true)]
18-
public string PackageName
19-
{
20-
get
21-
{
22-
return _name;
23-
}
24-
set
25-
{
26-
if (value is null)
27-
{
28-
_name = string.Empty;
29-
}
30-
else
31-
{
32-
_name = value;
33-
}
34-
}
35-
}
3615
/// <summary>
3716
/// Gets or sets the name of the package.
3817
/// </summary>
@@ -55,28 +34,6 @@ public string Name
5534
}
5635
}
5736

58-
/// <summary>
59-
/// Gets or sets the id of the package.
60-
/// </summary>
61-
[Obsolete("The property \"PackageId\" is deprecated, please use \"Id\" instead.", true)]
62-
public string PackageId
63-
{
64-
get
65-
{
66-
return _id;
67-
}
68-
set
69-
{
70-
if (value is null)
71-
{
72-
_id = string.Empty;
73-
}
74-
else
75-
{
76-
_id = value;
77-
}
78-
}
79-
}
8037
/// <summary>
8138
/// Gets or sets the id of the package.
8239
/// </summary>
@@ -99,28 +56,6 @@ public string Id
9956
}
10057
}
10158

102-
/// <summary>
103-
/// Gets or sets the version of the package.
104-
/// </summary>
105-
[Obsolete("The property \"PackageVersion\" is deprecated, please use \"Version\" instead.", true)]
106-
public string PackageVersion
107-
{
108-
get
109-
{
110-
return _version;
111-
}
112-
set
113-
{
114-
if (value is null)
115-
{
116-
_version = string.Empty;
117-
}
118-
else
119-
{
120-
_version = value;
121-
}
122-
}
123-
}
12459
/// <summary>
12560
/// Gets or sets the version of the package.
12661
/// </summary>
@@ -139,32 +74,22 @@ public string Version
13974
else
14075
{
14176
_version = value;
77+
_versionObject = VersionParser.Parse(_version);
14278
}
14379
}
14480
}
14581

14682
/// <summary>
147-
/// Gets or sets the newest available version of the package.
83+
/// Gets or sets the version of the package.
14884
/// </summary>
149-
[Obsolete("The property \"PackageAvailableVersion\" is deprecated, please use \"AvailableVersion\" instead.", true)]
150-
public string PackageAvailableVersion
85+
public Version VersionObject
15186
{
15287
get
15388
{
154-
return _availableVersion;
155-
}
156-
set
157-
{
158-
if (value is null)
159-
{
160-
_availableVersion = string.Empty;
161-
}
162-
else
163-
{
164-
_availableVersion = value;
165-
}
89+
return _versionObject;
16690
}
16791
}
92+
16893
/// <summary>
16994
/// Gets or sets the newest available version of the package.
17095
/// </summary>
@@ -187,28 +112,6 @@ public string AvailableVersion
187112
}
188113
}
189114

190-
/// <summary>
191-
/// Gets or sets the source name for the package.
192-
/// </summary>
193-
[Obsolete("The property \"PackageSourceName\" is deprecated, please use \"SourceName\" instead.", true)]
194-
public string PackageSourceName
195-
{
196-
get
197-
{
198-
return _sourceName;
199-
}
200-
set
201-
{
202-
if (value is null)
203-
{
204-
_sourceName = string.Empty;
205-
}
206-
else
207-
{
208-
_sourceName = value;
209-
}
210-
}
211-
}
212115
/// <summary>
213116
/// Gets or sets the source name for the package.
214117
/// </summary>
@@ -260,6 +163,7 @@ public bool HasShortenedId
260163
private string _name = string.Empty;
261164
private string _id = string.Empty;
262165
private string _version = string.Empty;
166+
private Version _versionObject = new(0, 0);
263167
private string _availableVersion = string.Empty;
264168
private string _sourceName = string.Empty;
265169

0 commit comments

Comments
 (0)