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

Remove reference to removed VersionCheckFile #1481

Open
wants to merge 4 commits 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
33 changes: 18 additions & 15 deletions PowerShell/ScubaGear/CheckVersion.ps1
twneale marked this conversation as resolved.
Show resolved Hide resolved
twneale marked this conversation as resolved.
Show resolved Hide resolved
twneale marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,43 +1,46 @@
function Invoke-CheckScubaGearVersionPSGallery {
function Invoke-CheckScubaGearVersion {

# Retrieve the installed version of ScubaGear from the system
$InstalledModule = Get-Module -Name ScubaGear -ListAvailable -ErrorAction 'Stop'
if ($InstalledModule) {
$CurrentVersion = [System.Version]$InstalledModule.Version
} else {

# If multiple different versions are installed, get the most recent.
if ($InstalledModule -is [array]) {
$InstalledModule = $InstalledModule | Sort-Object | Select-Object -First 1
}

# Check if no results found.
if (!$InstalledModule) {
# If we are here, ScubaGear is not installed from PSGallery.
# Or it may have been installed a different way in a nonstandard folder,
# or is running in an extracted release folder. Check github instead.
return Invoke-CheckScubaGearVersionGithub -ErrorAction 'Stop'
}

$LatestInstalledVersion = [System.Version]$InstalledModule.Version

# Retrieve the latest version from PowerShell Gallery
$ModuleInfo = Find-Module -Name ScubaGear -ErrorAction 'Stop'
$LatestVersion = [System.Version]$ModuleInfo.Version

if ($CurrentVersion -lt $LatestVersion) {
Write-Warning "A new version of ScubaGear ($LatestVersion) is available on PowerShell Gallery. This notification can be disabled by setting `$env:SCUBAGEAR_SKIP_VERSION_CHECK = `$true before running ScubaGear."
$LatestPSGalleryVersion = [System.Version]$ModuleInfo.Version

if ($LatestInstalledVersion -lt $LatestPSGalleryVersion) {
Write-Warning "A newer version of ScubaGear ($LatestPSGalleryVersion) is available on PowerShell Gallery. This notification can be disabled by setting `$env:SCUBAGEAR_SKIP_VERSION_CHECK = `$true before running ScubaGear."
}

# Store the current time in the file to mark the last check time
(Get-Date -ErrorAction 'Stop').ToString() | Set-Content $VersionCheckFile -ErrorAction 'Stop'
}


function Invoke-CheckScubaGearVersionGithub {
$ScubaManifest = Import-PowerShellDataFile (Join-Path -Path $PSScriptRoot -ChildPath 'ScubaGear.psd1' -Resolve -ErrorAction 'Stop' ) -ErrorAction 'Stop'
$CurrentVersion = $ScubaManifest.ModuleVersion
$LatestVersion = $(Invoke-RestMethod -Uri "https://api.github.com/repos/cisagov/ScubaGear/releases/latest" -ErrorAction 'Stop').tag_name.TrimStart("v")
if ($CurrentVersion -ne $LatestVersion) {
$CurrentVersion = [System.Version]$ScubaManifest.ModuleVersion
$LatestVersion = [System.Version]$(Invoke-RestMethod -Uri "https://api.github.com/repos/cisagov/ScubaGear/releases/latest" -ErrorAction 'Stop').tag_name.TrimStart("v")
if ($CurrentVersion -lt $LatestVersion) {
Write-Warning "A new version of ScubaGear ($latestVersion) is available. Please consider updating at: https://github.com/cisagov/ScubaGear/releases. This notification can be disabled by setting `$env:SCUBAGEAR_SKIP_VERSION_CHECK = `$true before running ScubaGear."
}
}

# Do the version check if the skip envvar is not defined.
if ([string]::IsNullOrWhiteSpace($env:SCUBAGEAR_SKIP_VERSION_CHECK)) {
try {
Invoke-CheckScubaGearVersionPSGallery -ErrorAction 'Stop'
Invoke-CheckScubaGearVersion -ErrorAction 'Stop'
}
catch {
Write-Warning "The ScubaGear version check failed to execute. This notification can be disabled by setting `$env:SCUBAGEAR_SKIP_VERSION_CHECK = `$true.`n$($_.Exception.Message)`n$($_.ScriptStackTrace)"
Expand Down
Loading