xcode.plugin.zsh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. # original author: @subdigital
  6. # source: http://gist.github.com/subdigital/5420709
  7. function xc {
  8. local xcode_proj
  9. if [[ $# == 0 ]]; then
  10. xcode_proj=(*.{xcworkspace,xcodeproj}(N))
  11. else
  12. xcode_proj=($1/*.{xcworkspace,xcodeproj}(N))
  13. fi
  14. if [[ ${#xcode_proj} -eq 0 ]]; then
  15. if [[ $# == 0 ]]; then
  16. echo "No xcworkspace/xcodeproj file found in the current directory."
  17. else
  18. echo "No xcworkspace/xcodeproj file found in $1."
  19. fi
  20. return 1
  21. else
  22. echo "Found ${xcode_proj[1]}"
  23. open "${xcode_proj[1]}"
  24. fi
  25. }
  26. # "XCode-SELect by Version" - select Xcode by just version number
  27. # Uses naming convention:
  28. # - different versions of Xcode are named Xcode-<version>.app or stored
  29. # in a folder named Xcode-<version>
  30. # - the special version name "default" refers to the "default" Xcode.app with no suffix
  31. function xcselv {
  32. emulate -L zsh
  33. if [[ $# == 0 ]]; then
  34. echo "xcselv: error: no option or argument given" >&2
  35. echo "xcselv: see 'xcselv -h' for help" >&2
  36. return 1
  37. elif [[ $1 == "-p" ]]; then
  38. _omz_xcode_print_active_version
  39. return
  40. elif [[ $1 == "-l" ]]; then
  41. _omz_xcode_list_versions
  42. return
  43. elif [[ $1 == "-L" ]]; then
  44. _omz_xcode_list_versions short
  45. return
  46. elif [[ $1 == "-h" ]]; then
  47. _omz_xcode_print_xcselv_usage
  48. return 0
  49. elif [[ $1 == -* && $1 != "-" ]]; then
  50. echo "xcselv: error: unrecognized option: $1" >&2
  51. echo "xcselv: see 'xcselv -h' for help" >&2
  52. return 1
  53. fi
  54. # Main case: "xcselv <version>" to select a version
  55. local version=$1
  56. local -A xcode_versions
  57. _omz_xcode_locate_versions
  58. if [[ -z ${xcode_versions[$version]} ]]; then
  59. echo "xcselv: error: Xcode version '$version' not found" >&2
  60. return 1
  61. fi
  62. app="${xcode_versions[$version]}"
  63. echo "selecting Xcode $version: $app"
  64. xcsel "$app"
  65. }
  66. function _omz_xcode_print_xcselv_usage {
  67. cat << EOF >&2
  68. Usage:
  69. xcselv <version>
  70. xcselv [options]
  71. Options:
  72. <version> set the active Xcode version
  73. -h print this help message and exit
  74. -p print the active Xcode version
  75. -l list installed Xcode versions (long human-readable form)
  76. -L list installed Xcode versions (short form, version names only)
  77. EOF
  78. }
  79. # Parses the Xcode version from a filename based on our conventions
  80. # Only meaningful when called from other _omz_xcode functions
  81. function _omz_xcode_parse_versioned_file {
  82. local file=$1
  83. local basename=${app:t}
  84. local dir=${app:h}
  85. local parent=${dir:t}
  86. #echo "parent=$parent basename=$basename verstr=$verstr ver=$ver" >&2
  87. local verstr
  88. if [[ $parent == Xcode* ]]; then
  89. if [[ $basename == "Xcode.app" ]]; then
  90. # "Xcode-<version>/Xcode.app" format
  91. verstr=$parent
  92. else
  93. # Both file and parent dir are versioned. Reject.
  94. return 1;
  95. fi
  96. elif [[ $basename == Xcode*.app ]]; then
  97. # "Xcode-<version>.app" format
  98. verstr=${basename:r}
  99. else
  100. # Invalid naming pattern
  101. return 1;
  102. fi
  103. local ver=${verstr#Xcode}
  104. ver=${ver#[- ]}
  105. if [[ -z $ver ]]; then
  106. # Unversioned "default" installation location
  107. ver="default"
  108. fi
  109. print -- "$ver"
  110. }
  111. # Print the active version, using xcselv's notion of versions
  112. function _omz_xcode_print_active_version {
  113. emulate -L zsh
  114. local -A xcode_versions
  115. local versions version active_path
  116. _omz_xcode_locate_versions
  117. active_path=$(xcode-select -p)
  118. active_path=${active_path%%/Contents/Developer*}
  119. versions=(${(kni)xcode_versions})
  120. for version ($versions); do
  121. if [[ "${xcode_versions[$version]}" == $active_path ]]; then
  122. printf "%s (%s)\n" $version $active_path
  123. return
  124. fi
  125. done
  126. printf "%s (%s)\n" "<unknown>" $active_path
  127. }
  128. # Locates all the installed versions of Xcode on this system, for this
  129. # plugin's internal use.
  130. # Populates the $xcode_versions associative array variable
  131. # Caller should local-ize $xcode_versions with `local -A xcode_versions`
  132. function _omz_xcode_locate_versions {
  133. emulate -L zsh
  134. local -a app_dirs
  135. local app_dir apps app xcode_ver
  136. # In increasing precedence order:
  137. app_dirs=(/Applications $HOME/Applications)
  138. for app_dir ($app_dirs); do
  139. apps=( $app_dir/Xcode*.app(N) $app_dir/Xcode*/Xcode.app(N) )
  140. for app ($apps); do
  141. xcode_ver=$(_omz_xcode_parse_versioned_file $app)
  142. if [[ $? != 0 ]]; then
  143. continue
  144. fi
  145. xcode_versions[$xcode_ver]=$app
  146. done
  147. done
  148. }
  149. function _omz_xcode_list_versions {
  150. emulate -L zsh
  151. local -A xcode_versions
  152. _omz_xcode_locate_versions
  153. local width=1 width_i versions do_short=0
  154. if [[ $1 == "short" ]]; then
  155. do_short=1
  156. fi
  157. versions=(${(kni)xcode_versions})
  158. for version ($versions); do
  159. if [[ $#version > $width ]]; then
  160. width=$#version;
  161. fi
  162. done
  163. for version ($versions); do
  164. if [[ $do_short == 1 ]]; then
  165. printf "%s\n" $version
  166. else
  167. printf "%-${width}s -> %s\n" "$version" "${xcode_versions[$version]}"
  168. fi
  169. done
  170. }
  171. function simulator {
  172. local devfolder
  173. devfolder="$(xcode-select -p)"
  174. # Xcode ≤ 5.x
  175. if [[ -d "${devfolder}/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone Simulator.app" ]]; then
  176. open "${devfolder}/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone Simulator.app"
  177. # Xcode ≥ 6.x
  178. elif [[ -d "${devfolder}/Applications/iOS Simulator.app" ]]; then
  179. open "${devfolder}/Applications/iOS Simulator.app"
  180. # Xcode ≥ 7.x
  181. else
  182. open "${devfolder}/Applications/Simulator.app"
  183. fi
  184. }