common-library.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/usr/bin/env bash
  2. function GetNativeInstallDirectory {
  3. local install_dir
  4. if [[ -z $NETCOREENG_INSTALL_DIRECTORY ]]; then
  5. install_dir=$HOME/.netcoreeng/native/
  6. else
  7. install_dir=$NETCOREENG_INSTALL_DIRECTORY
  8. fi
  9. echo $install_dir
  10. return 0
  11. }
  12. function GetTempDirectory {
  13. echo $(GetNativeInstallDirectory)temp/
  14. return 0
  15. }
  16. function ExpandZip {
  17. local zip_path=$1
  18. local output_directory=$2
  19. local force=${3:-false}
  20. echo "Extracting $zip_path to $output_directory"
  21. if [[ -d $output_directory ]] && [[ $force = false ]]; then
  22. echo "Directory '$output_directory' already exists, skipping extract"
  23. return 0
  24. fi
  25. if [[ -d $output_directory ]]; then
  26. echo "'Force flag enabled, but '$output_directory' exists. Removing directory"
  27. rm -rf $output_directory
  28. if [[ $? != 0 ]]; then
  29. Write-PipelineTelemetryError -category 'NativeToolsBootstrap' "Unable to remove '$output_directory'"
  30. return 1
  31. fi
  32. fi
  33. echo "Creating directory: '$output_directory'"
  34. mkdir -p $output_directory
  35. echo "Extracting archive"
  36. tar -xf $zip_path -C $output_directory
  37. if [[ $? != 0 ]]; then
  38. Write-PipelineTelemetryError -category 'NativeToolsBootstrap' "Unable to extract '$zip_path'"
  39. return 1
  40. fi
  41. return 0
  42. }
  43. function GetCurrentOS {
  44. local unameOut="$(uname -s)"
  45. case $unameOut in
  46. Linux*) echo "Linux";;
  47. Darwin*) echo "MacOS";;
  48. esac
  49. return 0
  50. }
  51. function GetFile {
  52. local uri=$1
  53. local path=$2
  54. local force=${3:-false}
  55. local download_retries=${4:-5}
  56. local retry_wait_time_seconds=${5:-30}
  57. if [[ -f $path ]]; then
  58. if [[ $force = false ]]; then
  59. echo "File '$path' already exists. Skipping download"
  60. return 0
  61. else
  62. rm -rf $path
  63. fi
  64. fi
  65. if [[ -f $uri ]]; then
  66. echo "'$uri' is a file path, copying file to '$path'"
  67. cp $uri $path
  68. return $?
  69. fi
  70. echo "Downloading $uri"
  71. # Use curl if available, otherwise use wget
  72. if command -v curl > /dev/null; then
  73. curl "$uri" -sSL --retry $download_retries --retry-delay $retry_wait_time_seconds --create-dirs -o "$path" --fail
  74. else
  75. wget -q -O "$path" "$uri" --tries="$download_retries"
  76. fi
  77. return $?
  78. }
  79. function GetTempPathFileName {
  80. local path=$1
  81. local temp_dir=$(GetTempDirectory)
  82. local temp_file_name=$(basename $path)
  83. echo $temp_dir$temp_file_name
  84. return 0
  85. }
  86. function DownloadAndExtract {
  87. local uri=$1
  88. local installDir=$2
  89. local force=${3:-false}
  90. local download_retries=${4:-5}
  91. local retry_wait_time_seconds=${5:-30}
  92. local temp_tool_path=$(GetTempPathFileName $uri)
  93. echo "downloading to: $temp_tool_path"
  94. # Download file
  95. GetFile "$uri" "$temp_tool_path" $force $download_retries $retry_wait_time_seconds
  96. if [[ $? != 0 ]]; then
  97. Write-PipelineTelemetryError -category 'NativeToolsBootstrap' "Failed to download '$uri' to '$temp_tool_path'."
  98. return 1
  99. fi
  100. # Extract File
  101. echo "extracting from $temp_tool_path to $installDir"
  102. ExpandZip "$temp_tool_path" "$installDir" $force $download_retries $retry_wait_time_seconds
  103. if [[ $? != 0 ]]; then
  104. Write-PipelineTelemetryError -category 'NativeToolsBootstrap' "Failed to extract '$temp_tool_path' to '$installDir'."
  105. return 1
  106. fi
  107. return 0
  108. }
  109. function NewScriptShim {
  110. local shimpath=$1
  111. local tool_file_path=$2
  112. local force=${3:-false}
  113. echo "Generating '$shimpath' shim"
  114. if [[ -f $shimpath ]]; then
  115. if [[ $force = false ]]; then
  116. echo "File '$shimpath' already exists." >&2
  117. return 1
  118. else
  119. rm -rf $shimpath
  120. fi
  121. fi
  122. if [[ ! -f $tool_file_path ]]; then
  123. # try to see if the path is lower cased
  124. tool_file_path="$(echo $tool_file_path | tr "[:upper:]" "[:lower:]")"
  125. if [[ ! -f $tool_file_path ]]; then
  126. Write-PipelineTelemetryError -category 'NativeToolsBootstrap' "Specified tool file path:'$tool_file_path' does not exist"
  127. return 1
  128. fi
  129. fi
  130. local shim_contents=$'#!/usr/bin/env bash\n'
  131. shim_contents+="SHIMARGS="$'$1\n'
  132. shim_contents+="$tool_file_path"$' $SHIMARGS\n'
  133. # Write shim file
  134. echo "$shim_contents" > $shimpath
  135. chmod +x $shimpath
  136. echo "Finished generating shim '$shimpath'"
  137. return $?
  138. }