Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e67a301

Browse files
ryanbrandenburgSteveSandersonMS
authored andcommittedSep 22, 2017
Update bootstrappers
1 parent d51bef1 commit e67a301

File tree

5 files changed

+264
-216
lines changed

5 files changed

+264
-216
lines changed
 

‎build.cmd

Lines changed: 1 addition & 1 deletion
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 = '';& '%~dp0run.ps1' default-build %*; exit $LASTEXITCODE"

‎build.sh

Lines changed: 3 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -1,199 +1,8 @@
11
#!/usr/bin/env bash
22

33
set -euo pipefail
4-
5-
#
6-
# variables
7-
#
8-
9-
RESET="\033[0m"
10-
RED="\033[0;31m"
11-
MAGENTA="\033[0;95m"
124
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
13-
[ -z "${DOTNET_HOME:-}" ] && DOTNET_HOME="$HOME/.dotnet"
14-
config_file="$DIR/version.xml"
15-
verbose=false
16-
update=false
17-
repo_path="$DIR"
18-
channel=''
19-
tools_source=''
20-
21-
#
22-
# Functions
23-
#
24-
__usage() {
25-
echo "Usage: $(basename "${BASH_SOURCE[0]}") [options] [[--] <MSBUILD_ARG>...]"
26-
echo ""
27-
echo "Arguments:"
28-
echo " <MSBUILD_ARG>... Arguments passed to MSBuild. Variable number of arguments allowed."
29-
echo ""
30-
echo "Options:"
31-
echo " --verbose Show verbose output."
32-
echo " -c|--channel <CHANNEL> The channel of KoreBuild to download. Overrides the value from the config file.."
33-
echo " --config-file <FILE> TThe path to the configuration file that stores values. Defaults to version.xml."
34-
echo " -d|--dotnet-home <DIR> The directory where .NET Core tools will be stored. Defaults to '\$DOTNET_HOME' or '\$HOME/.dotnet."
35-
echo " --path <PATH> The directory to build. Defaults to the directory containing the script."
36-
echo " -s|--tools-source <URL> The base url where build tools can be downloaded. Overrides the value from the config file."
37-
echo " -u|--update Update to the latest KoreBuild even if the lock file is present."
38-
echo ""
39-
echo "Description:"
40-
echo " This function will create a file \$DIR/korebuild-lock.txt. This lock file can be committed to source, but does not have to be."
41-
echo " When the lockfile is not present, KoreBuild will create one using latest available version from \$channel."
42-
43-
if [[ "${1:-}" != '--no-exit' ]]; then
44-
exit 2
45-
fi
46-
}
47-
48-
get_korebuild() {
49-
local version
50-
local lock_file="$repo_path/korebuild-lock.txt"
51-
if [ ! -f "$lock_file" ] || [ "$update" = true ]; then
52-
__get_remote_file "$tools_source/korebuild/channels/$channel/latest.txt" "$lock_file"
53-
fi
54-
version="$(grep 'version:*' -m 1 "$lock_file")"
55-
if [[ "$version" == '' ]]; then
56-
__error "Failed to parse version from $lock_file. Expected a line that begins with 'version:'"
57-
return 1
58-
fi
59-
version="$(echo "${version#version:}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
60-
local korebuild_path="$DOTNET_HOME/buildtools/korebuild/$version"
61-
62-
{
63-
if [ ! -d "$korebuild_path" ]; then
64-
mkdir -p "$korebuild_path"
65-
local remote_path="$tools_source/korebuild/artifacts/$version/korebuild.$version.zip"
66-
tmpfile="$(mktemp)"
67-
echo -e "${MAGENTA}Downloading KoreBuild ${version}${RESET}"
68-
if __get_remote_file "$remote_path" "$tmpfile"; then
69-
unzip -q -d "$korebuild_path" "$tmpfile"
70-
fi
71-
rm "$tmpfile" || true
72-
fi
73-
74-
source "$korebuild_path/KoreBuild.sh"
75-
} || {
76-
if [ -d "$korebuild_path" ]; then
77-
echo "Cleaning up after failed installation"
78-
rm -rf "$korebuild_path" || true
79-
fi
80-
return 1
81-
}
82-
}
83-
84-
__error() {
85-
echo -e "${RED}$*${RESET}" 1>&2
86-
}
87-
88-
__machine_has() {
89-
hash "$1" > /dev/null 2>&1
90-
return $?
91-
}
92-
93-
__get_remote_file() {
94-
local remote_path=$1
95-
local local_path=$2
96-
97-
if [[ "$remote_path" != 'http'* ]]; then
98-
cp "$remote_path" "$local_path"
99-
return 0
100-
fi
101-
102-
local failed=false
103-
if __machine_has wget; then
104-
wget --tries 10 --quiet -O "$local_path" "$remote_path" || failed=true
105-
else
106-
failed=true
107-
fi
108-
109-
if [ "$failed" = true ] && __machine_has curl; then
110-
failed=false
111-
curl --retry 10 -sSL -f --create-dirs -o "$local_path" "$remote_path" || failed=true
112-
fi
113-
114-
if [ "$failed" = true ]; then
115-
__error "Download failed: $remote_path" 1>&2
116-
return 1
117-
fi
118-
}
119-
120-
__read_dom () { local IFS=\> ; read -r -d \< ENTITY CONTENT ;}
121-
122-
#
123-
# main
124-
#
125-
126-
while [[ $# -gt 0 ]]; do
127-
case $1 in
128-
-\?|-h|--help)
129-
__usage --no-exit
130-
exit 0
131-
;;
132-
-c|--channel|-Channel)
133-
shift
134-
channel="${1:-}"
135-
[ -z "$channel" ] && __usage
136-
;;
137-
--config-file|-ConfigFile)
138-
shift
139-
config_file="${1:-}"
140-
[ -z "$config_file" ] && __usage
141-
;;
142-
-d|--dotnet-home|-DotNetHome)
143-
shift
144-
DOTNET_HOME="${1:-}"
145-
[ -z "$DOTNET_HOME" ] && __usage
146-
;;
147-
--path|-Path)
148-
shift
149-
repo_path="${1:-}"
150-
[ -z "$repo_path" ] && __usage
151-
;;
152-
-s|--tools-source|-ToolsSource)
153-
shift
154-
tools_source="${1:-}"
155-
[ -z "$tools_source" ] && __usage
156-
;;
157-
-u|--update|-Update)
158-
update=true
159-
;;
160-
--verbose|-Verbose)
161-
verbose=true
162-
;;
163-
--)
164-
shift
165-
break
166-
;;
167-
*)
168-
break
169-
;;
170-
esac
171-
shift
172-
done
173-
174-
if ! __machine_has unzip; then
175-
__error 'Missing required command: unzip'
176-
exit 1
177-
fi
178-
179-
if ! __machine_has curl && ! __machine_has wget; then
180-
__error 'Missing required command. Either wget or curl is required.'
181-
exit 1
182-
fi
183-
184-
if [ -f "$config_file" ]; then
185-
comment=false
186-
while __read_dom; do
187-
if [ "$comment" = true ]; then [[ $CONTENT == *'-->'* ]] && comment=false ; continue; fi
188-
if [[ $ENTITY == '!--'* ]]; then comment=true; continue; fi
189-
if [ -z "$channel" ] && [[ $ENTITY == "KoreBuildChannel" ]]; then channel=$CONTENT; fi
190-
if [ -z "$tools_source" ] && [[ $ENTITY == "KoreBuildToolsSource" ]]; then tools_source=$CONTENT; fi
191-
done < "$config_file"
192-
fi
193-
194-
[ -z "$channel" ] && channel='dev'
195-
[ -z "$tools_source" ] && tools_source='https://aspnetcore.blob.core.windows.net/buildtools'
1965

