Skip to content

Commit eabe53e

Browse files
committed
Updated version check
1 parent a708b33 commit eabe53e

File tree

5 files changed

+92
-36
lines changed

5 files changed

+92
-36
lines changed

build/Build-Debug.ps1

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,29 @@ $localPnPFrameworkPathValue = $env:PnPFrameworkPath
2424
$env:PnPCoreSdkPath = ""
2525
$env:PnPFrameworkPath = ""
2626

27-
$versionFileContents = Get-Content "$PSScriptRoot/../version.txt" -Raw
28-
if ($versionFileContents.Contains("%")) {
29-
$versionString = $versionFileContents.Replace("%", "0");
27+
$versionFileContents = Get-Content "$PSScriptRoot/../version.json" -Raw | ConvertFrom-Json
28+
29+
if ($versionFileContents.Version.Contains("%")) {
30+
$versionString = $versionFileContents.Version.Replace("%", "0");
3031
$versionObject = [System.Management.Automation.SemanticVersion]::Parse($versionString)
3132
$buildVersion = $versionObject.Patch;
3233
}
3334
else {
34-
$versionObject = [System.Management.Automation.SemanticVersion]::Parse($versionFileContents)
35+
$versionObject = [System.Management.Automation.SemanticVersion]::Parse($versionFileContents.Version)
3536
$buildVersion = $versionObject.Patch + 1;
3637
}
3738

39+
# $versionFileContents = Get-Content "$PSScriptRoot/../version.txt" -Raw
40+
# if ($versionFileContents.Contains("%")) {
41+
# $versionString = $versionFileContents.Replace("%", "0");
42+
# $versionObject = [System.Management.Automation.SemanticVersion]::Parse($versionString)
43+
# $buildVersion = $versionObject.Patch;
44+
# }
45+
# else {
46+
# $versionObject = [System.Management.Automation.SemanticVersion]::Parse($versionFileContents)
47+
# $buildVersion = $versionObject.Patch + 1;
48+
# }
49+
3850
$configuration = "net8.0-windows"
3951

4052
$version = "$($versionObject.Major).$($versionObject.Minor).$buildVersion"

build/Build-Nightly.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,8 @@ if ($runPublish -eq $true) {
199199

200200
# Write version back to version
201201
Set-Content ./version.txt -Value $version -Force -NoNewline
202+
203+
# Write version back to version.json
204+
$json = @{Version="$version";Message=""} | ConvertTo-Json
205+
Set-Content ./version.json -Value $json -Force -NoNewline
202206
}

src/Commands/Base/GetDiagnostics.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void FillCurrentSite(Diagnostics result)
6464
void FillNewerVersionAvailable(Diagnostics result)
6565
{
6666
var versionAvailable = VersionChecker.GetAvailableVersion();
67-
if (versionAvailable != null && VersionChecker.IsNewer(versionAvailable))
67+
if (versionAvailable != null && VersionChecker.IsNewer(versionAvailable.Version))
6868
{
6969
result.NewerVersionAvailable = versionAvailable.ToString();
7070
}

src/Commands/Utilities/VersionChecker.cs

Lines changed: 67 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ public static class VersionChecker
1919
/// URL to the PnP PowerShell release notes for the nightly release
2020
/// </summary>
2121
private static readonly Uri NightlyVersionCheckUrl = new Uri("https://raw.githubusercontent.com/pnp/powershell/dev/version.txt");
22+
private static readonly Uri NightlyVersionCheckJsonUrl = new Uri("https://raw.githubusercontent.com/pnp/powershell/dev/version.json");
2223

2324
/// <summary>
2425
/// URL to the PnP PowerShell release notes for the stable release
2526
/// </summary>
2627
private static readonly Uri ReleaseVersionCheckUrl = new Uri("https://raw.githubusercontent.com/pnp/powershell/master/version.txt");
28+
private static readonly Uri ReleaseVersionCheckJsonUrl = new Uri("https://raw.githubusercontent.com/pnp/powershell/master/version.json");
2729

2830
/// <summary>
2931
/// Boolean to indicate if the version check has already been performed
@@ -67,19 +69,29 @@ public static void CheckVersion(PSCmdlet cmdlet)
6769
cmdlet?.WriteVerbose($"Checking for updates, current version is {productVersion}. See https://pnp.github.io/powershell/articles/configuration.html#disable-or-enable-version-checks for more information.");
6870

6971
// Check for the latest available version
70-
var onlineVersion = GetAvailableVersion2(isNightly);
71-
72-
if (IsNewer(onlineVersion))
72+
var onlineVersion = GetAvailableVersion3(isNightly);
73+
if (onlineVersion != null)
7374
{
74-
if (cmdlet != null)
75+
76+
if (IsNewer(onlineVersion.Version))
7577
{
76-
var updateMessage = $"\nA newer version of PnP PowerShell is available: {onlineVersion}.\n\nUse 'Update-Module -Name PnP.PowerShell{(isNightly ? " -AllowPrerelease" : "")}' to update.\nUse 'Get-PnPChangeLog {(!isNightly ? $"-Release {onlineVersion}" : "-Nightly")}' to list changes.\n\nYou can turn this check off by setting the 'PNPPOWERSHELL_UPDATECHECK' environment variable to 'Off'.\n";
77-
CmdletMessageWriter.WriteFormattedWarning(cmdlet, updateMessage);
78+
if (cmdlet != null)
79+
{
80+
var updateMessage = $"\nA newer version of PnP PowerShell is available: {onlineVersion}.\n\nUse 'Update-Module -Name PnP.PowerShell{(isNightly ? " -AllowPrerelease" : "")}' to update.\nUse 'Get-PnPChangeLog {(!isNightly ? $"-Release {onlineVersion}" : "-Nightly")}' to list changes.\n\nYou can turn this check off by setting the 'PNPPOWERSHELL_UPDATECHECK' environment variable to 'Off'.\n";
81+
CmdletMessageWriter.WriteFormattedWarning(cmdlet, updateMessage);
82+
}
83+
}
84+
else
85+
{
86+
cmdlet?.WriteVerbose($"No newer version of PnP PowerShell is available, latest available version is {onlineVersion}");
87+
}
88+
if (!string.IsNullOrEmpty(onlineVersion.Message))
89+
{
90+
if (cmdlet != null)
91+
{
92+
CmdletMessageWriter.WriteFormattedMessage(cmdlet, new CmdletMessageWriter.Message() { Formatted = true, Text = onlineVersion.Message, Type = CmdletMessageWriter.MessageType.Message });
93+
}
7894
}
79-
}
80-
else
81-
{
82-
cmdlet?.WriteVerbose($"No newer version of PnP PowerShell is available, latest available version is {onlineVersion}");
8395
}
8496
VersionChecked = true;
8597
}
@@ -94,36 +106,35 @@ public static void CheckVersion(PSCmdlet cmdlet)
94106
/// </summary>
95107
/// <param name="availableVersionString">The version to check the current version against</param>
96108
/// <returns>True if the provided version is newer than the current version, false if it is not</returns>
97-
public static bool IsNewer(string availableVersionString)
109+
public static bool IsNewer(SemanticVersion availableVersion)
98110
{
99111
var assembly = Assembly.GetExecutingAssembly();
100112
var versionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);
101-
var productVersion = versionInfo.ProductVersion;
102-
103-
if (SemanticVersion.TryParse(availableVersionString, out SemanticVersion availableVersion))
113+
var productVersion = versionInfo.ProductVersion;
114+
115+
116+
if (availableVersion.Major > versionInfo.ProductMajorPart)
104117
{
105-
if (availableVersion.Major > versionInfo.ProductMajorPart)
118+
return true;
119+
}
120+
else
121+
{
122+
if (versionInfo.ProductMajorPart == availableVersion.Major && availableVersion.Minor > versionInfo.ProductMinorPart)
106123
{
107124
return true;
108125
}
109126
else
110127
{
111-
if (versionInfo.ProductMajorPart == availableVersion.Major && availableVersion.Minor > versionInfo.ProductMinorPart)
112-
{
113-
return true;
114-
}
115-
else
128+
if (productVersion.Contains("-"))
116129
{
117-
if (productVersion.Contains("-"))
130+
if (versionInfo.ProductMajorPart == availableVersion.Major && versionInfo.ProductMinorPart == availableVersion.Minor && availableVersion.Patch > versionInfo.ProductBuildPart)
118131
{
119-
if (versionInfo.ProductMajorPart == availableVersion.Major && versionInfo.ProductMinorPart == availableVersion.Minor && availableVersion.Patch > versionInfo.ProductBuildPart)
120-
{
121-
return true;
122-
}
132+
return true;
123133
}
124134
}
125135
}
126136
}
137+
127138
return false;
128139
}
129140

