Skip to content

Commit 0dfe93c

Browse files
profile: Remove duplicate update check code fixing edge case + add actual info to warning
In the previous implementation (PR ChrisTitusTech#127), there was an edge case where a powershell update check will take place and a profile update check might not due to floating point vs int values (.Date vs without (L84 vs L125 in PR)). Commonize the checks since no situation exists where one runs but the other does not. Add better dev-centered warning message since the previous warning was made to address the fact the profile has transitioned from update checks every launch to a periodic check, now that users are aware of it, we can notify them when the last update was rather than the fact it was just skipped. (Issue ChrisTitusTech#132) Signed-off-by: Anirudh Gupta <[email protected]>
1 parent 65e2819 commit 0dfe93c

File tree

1 file changed

+9
-23
lines changed

1 file changed

+9
-23
lines changed

Microsoft.PowerShell_profile.ps1

+9-23
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,6 @@ function Update-Profile {
7777
}
7878
}
7979

80-
# Check if not in debug mode AND (updateInterval is -1 OR file doesn't exist OR time difference is greater than the update interval)
81-
if (-not $debug -and `
82-
($updateInterval -eq -1 -or `
83-
-not (Test-Path $timeFilePath) -or `
84-
((Get-Date) - [datetime]::ParseExact((Get-Content -Path $timeFilePath), 'yyyy-MM-dd', $null)).TotalDays -gt $updateInterval)) {
85-
86-
Update-Profile
87-
$currentTime = Get-Date -Format 'yyyy-MM-dd'
88-
$currentTime | Out-File -FilePath $timeFilePath
89-
90-
} elseif (-not $debug) {
91-
Write-Warning "Profile update skipped. Last update check was within the last $updateInterval day(s)."
92-
} else {
93-
Write-Warning "Skipping profile update check in debug mode"
94-
}
95-
9680
function Update-PowerShell {
9781
try {
9882
Write-Host "Checking for PowerShell updates..." -ForegroundColor Cyan
@@ -117,20 +101,22 @@ function Update-PowerShell {
117101
}
118102
}
119103

120-
# skip in debug mode
104+
$updateDate = [datetime]::ParseExact((Get-Content -Path $timeFilePath), 'yyyy-MM-dd', $null)
105+
$currentDate = (Get-Date)
121106
# Check if not in debug mode AND (updateInterval is -1 OR file doesn't exist OR time difference is greater than the update interval)
122107
if (-not $debug -and `
123108
($updateInterval -eq -1 -or `
124-
-not (Test-Path $timeFilePath) -or `
125-
((Get-Date).Date - [datetime]::ParseExact((Get-Content -Path $timeFilePath), 'yyyy-MM-dd', $null).Date).TotalDays -gt $updateInterval)) {
109+
-not (Test-Path $timeFilePath) -or `
110+
($currentDate.Date - $updateDate.Date).TotalDays -gt $updateInterval)) {
126111

112+
Update-Profile
127113
Update-PowerShell
128-
$currentTime = Get-Date -Format 'yyyy-MM-dd'
129-
$currentTime | Out-File -FilePath $timeFilePath
114+
$currentDate.ToString('yyyy-MM-dd') | Out-File -FilePath $timeFilePath
115+
130116
} elseif (-not $debug) {
131-
Write-Warning "PowerShell update skipped. Last update check was within the last $updateInterval day(s)."
117+
Write-Warning "Profile and Powershell update skipped. Last update check was at $($updateDate.ToString('yyyy-MM-dd'))."
132118
} else {
133-
Write-Warning "Skipping PowerShell update in debug mode"
119+
Write-Warning "Skipping profile and Powershell update check in debug mode"
134120
}
135121

136122
function Clear-Cache {

0 commit comments

Comments
 (0)