2
0

run.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. #
  4. # variables
  5. #
  6. RESET="\033[0m"
  7. RED="\033[0;31m"
  8. YELLOW="\033[0;33m"
  9. MAGENTA="\033[0;95m"
  10. DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
  11. verbose=false
  12. update=false
  13. reinstall=false
  14. repo_path="$DIR"
  15. lockfile_path=''
  16. channel=''
  17. tools_source=''
  18. ci=false
  19. package_version_props_url=''
  20. asset_root_url=''
  21. access_token_suffix=''
  22. restore_sources=''
  23. product_build_id=''
  24. msbuild_args=()
  25. #
  26. # Functions
  27. #
  28. __usage() {
  29. echo "Usage: $(basename "${BASH_SOURCE[0]}") command [options] [[--] <Arguments>...]"
  30. echo ""
  31. echo "Arguments:"
  32. echo " command The command to be run."
  33. echo " <Arguments>... Arguments passed to the command. Variable number of arguments allowed."
  34. echo ""
  35. echo "Options:"
  36. echo " --verbose Show verbose output."
  37. echo " -c|--channel <CHANNEL> The channel of KoreBuild to download. Overrides the value from the config file.."
  38. echo " --config-file <FILE> The path to the configuration file that stores values. Defaults to korebuild.json."
  39. echo " -d|--dotnet-home <DIR> The directory where .NET Core tools will be stored. Defaults to '\$DOTNET_HOME' or '\$HOME/.dotnet."
  40. echo " --path <PATH> The directory to build. Defaults to the directory containing the script."
  41. echo " --lockfile <PATH> The path to the korebuild-lock.txt file. Defaults to \$repo_path/korebuild-lock.txt"
  42. echo " -s|--tools-source|-ToolsSource <URL> The base url where build tools can be downloaded. Overrides the value from the config file."
  43. echo " --package-version-props-url <URL> The url of the package versions props path containing dependency versions."
  44. echo " --access-token <Token> The query string to append to any blob store access for PackageVersionPropsUrl, if any."
  45. echo " --restore-sources <Sources> Semi-colon delimited list of additional NuGet feeds to use as part of restore."
  46. echo " --product-build-id <ID> The product build ID for correlation with orchestrated builds."
  47. echo " -u|--update Update to the latest KoreBuild even if the lock file is present."
  48. echo " --reinstall Reinstall KoreBuild."
  49. echo " --ci Apply CI specific settings and environment variables."
  50. echo ""
  51. echo "Description:"
  52. 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."
  53. echo " When the lockfile is not present, KoreBuild will create one using latest available version from \$channel."
  54. if [[ "${1:-}" != '--no-exit' ]]; then
  55. exit 2
  56. fi
  57. }
  58. get_korebuild() {
  59. local version
  60. if [ ! -f "$lockfile_path" ] || [ "$update" = true ]; then
  61. __get_remote_file "$tools_source/korebuild/channels/$channel/latest.txt" "$lockfile_path"
  62. fi
  63. version="$(grep 'version:*' -m 1 "$lockfile_path")"
  64. if [[ "$version" == '' ]]; then
  65. __error "Failed to parse version from $lockfile_path. Expected a line that begins with 'version:'"
  66. return 1
  67. fi
  68. version="$(echo "${version#version:}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
  69. local korebuild_path="$DOTNET_HOME/buildtools/korebuild/$version"
  70. if [ "$reinstall" = true ] && [ -d "$korebuild_path" ]; then
  71. rm -rf "$korebuild_path"
  72. fi
  73. {
  74. if [ ! -d "$korebuild_path" ]; then
  75. mkdir -p "$korebuild_path"
  76. local remote_path="$tools_source/korebuild/artifacts/$version/korebuild.$version.zip"
  77. tmpfile="$(mktemp)"
  78. echo -e "${MAGENTA}Downloading KoreBuild ${version}${RESET}"
  79. if __get_remote_file "$remote_path" "$tmpfile"; then
  80. unzip -q -d "$korebuild_path" "$tmpfile"
  81. fi
  82. rm "$tmpfile" || true
  83. fi
  84. source "$korebuild_path/KoreBuild.sh"
  85. } || {
  86. if [ -d "$korebuild_path" ]; then
  87. echo "Cleaning up after failed installation"
  88. rm -rf "$korebuild_path" || true
  89. fi
  90. return 1
  91. }
  92. }
  93. __error() {
  94. echo -e "${RED}error: $*${RESET}" 1>&2
  95. }
  96. __warn() {
  97. echo -e "${YELLOW}warning: $*${RESET}"
  98. }
  99. __machine_has() {
  100. hash "$1" > /dev/null 2>&1
  101. return $?
  102. }
  103. __get_remote_file() {
  104. local remote_path=$1
  105. local local_path=$2
  106. if [[ "$remote_path" != 'http'* ]]; then
  107. cp "$remote_path" "$local_path"
  108. return 0
  109. fi
  110. local failed=false
  111. if __machine_has wget; then
  112. wget --tries 10 --quiet -O "$local_path" "$remote_path" || failed=true
  113. else
  114. failed=true
  115. fi
  116. if [ "$failed" = true ] && __machine_has curl; then
  117. failed=false
  118. curl --retry 10 -sSL -f --create-dirs -o "$local_path" "$remote_path" || failed=true
  119. fi
  120. if [ "$failed" = true ]; then
  121. __error "Download failed: $remote_path" 1>&2
  122. return 1
  123. fi
  124. }
  125. #
  126. # main
  127. #
  128. command="${1:-}"
  129. shift
  130. while [[ $# -gt 0 ]]; do
  131. case $1 in
  132. -\?|-h|--help)
  133. __usage --no-exit
  134. exit 0
  135. ;;
  136. -c|--channel|-Channel)
  137. shift
  138. channel="${1:-}"
  139. [ -z "$channel" ] && __error "Missing value for parameter --channel" && __usage
  140. ;;
  141. --config-file|-ConfigFile)
  142. shift
  143. config_file="${1:-}"
  144. [ -z "$config_file" ] && __error "Missing value for parameter --config-file" && __usage
  145. if [ ! -f "$config_file" ]; then
  146. __error "Invalid value for --config-file. $config_file does not exist."
  147. exit 1
  148. fi
  149. ;;
  150. -d|--dotnet-home|-DotNetHome)
  151. shift
  152. DOTNET_HOME="${1:-}"
  153. [ -z "$DOTNET_HOME" ] && __error "Missing value for parameter --dotnet-home" && __usage
  154. ;;
  155. --path|-Path)
  156. shift
  157. repo_path="${1:-}"
  158. [ -z "$repo_path" ] && __error "Missing value for parameter --path" && __usage
  159. ;;
  160. --[Ll]ock[Ff]ile)
  161. shift
  162. lockfile_path="${1:-}"
  163. [ -z "$lockfile_path" ] && __error "Missing value for parameter --lockfile" && __usage
  164. ;;
  165. -s|--tools-source|-ToolsSource)
  166. shift
  167. tools_source="${1:-}"
  168. [ -z "$tools_source" ] && __error "Missing value for parameter --tools-source" && __usage
  169. ;;
  170. --package-version-props-url|-PackageVersionPropsUrl)
  171. shift
  172. # This parameter can be an empty string, but it should be set
  173. [ -z "${1+x}" ] && __error "Missing value for parameter --package-version-props-url" && __usage
  174. package_version_props_url="$1"
  175. ;;
  176. --access-token-suffix|-AccessTokenSuffix)
  177. shift
  178. # This parameter can be an empty string, but it should be set
  179. [ -z "${1+x}" ] && __error "Missing value for parameter --access-token-suffix" && __usage
  180. access_token_suffix="$1"
  181. ;;
  182. --restore-sources|-RestoreSources)
  183. shift
  184. # This parameter can be an empty string, but it should be set
  185. [ -z "${1+x}" ] && __error "Missing value for parameter --restore-sources" && __usage
  186. restore_sources="$1"
  187. ;;
  188. --asset-root-url|-AssetRootUrl)
  189. shift
  190. # This parameter can be an empty string, but it should be set
  191. [ -z "${1+x}" ] && __error "Missing value for parameter --asset-root-url" && __usage
  192. asset_root_url="$1"
  193. ;;
  194. --product-build-id|-ProductBuildId)
  195. shift
  196. # This parameter can be an empty string, but it should be set
  197. [ -z "${1+x}" ] && __error "Missing value for parameter --product-build-id" && __usage
  198. product_build_id="$1"
  199. ;;
  200. -u|--update|-Update)
  201. update=true
  202. ;;
  203. --reinstall|-Reinstall)
  204. reinstall=true
  205. ;;
  206. --ci|-[Cc][Ii])
  207. ci=true
  208. if [[ -z "${DOTNET_HOME:-}" ]]; then
  209. DOTNET_HOME="$DIR/.dotnet"
  210. fi
  211. ;;
  212. --verbose|-Verbose)
  213. verbose=true
  214. ;;
  215. *)
  216. msbuild_args[${#msbuild_args[*]}]="$1"
  217. ;;
  218. esac
  219. shift
  220. done
  221. if ! __machine_has unzip; then
  222. __error 'Missing required command: unzip'
  223. exit 1
  224. fi
  225. if ! __machine_has curl && ! __machine_has wget; then
  226. __error 'Missing required command. Either wget or curl is required.'
  227. exit 1
  228. fi
  229. [ -z "${config_file:-}" ] && config_file="$repo_path/korebuild.json"
  230. if [ -f "$config_file" ]; then
  231. if __machine_has jq ; then
  232. if jq '.' "$config_file" >/dev/null ; then
  233. config_channel="$(jq -r 'select(.channel!=null) | .channel' "$config_file")"
  234. config_tools_source="$(jq -r 'select(.toolsSource!=null) | .toolsSource' "$config_file")"
  235. else
  236. __error "$config_file is invalid JSON. Its settings will be ignored."
  237. exit 1
  238. fi
  239. elif __machine_has python ; then
  240. if python -c "import json,codecs;obj=json.load(codecs.open('$config_file', 'r', 'utf-8-sig'))" >/dev/null ; then
  241. 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 '')")"
  242. 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 '')")"
  243. else
  244. __error "$config_file is invalid JSON. Its settings will be ignored."
  245. exit 1
  246. fi
  247. else
  248. __error 'Missing required command: jq or python. Could not parse the JSON file. Its settings will be ignored.'
  249. exit 1
  250. fi
  251. [ ! -z "${config_channel:-}" ] && channel="$config_channel"
  252. [ ! -z "${config_tools_source:-}" ] && tools_source="$config_tools_source"
  253. fi
  254. [ -z "${DOTNET_HOME:-}" ] && DOTNET_HOME="$HOME/.dotnet"
  255. if [ ! -z "$package_version_props_url" ]; then
  256. intermediate_dir="$repo_path/obj"
  257. props_file_path="$intermediate_dir/external-dependencies.props"
  258. mkdir -p "$intermediate_dir"
  259. __get_remote_file "$package_version_props_url" "$props_file_path"
  260. msbuild_args[${#msbuild_args[*]}]="-p:DotNetPackageVersionPropsPath=$props_file_path"
  261. fi
  262. if [ ! -z "$restore_sources" ]; then
  263. msbuild_args[${#msbuild_args[*]}]="-p:DotNetAdditionalRestoreSources=$restore_sources"
  264. fi
  265. if [ ! -z "$asset_root_url" ]; then
  266. msbuild_args[${#msbuild_args[*]}]="-p:DotNetAssetRootUrl=$asset_root_url"
  267. fi
  268. if [ ! -z "$access_token_suffix" ]; then
  269. msbuild_args[${#msbuild_args[*]}]="-p:DotNetAssetRootAccessTokenSuffix=$access_token_suffix"
  270. fi
  271. if [ ! -z "$product_build_id" ]; then
  272. msbuild_args[${#msbuild_args[*]}]="-p:DotNetProductBuildId=$product_build_id"
  273. fi
  274. [ -z "$lockfile_path" ] && lockfile_path="$repo_path/korebuild-lock.txt"
  275. [ -z "$channel" ] && channel='master'
  276. [ -z "$tools_source" ] && tools_source='https://aspnetcore.blob.core.windows.net/buildtools'
  277. get_korebuild
  278. set_korebuildsettings "$tools_source" "$DOTNET_HOME" "$repo_path" "$config_file" "$ci"
  279. # This incantation avoids unbound variable issues if msbuild_args is empty
  280. # https://stackoverflow.com/questions/7577052/bash-empty-array-expansion-with-set-u
  281. invoke_korebuild_command "$command" ${msbuild_args[@]+"${msbuild_args[@]}"}