Test-NugetVersionRange.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
# This script for internal use only.
# To avoid Nuget.Versioning.dll assembly load conflict, we call this script as a job (as a external process).
param ([string]$Query)

# Load assembly
Add-Type -AssemblyName './NuGet.Versioning.dll'

$output = [pscustomobject]@{
    Result           = $false
    MinVersionString = $null
    IsMinInclusive   = $true
    MaxVersionString = $null
    IsMaxInclusive   = $true
    VerboseLogs      = [System.Collections.Generic.List[string]]::new()
}
$output.VerboseLogs.Add("Parameters : Query = $($Query), WorkingDirectory = $($pwd.Path)")
$output.VerboseLogs.Add("Loaded Assembly : $([NuGet.Versioning.VersionRange].Assembly.FullName)")
$output.VerboseLogs.Add("Assembly Location : $([NuGet.Versioning.VersionRange].Assembly.Location)")

# early return
$Query = $Query.Trim()
if ([string]::IsNullOrEmpty($Query)) {
    $output.Result = $true
    return $output
}
if ($Query -eq '*') {
    $output.Result = $true
    return $output
}

# try parse single version
# ref : https://docs.microsoft.com/ja-jp/nuget/concepts/package-versioning#version-ranges
$parsedVer = $null
if ([semver]::TryParse($Query, [ref]$parsedVer)) {
    $output.Result = $true
    $output.MinVersionString = $parsedVer.ToString()
    return $output
}

# try parse range version
$parsedVer = $null
if ([NuGet.Versioning.VersionRange]::TryParse($Query, [ref]$parsedVer)) {
    $output.Result = $true
    $output.VerboseLogs.Add("Parsed value : IsMinInclusive = $($parsedVer.IsMinInclusive), IsMaxInclusive = $($parsedVer.IsMaxInclusive)")
    $output.MinVersionString = $(if ($parsedVer.MinVersion) { $parsedVer.MinVersion.ToFullString() }else { $null })
    $output.IsMinInclusive = $parsedVer.IsMinInclusive
    $output.MaxVersionString = $(if ($parsedVer.MaxVersion) { $parsedVer.MaxVersion.ToFullString() }else { $null })
    $output.IsMaxInclusive = $parsedVer.IsMaxInclusive
    return $output
}

# faled parse
return $output