util.ps1 1.6 KB

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