]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/xcode/xcode.plugin.zsh
...
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / xcode / xcode.plugin.zsh
1 alias xcb='xcodebuild'
2 alias xcdd='rm -rf ~/Library/Developer/Xcode/DerivedData/*'
3 alias xcp='xcode-select --print-path'
4 alias xcsel='sudo xcode-select --switch'
5
6 # original author: @subdigital
7 # source: https://gist.github.com/subdigital/5420709
8 function xc {
9   local xcode_files
10   xcode_files=(${1:-.}/{*.{xcworkspace,xcodeproj,swiftpm},Package.swift}(N))
11
12   if [[ ${#xcode_files} -eq 0 ]]; then
13     echo "No Xcode files found in ${1:-the current directory}." >&2
14     return 1
15   fi
16
17   local active_path
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]}"
21 }
22
23 # Opens a file or files in the Xcode IDE. Multiple files are opened in multi-file browser
24 # original author: @possen
25 function xx {
26   if [[ $# == 0 ]]; then
27     echo "Specify file(s) to open in xcode."
28     return 1
29   fi
30   echo "${xcode_files}"
31   open -a "Xcode.app" "$@"
32 }
33
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
39 function xcselv {
40   emulate -L zsh
41   if [[ $# == 0 ]]; then
42     echo "xcselv: error: no option or argument given" >&2
43     echo "xcselv: see 'xcselv -h' for help" >&2
44     return 1
45   elif [[ $1 == "-p" ]]; then
46     _omz_xcode_print_active_version
47     return
48   elif [[ $1 == "-l" ]]; then
49     _omz_xcode_list_versions
50     return
51   elif [[ $1 == "-L" ]]; then
52     _omz_xcode_list_versions short
53     return
54   elif [[ $1 == "-h" ]]; then
55     _omz_xcode_print_xcselv_usage
56     return 0
57   elif [[ $1 == -* && $1 != "-" ]]; then
58     echo "xcselv: error: unrecognized option: $1" >&2
59     echo "xcselv: see 'xcselv -h' for help" >&2
60     return 1
61   fi
62   # Main case: "xcselv <version>" to select a version
63   local version=$1
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
68     return 1
69   fi
70   app="${xcode_versions[$version]}"
71   echo "selecting Xcode $version: $app"
72   xcsel "$app"
73 }
74
75 function _omz_xcode_print_xcselv_usage {
76   cat << EOF >&2
77 Usage:
78   xcselv <version>
79   xcselv [options]
80
81 Options:
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)
87 EOF
88 }
89
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 {
93   local file=$1
94   local basename=${app:t}
95   local dir=${app:h}
96   local parent=${dir:t}
97   #echo "parent=$parent basename=$basename verstr=$verstr ver=$ver" >&2
98   local verstr
99   if [[ $parent == Xcode* ]]; then
100     if [[ $basename == "Xcode.app" ]]; then
101       # "Xcode-<version>/Xcode.app" format
102       verstr=$parent
103     else
104       # Both file and parent dir are versioned. Reject.
105       return 1;
106     fi
107   elif [[ $basename == Xcode*.app ]]; then
108     # "Xcode-<version>.app" format
109     verstr=${basename:r}
110   else
111     # Invalid naming pattern
112     return 1;
113   fi
114
115   local ver=${verstr#Xcode}
116   ver=${ver#[- ]}
117   if [[ -z $ver ]]; then
118     # Unversioned "default" installation location
119     ver="default"
120   fi
121   print -- "$ver"
122 }
123
124 # Print the active version, using xcselv's notion of versions
125 function _omz_xcode_print_active_version {
126   emulate -L zsh
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
136       return
137     fi
138   done
139   printf "%s (%s)\n" "<unknown>" $active_path
140 }
141
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 {
147   emulate -L zsh
148   local -a app_dirs
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) )
154     for app ($apps); do
155       xcode_ver=$(_omz_xcode_parse_versioned_file $app)
156       if [[ $? != 0 ]]; then
157         continue
158       fi
159       xcode_versions[$xcode_ver]=$app
160     done
161   done
162 }
163
164 function _omz_xcode_list_versions {
165   emulate -L zsh
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
170     do_short=1
171   fi
172   versions=(${(kni)xcode_versions})
173   for version ($versions); do
174     if [[ $#version > $width ]]; then
175       width=$#version;
176     fi
177   done
178   for version ($versions); do
179     if [[ $do_short == 1 ]]; then
180       printf "%s\n" $version
181     else
182       printf "%-${width}s -> %s\n" "$version" "${xcode_versions[$version]}"
183     fi
184   done
185 }
186
187 function simulator {
188   local devfolder
189   devfolder="$(xcode-select -p)"
190
191   # Xcode ≤ 5.x
192   if [[ -d "${devfolder}/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone Simulator.app" ]]; then
193     open "${devfolder}/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone Simulator.app"
194   # Xcode ≥ 6.x
195   elif [[ -d "${devfolder}/Applications/iOS Simulator.app" ]]; then
196     open "${devfolder}/Applications/iOS Simulator.app"
197   # Xcode ≥ 7.x
198   else
199     open "${devfolder}/Applications/Simulator.app"
200   fi
201 }