@@ -134,7 +145,7 @@ public static bool IsNewer(string availableVersionString)
134145
internal static string GetAvailableVersion(bool isNightly)
135146
{
136147
var httpClient = PnP.Framework.Http.PnPHttpClient.Instance.GetHttpClient();
137-
148+
138149
// Deliberately lowering timeout as the version check is not critical so in case of a slower or blocked internet connection, this should not block the cmdlet for too long
139150
httpClient.Timeout = TimeSpan.FromSeconds(VersionCheckTimeOut);
140151
var request = new HttpRequestMessage(HttpMethod.Get, isNightly ? NightlyVersionCheckUrl : ReleaseVersionCheckUrl)
@@ -152,10 +163,29 @@ internal static string GetAvailableVersion(bool isNightly)
152163
return null;
153164
}
154165

166+
internal static PnPVersionResult GetAvailableVersion3(bool isNightly)
167+
{
168+
var httpClient = PnP.Framework.Http.PnPHttpClient.Instance.GetHttpClient();
169+
170+
// Deliberately lowering timeout as the version check is not critical so in case of a slower or blocked internet connection, this should not block the cmdlet for too long
171+
httpClient.Timeout = TimeSpan.FromSeconds(VersionCheckTimeOut);
172+
var request = new HttpRequestMessage(HttpMethod.Get, isNightly ? NightlyVersionCheckJsonUrl : ReleaseVersionCheckJsonUrl);
173+
174+
var response = httpClient.SendAsync(request).GetAwaiter().GetResult();
175+
if (response.IsSuccessStatusCode)
176+
{
177+
var onlineVersionRaw = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
178+
var onlineVersion = System.Text.Json.JsonSerializer.Deserialize<PnPVersionResult>(onlineVersionRaw);
179+
return onlineVersion;
180+
}
181+
return null;
182+
}
183+
184+
155185
internal static string GetAvailableVersion2(bool isNightly)
156186
{
157187
var httpClient = PnP.Framework.Http.PnPHttpClient.Instance.GetHttpClient();
158-
188+
159189
// Deliberately lowering timeout as the version check is not critical so in case of a slower or blocked internet connection, this should not block the cmdlet for too long
160190
httpClient.Timeout = TimeSpan.FromSeconds(VersionCheckTimeOut);
161191
var request = new HttpRequestMessage(HttpMethod.Get, $"https://www.powershellgallery.com/api/v2/FindPackagesById()?id='PnP.PowerShell'&$top=10&$orderby=Created%20desc{(isNightly ? "" : "&$filter=IsPrerelease%20eq%20false")}");
@@ -169,10 +199,10 @@ internal static string GetAvailableVersion2(bool isNightly)
169199
var onlineVersion = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
170200
var xml = XDocument.Parse(onlineVersion);
171201
var entry = xml.Root.Elements(atomNS + "entry").FirstOrDefault();
172-
if(entry!= null)
202+
if (entry != null)
173203
{
174204
var properties = entry.Elements(metadataNS + "properties").FirstOrDefault();
175-
if(properties != null)
205+
if (properties != null)
176206
{
177207
var version = properties.Element(dataServicesNS + "Version").Value;
178208
return version;
@@ -186,14 +216,20 @@ internal static string GetAvailableVersion2(bool isNightly)
186216
/// Retrieves the latest available version of PnP PowerShell. If the current version is a nightly build, it will check for the latest nightly build as well. If the current version is a stable build, it will only check for the latest stable build.
187217
/// </summary>
188218
/// <returns>The latest available version</returns>
189-
public static string GetAvailableVersion()
219+
public static PnPVersionResult GetAvailableVersion()
190220
{
191221
var assembly = Assembly.GetExecutingAssembly();
192222
var versionInfo = FileVersionInfo.GetVersionInfo(assembly.Location);
193223
var productVersion = versionInfo.ProductVersion;
194224
var isNightly = productVersion.Contains("-");
195225

196-
return GetAvailableVersion(isNightly);
226+
return GetAvailableVersion3(isNightly);
197227
}
198228
}
229+
230+
public class PnPVersionResult
231+
{
232+
public SemanticVersion Version { get; set; }
233+
public string Message { get; set; }
234+
}
199235
}

version.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"Version":"2.99.51",
3+
"Message":"Nightly Build"
4+
}

0 commit comments

Comments
 (0)