-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxcswitch.sh
executable file
·104 lines (91 loc) · 2.48 KB
/
xcswitch.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/sh
set -e
readonly PROGNAME="$(basename "$0")"
readonly ARGS="$@"
# some colors
fg_red="$(tput setaf 1)"
fg_green="$(tput setaf 2)"
fg_cyan="$(tput setaf 6)"
fg_reset="$(tput sgr0)"
# initialize
XCODE_VS=(`ls -1d /Applications/Xcode* | sed '1d;s/\/Applications\/Xcode//' | sed 's/\.app//'`)
CURR_XCODE=`xcodebuild -version | head -n1`
CURR_XCODE_V=${CURR_XCODE:6}
echo "${fg_green}Xcode switcher -- Currently: ${fg_reset}${fg_cyan}[${CURR_XCODE_V}]${fg_reset}"
echo ""
# ---------------------------------------------------------
usage() {
cat <<- EO_USAGE
Switch Xcode environment and keep it under /Applications/Xcode.app
usage:
$PROGNAME {version}
where:
{version} Xcode version: 8.3.3, 9.0, etc.
Available versions are:
- ${CURR_XCODE_V} <= Current version
EO_USAGE
local x=""
for item in "${XCODE_VS[@]}"; do
echo " - ${item}"
done
}
# ---------------------------------------------------------
pick_one() {
PS3="Select Xcode version to use: (CTRL+C to quit) "
select answer in "${XCODE_VS[@]}"; do
for item in "${XCODE_VS[@]}"; do
if [[ $item == $answer ]]; then
break 2
fi
done
done
if [[ "X$answer" = "X" ]]; then
echo ""
exit 1
fi
do_switch $answer
}
# ---------------------------------------------------------
do_switch() {
echo "Switching from: [${CURR_XCODE_V}] - to: ${fg_cyan}[${1}]${fg_reset}"
echo "..."
sudo mv /Applications/Xcode.app /Applications/Xcode${CURR_XCODE_V}.app
sudo mv /Applications/Xcode${1}.app /Applications/Xcode.app
echo "Double check versions below:"
xcodebuild -version
swift -version
echo ""
}
# ---------------------------------------------------------
main() {
#make sure only digits
local dev=
local regex="^[0-9]+\.[0-9]+(\.[0-9]+)?$"
if [[ $ARGS =~ $regex ]]; then
for item in "${XCODE_VS[@]}"; do
if [[ $item == $ARGS ]]; then
dev=$item
break 2
fi
done
fi
if [[ "X$dev" == "X" ]]; then
if [[ "$ARGS" == "${CURR_XCODE_V}" ]]; then
echo "Already running version: ${CURR_XCODE_V}..."
else
echo "${fg_red}Invalid version: [$ARGS]${fg_reset}"
usage
fi
echo ""
exit 1
fi
do_switch $dev
}
# ---------------------------------------------------------
# no args? select one
if (($# != 1)); then
pick_one
exit 1
fi
main
# EOF