2 alias xcdd='rm -rf ~/Library/Developer/Xcode/DerivedData/*'
3 alias xcp='xcode-select --print-path'
4 alias xcsel='sudo xcode-select --switch'
6 # original author: @subdigital
7 # source: https://gist.github.com/subdigital/5420709
10 xcode_files=(${1:-.}/{*.{xcworkspace,xcodeproj,swiftpm},Package.swift}(N))
12 if [[ ${#xcode_files} -eq 0 ]]; then
13 echo "No Xcode files found in ${1:-the current directory}." >&2
18 active_path=${"$(xcode-select -p)"%%/Contents/Developer*}
19 echo "Found ${xcode_files[1]}. Opening with ${active_path}"
20 open -a "$active_path" "${xcode_files[1]}"
23 # Opens a file or files in the Xcode IDE. Multiple files are opened in multi-file browser
24 # original author: @possen
26 if [[ $# == 0 ]]; then
27 echo "Specify file(s) to open in xcode."
31 open -a "Xcode.app" "$@"
34 # "XCode-SELect by Version" - select Xcode by just version number
35 # Uses naming convention:
36 # - different versions of Xcode are named Xcode-<version>.app or stored
37 # in a folder named Xcode-<version>
38 # - the special version name "default" refers to the "default" Xcode.app with no suffix
41 if [[ $# == 0 ]]; then
42 echo "xcselv: error: no option or argument given" >&2
43 echo "xcselv: see 'xcselv -h' for help" >&2
45 elif [[ $1 == "-p" ]]; then
46 _omz_xcode_print_active_version
48 elif [[ $1 == "-l" ]]; then
49 _omz_xcode_list_versions
51 elif [[ $1 == "-L" ]]; then
52 _omz_xcode_list_versions short
54 elif [[ $1 == "-h" ]]; then
55 _omz_xcode_print_xcselv_usage
57 elif [[ $1 == -* && $1 != "-" ]]; then
58 echo "xcselv: error: unrecognized option: $1" >&2
59 echo "xcselv: see 'xcselv -h' for help" >&2
62 # Main case: "xcselv <version>" to select a version
64 local -A xcode_versions
65 _omz_xcode_locate_versions
66 if [[ -z ${xcode_versions[$version]} ]]; then
67 echo "xcselv: error: Xcode version '$version' not found" >&2
70 app="${xcode_versions[$version]}"
71 echo "selecting Xcode $version: $app"
75 function _omz_xcode_print_xcselv_usage {
82 <version> set the active Xcode version
83 -h print this help message and exit
84 -p print the active Xcode version
85 -l list installed Xcode versions (long human-readable form)
86 -L list installed Xcode versions (short form, version names only)
90 # Parses the Xcode version from a filename based on our conventions
91 # Only meaningful when called from other _omz_xcode functions
92 function _omz_xcode_parse_versioned_file {
94 local basename=${app:t}
97 #echo "parent=$parent basename=$basename verstr=$verstr ver=$ver" >&2
99 if [[ $parent == Xcode* ]]; then
100 if [[ $basename == "Xcode.app" ]]; then
101 # "Xcode-<version>/Xcode.app" format
104 # Both file and parent dir are versioned. Reject.
107 elif [[ $basename == Xcode*.app ]]; then
108 # "Xcode-<version>.app" format
111 # Invalid naming pattern
115 local ver=${verstr#Xcode}
117 if [[ -z $ver ]]; then
118 # Unversioned "default" installation location
124 # Print the active version, using xcselv's notion of versions
125 function _omz_xcode_print_active_version {
127 local -A xcode_versions
128 local versions version active_path
129 _omz_xcode_locate_versions
130 active_path=$(xcode-select -p)
131 active_path=${active_path%%/Contents/Developer*}
132 versions=(${(kni)xcode_versions})
133 for version ($versions); do
134 if [[ "${xcode_versions[$version]}" == $active_path ]]; then
135 printf "%s (%s)\n" $version $active_path
139 printf "%s (%s)\n" "<unknown>" $active_path
142 # Locates all the installed versions of Xcode on this system, for this
143 # plugin's internal use.
144 # Populates the $xcode_versions associative array variable
145 # Caller should local-ize $xcode_versions with `local -A xcode_versions`
146 function _omz_xcode_locate_versions {
149 local app_dir apps app xcode_ver
150 # In increasing precedence order:
151 app_dirs=(/Applications $HOME/Applications)
152 for app_dir ($app_dirs); do
153 apps=( $app_dir/Xcode*.app(N) $app_dir/Xcode*/Xcode.app(N) )
155 xcode_ver=$(_omz_xcode_parse_versioned_file $app)
156 if [[ $? != 0 ]]; then
159 xcode_versions[$xcode_ver]=$app
164 function _omz_xcode_list_versions {
166 local -A xcode_versions
167 _omz_xcode_locate_versions
168 local width=1 width_i versions do_short=0
169 if [[ $1 == "short" ]]; then
172 versions=(${(kni)xcode_versions})
173 for version ($versions); do
174 if [[ $#version > $width ]]; then
178 for version ($versions); do
179 if [[ $do_short == 1 ]]; then
180 printf "%s\n" $version
182 printf "%-${width}s -> %s\n" "$version" "${xcode_versions[$version]}"
189 devfolder="$(xcode-select -p)"
192 if [[ -d "${devfolder}/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone Simulator.app" ]]; then
193 open "${devfolder}/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone Simulator.app"
195 elif [[ -d "${devfolder}/Applications/iOS Simulator.app" ]]; then
196 open "${devfolder}/Applications/iOS Simulator.app"
199 open "${devfolder}/Applications/Simulator.app"