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