common.psm1 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. function Assert-Git {
  2. if (!(Get-Command git -ErrorAction Ignore)) {
  3. Write-Error 'git is required to execute this script'
  4. exit 1
  5. }
  6. }
  7. function Invoke-Block([scriptblock]$cmd) {
  8. $cmd | Out-String | Write-Verbose
  9. & $cmd
  10. # Need to check both of these cases for errors as they represent different items
  11. # - $?: did the powershell script block throw an error
  12. # - $lastexitcode: did a windows command executed by the script block end in error
  13. if ((-not $?) -or ($lastexitcode -ne 0)) {
  14. throw "Command failed to execute: $cmd"
  15. }
  16. }
  17. function Get-Submodules {
  18. param(
  19. [Parameter(Mandatory = $true)]
  20. [string]$RepoRoot
  21. )
  22. Invoke-Block { & git submodule update --init } | Out-Null
  23. $moduleConfigFile = Join-Path $RepoRoot ".gitmodules"
  24. $submodules = @()
  25. Get-ChildItem "$RepoRoot/modules/*" -Directory | % {
  26. Push-Location $_ | Out-Null
  27. Write-Verbose "Attempting to get submodule info for $_"
  28. try {
  29. $data = @{
  30. path = $_
  31. module = $_.Name
  32. commit = $(git rev-parse HEAD)
  33. newCommit = $null
  34. changed = $false
  35. branch = $(git config -f $moduleConfigFile --get submodule.modules/$($_.Name).branch )
  36. }
  37. $submodules += $data
  38. }
  39. finally {
  40. Pop-Location | Out-Null
  41. }
  42. }
  43. return $submodules
  44. }