common.psm1 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. Write-Warning $error[0]
  15. throw "Command failed to execute: $cmd"
  16. }
  17. }
  18. function Get-Submodules {
  19. param(
  20. [Parameter(Mandatory = $true)]
  21. [string]$RepoRoot
  22. )
  23. Invoke-Block { & git submodule update --init } | Out-Null
  24. $moduleConfigFile = Join-Path $RepoRoot ".gitmodules"
  25. $submodules = @()
  26. Get-ChildItem "$RepoRoot/modules/*" -Directory | % {
  27. Push-Location $_ | Out-Null
  28. Write-Verbose "Attempting to get submodule info for $_"
  29. try {
  30. $data = @{
  31. path = $_
  32. module = $_.Name
  33. commit = $(git rev-parse HEAD)
  34. newCommit = $null
  35. changed = $false
  36. branch = $(git config -f $moduleConfigFile --get submodule.modules/$($_.Name).branch )
  37. }
  38. $submodules += $data
  39. }
  40. finally {
  41. Pop-Location | Out-Null
  42. }
  43. }
  44. return $submodules
  45. }