From 47b09e2709e1b5c8a754e467935c32b7da3ecea8 Mon Sep 17 00:00:00 2001 From: "T. R. Bernstein" <137705289+trbernstein@users.noreply.github.com> Date: Fri, 28 Jun 2024 03:36:48 +0200 Subject: [PATCH] Install command line tools if necessary --- bootstrap.sh | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/bootstrap.sh b/bootstrap.sh index 0638fea..1310ab3 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -1,6 +1,59 @@ #!/usr/bin/env zsh # vi: set expandtab ft=zsh tw=80 ts=2 +function versionGT() { + [[ "${1%.*}" -gt "${2%.*}" ]] || [[ "${1%.*}" -eq "${2%.*}" && "${1#*.}" -gt "${2#*.}" ]] +} + +function majorMinor() { + echo "${1%%.*}.$(x="${1#*.}" echo "${x%%.*}")" +} + +function shouldInstallCommandLineTools() { + local macosVersion=$(majorMinor $(/usr/bin/sw_vers -productVersion)) + if version_gt "${macosVersion}" "10.13" + then + ! [[ -e "/Library/Developer/CommandLineTools/usr/bin/git" ]] + else + ! [[ -e "/Library/Developer/CommandLineTools/usr/bin/git" ]] || + ! [[ -e "/usr/include/iconv.h" ]] + fi +} + +function removeNewlines() { + printf "%s" "${1/"$'\n'"/}" +} + +function acceptXcodeLicense() { + xcodebuild -license accept +} + +function installCommandLineTools() { + shouldInstallCommandLineTools || return + cltPlaceholder="/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress" + touch ${cltPlaceholder} + + cltLabelCommand="/usr/sbin/softwareupdate -l | + grep -B 1 -E 'Command Line Tools' | + awk -F'*' '/^ *\\*/ {print \$2}' | + sed -e 's/^ *Label: //' -e 's/^ *//' | + sort -V | + tail -n1" + cltLabel="$(removeNewlines "$(/bin/bash -c "${cltLabelCommand}")")" + + if [[ -n "${cltLabel}" ]] + then + /usr/sbin/softwareupdate -i ${cltLabel} + /usr/bin/xcode-select --switch /Library/Developer/CommandLineTools + fi + rm -f ${cltPlaceholder} +} + +function ensureCommandLineTools() { + installCommandLineTools + acceptXcodeLicense +} + function ensureDocopts() { which docopts > /dev/null && return local fileURL="${DOCOPTS_URL:-https://github.com/astzweig/docopts/releases/download/v.0.7.0/docopts_darwin_amd64}" @@ -85,9 +138,11 @@ function main() { pushd -q "${tmpdir}" print -l "Working directory is: ${tmpdir}" + ensureCommandLineTools ensureRepo 'macos-system' cloneMacOSSystemRepo || return ensureRepo 'zshlib' cloneZSHLibRepo || return ensureBinary 'docopts' ensureDocopts || return + print 'Ensure command line tools are available.' print 'Will now run the installer.' local -A colors=() errColors=()