pre_post_util.ps1 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. $header = @{ Authorization=("Bearer {0}" -f $oauth) }
  31. $info = Invoke-RestMethod -Headers $header -Uri $uri -Method GET
  32. return $info
  33. }
  34. function GetBuildPushId($info) {
  35. $buildPushId = $info.push.pushId
  36. $buildPushIdPart1 = [int]([math]::Floor($buildPushId / 65536))
  37. $buildPushIdPart2 = [int]($buildPushId % 65536)
  38. $buildPushIdString = "{0}.{1}" -f $buildPushIdPart1.ToString("00000"), $buildPushIdPart2.ToString("00000")
  39. return @($buildPushId, $buildPushIdPart1, $buildPushIdPart2, $buildPushIdString)
  40. }
  41. function EnsureVariables($functionName, $arch, $flavor, $OuterScriptRoot) {
  42. if (("$arch" -eq "") -or ("$flavor" -eq "") -or ("$OuterScriptRoot" -eq ""))
  43. {
  44. WriteErrorMessage @"
  45. ${functionName}: Required variables not set:
  46. `$arch = $arch
  47. `$flavor = $flavor
  48. `$OuterScriptRoot = $OuterScriptRoot
  49. "@
  50. throw "Cannot continue: ${functionName}: required variables not set."
  51. }
  52. }
  53. function ConstructBuildName($arch, $flavor, $subtype) {
  54. EnsureVariables "ConstructBuildName" $arch $flavor "(OuterScriptRoot not needed)"
  55. if ($subtype -eq "codecoverage") {
  56. # TODO eliminate tools' dependency on this particular formatting exception
  57. # Normalize the $BuildName of even if the $BuildType is e.g. x64_test_codecoverage
  58. return "${arch}_codecoverage"
  59. } elseif ($subtype -eq "pogo") {
  60. return "${arch}_${flavor}_${subtype}"
  61. } else {
  62. return "${arch}_${flavor}"
  63. }
  64. }
  65. function ComputePaths($arch, $flavor, $subtype, $OuterScriptRoot, $srcpath = "", $buildRoot = "", $objpath = "", $binpath = "") {
  66. EnsureVariables "ComputePaths" $arch $flavor $OuterScriptRoot
  67. $buildName = ConstructBuildName $arch $flavor $subtype
  68. $srcpath = UseValueOrDefault $srcpath "$Env:TF_BUILD_SOURCESDIRECTORY" (Resolve-Path "${OuterScriptRoot}\..\..")
  69. $buildRoot = UseValueOrDefault $buildRoot "$Env:BinariesDirectory" "$Env:TF_BUILD_BINARIESDIRECTORY" (Join-Path $srcpath "Build\VcBuild")
  70. $objpath = UseValueOrDefault $objpath "$Env:TF_BUILD_BUILDDIRECTORY" (Join-Path $buildRoot "obj\${buildName}")
  71. $binpath = Join-Path $buildRoot "bin\${buildName}"
  72. return @($srcpath, $buildRoot, $objpath, $binpath)
  73. }