-
Notifications
You must be signed in to change notification settings - Fork 31
/
xcode-clean.sh
executable file
·155 lines (131 loc) · 3.86 KB
/
xcode-clean.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/sh
text_magenta=$(tput setaf 5)
text_bold=$(tput bold)
text_normal=$(tput sgr0)
archives_path=~/"Library/Developer/Xcode/Archives"
derived_data_path=~/"Library/Developer/Xcode/DerivedData"
simulator_data_path=~/"Library/Developer/CoreSimulator/Devices"
device_support_path=~/"Library/Developer/Xcode/iOS DeviceSupport"
remove_archives=false
remove_derived_data=false
remove_device_support=false
remove_simulator_data=false
backup_dsyms=false
dsym_backup_path=""
dry_run=false
remove_contents() {
arg_path=$1
arg_name=$2
arg_flag_name=$3
size=$(du -hcs "$arg_path" | tail -1 | cut -f1 | xargs)
if $dry_run; then
printf "Clearing $arg_name($arg_flag_name flag) in ${arg_path}/* would free up ${text_bold}${size}${text_normal} disk space\n"
else
printf "Clearing $arg_name in ${arg_path}/* (freeing ${text_bold}${size}${text_normal} disk space)\n"
rm -Rf "$arg_path"/*
fi
}
do_dsym_backup() {
backup_path=$1
cd "$archives_path"
{ find . -name "dSYMs" -exec printf '%s\0' {} + | while IFS= read -ru3 -d '' file; do
dsym_backup_file "$file"; done 3<&0 <&4 4<&-; } 4<&0
}
dsym_backup_file() {
path=$1
absolute_path="${archives_path}/${path}"
output_path="${backup_path}/${path}"
output_path=$(dirname "$output_path")
if $dry_run; then
printf "Back up $absolute_path to $output_path\n"
else
cd "$archives_path"
printf "Backing up $absolute_path to $output_path \n"
mkdir -p "$output_path" # Create dir(s) if it doesn't exist
cp -R "$absolute_path" "$output_path"
fi
}
usage() {
cat << EOF
Usage: $0 [options]
Frees up disk space by removing Xcode data. NOTE: you might want to keep backups of the dSYM files in ~/Library/Developer/Xcode/Archives
EXAMPLE:
$0 -A
OPTIONS:
-h Show this help message
-b [path] Backup dSYM files to specified path before removing archives
-a Removed all Xcode archives
-d Remove everything in DerivedData folder
-D Remove everything in DeviceSupport folder
-s Remove simulator data
-A Remove all of the above(archived, DerivedData and simulator data)
--dry-run Dry run mode prints which directories would be cleared but don't remove any files
EOF
}
while getopts "hadDsAb:-:" OPTION
do
case $OPTION in
-)
case "${OPTARG}" in
dry-run)
value="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
dry_run=true
;;
*)
if [ "$OPTERR" = 1 ] && [ "${optspec:0:1}" != ":" ]; then
usage
exit 1
#echo "Unknown option --${OPTARG}" >&2
fi
;;
esac;;
h)
usage
exit 0
;;
a)
remove_archives=true
;;
d)
remove_derived_data=true
;;
D)
remove_device_support=true
;;
s)
remove_simulator_data=true
;;
A)
remove_archives=true
remove_derived_data=true
remove_device_support=true
remove_simulator_data=true
;;
b)
backup_dsyms=true
dsym_backup_path="$OPTARG"
;;
\?)
usage
exit 1
;;
esac
done
if $dry_run; then
printf "${text_magenta}Running in dry run mode. No files will be removed.${text_normal}\n"
fi
if $backup_dsyms; then
do_dsym_backup "$dsym_backup_path"
fi
if $remove_archives; then
remove_contents "$archives_path" "archives" "-a"
fi
if $remove_derived_data; then
remove_contents "$derived_data_path" "DerivedData content" "-d"
fi
if $remove_device_support; then
remove_contents "$device_support_path" "iOS DeviceSupport content" "-D"
fi
if $remove_simulator_data; then
remove_contents "$simulator_data_path" "simulator data" "-s"
fi