pre_build.ps1 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #-------------------------------------------------------------------------------------------------------
  2. # Copyright (C) Microsoft. All rights reserved.
  3. # Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. #-------------------------------------------------------------------------------------------------------
  5. #
  6. # Pre-Build script
  7. #
  8. # This script is fairly simple. It checks if it's running
  9. # in a VSO. If it is, it uses the VSO environment variables
  10. # to figure out the commit that triggered the build, and if
  11. # such a commit exists, it saves it's description to the build
  12. # output to make it easy to inspect builds.
  13. #
  14. # Require Environment:
  15. # $Env:TF_BUILD_SOURCEGETVERSION
  16. # $Env:TF_BUILD_DROPLOCATION
  17. #
  18. # Inferable Environment (if not specified, inferred by pre_post_util.ps1):
  19. # $Env:TF_BUILD_SOURCESDIRECTORY (a.k.a. $srcpath)
  20. # $Env:TF_BUILD_BUILDDIRECTORY (a.k.a. $objpath)
  21. # $Env:TF_BUILD_BINARIESDIRECTORY (a.k.a. $binpath)
  22. #
  23. # Optional information:
  24. # $Env:TF_BUILD_BUILDDEFINITIONNAME
  25. # $Env:TF_BUILD_BUILDNUMBER
  26. # $Env:TF_BUILD_BUILDURI
  27. param (
  28. [ValidateSet("x86", "x64", "arm", "")]
  29. [string]$arch = "",
  30. [ValidateSet("debug", "release", "test", "codecoverage", "")]
  31. [string]$flavor = "",
  32. [string]$srcpath = "",
  33. [string]$binpath = "",
  34. [string]$objpath = "",
  35. [string]$logFile = "",
  36. [string]$oauth
  37. )
  38. $OuterScriptRoot = $PSScriptRoot;
  39. . "$PSScriptRoot\pre_post_util.ps1"
  40. if (($logFile -eq "") -and (Test-Path Env:\TF_BUILD_BINARIESDIRECTORY)) {
  41. if (-not(Test-Path -Path "$Env:TF_BUILD_BINARIESDIRECTORY\logs")) {
  42. $dummy = New-Item -Path "$Env:TF_BUILD_BINARIESDIRECTORY\logs" -ItemType Directory -Force
  43. }
  44. $logFile = "$Env:TF_BUILD_BINARIESDIRECTORY\logs\pre_build.log"
  45. if (Test-Path -Path $logFile) {
  46. Remove-Item $logFile -Force
  47. }
  48. }
  49. WriteCommonArguments;
  50. if (Test-Path Env:\TF_BUILD_SOURCEGETVERSION)
  51. {
  52. $commitHash = ($Env:TF_BUILD_SOURCEGETVERSION).split(':')[2]
  53. $gitExe = GetGitPath;
  54. $outputDir = $Env:TF_BUILD_DROPLOCATION
  55. if (-not(Test-Path -Path $outputDir)) {
  56. $dummy = New-Item -Path $outputDir -ItemType Directory -Force
  57. }
  58. Push-Location $srcpath;
  59. $outputFile = Join-Path -Path $outputDir -ChildPath "change.txt"
  60. Write-Output "TF_BUILD_BUILDDEFINITIONNAME = $Env:TF_BUILD_BUILDDEFINITIONNAME" | Out-File $outputFile
  61. Write-Output "TF_BUILD_BUILDNUMBER = $Env:TF_BUILD_BUILDNUMBER" | Out-File $outputFile -Append
  62. Write-Output "TF_BUILD_SOURCEGETVERSION = $Env:TF_BUILD_SOURCEGETVERSION" | Out-File $outputFile -Append
  63. Write-Output "TF_BUILD_BUILDURI = $Env:TF_BUILD_BUILDURI" | Out-File $outputFile -Append
  64. Write-Output "" | Out-File $outputFile -Append
  65. # Get the git remote path and construct the rest API URI
  66. $remote = (iex "$gitExe remote -v")[0].split()[1].replace("_git", "_apis/git/repositories");
  67. $remote = $remote.replace("mshttps", "https");
  68. # Get the pushId and push date time to use that for build number and build date time
  69. $uri = ("{0}/commits/{1}?api-version=1.0" -f $remote, $commitHash)
  70. $oauthToken = Get-Content $oauth;
  71. $header = @{Authorization=("Basic {0}" -f $oauthToken) }
  72. $info = Invoke-RestMethod -Headers $header -Uri $uri -Method GET
  73. $buildDate = ([datetime]$info.push.date).toString("yyMMdd-HHmm")
  74. $buildPushId = $info.push.pushId
  75. $buildPushIdPart1 = [int]([math]::Floor($buildPushId / 65536))
  76. $buildPushIdPart2 = [int]($buildPushId % 65536)
  77. $buildPushIdString = "{0}.{1}" -f $buildPushIdPart1.ToString("00000"), $buildPushIdPart2.ToString("00000")
  78. Write-Output "PushId = $buildPushId $buildPushIdString" | Out-File $outputFile -Append
  79. Write-Output "PushDate = $buildDate" | Out-File $outputFile -Append
  80. Write-Output "" | Out-File $outputFile -Append
  81. # commit message
  82. $command = "$gitExe log -1 --name-status -p $commitHash"
  83. iex $command | Out-File $outputFile -Append
  84. Pop-Location
  85. # commit hash
  86. $buildCommit = ($Env:TF_BUILD_SOURCEGETVERSION).SubString(14);
  87. $commitHash = $buildCommit.Split(":")[1]
  88. $outputJsonFile = Join-Path -Path $outputDir -ChildPath "change.json"
  89. $changeJson = New-Object System.Object
  90. $changeJson | Add-Member -type NoteProperty -name BuildDefinitionName -value $Env:TF_BUILD_BUILDDEFINITIONNAME
  91. $changeJson | Add-Member -type NoteProperty -name BuildNumber -value $Env:TF_BUILD_BUILDNUMBER
  92. $changeJson | Add-Member -type NoteProperty -name BuildDate -value $buildDate
  93. $changeJson | Add-Member -type NoteProperty -name BuildUri -value $Env:TF_BUILD_BUILDURI
  94. $changeJson | Add-Member -type NoteProperty -name Branch -value $Env:BranchName
  95. $changeJson | Add-Member -type NoteProperty -name CommitHash -value $commitHash
  96. $changeJson | Add-Member -type NoteProperty -name PushId -value $buildPushId
  97. $changeJson | Add-Member -type NoteProperty -name PushIdPart1 -value $buildPushIdPart1
  98. $changeJson | Add-Member -type NoteProperty -name PushIdPart2 -value $buildPushIdPart2
  99. $changeJson | Add-Member -type NoteProperty -name PushIdString -value $buildPushIdString
  100. $changeJson | Add-Member -type NoteProperty -name SourceGetVersion -value $Env:TF_BUILD_SOURCEGETVERSION
  101. $changeJson | ConvertTo-Json | Write-Output
  102. $changeJson | ConvertTo-Json | Out-File $outputJsonFile -Encoding ascii
  103. $buildInfoOutputDir = $objpath
  104. if (-not(Test-Path -Path $buildInfoOutputDir)) {
  105. $dummy = New-Item -Path $buildInfoOutputDir -ItemType Directory -Force
  106. }
  107. # generate build version prop file
  108. $buildInfoOutputFile = Join-Path -Path $buildInfoOutputDir -ChildPath "Chakra.Generated.BuildInfo.props"
  109. $propsFile = @"
  110. <?xml version="1.0" encoding="utf-16"?>
  111. <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  112. <PropertyGroup>
  113. <OutBaseDir>{0}</OutBaseDir>
  114. <IntBaseDir>{1}</IntBaseDir>
  115. <ChakraVersionBuildNumber>{2}</ChakraVersionBuildNumber>
  116. <ChakraVersionBuildQFENumber>{3}</ChakraVersionBuildQFENumber>
  117. <ChakraVersionBuildCommit>{4}</ChakraVersionBuildCommit>
  118. <ChakraVersionBuildDate>{5}</ChakraVersionBuildDate>
  119. </PropertyGroup>
  120. </Project>
  121. "@
  122. Write-Output ($propsFile -f $binpath, $objpath, $buildPushIdPart1, $buildPushIdPart2, $buildCommit, $buildDate) | Out-File $buildInfoOutputFile
  123. }
  124. # Clean up code analysis summary files in case they get left behind
  125. if (Test-Path $objpath) {
  126. Get-ChildItem $objpath -include vc.nativecodeanalysis.all.xml -recurse | Remove-Item
  127. }