简体   繁体   中英

How to read package.json version in TFS build

i'm using TFS 2017 for CI, I wan't to get version stored in package.json file of my .net core project, i tried this powershell script:

$SemverVersion = (Get-Content -Raw -Path Path/package.json | ConvertFrom-Json).version 

I get this error in logs:

Get-Content : Impossible de trouver un paramètre positionnel acceptant l'argument «
Le processus s'est achevé avec le code de sortie 0. 1 erreur(s) écrite(s) dans le flux d'erreurs.

Is there any other way or tool to read package.json version in TFS Build?

@BenH is correct. Please check this website :

Raw is a dynamic parameter that the FileSystem provider adds to the Get-Content cmdlet. This parameter works only in file system drives.

This parameter is introduced in Windows PowerShell 3.0.

Please install a newer version of Powershell on the build agent machine , and restart build agent service, to have another try.

@BenH, this powerShell works well:

(Get-Childitem -Path .\ -Include *package.json* -Recurse -ErrorAction SilentlyContinue | Where {$_.FullName -notlike "*\wwwroot\*"} | Where {$_.FullName -notlike "*\no
de_modules\*"}| Where {$_.FullName -like "*\release\*"} | Select-Object -First 1).fullName
>>> $version = (Get-Content $path) -join "`n" | ConvertFrom-Json | Select -ExpandProperty "version"
>>> Write-Host ("##vso[task.setvariable SemverVersion=$version;]$version")

First i read the path and store it in $path varible, then use this variable as parameter in Get-Content to read version and store it $version , finally i set $version in SemverVersion env variable, is there any suggestion to improve this reponse ?

Something like this as non one liner:

$jsonFile = Get-Content "package.json"
$jsonObj = $jsonFile | ConvertFrom-Json
$versionPrefix = $jsonObj.version

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM