xcode.plugin.zsh 5.2 KB

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