xcode.plugin.zsh 5.9 KB

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