6642a99fac11dd4e10869566f1d38761f1e8746c

Author
Andrew Janke <andrew@apjanke.net>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

xcode: extend xcselv to list versions and do completion

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/xcode/README.md b/plugins/xcode/README.md
  2index 362940b9335c42345682e3fdbd67b50bb6bc9bea..15e65785905ff2c58024bb54229904f86432a561 100644
  3--- a/plugins/xcode/README.md
  4+++ b/plugins/xcode/README.md
  5@@ -4,7 +4,7 @@
  6 
  7 This plugin provides a few utilities that can help you on your daily use of Xcode and iOS development.
  8 
  9-To start using it, add the `xcode` plugin to your `plugins` array:
 10+To start using it, add the `xcode` plugin to your `plugins` array in `~/.zshrc`:
 11 
 12 ```zsh
 13 plugins=(... xcode)
 14@@ -24,12 +24,61 @@ plugins=(... xcode)
 15 
 16 ## Functions
 17 
 18-- **`xc`**:
 19-  Open one of the `.xcworkspace` and `.xcodeproj` files that it can find in the current working directory.
 20-  Returns 1 if it didn't find any relevant files.
 21+###  `xc`
 22 
 23-- **`simulator`**:
 24-  Open the iOS Simulator from your command line, dependant on whichever is the active developer directory for Xcode.
 25+Opens the current directory in Xcode as an Xcode project. This will open one of the `.xcworkspace` and `.xcodeproj` files that it can find in the current working directory.
 26+Returns 1 if it didn't find any relevant files.
 27 
 28-- **`xcselv`**:
 29-  Select different Xcode by version. Example: `xcselv 6.2`
 30+###  `simulator`
 31+
 32+Opens the iOS Simulator from your command line, dependent on whichever is the active developer directory for Xcode. (That is, it respects the `xcsel` setting.)
 33+
 34+### `xcselv`
 35+
 36+Selects different Xcode installations by version name. This is like `xcsel`, except it takes just a version name as an argument instead of the full path to the Xcode installation. Uses the naming conventions described below.
 37+
 38+* `xcselv <version>` selects a version
 39+ * Example: `xcselv 6.2`
 40+* `xcselv default` selects the default unversioned `Applications/Xcode.app`
 41+* `xcselv` with no argument lists the available Xcode versions in a human-readable format
 42+* `xcselv -l` lists the installed Xcode versions
 43+* `xcselv -L` lists the installed Xcode versions in a short version-name-only format
 44+* `xcselv -p` prints info about the active Xcode version
 45+* `xcselv -h` prints a help message
 46+
 47+The option parsing for `xcselv` is naive. Options may not be combined, and only the first option is recognized.
 48+
 49+## Multiple Xcode Versions
 50+
 51+The `xcselv` command provides support for switching between different Xcode installations using just a version number. Different Xcode versions are identified by file naming conventions.
 52+
 53+### Versioned Xcode Naming Conventions
 54+
 55diff --git a/plugins/xcode/_xcselv b/plugins/xcode/_xcselv
 56new file mode 100644
 57index 0000000000000000000000000000000000000000..f9861d54b6e74df6e717658d8573035c4aafd6e3
 58--- /dev/null
 59+++ b/plugins/xcode/_xcselv
 60@@ -0,0 +1,19 @@
 61+#compdef xcselv
 62+#autoload
 63+
 64+function _xcselv_compl_list_versions() {
 65+  _omz_xcode_list_versions short
 66+}
 67+
 68+_arguments \
 69+  '(-l -L -p)-h[prints a help message]' \
 70+  '(-L -p -h)-l[lists installed Xcode versions]' \
 71+  '(-l -p -h)-L[lists installed Xcode versions (long form)]' \
 72+  '(-h -l -L)-p[prints active Xcode version]' \
 73+  && ret=0
 74+
 75+local _xcode_versions
 76+_xcode_versions=($(_xcselv_compl_list_versions))
 77+_describe -t _xcode_versions 'version' _xcode_versions
 78+
 79+return 1
 80diff --git a/plugins/xcode/xcode.plugin.zsh b/plugins/xcode/xcode.plugin.zsh
 81index 1f547470d5faa2a0a2639bc4044aca5c5731a3a6..b63a857a7db47bbc213ec34073b36b4001318d6e 100644
 82--- a/plugins/xcode/xcode.plugin.zsh
 83+++ b/plugins/xcode/xcode.plugin.zsh
 84@@ -22,28 +22,153 @@ function xc {
 85 # Uses naming convention:
 86 #  - different versions of Xcode are named Xcode-<version>.app or stored
 87 #     in a folder named Xcode-<version>
 88-#  - the special version name "-" refers to the "default" Xcode.app with no suffix
 89+#  - the special version name "default" refers to the "default" Xcode.app with no suffix
 90 function xcselv {
 91   emulate -L zsh
 92+  if [[ $# == 0 ]]; then
 93+    echo "xcselv: error: no option or argument given" >&2
 94+    echo "xcselv: see 'xcselv -h' for help" >&2
 95+    return 1
 96+  elif [[ $1 == "-p" ]]; then
 97+    _omz_xcode_print_active_version
 98+    return
 99+  elif [[ $1 == "-l" ]]; then
100+    _omz_xcode_list_versions
101+    return
102+  elif [[ $1 == "-L" ]]; then
103+    _omz_xcode_list_versions short
104+    return
105+  elif [[ $1 == "-h" ]]; then
106+    _omz_xcode_print_xcselv_usage
107+    return 0
108+  elif [[ $1 == -* && $1 != "-" ]]; then
109+    echo "xcselv: error: unrecognized option: $1" >&2
110+    echo "xcselv: see 'xcselv -h' for help" >&2
111+    return 1
112+  fi
113+  # Main case: "xcselv <version>" to select a version
114   local version=$1
115-  local apps_dirs apps_dir apps app
116-  apps_dirs=( $HOME/Applications /Applications )
117-  for apps_dir ($apps_dirs); do
118-    if [[ $version == "-" ]]; then
119-      apps=( $apps_dir/Xcode.app $apps_dir/Xcode/Xcode.app )
120+  local -A xcode_versions
121+  _omz_xcode_locate_versions
122+  if [[ -z ${xcode_versions[$version]} ]]; then
123+    echo "xcselv: error: Xcode version '$version' not found" >&2
124+    return 1
125+  fi
126+  app="${xcode_versions[$version]}"
127+  echo "selecting Xcode $version: $app"
128+  xcsel "$app"
129+}
130+
131+function _omz_xcode_print_xcselv_usage {
132+  cat << EOF >&2
133+Usage: 
134+  xcselv <version>
135+  xcselv [options]
136+
137+Options:
138+  <version> set the active Xcode version
139+  -h        print this help message and exit
140+  -p        print the active Xcode version
141+  -l        list installed Xcode versions (long human-readable form)
142+  -L        list installed Xcode versions (short form, version names only)
143+EOF
144+}
145+
146+# Parses the Xcode version from a filename based on our conventions
147+# Only meaningful when called from other _omz_xcode functions
148+function _omz_xcode_parse_versioned_file {
149+  local file=$1
150+  local basename=${app:t}
151+  local dir=${app:h}
152+  local parent=${dir:t}
153+  #echo "parent=$parent basename=$basename verstr=$verstr ver=$ver" >&2
154+  local verstr
155+  if [[ $parent == Xcode* ]]; then
156+    if [[ $basename == "Xcode.app" ]]; then
157+      # "Xcode-<version>/Xcode.app" format
158+      verstr=$parent
159     else
160-      apps=( $apps_dir/Xcode-$version.app $apps_dir/Xcode-$version/Xcode.app )
161+      # Both file and parent dir are versioned. Reject.
162+      return 1;
163     fi
164+  elif [[ $basename == Xcode*.app ]]; then
165+    # "Xcode-<version>.app" format
166+    verstr=${basename:r}
167+  else
168+    # Invalid naming pattern
169+    return 1;
170+  fi
171+
172+  local ver=${verstr#Xcode}
173+  ver=${ver#[- ]}
174+  if [[ -z $ver ]]; then
175+    # Unversioned "default" installation location
176+    ver="default"
177+  fi
178+  print -- "$ver"
179+}
180+
181+# Print the active version, using xcselv's notion of versions
182+function _omz_xcode_print_active_version {
183+  emulate -L zsh