scripts/Get-TeamCityProperty.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
function Get-TeamCityProperty {
    [CmdletBinding()]
    param (
        [Parameter(Position = 0, Mandatory = $true, ValueFromPipeline = $true)]
        [string]$propName,

        [Parameter(Position = 1, Mandatory = $false)]
        [string]$propFile = $env:TEAMCITY_BUILD_PROPERTIES_FILE
    )
    process {
        if (-not ${teamcity-build-properties}.Keys.Count) {
            if (-not $propFile) {
                throw "`$env:TEAMCITY_BUILD_PROPERTIES_FILE is empty."
            }

            if (-not (Test-Path $propFile)) {
                throw "$propFile does not exist."
            }

            $params = Get-Content $propFile -Raw | ConvertFrom-StringData

            if ($params['teamcity.configuration.properties.file']) {
                ${teamcity-build-properties} = Get-Content $params['teamcity.configuration.properties.file'] -Raw | ConvertFrom-StringData
            } else {
                ${teamcity-build-properties} = $params
            }
        }

        if (-not ${teamcity-build-properties}[$propName]) {
            throw "Key '$propName' does not exist."
        }

        return ${teamcity-build-properties}[$propName]
    }
}