Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit f7e8efc

Browse files
Update bootstrappers
1 parent 127d23a commit f7e8efc

File tree

4 files changed

+371
-78
lines changed

4 files changed

+371
-78
lines changed

build.cmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
@ECHO OFF
2-
PowerShell -NoProfile -NoLogo -ExecutionPolicy unrestricted -Command "[System.Threading.Thread]::CurrentThread.CurrentCulture = ''; [System.Threading.Thread]::CurrentThread.CurrentUICulture = '';& '%~dp0build.ps1' %*; exit $LASTEXITCODE"
2+
PowerShell -NoProfile -NoLogo -ExecutionPolicy unrestricted -Command "[System.Threading.Thread]::CurrentThread.CurrentCulture = ''; [System.Threading.Thread]::CurrentThread.CurrentUICulture = '';& '%~dp0build.ps1' %*; exit $LASTEXITCODE"

build.ps1

+165-46
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,186 @@
1-
$ErrorActionPreference = "Stop"
1+
#!/usr/bin/env powershell
2+
#requires -version 4
23

3-
function DownloadWithRetry([string] $url, [string] $downloadLocation, [int] $retries)
4+
<#
5+
.SYNOPSIS
6+
Build this repository
7+
8+
.DESCRIPTION
9+
Downloads korebuild if required. Then builds the repository.
10+
11+
.PARAMETER Path
12+
The folder to build. Defaults to the folder containing this script.
13+
14+
.PARAMETER Channel
15+
The channel of KoreBuild to download. Overrides the value from the config file.
16+
17+
.PARAMETER DotNetHome
18+
The directory where .NET Core tools will be stored.
19+
20+
.PARAMETER ToolsSource
21+
The base url where build tools can be downloaded. Overrides the value from the config file.
22+
23+
.PARAMETER Update
24+
Updates KoreBuild to the latest version even if a lock file is present.
25+
26+
.PARAMETER ConfigFile
27+
The path to the configuration file that stores values. Defaults to version.props.
28+
29+
.PARAMETER MSBuildArgs
30+
Arguments to be passed to MSBuild
31+
32+
.NOTES
33+
This function will create a file $PSScriptRoot/korebuild-lock.txt. This lock file can be committed to source, but does not have to be.
34+
When the lockfile is not present, KoreBuild will create one using latest available version from $Channel.
35+
36+
The $ConfigFile is expected to be an JSON file. It is optional, and the configuration values in it are optional as well. Any options set
37+
in the file are overridden by command line parameters.
38+
39+
.EXAMPLE
40+
Example config file:
41+
```json
442
{
5-
while($true)
6-
{
7-
try
8-
{
9-
Invoke-WebRequest $url -OutFile $downloadLocation
10-
break
11-
}
12-
catch
13-
{
14-
$exceptionMessage = $_.Exception.Message
15-
Write-Host "Failed to download '$url': $exceptionMessage"
16-
if ($retries -gt 0) {
17-
$retries--
18-
Write-Host "Waiting 10 seconds before retrying. Retries left: $retries"
19-
Start-Sleep -Seconds 10
43+
"$schema": "https://raw.githubusercontent.com/aspnet/BuildTools/dev/tools/korebuild.schema.json",
44+
"channel": "dev",
45+
"toolsSource": "https://aspnetcore.blob.core.windows.net/buildtools"
46+
}
47+
```
48+
#>
49+
[CmdletBinding(PositionalBinding = $false)]
50+
param(
51+
[string]$Path = $PSScriptRoot,
52+
[Alias('c')]
53+
[string]$Channel,
54+
[Alias('d')]
55+
[string]$DotNetHome,
56+
[Alias('s')]
57+
[string]$ToolsSource,
58+
[Alias('u')]
59+
[switch]$Update,
60+
[string]$ConfigFile = $null,
61+
[Parameter(ValueFromRemainingArguments = $true)]
62+
[string[]]$MSBuildArgs
63+
)
64+
65+
Set-StrictMode -Version 2
66+
$ErrorActionPreference = 'Stop'
67+
68+
#
69+
# Functions
70+
#
71+
72+
function Get-KoreBuild {
73+
74+
$lockFile = Join-Path $Path 'korebuild-lock.txt'
75+
76+
if (!(Test-Path $lockFile) -or $Update) {
77+
Get-RemoteFile "$ToolsSource/korebuild/channels/$Channel/latest.txt" $lockFile
78+
}
79+
80+
$version = Get-Content $lockFile | Where-Object { $_ -like 'version:*' } | Select-Object -first 1
81+
if (!$version) {
82+
Write-Error "Failed to parse version from $lockFile. Expected a line that begins with 'version:'"
83+
}
84+
$version = $version.TrimStart('version:').Trim()
85+
$korebuildPath = Join-Paths $DotNetHome ('buildtools', 'korebuild', $version)
86+
87+
if (!(Test-Path $korebuildPath)) {
88+
Write-Host -ForegroundColor Magenta "Downloading KoreBuild $version"
89+
New-Item -ItemType Directory -Path $korebuildPath | Out-Null
90+
$remotePath = "$ToolsSource/korebuild/artifacts/$version/korebuild.$version.zip"
2091

92+
try {
93+
$tmpfile = Join-Path ([IO.Path]::GetTempPath()) "KoreBuild-$([guid]::NewGuid()).zip"
94+
Get-RemoteFile $remotePath $tmpfile
95+
if (Get-Command -Name 'Expand-Archive' -ErrorAction Ignore) {
96+
# Use built-in commands where possible as they are cross-plat compatible
97+
Expand-Archive -Path $tmpfile -DestinationPath $korebuildPath
2198
}
22-
else
23-
{
24-
$exception = $_.Exception
25-
throw $exception
99+
else {
100+
# Fallback to old approach for old installations of PowerShell
101+
Add-Type -AssemblyName System.IO.Compression.FileSystem
102+
[System.IO.Compression.ZipFile]::ExtractToDirectory($tmpfile, $korebuildPath)
26103
}
27104
}
105+
catch {
106+
remove-item -Recurse -Force $korebuildPath -ErrorAction Ignore
107+
throw
108+
}
109+
finally {
110+
remove-item $tmpfile -ErrorAction Ignore
111+
}
28112
}
113+
114+
return $korebuildPath
29115
}
30116

31-
cd $PSScriptRoot
117+
function Join-Paths([string]$path, [string[]]$childPaths) {
118+
$childPaths | ForEach-Object { $path = Join-Path $path $_ }
119+
return $path
120+
}
32121

33-
$repoFolder = $PSScriptRoot
34-
$env:REPO_FOLDER = $repoFolder
122+
function Get-RemoteFile([string]$RemotePath, [string]$LocalPath) {
123+
if ($RemotePath -notlike 'http*') {
124+
Copy-Item $RemotePath $LocalPath
125+
return
126+
}
35127

36-
$koreBuildZip="https://github.com/aspnet/KoreBuild/archive/rel/2.0.0.zip"
37-
if ($env:KOREBUILD_ZIP)
38-
{
39-
$koreBuildZip=$env:KOREBUILD_ZIP
128+
$retries = 10
129+
while ($retries -gt 0) {
130+
$retries -= 1
131+
try {
132+
Invoke-WebRequest -UseBasicParsing -Uri $RemotePath -OutFile $LocalPath
133+
return
134+
}
135+
catch {
136+
Write-Verbose "Request failed. $retries retries remaining"
137+
}
138+
}
139+
140+
Write-Error "Download failed: '$RemotePath'."
40141
}
41142

42-
$buildFolder = ".build"
43-
$buildFile="$buildFolder\KoreBuild.ps1"
143+
#
144+
# Main
145+
#
146+
147+
# Load configuration or set defaults
44148

45-
if (!(Test-Path $buildFolder)) {
46-
Write-Host "Downloading KoreBuild from $koreBuildZip"
149+
$Path = Resolve-Path $Path
150+
if (!$ConfigFile) { $ConfigFile = Join-Path $Path 'korebuild.json' }
47151

48-
$tempFolder=$env:TEMP + "\KoreBuild-" + [guid]::NewGuid()
49-
New-Item -Path "$tempFolder" -Type directory | Out-Null
152+
if (Test-Path $ConfigFile) {
153+
try {
154+
$config = Get-Content -Raw -Encoding UTF8 -Path $ConfigFile | ConvertFrom-Json
155+
if ($config) {
156+
if (!($Channel) -and (Get-Member -Name 'channel' -InputObject $config)) { [string] $Channel = $config.channel }
157+
if (!($ToolsSource) -and (Get-Member -Name 'toolsSource' -InputObject $config)) { [string] $ToolsSource = $config.toolsSource}
158+
}
159+
} catch {
160+
Write-Warning "$ConfigFile could not be read. Its settings will be ignored."
161+
Write-Warning $Error[0]
162+
}
163+
}
50164

51-
$localZipFile="$tempFolder\korebuild.zip"
165+
if (!$DotNetHome) {
166+
$DotNetHome = if ($env:DOTNET_HOME) { $env:DOTNET_HOME } `
167+
elseif ($env:USERPROFILE) { Join-Path $env:USERPROFILE '.dotnet'} `
168+
elseif ($env:HOME) {Join-Path $env:HOME '.dotnet'}`
169+
else { Join-Path $PSScriptRoot '.dotnet'}
170+
}
52171

53-
DownloadWithRetry -url $koreBuildZip -downloadLocation $localZipFile -retries 6
172+
if (!$Channel) { $Channel = 'dev' }
173+
if (!$ToolsSource) { $ToolsSource = 'https://aspnetcore.blob.core.windows.net/buildtools' }
54174

55-
Add-Type -AssemblyName System.IO.Compression.FileSystem
56-
[System.IO.Compression.ZipFile]::ExtractToDirectory($localZipFile, $tempFolder)
175+
# Execute
57176

58-
New-Item -Path "$buildFolder" -Type directory | Out-Null
59-
copy-item "$tempFolder\**\build\*" $buildFolder -Recurse
177+
$korebuildPath = Get-KoreBuild
178+
Import-Module -Force -Scope Local (Join-Path $korebuildPath 'KoreBuild.psd1')
60179

61-
# Cleanup
62-
if (Test-Path $tempFolder) {
63-
Remove-Item -Recurse -Force $tempFolder
64-
}
180+
try {
181+
Install-Tools $ToolsSource $DotNetHome
182+
Invoke-RepositoryBuild $Path @MSBuildArgs
183+
}
184+
finally {
185+
Remove-Module 'KoreBuild' -ErrorAction Ignore
65186
}
66-
67-
&"$buildFile" @args

0 commit comments

Comments
 (0)