Skip to content

Instantly share code, notes, and snippets.

@growse
Created February 9, 2013 15:37
Show Gist options
  • Save growse/4745746 to your computer and use it in GitHub Desktop.
Save growse/4745746 to your computer and use it in GitHub Desktop.
Teamcity powershell task to output current build number, date, and svn rev in JSON to 'build.json' in root.
Add-Type -Assembly System.ServiceModel.Web,System.Runtime.Serialization
function Read-Stream {
PARAM(
[Parameter(Position=0,ValueFromPipeline=$true)]$Stream
)
process {
$bytes = $Stream.ToArray()
[System.Text.Encoding]::UTF8.GetString($bytes,0,$bytes.Length)
}}
function New-Json {
[CmdletBinding()]
param([Parameter(ValueFromPipeline=$true)][HashTable]$InputObject)
begin {
$ser = @{}
$jsona = @()
}
process {
$jsoni =
foreach($input in $InputObject.GetEnumerator() | Where { $_.Value } ) {
if($input.Value -is [Hashtable]) {
'"'+$input.Key+'": ' + (New-JSon $input.Value)
} else {
$type = $input.Value.GetType()
if(!$Ser.ContainsKey($Type)) {
$Ser.($Type) = New-Object System.Runtime.Serialization.Json.DataContractJsonSerializer $type
}
$stream = New-Object System.IO.MemoryStream
$Ser.($Type).WriteObject( $stream, $Input.Value )
'"'+$input.Key+'": ' + (Read-Stream $stream)
}
}
$jsona += "{`n" +($jsoni -join ",`n")+ "`n}"
}
end {
if($jsona.Count -gt 1) {
"[$($jsona -join ",`n")]"
} else {
$jsona
}
}}
$date=(Get-Date).ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss");
@{BuildNumber="%build.number%"; SvnRevision="%env.BUILD_VCS_NUMBER%"; BuildTime=$date} | New-Json > build.json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment