|
| 1 | +# shellcheck shell=bash disable=SC2154,SC2148 |
| 2 | +need_xcodeCLT() { |
| 3 | + if [[ "${OSTYPE}" != [Dd][Aa][Rr][Ww][Ii][Nn]* ]]; then |
| 4 | + echo "false" |
| 5 | + else |
| 6 | + local xcode_dir |
| 7 | + xcode_dir="$(/usr/bin/xcode-select -print-path 2>/dev/null)" || true |
| 8 | + if [[ ! -d "${xcode_dir}" || ! -x "${xcode_dir}/usr/bin/make" ]]; then |
| 9 | + echo "true" |
| 10 | + else |
| 11 | + echo "false" |
| 12 | + fi |
| 13 | + fi |
| 14 | +} |
| 15 | + |
| 16 | +xcode_clt_install () { |
| 17 | + app_store_label="$(softwareupdate -l | grep -B 1 -E "Command Line (Developer|Tools)" | awk -F"*" '/^ +\\*/ {print $2}' | sed 's/^ *//' | tail -n1)" || return 0 |
| 18 | + if [[ "${arg_y}" == "${__flag_present}" ]]; then |
| 19 | + info "-y or --yes-to-all flag present. Proceeding with non-interactive download and install." |
| 20 | + else |
| 21 | + info "If you proceed with the Xcode-CLT installation you may be prompted for your password." |
| 22 | + printf "Ready to install %s? (Y/n)" "${app_store_label}" |
| 23 | + read -r install_xcodeclt |
| 24 | + printf '%s\n' "${install_xcodeclt}" |
| 25 | + if [[ "${install_xcodeclt}" == [nN]* ]]; then |
| 26 | + emergency "${this_script}: Aborting: cannot proceed without XCode CLT." |
| 27 | + fi |
| 28 | + fi |
| 29 | + info "install.sh will attempt to download and install XCode CLT for you now." |
| 30 | + info "Note: sudo privileges required. Please enter your password if prompted." |
| 31 | + place_holder="/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress" |
| 32 | + sudo /usr/bin/touch "${place_holder}" || true |
| 33 | + sudo /usr/sbin/softwareupdate -i app_store_label || return 0 |
| 34 | + sudo /bin/rm -f "${place_holder}" || true |
| 35 | + sudo /usr/bin/xcode-select --switch "/Library/Developer/CommandLineTools" || return 1 |
| 36 | +} |
| 37 | + |
| 38 | +headless_xcode_clt_install () { |
| 39 | + # Fallback here if headless install/upgrade failed. This is the usual path through this code. |
| 40 | + if [[ "${arg_y}" == "${__flag_present}" ]]; then |
| 41 | + info "-y or --yes-to-all flag present. Proceeding with non-interactive download and install." |
| 42 | + else |
| 43 | + info "If you proceed with the Xcode-CLT installation you may be prompted for your password." |
| 44 | + printf "Ready to install Xcode-CLT using xcode-select? (Y/n)" |
| 45 | + read -r install_xcodeclt |
| 46 | + printf '%s\n' "${install_xcodeclt}" |
| 47 | + if [[ "${install_xcodeclt}" == [nN]* ]]; then |
| 48 | + emergency "${this_script}: Aborting: cannot proceed without XCode CLT." |
| 49 | + fi |
| 50 | + fi |
| 51 | + info "Installing Xcode Command Line Tools (CLT), using \`xcode-select --install\`." |
| 52 | + info "Note: sudo privileges required. Please enter your password if prompted." |
| 53 | + sudo /usr/bin/xcode-select --install || emergency "${this_script}: unable to run \`sudo xcode-select --install\`" |
| 54 | + printf "Please press <enter> once installation of Xcode command line tools has completed." |
| 55 | + read -r |
| 56 | + sudo /usr/bin/xcode-select --switch "/Library/Developer/CommandLineTools" || \ |
| 57 | + emergency "${this_script}: Xcode-CLT installation and activation failed, unable to continue" |
| 58 | +} |
| 59 | + |
| 60 | +maybe_install_xcodeCLT () { |
| 61 | + if [[ "$(need_xcodeCLT)" == "true" ]]; then |
| 62 | + info "It appears that you are on Mac OS and do not have the Xcode command line tools (CLT) installed," |
| 63 | + info "or they need to be upgraded.install.sh will now attempt to install/upgrade the Xcode-CLT using \`softwareupdate\`" |
| 64 | + info "This may take some time, please be patient." |
| 65 | + xcode_clt_install || true # This usually fails since `softwareupdate -i` doesn't return CLTs needing update |
| 66 | + fi |
| 67 | + if [[ "$(need_xcodeCLT)" == "true" ]]; then |
| 68 | + info "\`softwareupdate\` installation/upgrade failed. This is normal. Now trying with \`xcode-select --install\`" |
| 69 | + headless_xcode_clt_install || emergency "${this_script}: Could not install Xcode command line tools, unable to proceed. Please install them via the app store before retrying this installation." |
| 70 | + fi |
| 71 | + |
| 72 | + clang_output="$(/usr/bin/xzrun clang 2>&1)" || true |
| 73 | + if [[ "${clang_output}" =~ license ]]; then |
| 74 | + emergency "${this_script}: It appears you have not agreed to the Xcode license. Please do so before attempting to run this script again. This may be achieved by opening Xcode.app or running \`sudo xcodebuild -license\`" |
| 75 | + fi |
| 76 | +} |
0 commit comments