forked from Azure/azure-functions-powershell-worker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ps1
212 lines (160 loc) · 6.67 KB
/
build.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env pwsh
#
# Copyright (c) Microsoft. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
#
[CmdletBinding()]
param(
[switch]
$Clean,
[switch]
$Bootstrap,
[switch]
$Test,
[switch]
$NoBuild,
[switch]
$Deploy,
[string]
$CoreToolsDir,
[string]
$Configuration = "Debug",
[string]
$BuildNumber = '0',
[switch]
$AddSBOM,
[string]
$SBOMUtilSASUrl
)
#Requires -Version 6.0
$PowerShellVersion = '7.2'
$TargetFramework = 'net6.0'
function Get-FunctionsCoreToolsDir {
if ($CoreToolsDir) {
$CoreToolsDir
} else {
$funcPath = (Get-Command func).Source
if (-not $funcPath) {
throw 'Cannot find "func" command. Please install Azure Functions Core Tools: ' +
'see https://github.com/Azure/azure-functions-core-tools#installing for instructions'
}
# func may be just a symbolic link, so we need to follow it until we find the true location
while ((((Get-Item $funcPath).Attributes) -band 'ReparsePoint') -ne 0) {
$funcPath = (Get-Item $funcPath).Target
}
$funcParentDir = Split-Path -Path $funcPath -Parent
if (-not (Test-Path -Path $funcParentDir/workers/powershell -PathType Container)) {
throw 'Cannot find Azure Function Core Tools installation directory. ' +
'Please provide the path in the CoreToolsDir parameter.'
}
$funcParentDir
}
}
function Install-SBOMUtil
{
if ([string]::IsNullOrEmpty($SBOMUtilSASUrl))
{
throw "The `$SBOMUtilSASUrl parameter cannot be null or empty when specifying the `$AddSBOM switch"
}
$MANIFESTOOLNAME = "ManifestTool"
Write-Host "Installing $MANIFESTOOLNAME..."
$MANIFESTOOL_DIRECTORY = Join-Path $PSScriptRoot $MANIFESTOOLNAME
Remove-Item -Recurse -Force $MANIFESTOOL_DIRECTORY -ErrorAction Ignore
Invoke-RestMethod -Uri $SBOMUtilSASUrl -OutFile "$MANIFESTOOL_DIRECTORY.zip"
Expand-Archive "$MANIFESTOOL_DIRECTORY.zip" -DestinationPath $MANIFESTOOL_DIRECTORY
$dllName = "Microsoft.ManifestTool.dll"
$manifestToolPath = "$MANIFESTOOL_DIRECTORY/$dllName"
if (-not (Test-Path $manifestToolPath))
{
throw "$MANIFESTOOL_DIRECTORY does not contain '$dllName'"
}
Write-Host 'Done.'
return $manifestToolPath
}
function Deploy-PowerShellWorker {
$ErrorActionPreference = 'Stop'
$powerShellWorkerDir = "$(Get-FunctionsCoreToolsDir)/workers/powershell/$PowerShellVersion"
Write-Log "Deploying worker to $powerShellWorkerDir..."
if (-not $IsWindows) {
sudo chmod -R a+w $powerShellWorkerDir
}
Remove-Item -Path $powerShellWorkerDir/* -Recurse -Force
Copy-Item -Path "./src/bin/$Configuration/$TargetFramework/publish/*" `
-Destination $powerShellWorkerDir -Recurse -Force
Write-Log "Deployed worker to $powerShellWorkerDir"
}
Import-Module "$PSScriptRoot/tools/helper.psm1" -Force
# Bootstrap step
if ($Bootstrap.IsPresent) {
Write-Log "Validate and install missing prerequisits for building ..."
Install-Dotnet
if (-not (Get-Module -Name PSDepend -ListAvailable)) {
Write-Log -Warning "Module 'PSDepend' is missing. Installing 'PSDepend' ..."
Install-Module -Name PSDepend -Scope CurrentUser -Force
}
if (-not (Get-Module -Name platyPS -ListAvailable)) {
Write-Log -Warning "Module 'platyPS' is missing. Installing 'platyPS' ..."
Install-Module -Name platyPS -Scope CurrentUser -Force
}
}
# Clean step
if ($Clean.IsPresent) {
Push-Location $PSScriptRoot
git clean -fdX
Pop-Location
}
# Common step required by both build and test
Find-Dotnet
# Build step
if (!$NoBuild.IsPresent) {
if (-not (Get-Module -Name PSDepend -ListAvailable)) {
throw "Cannot find the 'PSDepend' module. Please specify '-Bootstrap' to install build dependencies."
}
# Generate C# files for resources
Start-ResGen
# Generate csharp code from protobuf if needed
New-gRPCAutoGenCode
$requirements = "$PSScriptRoot/src/requirements.psd1"
$modules = Import-PowerShellDataFile $requirements
Write-Log "Install modules that are bundled with PowerShell Language worker, including"
foreach ($entry in $modules.GetEnumerator()) {
Write-Log -Indent "$($entry.Name) $($entry.Value.Version)"
}
Invoke-PSDepend -Path $requirements -Force
Write-Log "Deleting fullclr folder from PackageManagement module if the folder exists ..."
Get-Item "$PSScriptRoot/src/Modules/PackageManagement/1.1.7.0/fullclr" -ErrorAction SilentlyContinue |
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
# TODO: Remove this once the SDK properly bundles modules
Get-WebFile -Url 'https://raw.githubusercontent.com/PowerShell/PowerShell/master/src/Modules/Windows/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1' `
-OutFile "$PSScriptRoot/src/Modules/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility.psd1"
Get-WebFile -Url 'https://raw.githubusercontent.com/PowerShell/PowerShell/master/src/Modules/Windows/Microsoft.PowerShell.Management/Microsoft.PowerShell.Management.psd1' `
-OutFile "$PSScriptRoot/src/Modules/Microsoft.PowerShell.Management/Microsoft.PowerShell.Management.psd1"
dotnet publish -c $Configuration "/p:BuildNumber=$BuildNumber" $PSScriptRoot
if ($AddSBOM)
{
# Install manifest tool
$manifestTool = Install-SBOMUtil
Write-Log "manifestTool: $manifestTool "
# Generate manifest
$buildPath = "$PSScriptRoot/src/bin/$Configuration/$TargetFramework/publish"
$telemetryFilePath = Join-Path $PSScriptRoot ((New-Guid).Guid + ".json")
$packageName = "Microsoft.Azure.Functions.PowerShellWorker.nuspec"
# Delete the manifest folder if it exists
$manifestFolderPath = Join-Path $buildPath "_manifest"
if (Test-Path $manifestFolderPath)
{
Remove-Item $manifestFolderPath -Recurse -Force -ErrorAction Ignore
}
Write-Log "Running: dotnet $manifestTool generate -BuildDropPath $buildPath -BuildComponentPath $buildPath -Verbosity Information -t $telemetryFilePath"
& { dotnet $manifestTool generate -BuildDropPath $buildPath -BuildComponentPath $buildPath -Verbosity Information -t $telemetryFilePath -PackageName $packageName }
}
dotnet pack -c $Configuration "/p:BuildNumber=$BuildNumber" "$PSScriptRoot/package"
}
# Test step
if ($Test.IsPresent) {
dotnet test "$PSScriptRoot/test/Unit"
if ($LASTEXITCODE -ne 0) { throw "xunit tests failed." }
}
if ($Deploy.IsPresent) {
Deploy-PowerShellWorker
}