Parent directory

xcode.plugin.zsh

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