|
| 1 | +#requires -version 4 |
| 2 | + |
| 3 | +<# |
| 4 | +.SYNOPSIS |
| 5 | +Builds this repository. |
| 6 | +
|
| 7 | +.DESCRIPTION |
| 8 | +This build script installs required tools and runs an MSBuild command on this repository. |
| 9 | +This script can be used to invoke various targets, such as targets to produce packages, |
| 10 | +build projects, run tests, and generate code. |
| 11 | +
|
| 12 | +.PARAMETER RepoPath |
| 13 | +The folder to build. Defaults to the folder containing this script. This will be removed soon. |
| 14 | +
|
| 15 | +.PARAMETER CI |
| 16 | +Sets up CI specific settings and variables. |
| 17 | +
|
| 18 | +.PARAMETER Restore |
| 19 | +Run restore on projects. |
| 20 | +
|
| 21 | +.PARAMETER Build |
| 22 | +Compile projects. |
| 23 | +
|
| 24 | +.PARAMETER Pack |
| 25 | +Produce packages. |
| 26 | +
|
| 27 | +.PARAMETER Test |
| 28 | +Run tests. |
| 29 | +
|
| 30 | +.PARAMETER Sign |
| 31 | +Run code signing. |
| 32 | +
|
| 33 | +.PARAMETER Projects |
| 34 | +A list of projects to build. (Must be an absolute path.) Globbing patterns are supported, such as "$(pwd)/**/*.csproj" |
| 35 | +
|
| 36 | +.PARAMETER All |
| 37 | +Build all project types. |
| 38 | +
|
| 39 | +.PARAMETER Managed |
| 40 | +Build managed projects (C#, F#, VB). |
| 41 | +
|
| 42 | +.PARAMETER Native |
| 43 | +Build native projects (C++). |
| 44 | +
|
| 45 | +.PARAMETER NodeJS |
| 46 | +Build NodeJS projects (TypeScript, JS). |
| 47 | +
|
| 48 | +.PARAMETER MSBuildArguments |
| 49 | +Additional MSBuild arguments to be passed through. |
| 50 | +
|
| 51 | +.EXAMPLE |
| 52 | +Building both native and managed projects. |
| 53 | +
|
| 54 | + build.ps1 -managed -native |
| 55 | +
|
| 56 | +.EXAMPLE |
| 57 | +Building a subfolder of code. |
| 58 | +
|
| 59 | + build.ps1 "$(pwd)/src/SomeFolder/**/*.csproj" |
| 60 | +
|
| 61 | +.EXAMPLE |
| 62 | +Running tests. |
| 63 | +
|
| 64 | + build.ps1 -test |
| 65 | +
|
| 66 | +.LINK |
| 67 | +Online version: https://github.com/aspnet/AspNetCore/blob/master/docs/BuildFromSource.md |
| 68 | +#> |
| 69 | +[CmdletBinding(PositionalBinding = $false, DefaultParameterSetName='Groups')] |
| 70 | +param( |
| 71 | + # Bootstrapper options |
| 72 | + [Obsolete('This parameter will be removed when we finish https://github.com/aspnet/AspNetCore/issues/4246')] |
| 73 | + [string]$RepoRoot = $PSScriptRoot, |
| 74 | + |
| 75 | + [switch]$CI, |
| 76 | + |
| 77 | + # Build lifecycle options |
| 78 | + [switch]$Restore = $True, # Run tests |
| 79 | + [switch]$Build = $True, # Compile |
| 80 | + [switch]$Pack, # Produce packages |
| 81 | + [switch]$Test, # Run tests |
| 82 | + [switch]$Sign, # Code sign |
| 83 | + |
| 84 | + # Project selection |
| 85 | + [Parameter(ParameterSetName = 'All')] |
| 86 | + [switch]$All, # Build everything |
| 87 | + |
| 88 | + # A list of projects which should be built. |
| 89 | + [Parameter(ParameterSetName = 'Projects')] |
| 90 | + [string]$Projects, |
| 91 | + |
| 92 | + # Build a specified set of project groups |
| 93 | + [Parameter(ParameterSetName = 'Groups')] |
| 94 | + [switch]$Managed, |
| 95 | + [Parameter(ParameterSetName = 'Groups')] |
| 96 | + [switch]$Native, |
| 97 | + [Parameter(ParameterSetName = 'Groups')] |
| 98 | + [switch]$NodeJS, |
| 99 | + |
| 100 | + # Other lifecycle targets |
| 101 | + [switch]$Help, # Show help |
| 102 | + |
| 103 | + # Capture the rest |
| 104 | + [Parameter(ValueFromRemainingArguments = $true)] |
| 105 | + [string[]]$MSBuildArguments |
| 106 | +) |
| 107 | + |
| 108 | +Set-StrictMode -Version 2 |
| 109 | +$ErrorActionPreference = 'Stop' |
| 110 | + |
| 111 | +# |
| 112 | +# Functions |
| 113 | +# |
| 114 | + |
| 115 | +function Get-KoreBuild { |
| 116 | + |
| 117 | + if (!(Test-Path $LockFile)) { |
| 118 | + Get-RemoteFile "$ToolsSource/korebuild/channels/$Channel/latest.txt" $LockFile |
| 119 | + } |
| 120 | + |
| 121 | + $version = Get-Content $LockFile | Where-Object { $_ -like 'version:*' } | Select-Object -first 1 |
| 122 | + if (!$version) { |
| 123 | + Write-Error "Failed to parse version from $LockFile. Expected a line that begins with 'version:'" |
| 124 | + } |
| 125 | + $version = $version.TrimStart('version:').Trim() |
| 126 | + $korebuildPath = Join-Paths $DotNetHome ('buildtools', 'korebuild', $version) |
| 127 | + |
| 128 | + if (!(Test-Path $korebuildPath)) { |
| 129 | + Write-Host -ForegroundColor Magenta "Downloading KoreBuild $version" |
| 130 | + New-Item -ItemType Directory -Path $korebuildPath | Out-Null |
| 131 | + $remotePath = "$ToolsSource/korebuild/artifacts/$version/korebuild.$version.zip" |
| 132 | + |
| 133 | + try { |
| 134 | + $tmpfile = Join-Path ([IO.Path]::GetTempPath()) "KoreBuild-$([guid]::NewGuid()).zip" |
| 135 | + Get-RemoteFile $remotePath $tmpfile |
| 136 | + if (Get-Command -Name 'Expand-Archive' -ErrorAction Ignore) { |
| 137 | + # Use built-in commands where possible as they are cross-plat compatible |
| 138 | + Expand-Archive -Path $tmpfile -DestinationPath $korebuildPath |
| 139 | + } |
| 140 | + else { |
| 141 | + # Fallback to old approach for old installations of PowerShell |
| 142 | + Add-Type -AssemblyName System.IO.Compression.FileSystem |
| 143 | + [System.IO.Compression.ZipFile]::ExtractToDirectory($tmpfile, $korebuildPath) |
| 144 | + } |
| 145 | + } |
| 146 | + catch { |
| 147 | + Remove-Item -Recurse -Force $korebuildPath -ErrorAction Ignore |
| 148 | + throw |
| 149 | + } |
| 150 | + finally { |
| 151 | + Remove-Item $tmpfile -ErrorAction Ignore |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return $korebuildPath |
| 156 | +} |
| 157 | + |
| 158 | +function Join-Paths([string]$path, [string[]]$childPaths) { |
| 159 | + $childPaths | ForEach-Object { $path = Join-Path $path $_ } |
| 160 | + return $path |
| 161 | +} |
| 162 | + |
| 163 | +function Get-RemoteFile([string]$RemotePath, [string]$LocalPath) { |
| 164 | + if ($RemotePath -notlike 'http*') { |
| 165 | + Copy-Item $RemotePath $LocalPath |
| 166 | + return |
| 167 | + } |
| 168 | + |
| 169 | + $retries = 10 |
| 170 | + while ($retries -gt 0) { |
| 171 | + $retries -= 1 |
| 172 | + try { |
| 173 | + Invoke-WebRequest -UseBasicParsing -Uri $RemotePath -OutFile $LocalPath |
| 174 | + return |
| 175 | + } |
| 176 | + catch { |
| 177 | + Write-Verbose "Request failed. $retries retries remaining" |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + Write-Error "Download failed: '$RemotePath'." |
| 182 | +} |
| 183 | + |
| 184 | +# |
| 185 | +# Main |
| 186 | +# |
| 187 | + |
| 188 | +# Load configuration or set defaults |
| 189 | + |
| 190 | +if ($Help) { |
| 191 | + Get-Help $PSCommandPath |
| 192 | + exit 1 |
| 193 | +} |
| 194 | + |
| 195 | +$RepoRoot = Resolve-Path $RepoRoot |
| 196 | +$Channel = 'master' |
| 197 | +$ToolsSource = 'https://aspnetcore.blob.core.windows.net/buildtools' |
| 198 | +$ConfigFile = Join-Path $PSScriptRoot 'korebuild.json' |
| 199 | +$LockFile = Join-Path $PSScriptRoot 'korebuild-lock.txt' |
| 200 | + |
| 201 | +if (Test-Path $ConfigFile) { |
| 202 | + try { |
| 203 | + $config = Get-Content -Raw -Encoding UTF8 -Path $ConfigFile | ConvertFrom-Json |
| 204 | + if ($config) { |
| 205 | + if (!($Channel) -and (Get-Member -Name 'channel' -InputObject $config)) { [string] $Channel = $config.channel } |
| 206 | + if (!($ToolsSource) -and (Get-Member -Name 'toolsSource' -InputObject $config)) { [string] $ToolsSource = $config.toolsSource} |
| 207 | + } |
| 208 | + } catch { |
| 209 | + Write-Warning "$ConfigFile could not be read. Its settings will be ignored." |
| 210 | + Write-Warning $Error[0] |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +$DotNetHome = if ($env:DOTNET_HOME) { $env:DOTNET_HOME } ` |
| 215 | + elseif ($CI) { Join-Path $PSScriptRoot '.dotnet' } ` |
| 216 | + elseif ($env:USERPROFILE) { Join-Path $env:USERPROFILE '.dotnet'} ` |
| 217 | + elseif ($env:HOME) {Join-Path $env:HOME '.dotnet'}` |
| 218 | + else { Join-Path $PSScriptRoot '.dotnet'} |
| 219 | + |
| 220 | +$env:DOTNET_HOME = $DotNetHome |
| 221 | + |
| 222 | +# Execute |
| 223 | + |
| 224 | +$korebuildPath = Get-KoreBuild |
| 225 | + |
| 226 | +# Project selection |
| 227 | +if ($All) { |
| 228 | + $MSBuildArguments += '/p:BuildAllProjects=true' |
| 229 | +} |
| 230 | +elseif ($Projects) { |
| 231 | + $MSBuildArguments += "/p:Projects=$Projects" |
| 232 | +} |
| 233 | +else { |
| 234 | + # When adding new sub-group build flags, add them to this check |
| 235 | + if((-not $Native) -and (-not $Managed) -and (-not $NodeJS)) { |
| 236 | + Write-Warning "No default group of projects was specified, so building the 'managed' and 'native' subset of projects. Run ``build.cmd -help`` for more details." |
| 237 | + |
| 238 | + # This goal of this is to pick a sensible default for `build.cmd` with zero arguments. |
| 239 | + # We believe the most common thing our contributors will work on is C#, so if no other build group was picked, build the C# projects. |
| 240 | + |
| 241 | + $Managed = $true |
| 242 | + } |
| 243 | + |
| 244 | + $MSBuildArguments += "/p:BuildManaged=$Managed" |
| 245 | + $MSBuildArguments += "/p:BuildNative=$Native" |
| 246 | + $MSBuildArguments += "/p:BuildNodeJS=$NodeJS" |
| 247 | +} |
| 248 | + |
| 249 | +# Target selection |
| 250 | +$MSBuildArguments += "/p:_RunRestore=$Restore" |
| 251 | +$MSBuildArguments += "/p:_RunBuild=$Build" |
| 252 | +$MSBuildArguments += "/p:_RunPack=$Pack" |
| 253 | +$MSBuildArguments += "/p:_RunTests=$Test" |
| 254 | +$MSBuildArguments += "/p:_RunSign=$Sign" |
| 255 | + |
| 256 | +Import-Module -Force -Scope Local (Join-Path $korebuildPath 'KoreBuild.psd1') |
| 257 | + |
| 258 | +try { |
| 259 | + Set-KoreBuildSettings -ToolsSource $ToolsSource -DotNetHome $DotNetHome -RepoPath $RepoRoot -ConfigFile $ConfigFile -CI:$CI |
| 260 | + Invoke-KoreBuildCommand 'default-build' @MSBuildArguments |
| 261 | +} |
| 262 | +finally { |
| 263 | + Remove-Module 'KoreBuild' -ErrorAction Ignore |
| 264 | +} |
0 commit comments