xcode.plugin.zsh 5.7 KB

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