Thursday, April 4, 2019

In Azure DevOps, set the version number at package.json to be your build's version number.

Under "Builds" under "Pipelines" in Azure DevOps, click the "Edit" button at the upper right to see the steps in your build. Herein you may have a PowerShell script to set the version number. It could start like so:

$packageJsonPath = 'src/Trifecta/package.json'
function Format-Json([Parameter(Mandatory, ValueFromPipeline)][String] $json) {
   $indent = 0;
   ($json -Split '\n' |
      % {
         if ($_ -match '[\}\]]') {
            $indent--
         }
         $line = (' ' * $indent * 4) + $_.TrimStart().Replace(': ', ': ')
         if ($_ -match '[\{\[]') {
            $indent++
         }
         $line
   }) -Join "`n"
}
$package = ((Get-Content $packageJsonPath) -join "`n" | ConvertFrom-Json)

 
 

...and end like so:

$path = Resolve-Path $packageJsonPath
$content = ConvertTo-Json $package | Format-Json
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
[System.IO.File]::WriteAllLines($path, $content, $Utf8NoBomEncoding)

 
 

...and have this in-between to set a property on package.json:

$package.version = $env:version

 
 

Note that the first blob of code above shows off how to make and call a function in PowerShell. Another thing I learned in this process is that to have a comment in PowerShell you lead a line with a pound sign/hash tag/number symbol. The code here is one of the few examples where I am shamelessly stealing from others. I am loathe to rename variable names and like in the name of a dress-up as I fear breaking something.

No comments:

Post a Comment