197-
get_korebuild
198-
install_tools "$tools_source" "$DOTNET_HOME"
199-
invoke_repository_build "$repo_path" "$@"
6+
# Call "sync" between "chmod" and execution to prevent "text file busy" error in Docker (aufs)
7+
chmod +x "$DIR/run.sh"; sync
8+
"$DIR/run.sh" default-build "$@"

‎run.cmd

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

‎build.ps1 renamed to ‎run.ps1

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33

44
<#
55
.SYNOPSIS
6-
Build this repository
6+
Executes KoreBuild commands.
77
88
.DESCRIPTION
9-
Downloads korebuild if required. Then builds the repository.
9+
Downloads korebuild if required. Then executes the KoreBuild command. To see available commands, execute with `-Command help`.
10+
11+
.PARAMETER Command
12+
The KoreBuild command to run.
1013
1114
.PARAMETER Path
1215
The folder to build. Defaults to the folder containing this script.
@@ -24,31 +27,32 @@ The base url where build tools can be downloaded. Overrides the value from the c
2427
Updates KoreBuild to the latest version even if a lock file is present.
2528
2629
.PARAMETER ConfigFile
27-
The path to the configuration file that stores values. Defaults to version.xml.
30+
The path to the configuration file that stores values. Defaults to korebuild.json.
2831
29-
.PARAMETER MSBuildArgs
30-
Arguments to be passed to MSBuild
32+
.PARAMETER Arguments
33+
Arguments to be passed to the command
3134
3235
.NOTES
3336
This function will create a file $PSScriptRoot/korebuild-lock.txt. This lock file can be committed to source, but does not have to be.
3437
When the lockfile is not present, KoreBuild will create one using latest available version from $Channel.
3538
36-
The $ConfigFile is expected to be an XML file. It is optional, and the configuration values in it are optional as well.
39+
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
40+
in the file are overridden by command line parameters.
3741
3842
.EXAMPLE
3943
Example config file:
40-
```xml
41-
<!-- version.xml -->
42-
<Project>
43-
<PropertyGroup>
44-
<KoreBuildChannel>dev</KoreBuildChannel>
45-
<KoreBuildToolsSource>https://aspnetcore.blob.core.windows.net/buildtools</KoreBuildToolsSource>
46-
</PropertyGroup>
47-
</Project>
44+
```json
45+
{
46+
"$schema": "https://github.com/raw/aspnet/BuildTools/dev/tools/korebuild.schema.json",
47+
"channel": "dev",
48+
"toolsSource": "https://aspnetcore.blob.core.windows.net/buildtools"
49+
}
4850
```
4951
#>
5052
[CmdletBinding(PositionalBinding = $false)]
5153
param(
54+
[Parameter(Mandatory=$true, Position = 0)]
55+
[string]$Command,
5256
[string]$Path = $PSScriptRoot,
5357
[Alias('c')]
5458
[string]$Channel,
@@ -58,9 +62,9 @@ param(
5862
[string]$ToolsSource,
5963
[Alias('u')]
6064
[switch]$Update,
61-
[string]$ConfigFile = (Join-Path $PSScriptRoot 'version.xml'),
65+
[string]$ConfigFile,
6266
[Parameter(ValueFromRemainingArguments = $true)]
63-
[string[]]$MSBuildArgs
67+
[string[]]$Arguments
6468
)
6569

6670
Set-StrictMode -Version 2
@@ -147,10 +151,20 @@ function Get-RemoteFile([string]$RemotePath, [string]$LocalPath) {
147151

148152
# Load configuration or set defaults
149153

154+
$Path = Resolve-Path $Path
155+
if (!$ConfigFile) { $ConfigFile = Join-Path $Path 'korebuild.json' }
156+
150157
if (Test-Path $ConfigFile) {
151-
[xml] $config = Get-Content $ConfigFile
152-
if (!($Channel)) { [string] $Channel = Select-Xml -Xml $config -XPath '/Project/PropertyGroup/KoreBuildChannel' }
153-
if (!($ToolsSource)) { [string] $ToolsSource = Select-Xml -Xml $config -XPath '/Project/PropertyGroup/KoreBuildToolsSource' }
158+
try {
159+
$config = Get-Content -Raw -Encoding UTF8 -Path $ConfigFile | ConvertFrom-Json
160+
if ($config) {
161+
if (!($Channel) -and (Get-Member -Name 'channel' -InputObject $config)) { [string] $Channel = $config.channel }
162+
if (!($ToolsSource) -and (Get-Member -Name 'toolsSource' -InputObject $config)) { [string] $ToolsSource = $config.toolsSource}
163+
}
164+
} catch {
165+
Write-Warning "$ConfigFile could not be read. Its settings will be ignored."
166+
Write-Warning $Error[0]
167+
}
154168
}
155169

156170
if (!$DotNetHome) {
@@ -169,8 +183,8 @@ $korebuildPath = Get-KoreBuild
169183
Import-Module -Force -Scope Local (Join-Path $korebuildPath 'KoreBuild.psd1')
170184

171185
try {
172-
Install-Tools $ToolsSource $DotNetHome
173-
Invoke-RepositoryBuild $Path @MSBuildArgs
186+
Set-KoreBuildSettings -ToolsSource $ToolsSource -DotNetHome $DotNetHome -RepoPath $Path -ConfigFile $ConfigFile
187+
Invoke-KoreBuildCommand $Command @Arguments
174188
}
175189
finally {
176190
Remove-Module 'KoreBuild' -ErrorAction Ignore

‎run.sh

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
#
6+
# variables
7+
#
8+
9+
RESET="\033[0m"
10+
RED="\033[0;31m"
11+
YELLOW="\033[0;33m"
12+
MAGENTA="\033[0;95m"
13+
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
14+
[ -z "${DOTNET_HOME:-}" ] && DOTNET_HOME="$HOME/.dotnet"
15+
verbose=false
16+
update=false
17+
repo_path="$DIR"
18+
channel=''
19+
tools_source=''
20+
21+
#
22+
# Functions
23+
#
24+
__usage() {
25+
echo "Usage: $(basename "${BASH_SOURCE[0]}") command [options] [[--] <Arguments>...]"
26+
echo ""
27+
echo "Arguments:"
28+
echo " command The command to be run."
29+
echo " <Arguments>... Arguments passed to the command. Variable number of arguments allowed."
30+
echo ""
31+
echo "Options:"
32+
echo " --verbose Show verbose output."
33+
echo " -c|--channel <CHANNEL> The channel of KoreBuild to download. Overrides the value from the config file.."
34+
echo " --config-file <FILE> The path to the configuration file that stores values. Defaults to korebuild.json."
35+
echo " -d|--dotnet-home <DIR> The directory where .NET Core tools will be stored. Defaults to '\$DOTNET_HOME' or '\$HOME/.dotnet."
36+
echo " --path <PATH> The directory to build. Defaults to the directory containing the script."
37+
echo " -s|--tools-source|-ToolsSource <URL> The base url where build tools can be downloaded. Overrides the value from the config file."
38+
echo " -u|--update Update to the latest KoreBuild even if the lock file is present."
39+
echo ""
40+
echo "Description:"
41+
echo " This function will create a file \$DIR/korebuild-lock.txt. This lock file can be committed to source, but does not have to be."
42+
echo " When the lockfile is not present, KoreBuild will create one using latest available version from \$channel."
43+
44+
if [[ "${1:-}" != '--no-exit' ]]; then
45+
exit 2
46+
fi
47+
}
48+
49+
get_korebuild() {
50+
local version
51+
local lock_file="$repo_path/korebuild-lock.txt"
52+
if [ ! -f "$lock_file" ] || [ "$update" = true ]; then
53+
__get_remote_file "$tools_source/korebuild/channels/$channel/latest.txt" "$lock_file"
54+
fi
55+
version="$(grep 'version:*' -m 1 "$lock_file")"
56+
if [[ "$version" == '' ]]; then
57+
__error "Failed to parse version from $lock_file. Expected a line that begins with 'version:'"
58+
return 1
59+
fi
60+
version="$(echo "${version#version:}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
61+
local korebuild_path="$DOTNET_HOME/buildtools/korebuild/$version"
62+
63+
{
64+
if [ ! -d "$korebuild_path" ]; then
65+
mkdir -p "$korebuild_path"
66+
local remote_path="$tools_source/korebuild/artifacts/$version/korebuild.$version.zip"
67+
tmpfile="$(mktemp)"
68+
echo -e "${MAGENTA}Downloading KoreBuild ${version}${RESET}"
69+
if __get_remote_file "$remote_path" "$tmpfile"; then
70+
unzip -q -d "$korebuild_path" "$tmpfile"
71+
fi
72+
rm "$tmpfile" || true
73+
fi
74+
75+
source "$korebuild_path/KoreBuild.sh"
76+
} || {
77+
if [ -d "$korebuild_path" ]; then
78+
echo "Cleaning up after failed installation"
79+
rm -rf "$korebuild_path" || true
80+
fi
81+
return 1
82+
}
83+
}
84+
85+
__error() {
86+
echo -e "${RED}error: $*${RESET}" 1>&2
87+
}
88+
89+
__warn() {
90+
echo -e "${YELLOW}warning: $*${RESET}"
91+
}
92+
93+
__machine_has() {
94+
hash "$1" > /dev/null 2>&1
95+
return $?
96+
}
97+
98+
__get_remote_file() {
99+
local remote_path=$1
100+
local local_path=$2
101+
102+
if [[ "$remote_path" != 'http'* ]]; then
103+
cp "$remote_path" "$local_path"
104+
return 0
105+
fi
106+
107+
local failed=false
108+
if __machine_has wget; then
109+
wget --tries 10 --quiet -O "$local_path" "$remote_path" || failed=true
110+
else
111+
failed=true
112+
fi
113+
114+
if [ "$failed" = true ] && __machine_has curl; then
115+
failed=false
116+
curl --retry 10 -sSL -f --create-dirs -o "$local_path" "$remote_path" || failed=true
117+
fi
118+
119+
if [ "$failed" = true ]; then
120+
__error "Download failed: $remote_path" 1>&2
121+
return 1
122+
fi
123+
}
124+
125+
#
126+
# main
127+
#
128+
129+
command="${1:-}"
130+
shift
131+
132+
while [[ $# -gt 0 ]]; do
133+
case $1 in
134+
-\?|-h|--help)
135+
__usage --no-exit
136+
exit 0
137+
;;
138+
-c|--channel|-Channel)
139+
shift
140+
channel="${1:-}"
141+
[ -z "$channel" ] && __usage
142+
;;
143+
--config-file|-ConfigFile)
144+
shift
145+
config_file="${1:-}"
146+
[ -z "$config_file" ] && __usage
147+
if [ ! -f "$config_file" ]; then
148+
__error "Invalid value for --config-file. $config_file does not exist."
149+
exit 1
150+
fi
151+
;;
152+
-d|--dotnet-home|-DotNetHome)
153+
shift
154+
DOTNET_HOME="${1:-}"
155+
[ -z "$DOTNET_HOME" ] && __usage
156+
;;
157+
--path|-Path)
158+
shift
159+
repo_path="${1:-}"
160+
[ -z "$repo_path" ] && __usage
161+
;;
162+
-s|--tools-source|-ToolsSource)
163+
shift
164+
tools_source="${1:-}"
165+
[ -z "$tools_source" ] && __usage
166+
;;
167+
-u|--update|-Update)
168+
update=true
169+
;;
170+
--verbose|-Verbose)
171+
verbose=true
172+
;;
173+
--)
174+
shift
175+
break
176+
;;
177+
*)
178+
break
179+
;;
180+
esac
181+
shift
182+
done
183+
184+
if ! __machine_has unzip; then
185+
__error 'Missing required command: unzip'
186+
exit 1
187+
fi
188+
189+
if ! __machine_has curl && ! __machine_has wget; then
190+
__error 'Missing required command. Either wget or curl is required.'
191+
exit 1
192+
fi
193+
194+
[ -z "${config_file:-}" ] && config_file="$repo_path/korebuild.json"
195+
if [ -f "$config_file" ]; then
196+
if __machine_has jq ; then
197+
if jq '.' "$config_file" >/dev/null ; then
198+
config_channel="$(jq -r 'select(.channel!=null) | .channel' "$config_file")"
199+
config_tools_source="$(jq -r 'select(.toolsSource!=null) | .toolsSource' "$config_file")"
200+
else
201+
__warn "$config_file is invalid JSON. Its settings will be ignored."
202+
fi
203+
elif __machine_has python ; then
204+
if python -c "import json,codecs;obj=json.load(codecs.open('$config_file', 'r', 'utf-8-sig'))" >/dev/null ; then
205+
config_channel="$(python -c "import json,codecs;obj=json.load(codecs.open('$config_file', 'r', 'utf-8-sig'));print(obj['channel'] if 'channel' in obj else '')")"
206+
config_tools_source="$(python -c "import json,codecs;obj=json.load(codecs.open('$config_file', 'r', 'utf-8-sig'));print(obj['toolsSource'] if 'toolsSource' in obj else '')")"
207+
else
208+
__warn "$config_file is invalid JSON. Its settings will be ignored."
209+
fi
210+
else
211+
__warn 'Missing required command: jq or pyton. Could not parse the JSON file. Its settings will be ignored.'
212+
fi
213+
214+
[ ! -z "${config_channel:-}" ] && channel="$config_channel"
215+
[ ! -z "${config_tools_source:-}" ] && tools_source="$config_tools_source"
216+
fi
217+
218+
[ -z "$channel" ] && channel='dev'
219+
[ -z "$tools_source" ] && tools_source='https://aspnetcore.blob.core.windows.net/buildtools'
220+
221+
get_korebuild
222+
set_korebuildsettings "$tools_source" "$DOTNET_HOME" "$repo_path" "$config_file"
223+
invoke_korebuild_command "$command" "$@"

0 commit comments

Comments
 (0)
This repository has been archived.