run.sh 11 KB

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