Skip to content

Commit a4105ac

Browse files
authored
Merge pull request #118 from tigattack/feat/update-notification-improvements
2 parents d819391 + 9cd32b9 commit a4105ac

2 files changed

Lines changed: 30 additions & 9 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ ExampleData
33
/Test-*
44
log/*.log
55
config/conf.json
6+
update-notification.marker

resources/UpdateInfo.psm1

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function Get-UpdateShouldNotify {
1414

1515
$result = [UpdateShouldNotifyResult]@{
1616
ShouldNotify = $true
17+
Message = ''
1718
}
1819

1920
# If no update is available, no need to notify
@@ -26,27 +27,46 @@ function Get-UpdateShouldNotify {
2627
# Define marker file path
2728
$markerFilePath = "$PSScriptRoot\update-notification.marker"
2829

30+
$currentVersion = $UpdateStatus.CurrentVersion
31+
2932
# Check if marker file exists
3033
if (Test-Path $markerFilePath) {
34+
$versionChanged = $false
35+
$markerVersion = Get-Content -Path $markerFilePath -Raw -ErrorAction SilentlyContinue
36+
if ($null -ne $markerVersion) {
37+
# Trim version and compare with current version
38+
$markerVersion = $markerVersion.Trim()
39+
$versionChanged = $markerVersion -ne $currentVersion
40+
}
41+
3142
$markerFile = Get-Item $markerFilePath
3243
$timeSinceLastNotification = (Get-Date) - $markerFile.LastWriteTime
3344

34-
# If less than 24 hours have passed since last notification, don't notify
45+
# If version has changed, always notify regardless of time
46+
if ($versionChanged) {
47+
$result.Message = "Version changed from $markerVersion to $currentVersion since last notification. Proceeding to notify."
48+
# Update the marker file with current version
49+
$currentVersion | Out-File -FilePath $markerFilePath -Force -NoNewline
50+
return $result
51+
}
52+
53+
# If less than 24 hours have passed since last notification for the same version, don't notify
3554
if ($timeSinceLastNotification.TotalHours -lt 24) {
3655
$result.ShouldNotify = $false
3756
$result.Message = "Update notification suppressed. Last notification was $($timeSinceLastNotification.TotalHours.ToString('0.00')) hours ago."
38-
3957
return $result
4058
}
41-
}
42-
43-
# Create or touch the marker file to indicate notification was sent
44-
if (Test-Path $markerFilePath) {
45-
(Get-Item $markerFilePath).LastWriteTime = Get-Date
59+
# If more than 24 hours have passed, proceed to notify and update the marker file contents
60+
else {
61+
$result.Message = "Update notification marker file found. Last notification was $($timeSinceLastNotification.TotalHours.ToString('0.00')) hours ago. Proceeding to notify."
62+
# Update the marker file with current version - Also updates the file's modtime as a side effect.
63+
$currentVersion | Out-File -FilePath $markerFilePath -Force -NoNewline
64+
}
4665
}
4766
else {
48-
New-Item -Path $markerFilePath -ItemType File -Force | Out-Null
49-
$result.Message = "Created update notification marker file at $markerFilePath"
67+
# Create the marker file to indicate notification was sent and store current version
68+
$currentVersion | Out-File -FilePath $markerFilePath -Force -NoNewline
69+
$result.Message = "Created update notification marker file at $markerFilePath with version $currentVersion"
5070
}
5171

5272
return $result

0 commit comments

Comments
 (0)