xcode.plugin.zsh 5.31 KB
Newer Older
Marc Cornellà's avatar
Marc Cornellà committed
1
2
3
4
5
6
7
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
8
function xc {
9
10
  local xcode_proj
  xcode_proj=(*.{xcworkspace,xcodeproj}(N))
11

12
  if [[ ${#xcode_proj} -eq 0 ]]; then
13
    echo "No xcworkspace/xcodeproj file found in the current directory."
14
    return 1
15
  else
16
17
    echo "Found ${xcode_proj[1]}"
    open "${xcode_proj[1]}"
18
19
20
  fi
}

21
22
23
24
# "XCode-SELect by Version" - select Xcode by just version number
# Uses naming convention:
#  - different versions of Xcode are named Xcode-<version>.app or stored
#     in a folder named Xcode-<version>
25
#  - the special version name "default" refers to the "default" Xcode.app with no suffix
26
27
function xcselv {
  emulate -L zsh
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  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 <version>" to select a version
50
  local version=$1
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  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 <version>
  xcselv [options]

Options:
  <version> 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-<version>/Xcode.app" format
      verstr=$parent
90
    else
91
92
      # Both file and parent dir are versioned. Reject.
      return 1;
93
    fi
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  elif [[ $basename == Xcode*.app ]]; then
    # "Xcode-<version>.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" "<unknown>" $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) )
141
    for app ($apps); do
142
143
144
      xcode_ver=$(_omz_xcode_parse_versioned_file $app)
      if [[ $? != 0 ]]; then
        continue
145
      fi
146
      xcode_versions[$xcode_ver]=$app
147
148
    done
  done
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
}

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
172
173
}

174
175
176
177
178
179
180
181
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
Gutem's avatar
Gutem committed
182
  elif [[ -d "${devfolder}/Applications/iOS Simulator.app" ]]; then
183
    open "${devfolder}/Applications/iOS Simulator.app"
Gutem's avatar
Gutem committed
184
185
186
  # Xcode ≥ 7.x
  else
    open "${devfolder}/Applications/Simulator.app"
187
188
  fi
}