common.psm1 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. [switch]$Shipping
  23. )
  24. $moduleConfigFile = Join-Path $RepoRoot ".gitmodules"
  25. $submodules = @()
  26. [xml] $submoduleConfig = Get-Content "$RepoRoot/build/submodules.props"
  27. $repos = $submoduleConfig.Project.ItemGroup.Repository | % { $_.Include }
  28. Get-ChildItem "$RepoRoot/modules/*" -Directory `
  29. | ? { (-not $Shipping) -or $($repos -contains $($_.Name)) -or $_.Name -eq 'Templating' } `
  30. | % {
  31. Push-Location $_ | Out-Null
  32. Write-Verbose "Attempting to get submodule info for $_"
  33. if (Test-Path 'version.props') {
  34. [xml] $versionXml = Get-Content 'version.props'
  35. $versionPrefix = $versionXml.Project.PropertyGroup.VersionPrefix | select-object -first 1
  36. $versionSuffix = $versionXml.Project.PropertyGroup.VersionSuffix | select-object -first 1
  37. }
  38. else {
  39. $versionPrefix = ''
  40. $versionSuffix = ''
  41. }
  42. try {
  43. $data = [PSCustomObject] @{
  44. path = $_
  45. module = $_.Name
  46. commit = $(git rev-parse HEAD)
  47. newCommit = $null
  48. changed = $false
  49. branch = $(git config -f $moduleConfigFile --get submodule.modules/$($_.Name).branch )
  50. versionPrefix = $versionPrefix
  51. versionSuffix = $versionSuffix
  52. }
  53. $submodules += $data
  54. }
  55. finally {
  56. Pop-Location | Out-Null
  57. }
  58. }
  59. return $submodules
  60. }
  61. function SaveXml([xml]$xml, [string]$path) {
  62. Write-Verbose "Saving to $path"
  63. $ErrorActionPreference = 'stop'
  64. $settings = New-Object System.XML.XmlWriterSettings
  65. $settings.OmitXmlDeclaration = $true
  66. $settings.Encoding = New-Object System.Text.UTF8Encoding( $true )
  67. $writer = [System.XML.XMLTextWriter]::Create($path, $settings)
  68. $xml.Save($writer)
  69. $writer.Close()
  70. }
  71. function LoadXml([string]$path) {
  72. Write-Verbose "Reading from $path"
  73. $ErrorActionPreference = 'stop'
  74. $obj = new-object xml
  75. $obj.PreserveWhitespace = $true
  76. $obj.Load($path)
  77. return $obj
  78. }
  79. function PackageIdVarName([string]$packageId) {
  80. $canonicalVarName = ''
  81. $upperCaseNext = $true
  82. for ($i = 0; $i -lt $packageId.Length; $i++) {
  83. $ch = $packageId[$i]
  84. if (-not [System.Char]::IsLetterOrDigit(($ch))) {
  85. $upperCaseNext = $true
  86. continue
  87. }
  88. if ($upperCaseNext) {
  89. $ch = [System.Char]::ToUpperInvariant($ch)
  90. $upperCaseNext = $false
  91. }
  92. $canonicalVarName += $ch
  93. }
  94. $canonicalVarName += "PackageVersion"
  95. return $canonicalVarName
  96. }