-
Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathrepackageBinaries.ps1
131 lines (108 loc) · 4.54 KB
/
repackageBinaries.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
Set-Location ".\build"
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip([string]$zipfilePath, [string]$outputpath) {
try {
[System.IO.Compression.ZipFile]::ExtractToDirectory($zipfilePath, $outputpath)
LogSuccess "Unzipped:$zipfilePath at $outputpath"
}
catch {
LogErrorAndExit "Unzip failed for:$zipfilePath" $_.Exception
}
}
function Zip([string]$directoryPath, [string]$zipPath) {
try {
LogSuccess "start zip:$directoryPath to $zipPath"
[System.IO.Compression.ZipFile]::CreateFromDirectory($directoryPath, $zipPath, [System.IO.Compression.CompressionLevel]::Optimal, $false);
LogSuccess "Zipped:$directoryPath to $zipPath"
}
catch {
LogErrorAndExit "Zip operation failed for:$directoryPath" $_.Exception
}
}
function LogErrorAndExit($errorMessage, $exception) {
Write-Output $errorMessage
if ($exception -ne $null) {
Write-Output $exception|format-list -force
}
Exit 1
}
function LogSuccess($message) {
Write-Output `n
Write-Output $message
}
try
{
$artifactsPath = Resolve-Path "..\artifacts\"
$tempDirectoryPath = "..\artifacts\temp\"
if (Test-Path $tempDirectoryPath)
{
Remove-Item $tempDirectoryPath -Force -Recurse
}
# Runtimes with signed binaries
$runtimesIdentifiers = @("min.win-arm64", "min.win-x86","min.win-x64", "osx-arm64", "osx-x64")
$tempDirectory = New-Item $tempDirectoryPath -ItemType Directory
LogSuccess "$tempDirectoryPath created"
# Unzip the coretools artifact to add signed binaries
foreach($rid in $runtimesIdentifiers)
{
$files= Get-ChildItem -Path "..\artifacts\*.zip"
foreach($file in $files)
{
if ($file.Name.Contains($rid))
{
$fileName = [io.path]::GetFileNameWithoutExtension($file.Name)
$targetDirectory = Join-Path $tempDirectoryPath $fileName
$dir = New-Item $targetDirectory -ItemType Directory
$targetDirectory = Resolve-Path $targetDirectory
$filePath = Resolve-Path $file.FullName
Unzip $filePath $targetDirectory
# Removing file after extraction
Remove-Item $filePath
LogSuccess "Removed $filePath"
}
}
}
# Store file count before replacing the binaries
$fileCountBefore = (Get-ChildItem $tempDirectoryPath -Recurse | Measure-Object).Count
# copy authenticode signed binaries into extracted directories
$authenticodeDirectory = "..\artifacts\ToSign\Authenticode\"
$authenticodeDirectories = Get-ChildItem $authenticodeDirectory -Directory
foreach($directory in $authenticodeDirectories)
{
$sourcePath = $directory.FullName
Copy-Item -Path $sourcePath -Destination $tempDirectoryPath -Recurse -Force
}
# copy thirdparty signed directory into extracted directories
$thirdPathDirectory = "..\artifacts\ToSign\ThirdParty\"
$thirdPathDirectories = Get-ChildItem $thirdPathDirectory -Directory
foreach($directory in $thirdPathDirectories)
{
$sourcePath = $directory.FullName
Copy-Item -Path $sourcePath -Destination $tempDirectoryPath -Recurse -Force
}
# mac signing requires the files to be in a zip to sign, so we have to extract the zip before copying over the signed files
$macZipFiles = Get-ChildItem "..\artifacts\ToSign\Mac\*.zip"
foreach($macZipFile in $macZipFiles)
{
$macUnzippedDir = Join-Path $macZipFile.DirectoryName $macZipFile.BaseName
Unzip $macZipFile.FullName $macUnzippedDir
Copy-Item -Path $macUnzippedDir -Destination $tempDirectoryPath -Recurse -Force
}
$fileCountAfter = (Get-ChildItem $tempDirectoryPath -Recurse | Measure-Object).Count
if ($fileCountBefore -ne $fileCountAfter)
{
LogErrorAndExit "File count does not match. File count before copy: $fileCountBefore != file count after copy:$fileCountAfter" $_.Exception
}
$tempDirectories = Get-ChildItem $tempDirectoryPath -Directory
foreach($directory in $tempDirectories)
{
$directoryName = $directory.Name
$zipPath = Join-Path $artifactsPath $directoryName
$zipPath = $zipPath + ".zip"
$directoryPath = $directory.FullName
Zip $directoryPath $zipPath
}
}
catch {
LogErrorAndExit "Execution Failed" $_.Exception
}