2017-07-26 02:32:22 +03:00
|
|
|
#!/usr/bin/env powershell
|
|
|
|
#requires -version 4
|
|
|
|
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
2017-09-20 23:20:43 +03:00
|
|
|
Executes KoreBuild commands.
|
2017-07-26 02:32:22 +03:00
|
|
|
|
|
|
|
.DESCRIPTION
|
2017-09-20 23:20:43 +03:00
|
|
|
Downloads korebuild if required. Then executes the KoreBuild command. To see available commands, execute with `-Command help`.
|
|
|
|
|
|
|
|
.PARAMETER Command
|
|
|
|
The KoreBuild command to run.
|
2017-07-26 02:32:22 +03:00
|
|
|
|
|
|
|
.PARAMETER Path
|
|
|
|
The folder to build. Defaults to the folder containing this script.
|
|
|
|
|
|
|
|
.PARAMETER Channel
|
|
|
|
The channel of KoreBuild to download. Overrides the value from the config file.
|
|
|
|
|
|
|
|
.PARAMETER DotNetHome
|
|
|
|
The directory where .NET Core tools will be stored.
|
|
|
|
|
|
|
|
.PARAMETER ToolsSource
|
|
|
|
The base url where build tools can be downloaded. Overrides the value from the config file.
|
|
|
|
|
|
|
|
.PARAMETER Update
|
|
|
|
Updates KoreBuild to the latest version even if a lock file is present.
|
|
|
|
|
|
|
|
.PARAMETER ConfigFile
|
2017-09-20 23:20:43 +03:00
|
|
|
The path to the configuration file that stores values. Defaults to korebuild.json.
|
2017-07-26 02:32:22 +03:00
|
|
|
|
2017-12-01 21:24:09 +03:00
|
|
|
.PARAMETER ToolsSourceSuffix
|
|
|
|
The Suffix to append to the end of the ToolsSource. Useful for query strings in blob stores.
|
|
|
|
|
2017-09-20 23:20:43 +03:00
|
|
|
.PARAMETER Arguments
|
|
|
|
Arguments to be passed to the command
|
2017-07-26 02:32:22 +03:00
|
|
|
|
|
|
|
.NOTES
|
|
|
|
This function will create a file $PSScriptRoot/korebuild-lock.txt. This lock file can be committed to source, but does not have to be.
|
|
|
|
When the lockfile is not present, KoreBuild will create one using latest available version from $Channel.
|
|
|
|
|
2017-09-20 23:20:43 +03:00
|
|
|
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
|
|
|
|
in the file are overridden by command line parameters.
|
2017-07-26 02:32:22 +03:00
|
|
|
|
|
|
|
.EXAMPLE
|
|
|
|
Example config file:
|
2017-09-20 23:20:43 +03:00
|
|
|
```json
|
|
|
|
{
|
|
|
|
"$schema": "https://raw.githubusercontent.com/aspnet/BuildTools/dev/tools/korebuild.schema.json",
|
|
|
|
"channel": "dev",
|
|
|
|
"toolsSource": "https://aspnetcore.blob.core.windows.net/buildtools"
|
|
|
|
}
|
2017-07-26 02:32:22 +03:00
|
|
|
```
|
|
|
|
#>
|
|
|
|
[CmdletBinding(PositionalBinding = $false)]
|
|
|
|
param(
|
2017-12-01 21:24:09 +03:00
|
|
|
[Parameter(Mandatory = $true, Position = 0)]
|
2017-09-20 23:20:43 +03:00
|
|
|
[string]$Command,
|
2017-07-26 02:32:22 +03:00
|
|
|
[string]$Path = $PSScriptRoot,
|
|
|
|
[Alias('c')]
|
|
|
|
[string]$Channel,
|
|
|
|
[Alias('d')]
|
|
|
|
[string]$DotNetHome,
|
|
|
|
[Alias('s')]
|
|
|
|
[string]$ToolsSource,
|
|
|
|
[Alias('u')]
|
|
|
|
[switch]$Update,
|
2017-09-20 23:20:43 +03:00
|
|
|
[string]$ConfigFile,
|
2017-12-01 21:24:09 +03:00
|
|
|
[string]$ToolsSourceSuffix,
|
2017-07-26 02:32:22 +03:00
|
|
|
[Parameter(ValueFromRemainingArguments = $true)]
|
2017-09-20 23:20:43 +03:00
|
|
|
[string[]]$Arguments
|
2017-07-26 02:32:22 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
Set-StrictMode -Version 2
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
|
|
|
|
#
|
|
|
|
# Functions
|
|
|
|
#
|
|
|
|
|
|
|
|
function Get-KoreBuild {
|
|
|
|
|
|
|
|
$lockFile = Join-Path $Path 'korebuild-lock.txt'
|
|
|
|
|
|
|
|
if (!(Test-Path $lockFile) -or $Update) {
|
2017-12-01 21:24:09 +03:00
|
|
|
Get-RemoteFile "$ToolsSource/korebuild/channels/$Channel/latest.txt" $lockFile $ToolsSourceSuffix
|
2017-07-26 02:32:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$version = Get-Content $lockFile | Where-Object { $_ -like 'version:*' } | Select-Object -first 1
|
|
|
|
if (!$version) {
|
|
|
|
Write-Error "Failed to parse version from $lockFile. Expected a line that begins with 'version:'"
|
|
|
|
}
|
|
|
|
$version = $version.TrimStart('version:').Trim()
|
|
|
|
$korebuildPath = Join-Paths $DotNetHome ('buildtools', 'korebuild', $version)
|
2016-03-08 07:55:00 +03:00
|
|
|
|
2017-07-26 02:32:22 +03:00
|
|
|
if (!(Test-Path $korebuildPath)) {
|
|
|
|
Write-Host -ForegroundColor Magenta "Downloading KoreBuild $version"
|
|
|
|
New-Item -ItemType Directory -Path $korebuildPath | Out-Null
|
|
|
|
$remotePath = "$ToolsSource/korebuild/artifacts/$version/korebuild.$version.zip"
|
|
|
|
|
|
|
|
try {
|
|
|
|
$tmpfile = Join-Path ([IO.Path]::GetTempPath()) "KoreBuild-$([guid]::NewGuid()).zip"
|
2017-12-01 21:24:09 +03:00
|
|
|
Get-RemoteFile $remotePath $tmpfile $ToolsSourceSuffix
|
2017-07-26 02:32:22 +03:00
|
|
|
if (Get-Command -Name 'Expand-Archive' -ErrorAction Ignore) {
|
|
|
|
# Use built-in commands where possible as they are cross-plat compatible
|
|
|
|
Expand-Archive -Path $tmpfile -DestinationPath $korebuildPath
|
2016-03-08 07:55:00 +03:00
|
|
|
}
|
2017-07-26 02:32:22 +03:00
|
|
|
else {
|
|
|
|
# Fallback to old approach for old installations of PowerShell
|
|
|
|
Add-Type -AssemblyName System.IO.Compression.FileSystem
|
|
|
|
[System.IO.Compression.ZipFile]::ExtractToDirectory($tmpfile, $korebuildPath)
|
2016-03-08 07:55:00 +03:00
|
|
|
}
|
|
|
|
}
|
2017-07-26 02:32:22 +03:00
|
|
|
catch {
|
|
|
|
Remove-Item -Recurse -Force $korebuildPath -ErrorAction Ignore
|
|
|
|
throw
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
Remove-Item $tmpfile -ErrorAction Ignore
|
|
|
|
}
|
2016-03-08 07:55:00 +03:00
|
|
|
}
|
2017-07-26 02:32:22 +03:00
|
|
|
|
|
|
|
return $korebuildPath
|
|
|
|
}
|
|
|
|
|
|
|
|
function Join-Paths([string]$path, [string[]]$childPaths) {
|
|
|
|
$childPaths | ForEach-Object { $path = Join-Path $path $_ }
|
|
|
|
return $path
|
2016-03-08 07:55:00 +03:00
|
|
|
}
|
|
|
|
|
2017-12-01 21:24:09 +03:00
|
|
|
function Get-RemoteFile([string]$RemotePath, [string]$LocalPath, [string]$RemoteSuffix) {
|
2017-07-26 02:32:22 +03:00
|
|
|
if ($RemotePath -notlike 'http*') {
|
|
|
|
Copy-Item $RemotePath $LocalPath
|
|
|
|
return
|
|
|
|
}
|
2016-02-27 23:51:11 +03:00
|
|
|
|
2017-07-26 02:32:22 +03:00
|
|
|
$retries = 10
|
|
|
|
while ($retries -gt 0) {
|
|
|
|
$retries -= 1
|
|
|
|
try {
|
2017-12-01 21:24:09 +03:00
|
|
|
Invoke-WebRequest -UseBasicParsing -Uri $($RemotePath + $RemoteSuffix) -OutFile $LocalPath
|
2017-07-26 02:32:22 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
Write-Verbose "Request failed. $retries retries remaining"
|
|
|
|
}
|
|
|
|
}
|
2016-02-27 23:51:11 +03:00
|
|
|
|
2017-07-26 02:32:22 +03:00
|
|
|
Write-Error "Download failed: '$RemotePath'."
|
2016-02-27 23:51:11 +03:00
|
|
|
}
|
|
|
|
|
2017-07-26 02:32:22 +03:00
|
|
|
#
|
|
|
|
# Main
|
|
|
|
#
|
2016-02-27 23:51:11 +03:00
|
|
|
|
2017-07-26 02:32:22 +03:00
|
|
|
# Load configuration or set defaults
|
2017-03-02 05:14:13 +03:00
|
|
|
|
2017-09-20 23:20:43 +03:00
|
|
|
$Path = Resolve-Path $Path
|
|
|
|
if (!$ConfigFile) { $ConfigFile = Join-Path $Path 'korebuild.json' }
|
|
|
|
|
2017-07-26 02:32:22 +03:00
|
|
|
if (Test-Path $ConfigFile) {
|
2017-09-20 23:20:43 +03:00
|
|
|
try {
|
|
|
|
$config = Get-Content -Raw -Encoding UTF8 -Path $ConfigFile | ConvertFrom-Json
|
|
|
|
if ($config) {
|
|
|
|
if (!($Channel) -and (Get-Member -Name 'channel' -InputObject $config)) { [string] $Channel = $config.channel }
|
|
|
|
if (!($ToolsSource) -and (Get-Member -Name 'toolsSource' -InputObject $config)) { [string] $ToolsSource = $config.toolsSource}
|
|
|
|
}
|
2017-12-01 21:24:09 +03:00
|
|
|
}
|
|
|
|
catch {
|
2017-09-20 23:20:43 +03:00
|
|
|
Write-Warning "$ConfigFile could not be read. Its settings will be ignored."
|
|
|
|
Write-Warning $Error[0]
|
|
|
|
}
|
2017-07-26 02:32:22 +03:00
|
|
|
}
|
2016-02-27 23:51:11 +03:00
|
|
|
|
2017-07-26 02:32:22 +03:00
|
|
|
if (!$DotNetHome) {
|
|
|
|
$DotNetHome = if ($env:DOTNET_HOME) { $env:DOTNET_HOME } `
|
|
|
|
elseif ($env:USERPROFILE) { Join-Path $env:USERPROFILE '.dotnet'} `
|
|
|
|
elseif ($env:HOME) {Join-Path $env:HOME '.dotnet'}`
|
|
|
|
else { Join-Path $PSScriptRoot '.dotnet'}
|
|
|
|
}
|
2017-03-02 05:14:13 +03:00
|
|
|
|
2017-07-26 02:32:22 +03:00
|
|
|
if (!$Channel) { $Channel = 'dev' }
|
|
|
|
if (!$ToolsSource) { $ToolsSource = 'https://aspnetcore.blob.core.windows.net/buildtools' }
|
2016-03-08 07:55:00 +03:00
|
|
|
|
2017-07-26 02:32:22 +03:00
|
|
|
# Execute
|
2017-03-02 05:14:13 +03:00
|
|
|
|
2017-07-26 02:32:22 +03:00
|
|
|
$korebuildPath = Get-KoreBuild
|
|
|
|
Import-Module -Force -Scope Local (Join-Path $korebuildPath 'KoreBuild.psd1')
|
2016-02-27 23:51:11 +03:00
|
|
|
|
2017-07-26 02:32:22 +03:00
|
|
|
try {
|
2017-09-20 23:20:43 +03:00
|
|
|
Set-KoreBuildSettings -ToolsSource $ToolsSource -DotNetHome $DotNetHome -RepoPath $Path -ConfigFile $ConfigFile
|
|
|
|
Invoke-KoreBuildCommand $Command @Arguments
|
2017-07-26 02:32:22 +03:00
|
|
|
}
|
|
|
|
finally {
|
|
|
|
Remove-Module 'KoreBuild' -ErrorAction Ignore
|
2016-02-27 23:51:11 +03:00
|
|
|
}
|