From dd2dd41430822209f09ffd243b3ef242ffe831b6 Mon Sep 17 00:00:00 2001 From: MaksimZhukov Date: Wed, 1 Jul 2020 14:51:32 +0300 Subject: [PATCH 01/11] Add script to get new available tool versions --- check-new-tool-versions.ps1 | 127 ++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 check-new-tool-versions.ps1 diff --git a/check-new-tool-versions.ps1 b/check-new-tool-versions.ps1 new file mode 100644 index 0000000..3b83239 --- /dev/null +++ b/check-new-tool-versions.ps1 @@ -0,0 +1,127 @@ +<# +.SYNOPSIS +Check and return list of new available tool versions +#> + +param ( + [Parameter(Mandatory)] [string] $DistURL, + [Parameter(Mandatory)] [string] $ManifestLink, + [string] $VersionFilterToInclude, + [string] $VersionFilterToExclude, + [UInt32] $RetryIntervalSec = 60, + [UInt32] $RetryCount = 3 +) + +function Get-VersionsByUrl { + param ( + [Parameter(Mandatory)] [string] $ToolPackagesUrl, + [Parameter(Mandatory)] [UInt32] $RetryIntervalSec, + [Parameter(Mandatory)] [UInt32] $RetryCount + ) + + $packages = Invoke-RestMethod $ToolPackagesUrl -MaximumRetryCount $RetryCount -RetryIntervalSec $RetryIntervalSec + return $packages.version +} + +function Format-Versions { + param ( + [Parameter(Mandatory)] [string[]] $Versions + ) + + [Version[]] $formattedVersions = @() + + foreach($version in $Versions) { + $substredVersion = $null + + # We cut a string from index of first digit because initially it has invalid format (v14.4.0 or go1.14.4) + if ($version -match '(?\d)') { + $firstDigitIndex = $version.indexof($Matches.number) + $substredVersion = $version.substring($firstDigitIndex) + } else { + Write-Host "Invalid version format - $version" + exit 1 + } + + # We filter versions to exclude unstable (for example: "go1.15beta1") + # Valid version format: x.x or x.x.x + if ($substredVersion -notmatch '^\d+\.+\d+\.*\d*$') { + continue + } + + if ($substredVersion.Split(".").Length -lt 3) { + $formattedVersions += "$substredVersion.0" + } else { + $formattedVersions += $substredVersion + } + } + + return $formattedVersions +} + +function Filter-Versions { + param ( + [Parameter(Mandatory)] [string[]] $Versions, + [Parameter(Mandatory)] [string] $VersionFilter, + [Parameter(Mandatory)] [bool] $IncludeVersions + ) + + $versionFilters = $VersionFilter.Split(',') + [Version[]] $filteredVersions = @() + + foreach($filter in $versionFilters) { + if ($IncludeVersions) { + $filteredVersions += $Versions | Where-Object { $_ -like $filter } + } else { + $filteredVersions += $Versions | Where-Object { $_ -notlike $filter } + } + } + + return $filteredVersions +} + +function Get-VersionsToBuild { + param ( + [Parameter(Mandatory)] [string[]] $VersionsFromManifest, + [Parameter(Mandatory)] [string[]] $VersionsFromDist + ) + + [System.Collections.ArrayList]$versionsToBuid = $VersionsFromDist + $VersionsFromManifest | ForEach-Object { $versionsToBuid.Remove($_) } + + return $versionsToBuid +} + +Write-Host "Get the packages list from $DistURL" +$versionsFromDist = Get-VersionsByUrl -ToolPackagesUrl $DistURL ` + -RetryIntervalSec $RetryIntervalSec ` + -RetryCount $RetryCount + +Write-Host "Get the packages list from $ManifestLink" +[Version[]] $versionsFromManifest = Get-VersionsByUrl -ToolPackagesUrl $ManifestLink ` + -RetryIntervalSec $RetryIntervalSec ` + -RetryCount $RetryCount + +[Version[]] $formattedVersions = Format-Versions -Versions $versionsFromDist + +if (-not ([string]::IsNullOrEmpty($VersionFilterToInclude))) { + $formattedVersions = Filter-Versions -Versions $formattedVersions ` + -VersionFilter $VersionFilterToInclude ` + -IncludeVersions $true +} + +if (-not ([string]::IsNullOrEmpty($VersionFilterToExclude))) { + $formattedVersions = Filter-Versions -Versions $formattedVersions ` + -VersionFilter $VersionFilterToExclude ` + -IncludeVersions $false +} + +$versionsToBuild = Get-VersionsToBuild -VersionsFromManifest $versionsFromManifest ` + -VersionsFromDist $formattedVersions + +if ([string]::IsNullOrEmpty($versionsToBuild)) { + Write-Host "There isn't versions to build" + return $null +} else { + Write-Host "The following versions are available to build:`n$versionsToBuild" + return "$versionsToBuild" +} From 01b329d6ef089a88a26e4bf5ac8635ce9ba19793 Mon Sep 17 00:00:00 2001 From: MaksimZhukov Date: Wed, 1 Jul 2020 16:52:10 +0300 Subject: [PATCH 02/11] Change structure --- .../get-new-tool-versions.ps1 | 70 +------------------ get-new-tool-versions/helpers.psm1 | 67 ++++++++++++++++++ 2 files changed, 69 insertions(+), 68 deletions(-) rename check-new-tool-versions.ps1 => get-new-tool-versions/get-new-tool-versions.ps1 (53%) create mode 100644 get-new-tool-versions/helpers.psm1 diff --git a/check-new-tool-versions.ps1 b/get-new-tool-versions/get-new-tool-versions.ps1 similarity index 53% rename from check-new-tool-versions.ps1 rename to get-new-tool-versions/get-new-tool-versions.ps1 index 3b83239..7eebb55 100644 --- a/check-new-tool-versions.ps1 +++ b/get-new-tool-versions/get-new-tool-versions.ps1 @@ -12,6 +12,8 @@ param ( [UInt32] $RetryCount = 3 ) +Import-Module (Join-Path $PSScriptRoot "helpers.psm1") + function Get-VersionsByUrl { param ( [Parameter(Mandatory)] [string] $ToolPackagesUrl, @@ -23,74 +25,6 @@ function Get-VersionsByUrl { return $packages.version } -function Format-Versions { - param ( - [Parameter(Mandatory)] [string[]] $Versions - ) - - [Version[]] $formattedVersions = @() - - foreach($version in $Versions) { - $substredVersion = $null - - # We cut a string from index of first digit because initially it has invalid format (v14.4.0 or go1.14.4) - if ($version -match '(?\d)') { - $firstDigitIndex = $version.indexof($Matches.number) - $substredVersion = $version.substring($firstDigitIndex) - } else { - Write-Host "Invalid version format - $version" - exit 1 - } - - # We filter versions to exclude unstable (for example: "go1.15beta1") - # Valid version format: x.x or x.x.x - if ($substredVersion -notmatch '^\d+\.+\d+\.*\d*$') { - continue - } - - if ($substredVersion.Split(".").Length -lt 3) { - $formattedVersions += "$substredVersion.0" - } else { - $formattedVersions += $substredVersion - } - } - - return $formattedVersions -} - -function Filter-Versions { - param ( - [Parameter(Mandatory)] [string[]] $Versions, - [Parameter(Mandatory)] [string] $VersionFilter, - [Parameter(Mandatory)] [bool] $IncludeVersions - ) - - $versionFilters = $VersionFilter.Split(',') - [Version[]] $filteredVersions = @() - - foreach($filter in $versionFilters) { - if ($IncludeVersions) { - $filteredVersions += $Versions | Where-Object { $_ -like $filter } - } else { - $filteredVersions += $Versions | Where-Object { $_ -notlike $filter } - } - } - - return $filteredVersions -} - -function Get-VersionsToBuild { - param ( - [Parameter(Mandatory)] [string[]] $VersionsFromManifest, - [Parameter(Mandatory)] [string[]] $VersionsFromDist - ) - - [System.Collections.ArrayList]$versionsToBuid = $VersionsFromDist - $VersionsFromManifest | ForEach-Object { $versionsToBuid.Remove($_) } - - return $versionsToBuid -} - Write-Host "Get the packages list from $DistURL" $versionsFromDist = Get-VersionsByUrl -ToolPackagesUrl $DistURL ` -RetryIntervalSec $RetryIntervalSec ` diff --git a/get-new-tool-versions/helpers.psm1 b/get-new-tool-versions/helpers.psm1 new file mode 100644 index 0000000..c70b3fe --- /dev/null +++ b/get-new-tool-versions/helpers.psm1 @@ -0,0 +1,67 @@ +function Format-Versions { + param ( + [Parameter(Mandatory)] [string[]] $Versions + ) + + [Version[]] $formattedVersions = @() + + foreach($version in $Versions) { + $substredVersion = $null + + # We cut a string from index of first digit because initially it has invalid format (v14.4.0 or go1.14.4) + if ($version -match '(?\d)') { + $firstDigitIndex = $version.indexof($Matches.number) + $substredVersion = $version.substring($firstDigitIndex) + } else { + Write-Host "Invalid version format - $version" + exit 1 + } + + # We filter versions to exclude unstable (for example: "go1.15beta1") + # Valid version format: x.x or x.x.x + if ($substredVersion -notmatch '^\d+\.+\d+\.*\d*$') { + continue + } + + if ($substredVersion.Split(".").Length -lt 3) { + $formattedVersions += "$substredVersion.0" + } else { + $formattedVersions += $substredVersion + } + } + + return $formattedVersions +} + +function Filter-Versions { + param ( + [Parameter(Mandatory)] [string[]] $Versions, + [Parameter(Mandatory)] [string] $VersionFilter, + [Parameter(Mandatory)] [bool] $IncludeVersions + ) + + $versionFilters = $VersionFilter.Split(',') + [Version[]] $filteredVersions = @() + + foreach($filter in $versionFilters) { + if ($IncludeVersions) { + $filteredVersions += $Versions | Where-Object { $_ -like $filter } + } else { + $filteredVersions += $Versions | Where-Object { $_ -notlike $filter } + } + } + + return $filteredVersions +} + +function Get-VersionsToBuild { + param ( + [Parameter(Mandatory)] [string[]] $VersionsFromManifest, + [Parameter(Mandatory)] [string[]] $VersionsFromDist + ) + + [System.Collections.ArrayList]$versionsToBuid = $VersionsFromDist + $VersionsFromManifest | ForEach-Object { $versionsToBuid.Remove($_) } + + return $versionsToBuid +} \ No newline at end of file From 17f3be07b9cd4738b4bcbab1b1ec72f42d73bbcd Mon Sep 17 00:00:00 2001 From: Maxim Lobanov Date: Wed, 1 Jul 2020 16:55:11 +0300 Subject: [PATCH 03/11] Create get-new-tool-versions.Tests.ps1 --- .../get-new-tool-versions.Tests.ps1 | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 get-new-tool-versions/get-new-tool-versions.Tests.ps1 diff --git a/get-new-tool-versions/get-new-tool-versions.Tests.ps1 b/get-new-tool-versions/get-new-tool-versions.Tests.ps1 new file mode 100644 index 0000000..b9802ce --- /dev/null +++ b/get-new-tool-versions/get-new-tool-versions.Tests.ps1 @@ -0,0 +1,64 @@ +#Requires -Modules Pester + +Import-Module (Join-Path $PSScriptRoot "helpers.psm1") -Force + +Describe "Format-Versions" { + It "Clean versions" { + $actualOutput = Format-Versions -Versions @("14.2.0", "1.14.0") + $expectedOutput = @("14.2.0", "1.14.0") + $actualOutput | Should -Be $expectedOutput + } + + It "Versions with prefixes" { + $actualOutput = Format-Versions -Versions @("v14.2.0", "go1.14.0") + $expectedOutput = @("14.2.0", "1.14.0") + $actualOutput | Should -Be $expectedOutput + } + + It "Skip beta and rc versions" { + $actualOutput = Format-Versions -Versions @("14.2.0-beta", "v1.14.0-rc-1") + $expectedOutput = @() + $actualOutput | Should -Be $expectedOutput + } + + It "Short version" { + $actualOutput = Format-Versions -Versions @("14.2", "v2.0") + $expectedOutput = @("14.2.0", "2.0.0") + $actualOutput | Should -Be $expectedOutput + } + + It "Skip versions with 1 digin" { + $actualOutput = Format-Versions -Versions @("14", "v2") + $expectedOutput = @() + $actualOutput | Should -Be $expectedOutput + } +} + +Describe "Filter-Versions" { + $inputVersions = @("8.2.1", "9.3.3", "10.0.2", "10.0.3", "10.5.6", "12.4.3", "12.5.1", "14.2.0") + + It "Include filter" { + $includeFilters = @("8.*", "14.*") + $actualOutput = Filter-Versions -Versions $inputVersions -IncludeFilters $includeFilters + $expectedOutput = @("8.2.1", "14.2.0") + $actualOutput | Should -Be $expectedOutput + } + + It "Exclude filter" { + $includeFilters = @("10.*", "12.*") + $excludeFilters = @("10.0.*", "12.4.3") + $actualOutput = Filter-Versions -Versions $inputVersions -IncludeFilters $includeFilters -ExcludeFilters $excludeFilters + $expectedOutput = @("10.5.6", "12.5.1") + $actualOutput | Should -Be $expectedOutput + } +} + +Describe "Get-VersionsToBuild" { + It "Substract versions correctly" { + $distInput = @("14.2.0", "14.3.0", "14.4.0", "14.4.1") + $manifestInput = @("12.0.0", "14.2.0", "14.4.0") + $actualOutput = Get-VersionsToBuild -VersionsFromDist $distInput -VersionsFromManifest $manifestInput + $expectedOutput = @("14.3.0", "14.4.1") + $actualOutput | Should -Be $expectedOutput + } +} \ No newline at end of file From e09bde7dec25eb98454ec8275b3fc09d5ac4ded7 Mon Sep 17 00:00:00 2001 From: MaksimZhukov Date: Wed, 1 Jul 2020 18:07:43 +0300 Subject: [PATCH 04/11] Fix comments --- .../get-new-tool-versions.ps1 | 16 +++++++------- get-new-tool-versions/helpers.psm1 | 21 +++++++++---------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/get-new-tool-versions/get-new-tool-versions.ps1 b/get-new-tool-versions/get-new-tool-versions.ps1 index 7eebb55..6df0727 100644 --- a/get-new-tool-versions/get-new-tool-versions.ps1 +++ b/get-new-tool-versions/get-new-tool-versions.ps1 @@ -37,25 +37,23 @@ Write-Host "Get the packages list from $ManifestLink" [Version[]] $formattedVersions = Format-Versions -Versions $versionsFromDist -if (-not ([string]::IsNullOrEmpty($VersionFilterToInclude))) { +if ($VersionFilterToInclude) { $formattedVersions = Filter-Versions -Versions $formattedVersions ` -VersionFilter $VersionFilterToInclude ` -IncludeVersions $true } -if (-not ([string]::IsNullOrEmpty($VersionFilterToExclude))) { +if ($VersionFilterToExclude) { $formattedVersions = Filter-Versions -Versions $formattedVersions ` -VersionFilter $VersionFilterToExclude ` -IncludeVersions $false } -$versionsToBuild = Get-VersionsToBuild -VersionsFromManifest $versionsFromManifest ` - -VersionsFromDist $formattedVersions +$versionsToBuild = Skip-ExistingVersions -VersionsFromManifest $versionsFromManifest ` + -VersionsFromDist $formattedVersions -if ([string]::IsNullOrEmpty($versionsToBuild)) { - Write-Host "There isn't versions to build" - return $null -} else { +if ($versionsToBuild) { Write-Host "The following versions are available to build:`n$versionsToBuild" - return "$versionsToBuild" +} else { + Write-Host "There isn't versions to build" } diff --git a/get-new-tool-versions/helpers.psm1 b/get-new-tool-versions/helpers.psm1 index c70b3fe..8463fd4 100644 --- a/get-new-tool-versions/helpers.psm1 +++ b/get-new-tool-versions/helpers.psm1 @@ -8,16 +8,15 @@ function Format-Versions { foreach($version in $Versions) { $substredVersion = $null - # We cut a string from index of first digit because initially it has invalid format (v14.4.0 or go1.14.4) - if ($version -match '(?\d)') { - $firstDigitIndex = $version.indexof($Matches.number) - $substredVersion = $version.substring($firstDigitIndex) - } else { + # Cut a string from index of first digit because initially it has invalid format (v14.4.0 or go1.14.4) + if (-not ($version -match '(?\d)')) { Write-Host "Invalid version format - $version" - exit 1 + exit 1 } + $firstDigitIndex = $version.indexof($Matches.number) + $substredVersion = $version.substring($firstDigitIndex) - # We filter versions to exclude unstable (for example: "go1.15beta1") + # Filter versions to exclude unstable (for example: "go1.15beta1") # Valid version format: x.x or x.x.x if ($substredVersion -notmatch '^\d+\.+\d+\.*\d*$') { continue @@ -54,14 +53,14 @@ function Filter-Versions { return $filteredVersions } -function Get-VersionsToBuild { +function Skip-ExistingVersions { param ( [Parameter(Mandatory)] [string[]] $VersionsFromManifest, [Parameter(Mandatory)] [string[]] $VersionsFromDist ) - [System.Collections.ArrayList]$versionsToBuid = $VersionsFromDist - $VersionsFromManifest | ForEach-Object { $versionsToBuid.Remove($_) } + $newVersions = @() + $newVersions += $VersionsFromDist | Where-Object { $VersionsFromManifest -notcontains $_ } - return $versionsToBuid + return $newVersions } \ No newline at end of file From 594c0c88990d179923aea7f591cb1dcd6c606a5f Mon Sep 17 00:00:00 2001 From: MaksimZhukov Date: Thu, 2 Jul 2020 02:04:37 +0300 Subject: [PATCH 05/11] Add test cases and fix Filter-Versions function --- .../get-new-tool-versions.Tests.ps1 | 27 +++++++++++---- .../get-new-tool-versions.ps1 | 18 +++------- get-new-tool-versions/helpers.psm1 | 34 ++++++++----------- 3 files changed, 40 insertions(+), 39 deletions(-) diff --git a/get-new-tool-versions/get-new-tool-versions.Tests.ps1 b/get-new-tool-versions/get-new-tool-versions.Tests.ps1 index b9802ce..5638fa4 100644 --- a/get-new-tool-versions/get-new-tool-versions.Tests.ps1 +++ b/get-new-tool-versions/get-new-tool-versions.Tests.ps1 @@ -27,7 +27,7 @@ Describe "Format-Versions" { $actualOutput | Should -Be $expectedOutput } - It "Skip versions with 1 digin" { + It "Skip versions with 1 digit" { $actualOutput = Format-Versions -Versions @("14", "v2") $expectedOutput = @() $actualOutput | Should -Be $expectedOutput @@ -37,27 +37,42 @@ Describe "Format-Versions" { Describe "Filter-Versions" { $inputVersions = @("8.2.1", "9.3.3", "10.0.2", "10.0.3", "10.5.6", "12.4.3", "12.5.1", "14.2.0") - It "Include filter" { + It "Include filter only" { $includeFilters = @("8.*", "14.*") - $actualOutput = Filter-Versions -Versions $inputVersions -IncludeFilters $includeFilters + $excludeFilters = @() + $actualOutput = Filter-Versions -Versions $inputVersions -IncludeFilters $includeFilters -ExcludeFilters $excludeFilters $expectedOutput = @("8.2.1", "14.2.0") $actualOutput | Should -Be $expectedOutput } - It "Exclude filter" { + It "Include and exclude filters" { $includeFilters = @("10.*", "12.*") $excludeFilters = @("10.0.*", "12.4.3") $actualOutput = Filter-Versions -Versions $inputVersions -IncludeFilters $includeFilters -ExcludeFilters $excludeFilters $expectedOutput = @("10.5.6", "12.5.1") $actualOutput | Should -Be $expectedOutput } + + It "Exclude filter only" { + $includeFilters = @() + $excludeFilters = @("10.*", "12.*") + $actualOutput = Filter-Versions -Versions $inputVersions -IncludeFilters $includeFilters -ExcludeFilters $excludeFilters + $expectedOutput = @("8.2.1", "9.3.3", "14.2.0") + $actualOutput | Should -Be $expectedOutput + } + + It "Include and exclude filters are empty" { + $actualOutput = Filter-Versions -Versions $inputVersions + $expectedOutput = @("8.2.1", "9.3.3", "10.0.2", "10.0.3", "10.5.6", "12.4.3", "12.5.1", "14.2.0") + $actualOutput | Should -Be $expectedOutput + } } -Describe "Get-VersionsToBuild" { +Describe "Skip-ExistingVersions" { It "Substract versions correctly" { $distInput = @("14.2.0", "14.3.0", "14.4.0", "14.4.1") $manifestInput = @("12.0.0", "14.2.0", "14.4.0") - $actualOutput = Get-VersionsToBuild -VersionsFromDist $distInput -VersionsFromManifest $manifestInput + $actualOutput = Skip-ExistingVersions -VersionsFromDist $distInput -VersionsFromManifest $manifestInput $expectedOutput = @("14.3.0", "14.4.1") $actualOutput | Should -Be $expectedOutput } diff --git a/get-new-tool-versions/get-new-tool-versions.ps1 b/get-new-tool-versions/get-new-tool-versions.ps1 index 6df0727..e20094f 100644 --- a/get-new-tool-versions/get-new-tool-versions.ps1 +++ b/get-new-tool-versions/get-new-tool-versions.ps1 @@ -6,8 +6,8 @@ Check and return list of new available tool versions param ( [Parameter(Mandatory)] [string] $DistURL, [Parameter(Mandatory)] [string] $ManifestLink, - [string] $VersionFilterToInclude, - [string] $VersionFilterToExclude, + [string[]] $VersionFilterToInclude, + [string[]] $VersionFilterToExclude, [UInt32] $RetryIntervalSec = 60, [UInt32] $RetryCount = 3 ) @@ -37,17 +37,9 @@ Write-Host "Get the packages list from $ManifestLink" [Version[]] $formattedVersions = Format-Versions -Versions $versionsFromDist -if ($VersionFilterToInclude) { - $formattedVersions = Filter-Versions -Versions $formattedVersions ` - -VersionFilter $VersionFilterToInclude ` - -IncludeVersions $true -} - -if ($VersionFilterToExclude) { - $formattedVersions = Filter-Versions -Versions $formattedVersions ` - -VersionFilter $VersionFilterToExclude ` - -IncludeVersions $false -} +$formattedVersions = Filter-Versions -Versions $formattedVersions ` + -IncludeFilters $VersionFilterToInclude ` + -ExcludeFilters $VersionFilterToExclude $versionsToBuild = Skip-ExistingVersions -VersionsFromManifest $versionsFromManifest ` -VersionsFromDist $formattedVersions diff --git a/get-new-tool-versions/helpers.psm1 b/get-new-tool-versions/helpers.psm1 index 8463fd4..2cdc330 100644 --- a/get-new-tool-versions/helpers.psm1 +++ b/get-new-tool-versions/helpers.psm1 @@ -5,9 +5,7 @@ function Format-Versions { [Version[]] $formattedVersions = @() - foreach($version in $Versions) { - $substredVersion = $null - + foreach($version in $Versions) { # Cut a string from index of first digit because initially it has invalid format (v14.4.0 or go1.14.4) if (-not ($version -match '(?\d)')) { Write-Host "Invalid version format - $version" @@ -34,23 +32,22 @@ function Format-Versions { function Filter-Versions { param ( - [Parameter(Mandatory)] [string[]] $Versions, - [Parameter(Mandatory)] [string] $VersionFilter, - [Parameter(Mandatory)] [bool] $IncludeVersions + [Parameter(Mandatory)] [version[]] $Versions, + [string[]] $IncludeFilters, + [string[]] $ExcludeFilters ) - $versionFilters = $VersionFilter.Split(',') - [Version[]] $filteredVersions = @() - - foreach($filter in $versionFilters) { - if ($IncludeVersions) { - $filteredVersions += $Versions | Where-Object { $_ -like $filter } - } else { - $filteredVersions += $Versions | Where-Object { $_ -notlike $filter } - } + if ($IncludeFilters.Length -eq 0) { + $IncludeFilters = @('*') } - return $filteredVersions + return $Versions | Where-Object { + $ver = $_ + $matchedIncludeFilters = $IncludeFilters | Where-Object { $ver -like $_ } + $matchedExcludeFilters = $ExcludeFilters | Where-Object { $ver -like $_ } + + return ($matchedIncludeFilters -ne $null) -and ($matchedExcludeFilters -eq $null) + } } function Skip-ExistingVersions { @@ -59,8 +56,5 @@ function Skip-ExistingVersions { [Parameter(Mandatory)] [string[]] $VersionsFromDist ) - $newVersions = @() - $newVersions += $VersionsFromDist | Where-Object { $VersionsFromManifest -notcontains $_ } - - return $newVersions + return $VersionsFromDist | Where-Object { $VersionsFromManifest -notcontains $_ } } \ No newline at end of file From 9b7f127678ec73d8bf3169d8768ca4e2418aa5cc Mon Sep 17 00:00:00 2001 From: MaksimZhukov Date: Thu, 2 Jul 2020 10:33:01 +0300 Subject: [PATCH 06/11] Set env variable --- get-new-tool-versions/get-new-tool-versions.ps1 | 1 + 1 file changed, 1 insertion(+) diff --git a/get-new-tool-versions/get-new-tool-versions.ps1 b/get-new-tool-versions/get-new-tool-versions.ps1 index e20094f..a622b57 100644 --- a/get-new-tool-versions/get-new-tool-versions.ps1 +++ b/get-new-tool-versions/get-new-tool-versions.ps1 @@ -46,6 +46,7 @@ $versionsToBuild = Skip-ExistingVersions -VersionsFromManifest $versionsFromMani if ($versionsToBuild) { Write-Host "The following versions are available to build:`n$versionsToBuild" + Write-Output "##vso[task.setvariable variable=TOOL_VERSIONS]$versionsToBuild" } else { Write-Host "There isn't versions to build" } From 7cfb6816a8b94fe2d5ccf00fc7a4ee9a9934fe99 Mon Sep 17 00:00:00 2001 From: Maxim Lobanov Date: Thu, 2 Jul 2020 11:34:42 +0300 Subject: [PATCH 07/11] Update get-new-tool-versions.ps1 --- get-new-tool-versions/get-new-tool-versions.ps1 | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/get-new-tool-versions/get-new-tool-versions.ps1 b/get-new-tool-versions/get-new-tool-versions.ps1 index a622b57..25fdd77 100644 --- a/get-new-tool-versions/get-new-tool-versions.ps1 +++ b/get-new-tool-versions/get-new-tool-versions.ps1 @@ -14,6 +14,10 @@ param ( Import-Module (Join-Path $PSScriptRoot "helpers.psm1") +$VersionFilterToInclude.GetType() +$VersionFilterToInclude.Length +$VersionFilterToInclude | ForEach-Object { Write-Host $_ } + function Get-VersionsByUrl { param ( [Parameter(Mandatory)] [string] $ToolPackagesUrl, From e48cddbe4fc1977046e006860531fbd84cf0ac37 Mon Sep 17 00:00:00 2001 From: MaksimZhukov Date: Thu, 2 Jul 2020 14:01:13 +0300 Subject: [PATCH 08/11] Add Validate-FiltersFormat function --- .../get-new-tool-versions.Tests.ps1 | 25 +++++++++++++++---- .../get-new-tool-versions.ps1 | 18 +++++++------ get-new-tool-versions/helpers.psm1 | 19 +++++++++++--- 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/get-new-tool-versions/get-new-tool-versions.Tests.ps1 b/get-new-tool-versions/get-new-tool-versions.Tests.ps1 index 5638fa4..3d5e9c1 100644 --- a/get-new-tool-versions/get-new-tool-versions.Tests.ps1 +++ b/get-new-tool-versions/get-new-tool-versions.Tests.ps1 @@ -2,6 +2,21 @@ Import-Module (Join-Path $PSScriptRoot "helpers.psm1") -Force +Describe "Validate-FiltersFormat" { + It "Filter with word" { + { Validate-FiltersFormat -Filters @("1two.2") } | Should -Throw "Invalid filter format" + } + + It "Filter with non-word character" { + { Validate-FiltersFormat -Filters @("1,.2") } | Should -Throw "Invalid filter format" + } + + It "Valid filters" { + { Validate-FiltersFormat -Filters @("*", "1", "1.*", "1.2", "1.2.*") } | Should -Not -Throw "Invalid filter format" + } + +} + Describe "Format-Versions" { It "Clean versions" { $actualOutput = Format-Versions -Versions @("14.2.0", "1.14.0") @@ -34,13 +49,13 @@ Describe "Format-Versions" { } } -Describe "Filter-Versions" { +Describe "Select-VersionsByFilter" { $inputVersions = @("8.2.1", "9.3.3", "10.0.2", "10.0.3", "10.5.6", "12.4.3", "12.5.1", "14.2.0") It "Include filter only" { $includeFilters = @("8.*", "14.*") $excludeFilters = @() - $actualOutput = Filter-Versions -Versions $inputVersions -IncludeFilters $includeFilters -ExcludeFilters $excludeFilters + $actualOutput = Select-VersionsByFilter -Versions $inputVersions -IncludeFilters $includeFilters -ExcludeFilters $excludeFilters $expectedOutput = @("8.2.1", "14.2.0") $actualOutput | Should -Be $expectedOutput } @@ -48,7 +63,7 @@ Describe "Filter-Versions" { It "Include and exclude filters" { $includeFilters = @("10.*", "12.*") $excludeFilters = @("10.0.*", "12.4.3") - $actualOutput = Filter-Versions -Versions $inputVersions -IncludeFilters $includeFilters -ExcludeFilters $excludeFilters + $actualOutput = Select-VersionsByFilter -Versions $inputVersions -IncludeFilters $includeFilters -ExcludeFilters $excludeFilters $expectedOutput = @("10.5.6", "12.5.1") $actualOutput | Should -Be $expectedOutput } @@ -56,13 +71,13 @@ Describe "Filter-Versions" { It "Exclude filter only" { $includeFilters = @() $excludeFilters = @("10.*", "12.*") - $actualOutput = Filter-Versions -Versions $inputVersions -IncludeFilters $includeFilters -ExcludeFilters $excludeFilters + $actualOutput = Select-VersionsByFilter -Versions $inputVersions -IncludeFilters $includeFilters -ExcludeFilters $excludeFilters $expectedOutput = @("8.2.1", "9.3.3", "14.2.0") $actualOutput | Should -Be $expectedOutput } It "Include and exclude filters are empty" { - $actualOutput = Filter-Versions -Versions $inputVersions + $actualOutput = Select-VersionsByFilter -Versions $inputVersions $expectedOutput = @("8.2.1", "9.3.3", "10.0.2", "10.0.3", "10.5.6", "12.4.3", "12.5.1", "14.2.0") $actualOutput | Should -Be $expectedOutput } diff --git a/get-new-tool-versions/get-new-tool-versions.ps1 b/get-new-tool-versions/get-new-tool-versions.ps1 index 25fdd77..6d3a021 100644 --- a/get-new-tool-versions/get-new-tool-versions.ps1 +++ b/get-new-tool-versions/get-new-tool-versions.ps1 @@ -14,10 +14,6 @@ param ( Import-Module (Join-Path $PSScriptRoot "helpers.psm1") -$VersionFilterToInclude.GetType() -$VersionFilterToInclude.Length -$VersionFilterToInclude | ForEach-Object { Write-Host $_ } - function Get-VersionsByUrl { param ( [Parameter(Mandatory)] [string] $ToolPackagesUrl, @@ -29,6 +25,14 @@ function Get-VersionsByUrl { return $packages.version } +if ($VersionFilterToInclude) { + Validate-FiltersFormat -Filters $VersionFilterToInclude +} + +if ($VersionFilterToExclude) { + Validate-FiltersFormat -Filters $VersionFilterToExclude +} + Write-Host "Get the packages list from $DistURL" $versionsFromDist = Get-VersionsByUrl -ToolPackagesUrl $DistURL ` -RetryIntervalSec $RetryIntervalSec ` @@ -41,9 +45,9 @@ Write-Host "Get the packages list from $ManifestLink" [Version[]] $formattedVersions = Format-Versions -Versions $versionsFromDist -$formattedVersions = Filter-Versions -Versions $formattedVersions ` - -IncludeFilters $VersionFilterToInclude ` - -ExcludeFilters $VersionFilterToExclude +$formattedVersions = Select-VersionsByFilter -Versions $formattedVersions ` + -IncludeFilters $VersionFilterToInclude ` + -ExcludeFilters $VersionFilterToExclude $versionsToBuild = Skip-ExistingVersions -VersionsFromManifest $versionsFromManifest ` -VersionsFromDist $formattedVersions diff --git a/get-new-tool-versions/helpers.psm1 b/get-new-tool-versions/helpers.psm1 index 2cdc330..7b5af82 100644 --- a/get-new-tool-versions/helpers.psm1 +++ b/get-new-tool-versions/helpers.psm1 @@ -1,3 +1,17 @@ +function Validate-FiltersFormat { + param ( + [Parameter(Mandatory)] [string[]] $Filters + ) + + foreach($filter in $Filters) { + $filter.Split('.') | ForEach-Object { + if (($_ -notmatch '^\d+$') -and ($_ -ne '*')) { + throw "Invalid filter format - $filter" + } + } + } +} + function Format-Versions { param ( [Parameter(Mandatory)] [string[]] $Versions @@ -30,7 +44,7 @@ function Format-Versions { return $formattedVersions } -function Filter-Versions { +function Select-VersionsByFilter { param ( [Parameter(Mandatory)] [version[]] $Versions, [string[]] $IncludeFilters, @@ -45,8 +59,7 @@ function Filter-Versions { $ver = $_ $matchedIncludeFilters = $IncludeFilters | Where-Object { $ver -like $_ } $matchedExcludeFilters = $ExcludeFilters | Where-Object { $ver -like $_ } - - return ($matchedIncludeFilters -ne $null) -and ($matchedExcludeFilters -eq $null) + return ($null -ne $matchedIncludeFilters) -and ($null -eq $matchedExcludeFilters) } } From 85b3f1af5dc3369c4cbad728a0efbab6f4503d16 Mon Sep 17 00:00:00 2001 From: MaksimZhukov Date: Thu, 2 Jul 2020 14:09:10 +0300 Subject: [PATCH 09/11] Convert array to string --- get-new-tool-versions/get-new-tool-versions.ps1 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/get-new-tool-versions/get-new-tool-versions.ps1 b/get-new-tool-versions/get-new-tool-versions.ps1 index 6d3a021..60952e6 100644 --- a/get-new-tool-versions/get-new-tool-versions.ps1 +++ b/get-new-tool-versions/get-new-tool-versions.ps1 @@ -53,8 +53,9 @@ $versionsToBuild = Skip-ExistingVersions -VersionsFromManifest $versionsFromMani -VersionsFromDist $formattedVersions if ($versionsToBuild) { - Write-Host "The following versions are available to build:`n$versionsToBuild" - Write-Output "##vso[task.setvariable variable=TOOL_VERSIONS]$versionsToBuild" + $availableVersions = $versionsToBuild -join "," + Write-Host "The following versions are available to build:`n$availableVersions" + Write-Output "##vso[task.setvariable variable=TOOL_VERSIONS]$availableVersions" } else { Write-Host "There isn't versions to build" } From 51d1b9ae5c4e10287ab66c52d90389036c8267e4 Mon Sep 17 00:00:00 2001 From: MaksimZhukov Date: Thu, 2 Jul 2020 15:21:49 +0300 Subject: [PATCH 10/11] Fix comments --- get-new-tool-versions/get-new-tool-versions.Tests.ps1 | 1 - get-new-tool-versions/get-new-tool-versions.ps1 | 5 +++++ get-new-tool-versions/helpers.psm1 | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/get-new-tool-versions/get-new-tool-versions.Tests.ps1 b/get-new-tool-versions/get-new-tool-versions.Tests.ps1 index 3d5e9c1..f16728b 100644 --- a/get-new-tool-versions/get-new-tool-versions.Tests.ps1 +++ b/get-new-tool-versions/get-new-tool-versions.Tests.ps1 @@ -14,7 +14,6 @@ Describe "Validate-FiltersFormat" { It "Valid filters" { { Validate-FiltersFormat -Filters @("*", "1", "1.*", "1.2", "1.2.*") } | Should -Not -Throw "Invalid filter format" } - } Describe "Format-Versions" { diff --git a/get-new-tool-versions/get-new-tool-versions.ps1 b/get-new-tool-versions/get-new-tool-versions.ps1 index 60952e6..f95984a 100644 --- a/get-new-tool-versions/get-new-tool-versions.ps1 +++ b/get-new-tool-versions/get-new-tool-versions.ps1 @@ -49,6 +49,11 @@ $formattedVersions = Select-VersionsByFilter -Versions $formattedVersions ` -IncludeFilters $VersionFilterToInclude ` -ExcludeFilters $VersionFilterToExclude +if (-not $formattedVersions) { + Write-Host "Couldn't find available versions with current filters" + exit 1 +} + $versionsToBuild = Skip-ExistingVersions -VersionsFromManifest $versionsFromManifest ` -VersionsFromDist $formattedVersions diff --git a/get-new-tool-versions/helpers.psm1 b/get-new-tool-versions/helpers.psm1 index 7b5af82..2f9d582 100644 --- a/get-new-tool-versions/helpers.psm1 +++ b/get-new-tool-versions/helpers.psm1 @@ -59,7 +59,7 @@ function Select-VersionsByFilter { $ver = $_ $matchedIncludeFilters = $IncludeFilters | Where-Object { $ver -like $_ } $matchedExcludeFilters = $ExcludeFilters | Where-Object { $ver -like $_ } - return ($null -ne $matchedIncludeFilters) -and ($null -eq $matchedExcludeFilters) + $matchedIncludeFilters -and (-not $matchedExcludeFilters) } } From 7618cbc882323b7f641506e423f51297f1efbf07 Mon Sep 17 00:00:00 2001 From: MaksimZhukov Date: Fri, 3 Jul 2020 13:25:55 +0300 Subject: [PATCH 11/11] Add descriptions for parameters --- get-new-tool-versions/get-new-tool-versions.ps1 | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/get-new-tool-versions/get-new-tool-versions.ps1 b/get-new-tool-versions/get-new-tool-versions.ps1 index f95984a..c6f42fe 100644 --- a/get-new-tool-versions/get-new-tool-versions.ps1 +++ b/get-new-tool-versions/get-new-tool-versions.ps1 @@ -1,6 +1,19 @@ <# .SYNOPSIS Check and return list of new available tool versions + +.PARAMETER DistURL +Required parameter. Link to the json file included all available tool versions +.PARAMETER ManifestLink +Required parameter. Link to the the version-manifest.json file +.PARAMETER VersionFilterToInclude +Optional parameter. List of filters to include particular versions +.PARAMETER VersionFilterToExclude +Optional parameter. List of filters to exclude particular versions +.PARAMETER RetryIntervalSec +Optional parameter. Retry interval in seconds +.PARAMETER RetryCount +Optional parameter. Retry count #> param ( @@ -62,5 +75,5 @@ if ($versionsToBuild) { Write-Host "The following versions are available to build:`n$availableVersions" Write-Output "##vso[task.setvariable variable=TOOL_VERSIONS]$availableVersions" } else { - Write-Host "There isn't versions to build" + Write-Host "There aren't versions to build" }