From cbdad588e65f4205fcdc4e34e97b04fa7fdb5457 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Fri, 7 Aug 2015 18:56:57 +0200 Subject: [PATCH 1/9] Use zsh globbing instead of ls + grep in xc function Uses zsh arrays and globbing to find .xcworkspace and .xcodeproj files. --- plugins/xcode/xcode.plugin.zsh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugins/xcode/xcode.plugin.zsh b/plugins/xcode/xcode.plugin.zsh index 1d1205b8..4aa6bfc9 100644 --- a/plugins/xcode/xcode.plugin.zsh +++ b/plugins/xcode/xcode.plugin.zsh @@ -1,12 +1,12 @@ #xc function courtesy of http://gist.github.com/subdigital/5420709 function xc { - xcode_proj=`ls | grep "\.xc" | sort -r | head -1` - if [[ `echo -n $xcode_proj | wc -m` == 0 ]] - then + local xcode_proj + xcode_proj=(*.{xcworkspace,xcodeproj}(N)) + if [[ ${#xcode_proj} -eq 0 ]]; then echo "No xcworkspace/xcodeproj file found in the current directory." else - echo "Found $xcode_proj" - open "$xcode_proj" + echo "Found ${xcode_proj[1]}" + open "${xcode_proj[1]}" fi } From 6a830f39b68300cf86504f1c60d9ab77c09b7971 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Fri, 4 Sep 2015 01:43:48 +0200 Subject: [PATCH 2/9] Return error if no xcode files where found --- plugins/xcode/xcode.plugin.zsh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugins/xcode/xcode.plugin.zsh b/plugins/xcode/xcode.plugin.zsh index 4aa6bfc9..274bd92e 100644 --- a/plugins/xcode/xcode.plugin.zsh +++ b/plugins/xcode/xcode.plugin.zsh @@ -2,8 +2,10 @@ function xc { local xcode_proj xcode_proj=(*.{xcworkspace,xcodeproj}(N)) + if [[ ${#xcode_proj} -eq 0 ]]; then echo "No xcworkspace/xcodeproj file found in the current directory." + return 1 else echo "Found ${xcode_proj[1]}" open "${xcode_proj[1]}" From 24bdd8e29a344f0282b842f89a4a10f145344939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Fri, 7 Aug 2015 19:04:26 +0200 Subject: [PATCH 3/9] Replace xcsel function with just an alias --- plugins/xcode/xcode.plugin.zsh | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/plugins/xcode/xcode.plugin.zsh b/plugins/xcode/xcode.plugin.zsh index 274bd92e..059c78fd 100644 --- a/plugins/xcode/xcode.plugin.zsh +++ b/plugins/xcode/xcode.plugin.zsh @@ -12,10 +12,7 @@ function xc { fi } -function xcsel { - sudo xcode-select --switch "$*" -} - +alias xcsel='sudo xcode-select --switch' alias xcb='xcodebuild' alias xcp='xcode-select --print-path' alias xcdd='rm -rf ~/Library/Developer/Xcode/DerivedData/*' From ec3694c788d2b32eb1735687cf89b057d4ca4d4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Fri, 4 Sep 2015 22:08:57 +0200 Subject: [PATCH 4/9] Make simulator dependant of currently active dev directory This also gets rid of `xcode-select` command-not-found errors. --- plugins/xcode/xcode.plugin.zsh | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/plugins/xcode/xcode.plugin.zsh b/plugins/xcode/xcode.plugin.zsh index 059c78fd..f80de36d 100644 --- a/plugins/xcode/xcode.plugin.zsh +++ b/plugins/xcode/xcode.plugin.zsh @@ -17,8 +17,15 @@ alias xcb='xcodebuild' alias xcp='xcode-select --print-path' alias xcdd='rm -rf ~/Library/Developer/Xcode/DerivedData/*' -if [[ -d $(xcode-select -p)/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone\ Simulator.app ]]; then - alias simulator='open $(xcode-select -p)/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone\ Simulator.app' -else - alias simulator='open $(xcode-select -p)/Applications/iOS\ Simulator.app' -fi +function simulator { + local devfolder + devfolder="$(xcode-select -p)" + + # Xcode ≤ 5.x + if [[ -d "${devfolder}/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone Simulator.app" ]]; then + open "${devfolder}/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone Simulator.app" + # Xcode ≥ 6.x + else + open "${devfolder}/Applications/iOS Simulator.app" + fi +} From d95589d4e43d3a6288b8a66492c17d12c4e4eca0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Sat, 5 Sep 2015 23:17:38 +0200 Subject: [PATCH 5/9] Add README for the xcode plugin --- plugins/xcode/README.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 plugins/xcode/README.md diff --git a/plugins/xcode/README.md b/plugins/xcode/README.md new file mode 100644 index 00000000..b8efef38 --- /dev/null +++ b/plugins/xcode/README.md @@ -0,0 +1,31 @@ +# Xcode + +## Description + +This plugin provides a few utilities that can help you on your daily use of Xcode and iOS development. + +To start using it, add the `xcode` plugin to your `plugins` array: + +```zsh +plugins=(... xcode) +``` + + +## Aliases + +| Alias | Description | Command | +|-------|---------------------------------------|------------------------------------------------| +| xcsel | Change active developer directory | sudo xcode-select --switch | +| xcp | Show currently active dev. directory | xcode-select --print-path | +| xcb | Build Xcode projects and workspaces | xcodebuild | +| xcdd | Purge all temporary build information | rm -rf ~/Library/Developer/Xcode/DerivedData/* | + + +## Functions + +- **`xc`**: + Open one of the `.xcworkspace` and `.xcodeproj` files that it can find in the current working directory. + Returns 1 if it didn't find any relevant files. + +- **`simulator`**: + Open the iOS Simulator from your command line, dependant on whichever is the active developer directory for Xcode. From 5584aa05c7c94949665f024ad0d88b8162654cb9 Mon Sep 17 00:00:00 2001 From: Andrew Janke Date: Fri, 4 Sep 2015 17:17:16 -0400 Subject: [PATCH 6/9] xcode: add version-based xcselv() --- plugins/xcode/xcode.plugin.zsh | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/plugins/xcode/xcode.plugin.zsh b/plugins/xcode/xcode.plugin.zsh index f80de36d..b3058cef 100644 --- a/plugins/xcode/xcode.plugin.zsh +++ b/plugins/xcode/xcode.plugin.zsh @@ -17,6 +17,35 @@ alias xcb='xcodebuild' alias xcp='xcode-select --print-path' alias xcdd='rm -rf ~/Library/Developer/Xcode/DerivedData/*' + +# "XCode-SELect by Version" - select Xcode by just version number +# Uses naming convention: +# - different versions of Xcode are named Xcode-.app or stored +# in a folder named Xcode- +# - the special version name "-" refers to the "default" Xcode.app with no suffix +function xcselv { + emulate -L zsh + local version=$1 + local apps_dirs apps_dir apps app + apps_dirs=( $HOME/Applications /Applications ) + for apps_dir ($apps_dirs); do + if [[ $version == "-" ]]; then + apps=( $apps_dir/Xcode.app $apps_dir/Xcode/Xcode.app ) + else + apps=( $apps_dir/Xcode-$version.app $apps_dir/Xcode-$version/Xcode.app ) + fi + for app ($apps); do + if [[ -e "$app" ]]; then + echo "selecting Xcode $version: $app" + xcsel "$app" + return + fi + done + done + echo "xcselv: Xcode version $version not found" + return 1 +} + function simulator { local devfolder devfolder="$(xcode-select -p)" From 213f46a296bd76031eb6a2fa66c72efeb912481d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Mon, 7 Sep 2015 12:08:50 +0200 Subject: [PATCH 7/9] Add xcselv documentation in the README --- plugins/xcode/README.md | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/plugins/xcode/README.md b/plugins/xcode/README.md index b8efef38..362940b9 100644 --- a/plugins/xcode/README.md +++ b/plugins/xcode/README.md @@ -13,12 +13,13 @@ plugins=(... xcode) ## Aliases -| Alias | Description | Command | -|-------|---------------------------------------|------------------------------------------------| -| xcsel | Change active developer directory | sudo xcode-select --switch | -| xcp | Show currently active dev. directory | xcode-select --print-path | -| xcb | Build Xcode projects and workspaces | xcodebuild | -| xcdd | Purge all temporary build information | rm -rf ~/Library/Developer/Xcode/DerivedData/* | +| Alias | Description | Command | +|-------|------------------------------------------|------------------------------------------------| +| xcb | Build Xcode projects and workspaces | xcodebuild | +| xcdd | Purge all temporary build information | rm -rf ~/Library/Developer/Xcode/DerivedData/* | +| xcp | Show currently selected Xcode directory | xcode-select --print-path | +| xcsel | Select different Xcode directory by path | sudo xcode-select --switch | + ## Functions @@ -29,3 +30,6 @@ plugins=(... xcode) - **`simulator`**: Open the iOS Simulator from your command line, dependant on whichever is the active developer directory for Xcode. + +- **`xcselv`**: + Select different Xcode by version. Example: `xcselv 6.2` From 82ae41ca4cf4fac253294f17b33b0c718755ffc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Mon, 7 Sep 2015 12:09:08 +0200 Subject: [PATCH 8/9] Reorder xcode plugin file --- plugins/xcode/xcode.plugin.zsh | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/plugins/xcode/xcode.plugin.zsh b/plugins/xcode/xcode.plugin.zsh index b3058cef..1f547470 100644 --- a/plugins/xcode/xcode.plugin.zsh +++ b/plugins/xcode/xcode.plugin.zsh @@ -1,4 +1,10 @@ -#xc function courtesy of http://gist.github.com/subdigital/5420709 +alias xcb='xcodebuild' +alias xcdd='rm -rf ~/Library/Developer/Xcode/DerivedData/*' +alias xcp='xcode-select --print-path' +alias xcsel='sudo xcode-select --switch' + +# original author: @subdigital +# source: http://gist.github.com/subdigital/5420709 function xc { local xcode_proj xcode_proj=(*.{xcworkspace,xcodeproj}(N)) @@ -12,12 +18,6 @@ function xc { fi } -alias xcsel='sudo xcode-select --switch' -alias xcb='xcodebuild' -alias xcp='xcode-select --print-path' -alias xcdd='rm -rf ~/Library/Developer/Xcode/DerivedData/*' - - # "XCode-SELect by Version" - select Xcode by just version number # Uses naming convention: # - different versions of Xcode are named Xcode-.app or stored From 6642a99fac11dd4e10869566f1d38761f1e8746c Mon Sep 17 00:00:00 2001 From: Andrew Janke Date: Thu, 10 Sep 2015 01:48:24 -0400 Subject: [PATCH 9/9] xcode: extend xcselv to list versions and do completion --- plugins/xcode/README.md | 65 ++++++++++++-- plugins/xcode/_xcselv | 19 +++++ plugins/xcode/xcode.plugin.zsh | 151 ++++++++++++++++++++++++++++++--- 3 files changed, 214 insertions(+), 21 deletions(-) create mode 100644 plugins/xcode/_xcselv diff --git a/plugins/xcode/README.md b/plugins/xcode/README.md index 362940b9..15e65785 100644 --- a/plugins/xcode/README.md +++ b/plugins/xcode/README.md @@ -4,7 +4,7 @@ This plugin provides a few utilities that can help you on your daily use of Xcode and iOS development. -To start using it, add the `xcode` plugin to your `plugins` array: +To start using it, add the `xcode` plugin to your `plugins` array in `~/.zshrc`: ```zsh plugins=(... xcode) @@ -24,12 +24,61 @@ plugins=(... xcode) ## Functions -- **`xc`**: - Open one of the `.xcworkspace` and `.xcodeproj` files that it can find in the current working directory. - Returns 1 if it didn't find any relevant files. +### `xc` -- **`simulator`**: - Open the iOS Simulator from your command line, dependant on whichever is the active developer directory for Xcode. +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. +Returns 1 if it didn't find any relevant files. -- **`xcselv`**: - Select different Xcode by version. Example: `xcselv 6.2` +### `simulator` + +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.) + +### `xcselv` + +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. + +* `xcselv ` selects a version + * Example: `xcselv 6.2` +* `xcselv default` selects the default unversioned `Applications/Xcode.app` +* `xcselv` with no argument lists the available Xcode versions in a human-readable format +* `xcselv -l` lists the installed Xcode versions +* `xcselv -L` lists the installed Xcode versions in a short version-name-only format +* `xcselv -p` prints info about the active Xcode version +* `xcselv -h` prints a help message + +The option parsing for `xcselv` is naive. Options may not be combined, and only the first option is recognized. + +## Multiple Xcode Versions + +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. + +### Versioned Xcode Naming Conventions + +Apple does not seem to explicitly define or provide tooling support for a naming convention or other organizational mechanism for managing versioned Xcode installations. Apple seems to have released beta versions with both `Xcode.app` and `Xcode-.app` style names in the past, and both styles show up in forum and blog discussions. + +We've adopted the following naming convention: + +* Versioned Xcode installations are identified by the name `Xcode-` or `Xcode`. +* The `-` separating `"Xcode"` and the version name is optional, and may be replaced by a space. +* The versioned name may be applied to the `Xcode.app` itself, or a subdirectory underneath `Applications/` containing it. +* You cannot version both the `Xcode.app` filename itself and the containing subfolder. +* Thus, all of the following are equivalent. + * `Applications/Xcode-.app` + * `Applications/Xcode-/Xcode.app` + * `Applications/Xcode.app` + * `Applications/Xcode .app` + * `Applications/Xcode /Xcode.app` +* Both the system `/Applications/` and user `$HOME/Applications/` directories are searched. + * The user's `$HOME/Applications/` takes precedence over `/Applications` for a given version. + * If multiple naming variants within the same `Applications/` folder indicate the same version (for example, `Xcode-3.2.1.app`, `Xcode3.2.1.app`, and `Xcode-3.2.1/Xcode.app`), the precedence order is unspecified and implementation-dependent. +* The `` may be any string that is valid in a filename. +* The special version name `"default"` refers to the "default" unversioned Xcode at `Applications/Xcode.app` (in either `/Applications/` or `$HOME/Applications/`). +* Version names may not start with ``"-"`` or whitespace. + +The restrictions on the naming convention may need to be tightened in the future. In particular, if there are other well-known applications whose names begin with the string `"Xcode"`, the strings allowed for `` may need to be restricted to avoid colliding with other applications. If there's evidence that one of these naming techniques is strongly favored either in practice or by Apple, we may tighten the naming convention to favor it. + +## Caveats + +Using `xcsel` or `xcselv` to select an Xcode that is installed under your `$HOME` may break things for other users, depending on your system setup. We let you do this anyway because some people run OS X as effectively single-user, or have open permissions so this will work. You could also use `$DEVELOPER_DIR` as an alternative to `xcsel` that is scoped to the current user or session, instead of a global setting. + +This does not verify that the version name in the Xcode filename matches the actual version of that binary. It is the user's responsibility to get the names right. diff --git a/plugins/xcode/_xcselv b/plugins/xcode/_xcselv new file mode 100644 index 00000000..f9861d54 --- /dev/null +++ b/plugins/xcode/_xcselv @@ -0,0 +1,19 @@ +#compdef xcselv +#autoload + +function _xcselv_compl_list_versions() { + _omz_xcode_list_versions short +} + +_arguments \ + '(-l -L -p)-h[prints a help message]' \ + '(-L -p -h)-l[lists installed Xcode versions]' \ + '(-l -p -h)-L[lists installed Xcode versions (long form)]' \ + '(-h -l -L)-p[prints active Xcode version]' \ + && ret=0 + +local _xcode_versions +_xcode_versions=($(_xcselv_compl_list_versions)) +_describe -t _xcode_versions 'version' _xcode_versions + +return 1 diff --git a/plugins/xcode/xcode.plugin.zsh b/plugins/xcode/xcode.plugin.zsh index 1f547470..b63a857a 100644 --- a/plugins/xcode/xcode.plugin.zsh +++ b/plugins/xcode/xcode.plugin.zsh @@ -22,28 +22,153 @@ function xc { # Uses naming convention: # - different versions of Xcode are named Xcode-.app or stored # in a folder named Xcode- -# - the special version name "-" refers to the "default" Xcode.app with no suffix +# - the special version name "default" refers to the "default" Xcode.app with no suffix function xcselv { emulate -L zsh + if [[ $# == 0 ]]; then + echo "xcselv: error: no option or argument given" >&2 + echo "xcselv: see 'xcselv -h' for help" >&2 + return 1 + elif [[ $1 == "-p" ]]; then + _omz_xcode_print_active_version + return + elif [[ $1 == "-l" ]]; then + _omz_xcode_list_versions + return + elif [[ $1 == "-L" ]]; then + _omz_xcode_list_versions short + return + elif [[ $1 == "-h" ]]; then + _omz_xcode_print_xcselv_usage + return 0 + elif [[ $1 == -* && $1 != "-" ]]; then + echo "xcselv: error: unrecognized option: $1" >&2 + echo "xcselv: see 'xcselv -h' for help" >&2 + return 1 + fi + # Main case: "xcselv " to select a version local version=$1 - local apps_dirs apps_dir apps app - apps_dirs=( $HOME/Applications /Applications ) - for apps_dir ($apps_dirs); do - if [[ $version == "-" ]]; then - apps=( $apps_dir/Xcode.app $apps_dir/Xcode/Xcode.app ) + local -A xcode_versions + _omz_xcode_locate_versions + if [[ -z ${xcode_versions[$version]} ]]; then + echo "xcselv: error: Xcode version '$version' not found" >&2 + return 1 + fi + app="${xcode_versions[$version]}" + echo "selecting Xcode $version: $app" + xcsel "$app" +} + +function _omz_xcode_print_xcselv_usage { + cat << EOF >&2 +Usage: + xcselv + xcselv [options] + +Options: + set the active Xcode version + -h print this help message and exit + -p print the active Xcode version + -l list installed Xcode versions (long human-readable form) + -L list installed Xcode versions (short form, version names only) +EOF +} + +# Parses the Xcode version from a filename based on our conventions +# Only meaningful when called from other _omz_xcode functions +function _omz_xcode_parse_versioned_file { + local file=$1 + local basename=${app:t} + local dir=${app:h} + local parent=${dir:t} + #echo "parent=$parent basename=$basename verstr=$verstr ver=$ver" >&2 + local verstr + if [[ $parent == Xcode* ]]; then + if [[ $basename == "Xcode.app" ]]; then + # "Xcode-/Xcode.app" format + verstr=$parent else - apps=( $apps_dir/Xcode-$version.app $apps_dir/Xcode-$version/Xcode.app ) + # Both file and parent dir are versioned. Reject. + return 1; fi + elif [[ $basename == Xcode*.app ]]; then + # "Xcode-.app" format + verstr=${basename:r} + else + # Invalid naming pattern + return 1; + fi + + local ver=${verstr#Xcode} + ver=${ver#[- ]} + if [[ -z $ver ]]; then + # Unversioned "default" installation location + ver="default" + fi + print -- "$ver" +} + +# Print the active version, using xcselv's notion of versions +function _omz_xcode_print_active_version { + emulate -L zsh + local -A xcode_versions + local versions version active_path + _omz_xcode_locate_versions + active_path=$(xcode-select -p) + active_path=${active_path%%/Contents/Developer*} + versions=(${(kni)xcode_versions}) + for version ($versions); do + if [[ "${xcode_versions[$version]}" == $active_path ]]; then + printf "%s (%s)\n" $version $active_path + return + fi + done + printf "%s (%s)\n" "" $active_path +} + +# Locates all the installed versions of Xcode on this system, for this +# plugin's internal use. +# Populates the $xcode_versions associative array variable +# Caller should local-ize $xcode_versions with `local -A xcode_versions` +function _omz_xcode_locate_versions { + emulate -L zsh + local -a app_dirs + local app_dir apps app xcode_ver + # In increasing precedence order: + app_dirs=(/Applications $HOME/Applications) + for app_dir ($app_dirs); do + apps=( $app_dir/Xcode*.app(N) $app_dir/Xcode*/Xcode.app(N) ) for app ($apps); do - if [[ -e "$app" ]]; then - echo "selecting Xcode $version: $app" - xcsel "$app" - return + xcode_ver=$(_omz_xcode_parse_versioned_file $app) + if [[ $? != 0 ]]; then + continue fi + xcode_versions[$xcode_ver]=$app done done - echo "xcselv: Xcode version $version not found" - return 1 +} + +function _omz_xcode_list_versions { + emulate -L zsh + local -A xcode_versions + _omz_xcode_locate_versions + local width=1 width_i versions do_short=0 + if [[ $1 == "short" ]]; then + do_short=1 + fi + versions=(${(kni)xcode_versions}) + for version ($versions); do + if [[ $#version > $width ]]; then + width=$#version; + fi + done + for version ($versions); do + if [[ $do_short == 1 ]]; then + printf "%s\n" $version + else + printf "%-${width}s -> %s\n" "$version" "${xcode_versions[$version]}" + fi + done } function simulator {