Last active
November 7, 2018 14:22
-
-
Save mwallner/94ad05bd44f0ec1ef4ca37e20ed991a9 to your computer and use it in GitHub Desktop.
fetch the version of a choco package and "increment" it by adding a prerelease tag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[Parameter(Mandatory = $True)] | |
[string] $id, | |
[Parameter(Mandatory = $False)] | |
[string ]$sources | |
) | |
$scriptName = $MyInvocation.MyCommand.Name | |
Write-Verbose "$scriptName / id: $id / sources: $sources" | |
if (-Not (Get-Command choco -ErrorAction SilentlyContinue)) { | |
throw "you need choco to run this script!" | |
} | |
$pkgInfoString = if ($sources) { | |
choco search --exact $id -r --pre --source="$sources" | |
} | |
else { | |
choco search --exact $id -r --pre | |
} | |
$ecode = $LASTEXITCODE | |
Write-Verbose "choco.exe returned '$pkgInfoString' (err: $ecode)" | |
if ($ecode -ne 0) { | |
throw "choco.exe exited with '$ecode'" | |
} | |
if (-Not $pkgInfoString) { | |
Write-Verbose "pkg '$id' not found" | |
return "0.0.1-pre1" | |
} | |
$ver = $pkgInfoString.Split("|")[1] | |
Write-Verbose "latest version based on sources is '$ver'" | |
[regex]$prereleaseVersionRegex = "\d+\.\d+(\.\d+(\.\d+)?)?\-" | |
$verMatches = $prereleaseVersionRegex.Match($ver) | |
if ($verMatches.Success) { | |
[int]$verNum = 0 | |
$baseVer = $verMatches.Value | |
$baseExt = $ver -replace $baseVer | |
if ($baseExt -match ".+\d+$") { | |
$verNum = $baseExt -replace ".+[^0-9]" | |
$baseExt = $baseExt -replace "$verNum$" | |
} | |
Write-Verbose "version base is '$baseVer'" | |
Write-Verbose "prerelease extension is '$baseExt'" | |
Write-Verbose "old prerelease version is '$verNum'" | |
$verNum++ | |
return "$baseVer$baseExt$verNum" | |
} | |
else { | |
Write-Verbose "no prerelease tag found - creating one!" | |
return "$ver-pre1" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment