pre_post_util.ps1 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. . $PSScriptRoot\util.ps1
  6. function WriteCommonArguments() {
  7. WriteMessage " Source Path: $srcpath"
  8. WriteMessage " Build Root: $buildRoot"
  9. WriteMessage " Object Path: $objpath"
  10. WriteMessage "Binaries Path: $binpath"
  11. }
  12. function GetVersionField($fieldname) {
  13. $gitExe = GetGitPath
  14. $query = "#define ${fieldname} (\d+)"
  15. $line = (Invoke-Expression "${gitExe} grep -P ""${query}"" :/")
  16. $matches = $line | Select-String $query
  17. if ($matches) {
  18. return $matches[0].Matches.Groups[1].Value
  19. }
  20. return ""
  21. }
  22. function GetBuildInfo($oauth, $commitHash) {
  23. # Get the git remote path and construct the REST API URI
  24. $gitExe = GetGitPath
  25. $remote = (Invoke-Expression "$gitExe remote -v" `
  26. | Where-Object { $_.contains("_git") })[0].split()[1].replace("_git", "_apis/git/repositories")
  27. $remote = $remote.replace("mshttps", "https")
  28. # Get the pushId and push date time to use that for build number and build date time
  29. $uri = ("{0}/commits/{1}?api-version=1.0" -f $remote, $commitHash)
  30. $oauthToken = Get-Content $oauth
  31. $header = @{ Authorization=("Basic {0}" -f $oauthToken) }
  32. $info = Invoke-RestMethod -Headers $header -Uri $uri -Method GET
  33. return $info
  34. }
  35. function GetBuildPushId($info) {
  36. $buildPushId = $info.push.pushId
  37. $buildPushIdPart1 = [int]([math]::Floor($buildPushId / 65536))
  38. $buildPushIdPart2 = [int]($buildPushId % 65536)
  39. $buildPushIdString = "{0}.{1}" -f $buildPushIdPart1.ToString("00000"), $buildPushIdPart2.ToString("00000")
  40. return @($buildPushId, $buildPushIdPart1, $buildPushIdPart2, $buildPushIdString)
  41. }
  42. function EnsureVariables($functionName, $arch, $flavor, $OuterScriptRoot) {
  43. if (("$arch" -eq "") -or ("$flavor" -eq "") -or ("$OuterScriptRoot" -eq ""))
  44. {
  45. WriteErrorMessage @"
  46. ${functionName}: Required variables not set:
  47. `$arch = $arch
  48. `$flavor = $flavor
  49. `$OuterScriptRoot = $OuterScriptRoot
  50. "@
  51. throw "Cannot continue: ${functionName}: required variables not set."
  52. }
  53. }
  54. function ConstructBuildName($arch, $flavor, $subtype) {
  55. EnsureVariables "ConstructBuildName" $arch $flavor "(OuterScriptRoot not needed)"
  56. if ($subtype -eq "codecoverage") {
  57. # TODO eliminate tools' dependency on this particular formatting exception
  58. # Normalize the $BuildName of even if the $BuildType is e.g. x64_test_codecoverage
  59. return "${arch}_codecoverage"
  60. } elseif ($subtype -eq "pogo") {
  61. return "${arch}_${flavor}_${subtype}"
  62. } else {
  63. return "${arch}_${flavor}"
  64. }
  65. }
  66. function ComputePaths($arch, $flavor, $subtype, $OuterScriptRoot, $srcpath = "", $buildRoot = "", $objpath = "", $binpath = "") {
  67. EnsureVariables "ComputePaths" $arch $flavor $OuterScriptRoot
  68. $buildName = ConstructBuildName $arch $flavor $subtype
  69. $srcpath = UseValueOrDefault $srcpath "$Env:TF_BUILD_SOURCESDIRECTORY" (Resolve-Path "${OuterScriptRoot}\..\..")
  70. $buildRoot = UseValueOrDefault $buildRoot "$Env:BinariesDirectory" "$Env:TF_BUILD_BINARIESDIRECTORY" (Join-Path $srcpath "Build\VcBuild")
  71. $objpath = UseValueOrDefault $objpath "$Env:TF_BUILD_BUILDDIRECTORY" (Join-Path $buildRoot "obj\${buildName}")
  72. $binpath = Join-Path $buildRoot "bin\${buildName}"
  73. return @($srcpath, $buildRoot, $objpath, $binpath)
  74. }