|
1 | 1 | #!/usr/bin/env bash |
2 | 2 |
|
3 | 3 | set -euo pipefail |
4 | | - |
5 | | -# |
6 | | -# variables |
7 | | -# |
8 | | - |
9 | | -RESET="\033[0m" |
10 | | -RED="\033[0;31m" |
11 | | -MAGENTA="\033[0;95m" |
12 | 4 | 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' |
196 | 5 |
|
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 "$@" |
0 commit comments