Test-LatestVersion.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<#
.SYNOPSIS
    Test current PowerShell Core is the latest version.
#>

function Test-LatestVersion {
    [CmdletBinding()]
    param (
        [ReleaseTypes]$Release = [ReleaseTypes]::Stable,
        [switch]$PassThru
    )

    # get latest build status
    $buildStatus = Find-PowerShellBuildStatus -Release $Release
    if (-not $buildStatus) {
        Write-Error $Messages.Test_LatestVersion_001
        return
    }

    if ($PSVersionTable.PSVersion -gt $buildStatus.Version) {
        # Note : This pattern occurs when using LTS, Preview version.
        WriteInfo ($Messages.Test_LatestVersion_002 -f $PSVersionTable.PSVersion, $buildStatus.Version)
        NotifyNewVersion -Published $buildStatus.ReleaseDate -Release $Release
        if ($PassThru) {
            return [PSCustomObject]@{ Result = $true; Release = $Release; LatestVersion = $buildStatus.Version }
        }
        return
    }
    if ($PSVersionTable.PSVersion -eq $buildStatus.Version) {
        WriteInfo ($Messages.Test_LatestVersion_003 -f $PSVersionTable.PSVersion)
        NotifyNewVersion -Published $buildStatus.ReleaseDate -Release $Release
        if ($PassThru) {
            return [PSCustomObject]@{ Result = $true; Release = $Release; LatestVersion = $buildStatus.Version }
        }
        return
    }
    Write-Warning ($Messages.Test_LatestVersion_004 -f $buildStatus.Version)
    if ($PassThru) {
        return [PSCustomObject]@{ Result = $false; Release = $Release; LatestVersion = $buildStatus.Version }
    }
}

function NotifyNewVersion ([datetime]$Published, [ReleaseTypes]$Release) {
    # Note : Notify the new version releasing is coming.
    # * Stable Release : after a half year (180 days)
    # * Preview Release : after 3 weeks (21 days)
    # * LTS : no notification
    if ($Release -eq 'LTS') {
        return
    }
    $span = if ($Release -eq 'Preview') { [timespan]::new(21, 0, 0, 0) } else { [timespan]::new(180, 0, 0, 0) }
    $elapsed = (Get-Date).Subtract($Published)
    if ($elapsed -ge $span) {
        WriteInfo ""
        WriteInfo ($Messages.Test_LatestVersion_005 -f $elapsed.Days)
        WriteInfo $Messages.Test_LatestVersion_006
    }
}