Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix update interval edge case, remove duplicate code and add better startup warning #134

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 9 additions & 23 deletions Microsoft.PowerShell_profile.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,6 @@ function Update-Profile {
}
}

# Check if not in debug mode AND (updateInterval is -1 OR file doesn't exist OR time difference is greater than the update interval)
if (-not $debug -and `
($updateInterval -eq -1 -or `
-not (Test-Path $timeFilePath) -or `
((Get-Date) - [datetime]::ParseExact((Get-Content -Path $timeFilePath), 'yyyy-MM-dd', $null)).TotalDays -gt $updateInterval)) {

Update-Profile
$currentTime = Get-Date -Format 'yyyy-MM-dd'
$currentTime | Out-File -FilePath $timeFilePath

} elseif (-not $debug) {
Write-Warning "Profile update skipped. Last update check was within the last $updateInterval day(s)."
} else {
Write-Warning "Skipping profile update check in debug mode"
}

function Update-PowerShell {
try {
Write-Host "Checking for PowerShell updates..." -ForegroundColor Cyan
Expand All @@ -117,20 +101,22 @@ function Update-PowerShell {
}
}

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

Update-Profile
Update-PowerShell
$currentTime = Get-Date -Format 'yyyy-MM-dd'
$currentTime | Out-File -FilePath $timeFilePath
$currentDate.ToString('yyyy-MM-dd') | Out-File -FilePath $timeFilePath

} elseif (-not $debug) {
Write-Warning "PowerShell update skipped. Last update check was within the last $updateInterval day(s)."
Write-Warning "Profile and Powershell update skipped. Next update check at $($updateDate.AddDays($updateInterval).ToString('yyyy-MM-dd'))."
} else {
Write-Warning "Skipping PowerShell update in debug mode"
Write-Warning "Skipping profile and Powershell update check in debug mode"
}

function Clear-Cache {
Expand Down