util.ps1 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. function UseValueOrDefault() {
  6. foreach ($value in $args) {
  7. if ($value) {
  8. return $value
  9. }
  10. }
  11. return ""
  12. }
  13. function GetGitPath() {
  14. $gitExe = "git.exe"
  15. if (!(Get-Command $gitExe -ErrorAction SilentlyContinue)) {
  16. $gitExe = "C:\1image\Git\bin\git.exe"
  17. if (!(Test-Path $gitExe)) {
  18. throw "git.exe not found in path -- aborting."
  19. }
  20. }
  21. return $gitExe
  22. }
  23. function GetRepoRoot() {
  24. $gitExe = GetGitPath
  25. return Invoke-Expression "$gitExe rev-parse --show-toplevel"
  26. }
  27. function WriteMessage($str) {
  28. Write-Output $str
  29. if ($logFile) {
  30. Write-Output $str | Out-File $logFile -Append
  31. }
  32. }
  33. function WriteErrorMessage($str) {
  34. $host.ui.WriteErrorLine($str)
  35. if ($logFile) {
  36. Write-Output $str | Out-File $logFile -Append
  37. }
  38. }
  39. function ExecuteCommand($cmd) {
  40. if ($cmd -eq "") {
  41. return
  42. }
  43. WriteMessage "-------------------------------------"
  44. WriteMessage "Running $cmd"
  45. if ($noaction) {
  46. return
  47. }
  48. Invoke-Expression $cmd
  49. if ($lastexitcode -ne 0) {
  50. WriteErrorMessage "ERROR: Command failed: exit code $LastExitCode"
  51. $global:exitcode = $LastExitCode
  52. }
  53. WriteMessage ""
  54. }