|
| 1 | +function Get-Version { |
| 2 | + param( |
| 3 | + [string] |
| 4 | + $BranchOrTagName = "latest" |
| 5 | + ) |
| 6 | + # Get the version |
| 7 | + $version = "$BranchOrTagName" |
| 8 | + $version = $version.TrimStart('refs/tags/v') |
| 9 | + # Return the version |
| 10 | + return $version |
| 11 | +} |
| 12 | + |
| 13 | +function Get-ZipName { |
| 14 | + param( |
| 15 | + [string] |
| 16 | + $FileNameTemplate = "msgraph-cli-{0}-{1}.zip", |
| 17 | + [string] |
| 18 | + $BranchOrTagName = "latest", |
| 19 | + [string] |
| 20 | + $RuntimeIdentifier = "unknown" |
| 21 | + ) |
| 22 | + $version = Get-Version $BranchOrTagName |
| 23 | + return "$FileNameTemplate" -f "$RuntimeIdentifier","$version" |
| 24 | +} |
| 25 | + |
| 26 | +function Compress-BuildOutput { |
| 27 | + param( |
| 28 | + [Parameter(Mandatory)] |
| 29 | + [string] |
| 30 | + $OutputDir, |
| 31 | + [Parameter(Mandatory)] |
| 32 | + [string] |
| 33 | + $SourceDir, |
| 34 | + [string] |
| 35 | + $FileNameTemplate = "msgraph-cli-{0}-{1}.zip", |
| 36 | + [string] |
| 37 | + $BranchOrTagName = "latest", |
| 38 | + [string] |
| 39 | + $RuntimeIdentifier = "unknown", |
| 40 | + [switch] |
| 41 | + $Cleanup |
| 42 | + ) |
| 43 | + |
| 44 | + if (-Not (Test-Path -Path $SourceDir/*)) { |
| 45 | + Write-Error "Source dir is empty." |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + if (-Not (Test-Path -Path $OutputDir)) { |
| 50 | + New-Item $OutputDir -ItemType Directory |
| 51 | + } |
| 52 | + |
| 53 | + $zipName = Get-ZipName -FileNameTemplate $FileNameTemplate -BranchOrTagName $BranchOrTagName -RuntimeIdentifier $RuntimeIdentifier |
| 54 | + $zipPath = Join-Path -Path $OutputDir -ChildPath $zipName |
| 55 | + |
| 56 | + Compress-Archive -Path $SourceDir/* -DestinationPath $zipPath -Force |
| 57 | + |
| 58 | + if ($Cleanup) { |
| 59 | + Write-Information "Cleaning up $SourceDir" |
| 60 | + Remove-Item $SourceDir -Recurse -Force |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +function Expand-EsrpArtifacts { |
| 65 | + param( |
| 66 | + [Parameter(Mandatory)] |
| 67 | + [string] |
| 68 | + $SourceDir, |
| 69 | + [string] |
| 70 | + $OutputDir, |
| 71 | + [string] |
| 72 | + $FileNameTemplate = "msgraph-cli-{0}-{1}.zip", |
| 73 | + [string] |
| 74 | + $BranchOrTagName = "latest", |
| 75 | + [string] |
| 76 | + $RuntimeIdentifier = "unknown", |
| 77 | + [switch] |
| 78 | + $Cleanup |
| 79 | + ) |
| 80 | + # Get the archive name |
| 81 | + $zipName = Get-ZipName -FileNameTemplate $FileNameTemplate -BranchOrTagName $BranchOrTagName -RuntimeIdentifier $RuntimeIdentifier |
| 82 | + $zipPath = Join-Path -Path $SourceDir -ChildPath $zipName |
| 83 | + |
| 84 | + Expand-Archive -Path $zipPath -DestinationPath $OutputDir |
| 85 | + |
| 86 | + if ($Cleanup) { |
| 87 | + # -Force so there's no confirmation |
| 88 | + # -Recurse so the child items warning isn't shown |
| 89 | + Write-Information "Cleaning up $SourceDir" |
| 90 | + Remove-Item $SourceDir -Recurse -Force |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +function Move-NonExecutableItems { |
| 95 | + param( |
| 96 | + [Parameter(Mandatory)] |
| 97 | + [string] |
| 98 | + $SourcePath, |
| 99 | + [string[]] |
| 100 | + $ExecutableItemNames |
| 101 | + ) |
| 102 | + |
| 103 | + $parentDir = Split-Path -Path $SourcePath -Parent -Resolve |
| 104 | + $backupDir = Join-Path -Path $parentDir -ChildPath backup |
| 105 | + |
| 106 | + if (-Not (Test-Path -Path $backupDir/*)) { |
| 107 | + New-Item $backupDir -ItemType Directory |
| 108 | + } else { |
| 109 | + Write-Error "The directory $backupDir already exists and is not empty." |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + Move-Item -Path $SourcePath/* -Exclude $ExecutableItemNames -Destination $backupDir |
| 114 | +} |
| 115 | + |
| 116 | +function Compress-SignedFiles { |
| 117 | + param( |
| 118 | + [Parameter(Mandatory)] |
| 119 | + [string] |
| 120 | + $SourceDir, |
| 121 | + [string] |
| 122 | + $ReportDir, |
| 123 | + [Parameter(Mandatory)] |
| 124 | + [string] |
| 125 | + $OutputFile, |
| 126 | + [switch] |
| 127 | + $Cleanup |
| 128 | + ) |
| 129 | + |
| 130 | + if (-Not (Test-Path -Path "$SourceDir/*")) { |
| 131 | + Write-Error "Source dir is invalid." |
| 132 | + return |
| 133 | + } |
| 134 | + |
| 135 | + if (-Not $OutputFile) { |
| 136 | + Write-Error "Output zip file path not provided." |
| 137 | + return |
| 138 | + } |
| 139 | + |
| 140 | + if ($ReportDir -and (Test-Path -Path "$ReportDir/*.md")) { |
| 141 | + Write-Information "Moving signing report to $ReportDir" |
| 142 | + Move-Item -Path "$SourceDir/*.md" -Destination $ReportDir |
| 143 | + } |
| 144 | + |
| 145 | + $parentDir = Split-Path -Path $SourceDir -Parent -Resolve |
| 146 | + $backupDir = Join-Path -Path $parentDir -ChildPath backup |
| 147 | + |
| 148 | + if ($backupDir -and (Test-Path -Path "$backupDir/*")) { |
| 149 | + Write-Information "Moving the following files to archive source location" |
| 150 | + Get-ChildItem -Path "$backupDir/*" -Recurse -Force -ErrorAction SilentlyContinue | Select-Object FullName |
| 151 | + Move-Item -Path "$backupDir/*" -Destination "$SourceDir" |
| 152 | + } |
| 153 | + |
| 154 | + Compress-Archive -Path "$SourceDir/*" -DestinationPath $OutputFile -Force |
| 155 | + |
| 156 | + if ($Cleanup) { |
| 157 | + Write-Information "Cleaning up $SourceDir and $backupDir" |
| 158 | + Remove-Item "$SourceDir" -Recurse -Force |
| 159 | + Remove-Item "$backupDir" -Recurse -Force |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +function Update-SignedArchive { |
| 164 | + param( |
| 165 | + [Parameter(Mandatory)] |
| 166 | + [string] |
| 167 | + $InputFile, |
| 168 | + [Parameter(Mandatory)] |
| 169 | + [string] |
| 170 | + $ReportDir, |
| 171 | + [Parameter(Mandatory)] |
| 172 | + [string] |
| 173 | + $OutputFile, |
| 174 | + [switch] |
| 175 | + $Cleanup |
| 176 | + ) |
| 177 | + |
| 178 | + if (-Not (Test-Path -Path "$InputFile" -PathType Leaf)) { |
| 179 | + Write-Error "Input zip file does not exist." |
| 180 | + return |
| 181 | + } |
| 182 | + |
| 183 | + if (-Not $OutputFile) { |
| 184 | + Write-Error "Output zip file path not provided." |
| 185 | + return |
| 186 | + } |
| 187 | + |
| 188 | + $extractOutput = Split-Path -Path $InputFile -Parent -Resolve |
| 189 | + |
| 190 | + Expand-Archive -Path "$InputFile" -DestinationPath "$extractOutput" |
| 191 | + |
| 192 | + Remove-Item -Path "$InputFile" -Force |
| 193 | + |
| 194 | + Compress-SignedFiles -SourceDir $extractOutput -ReportDir $ReportDir -OutputFile $OutputFile -Cleanup:$Cleanup |
| 195 | +} |
0 commit comments