-
Notifications
You must be signed in to change notification settings - Fork 4k
/
build-utils.ps1
269 lines (226 loc) · 8.07 KB
/
build-utils.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# Collection of powershell build utility functions that we use across our scripts.
Set-StrictMode -version 2.0
$ErrorActionPreference="Stop"
# Import Arcade functions
. (Join-Path $PSScriptRoot "common\tools.ps1")
$VSSetupDir = Join-Path $ArtifactsDir "VSSetup\$configuration"
$PackagesDir = Join-Path $ArtifactsDir "packages\$configuration"
$PublishDataUrl = "https://raw.githubusercontent.com/dotnet/roslyn/main/eng/config/PublishData.json"
$binaryLog = if (Test-Path variable:binaryLog) { $binaryLog } else { $false }
$nodeReuse = if (Test-Path variable:nodeReuse) { $nodeReuse } else { $false }
$properties = if (Test-Path variable:properties) { $properties } else { @() }
$originalTemp = $env:TEMP;
function GetProjectOutputBinary([string]$fileName, [string]$projectName = "", [string]$configuration = $script:configuration, [string]$tfm = "net472", [string]$rid = "", [bool]$published = $false) {
$projectName = if ($projectName -ne "") { $projectName } else { [System.IO.Path]::GetFileNameWithoutExtension($fileName) }
$publishDir = if ($published) { "publish\" } else { "" }
$ridDir = if ($rid -ne "") { "$rid\" } else { "" }
return Join-Path $ArtifactsDir "bin\$projectName\$configuration\$tfm\$ridDir$publishDir$fileName"
}
function GetPublishData() {
if (Test-Path variable:global:_PublishData) {
return $global:_PublishData
}
Write-Host "Downloading $PublishDataUrl"
$content = (Invoke-WebRequest -Uri $PublishDataUrl -UseBasicParsing).Content
return $global:_PublishData = ConvertFrom-Json $content
}
function GetBranchPublishData([string]$branchName) {
$data = GetPublishData
if (Get-Member -InputObject $data.branches -Name $branchName) {
return $data.branches.$branchName
} else {
return $null
}
}
function GetFeedPublishData() {
$data = GetPublishData
return $data.feeds
}
function GetPackagesPublishData([string]$packageFeeds) {
$data = GetPublishData
if (Get-Member -InputObject $data.packages -Name $packageFeeds) {
return $data.packages.$packageFeeds
} else {
return $null
}
}
function GetReleasePublishData([string]$releaseName) {
$data = GetPublishData
if (Get-Member -InputObject $data.releases -Name $releaseName) {
return $data.releases.$releaseName
} else {
return $null
}
}
function Exec-CommandCore([string]$command, [string]$commandArgs, [switch]$useConsole = $true, [switch]$echoCommand = $true) {
if ($echoCommand) {
Write-Host "$command $commandArgs"
}
if ($useConsole) {
$exitCode = Exec-Process $command $commandArgs
if ($exitCode -ne 0) {
throw "Command failed to execute with exit code $($exitCode): $command $commandArgs"
}
return
}
$startInfo = New-Object System.Diagnostics.ProcessStartInfo
$startInfo.FileName = $command
$startInfo.Arguments = $commandArgs
$startInfo.UseShellExecute = $false
$startInfo.WorkingDirectory = Get-Location
$startInfo.RedirectStandardOutput = $true
$startInfo.CreateNoWindow = $true
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $startInfo
$process.Start() | Out-Null
$finished = $false
try {
# The OutputDataReceived event doesn't fire as events are sent by the
# process in powershell. Possibly due to subtlties of how Powershell
# manages the thread pool that I'm not aware of. Using blocking
# reading here as an alternative which is fine since this blocks
# on completion already.
$out = $process.StandardOutput
while (-not $out.EndOfStream) {
$line = $out.ReadLine()
Write-Output $line
}
while (-not $process.WaitForExit(100)) {
# Non-blocking loop done to allow ctr-c interrupts
}
$finished = $true
if ($process.ExitCode -ne 0) {
throw "Command failed to execute with exit code $($process.ExitCode): $command $commandArgs"
}
}
finally {
# If we didn't finish then an error occurred or the user hit ctrl-c. Either
# way kill the process
if (-not $finished) {
$process.Kill()
}
}
}
# Handy function for executing a windows command which needs to go through
# windows command line parsing.
#
# Use this when the command arguments are stored in a variable. Particularly
# when the variable needs reparsing by the windows command line. Example:
#
# $args = "/p:ManualBuild=true Test.proj"
# Exec-Command $msbuild $args
#
# The -useConsole argument controls if the process should re-use the current
# console for output or return output as a string
function Exec-Command([string]$command, [string]$commandArgs, [switch]$useConsole = $false, [switch]$echoCommand = $true) {
if ($args -ne "") {
throw "Extra arguments passed to Exec-Command: $args"
}
Exec-CommandCore -command $command -commandArgs $commandArgs -useConsole:$useConsole -echoCommand:$echoCommand
}
# Handy function for executing a dotnet command without having to track down the
# proper dotnet executable or ensure it's on the path.
function Exec-DotNet([string]$commandArgs = "", [switch]$useConsole = $true, [switch]$echoCommand = $true) {
if ($args -ne "") {
throw "Extra arguments passed to Exec-DotNet: $args"
}
$dotnet = Ensure-DotNetSdk
Exec-CommandCore -command $dotnet -commandArgs $commandArgs -useConsole:$useConsole -echoCommand:$echoCommand
}
# Ensure the proper .NET Core SDK is available. Returns the location to the dotnet.exe.
function Ensure-DotnetSdk() {
$dotnetInstallDir = (InitializeDotNetCli -install:$true)
$dotnetTestPath = Join-Path $dotnetInstallDir "dotnet.exe"
if (Test-Path -Path $dotnetTestPath) {
return $dotnetTestPath
}
$dotnetTestPath = Join-Path $dotnetInstallDir "dotnet"
if (Test-Path -Path $dotnetTestPath) {
return $dotnetTestPath
}
throw "Could not find dotnet executable in $dotnetInstallDir"
}
function Test-LastExitCode() {
if ($LASTEXITCODE -ne 0) {
throw "Last command failed with exit code $LASTEXITCODE"
}
}
# Walks up the source tree, starting at the given file's directory, and returns a FileInfo object for the first .csproj file it finds, if any.
function Get-ProjectFile([object]$fileInfo) {
Push-Location
Set-Location $fileInfo.Directory
try {
while ($true) {
# search up from the current file for a folder containing a project file
$files = Get-ChildItem *.csproj,*.vbproj
if ($files) {
return $files[0]
}
else {
$location = Get-Location
Set-Location ..
if ((Get-Location).Path -eq $location.Path) {
# our location didn't change. We must be at the drive root, so give up
return $null
}
}
}
}
finally {
Pop-Location
}
}
function Get-VersionCore([string]$name, [string]$versionFile) {
$name = $name.Replace(".", "")
$name = $name.Replace("-", "")
$nodeName = "$($name)Version"
$x = [xml](Get-Content -raw $versionFile)
$node = $x.SelectSingleNode("//Project/PropertyGroup/$nodeName")
if ($node -ne $null) {
return $node.InnerText
}
throw "Cannot find package $name in $versionFile"
}
# Return the version of the NuGet package as used in this repo
function Get-PackageVersion([string]$name) {
return Get-VersionCore $name (Join-Path $EngRoot "Versions.props")
}
# Locate the directory where our NuGet packages will be deployed. Needs to be kept in sync
# with the logic in Version.props
function Get-PackagesDir() {
$d = $null
if ($env:NUGET_PACKAGES -ne $null) {
$d = $env:NUGET_PACKAGES
}
else {
$d = Join-Path $env:UserProfile ".nuget\packages\"
}
Create-Directory $d
return $d
}
# Locate the directory of a specific NuGet package which is restored via our main
# toolset values.
function Get-PackageDir([string]$name, [string]$version = "") {
if ($version -eq "") {
$version = Get-PackageVersion $name
}
$p = Get-PackagesDir
$p = Join-Path $p $name.ToLowerInvariant()
$p = Join-Path $p $version
return $p
}
function Subst-TempDir() {
if ($ci) {
Exec-Command "subst" "T: $TempDir"
$env:TEMP='T:\'
$env:TMP='T:\'
}
}
function Unsubst-TempDir() {
if ($ci) {
Exec-Command "subst" "T: /d"
# Restore the original temp directory
$env:TEMP=$originalTemp
$env:TMP=$originalTemp
}
}