Created
February 9, 2013 15:37
-
-
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.
This file contains 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
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