xcode.plugin.zsh 5.8 KB

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