Blame view

antigen.zsh 5.89 KB
e5dfac7ea   Shrikant Sharat   Initial commit. W...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  #!/bin/zsh
  
  # Each line in this string has the following entries separated by a space
  # character.
  # <bundle-name>, <repo-url>, <plugin-location>, <repo-local-clone-dir>
  # FIXME: Is not kept local by zsh!
  local bundles=""
  
  # Syntaxes
  #   bundle <url> [<loc>=/] [<name>]
  bundle () {
  
      # Bundle spec arguments' default values.
      local url="$ANTIGEN_DEFAULT_REPO_URL"
      local loc=/
      local name=
      local load=true
  
      # Set spec values based on the positional arguments.
      local position_args='url loc name'
      local i=1
      while ! [[ -z $1 || $1 == --*=* ]]; do
          arg_name="$(echo "$position_args" | cut -d\  -f$i)"
          arg_value="$1"
          eval "local $arg_name='$arg_value'"
          shift
          i=$(($i + 1))
      done
1abf1faf6   Shrikant Sharat   Plugins can be sp...
29
30
31
32
33
      # Check if url is just the plugin name. Super short syntax.
      if [[ "$url" != */* ]]; then
          loc="plugins/$url"
          url="$ANTIGEN_DEFAULT_REPO_URL"
      fi
e5dfac7ea   Shrikant Sharat   Initial commit. W...
34
35
36
37
38
39
40
41
42
43
44
      # Set spec values from keyword arguments, if any. The remaining arguments
      # are all assumed to be keyword arguments.
      while [[ $1 == --*=* ]]; do
          arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')"
          arg_value="$(echo "$1" | cut -d= -f2)"
          eval "local $arg_name='$arg_value'"
          shift
      done
  
      # Resolve the url.
      if [[ $url != git://* && $url != https://* ]]; then
bb140864c   Shrikant Sharat   The plugin name s...
45
          url="${url%.git}"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
46
          name="$(basename "$url")"
bb140864c   Shrikant Sharat   The plugin name s...
47
          url="https://github.com/$url.git"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
48
49
50
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
      fi
  
      # Plugin's repo will be cloned here.
      local clone_dir="$ANTIGEN_REPO_CACHE/$(echo "$url" \
          | sed -e 's/\.git$//' -e 's./.-SLASH-.g' -e 's.:.-COLON-.g')"
  
      # Make an intelligent guess about the name of the plugin, if not already
      # done or is explicitly specified.
      if [[ -z $name ]]; then
          name="$(basename $url/$loc)"
      fi
  
      # Add it to the record.
      bundles="$bundles
  $name $url $loc $clone_dir"
  
      # Load it, unless specified otherwise.
      if $load; then
          bundle-load "$name"
      fi
  }
  
  bundle-install () {
  
      if [[ $1 == --update ]]; then
          local update=true
      else
          local update=false
      fi
  
      mkdir -p "$ANTIGEN_BUNDLE_DIR"
  
      local handled_repos=""
  
      echo-non-empty "$bundles" | while read spec; do
1ba08957c   Shrikant Sharat   Fix repo plugins ...
83
          echo "-> $spec"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
84
85
86
87
88
89
90
  
          local name="$(echo "$spec" | awk '{print $1}')"
          local url="$(echo "$spec" | awk '{print $2}')"
          local loc="$(echo "$spec" | awk '{print $3}')"
          local clone_dir="$(echo "$spec" | awk '{print $4}')"
  
          if [[ -z "$(echo "$handled_repos" | grep -Fm1 "$url")" ]]; then
78b101b2e   Shrikant Sharat   bundle-install do...
91
              if [[ ! -d $clone_dir ]]; then
e5dfac7ea   Shrikant Sharat   Initial commit. W...
92
                  git clone "$url" "$clone_dir"
78b101b2e   Shrikant Sharat   bundle-install do...
93
94
              elif $update; then
                  git --git-dir "$clone_dir/.git" pull
e5dfac7ea   Shrikant Sharat   Initial commit. W...
95
96
97
98
99
100
101
              fi
  
              handled_repos="$handled_repos
  $url"
          fi
  
          if [[ $name != *.theme ]]; then
1ba08957c   Shrikant Sharat   Fix repo plugins ...
102
              echo Installing $name
2b5d4a0c3   Shrikant Sharat   Create symlinks t...
103
104
              local bundle_dest="$ANTIGEN_BUNDLE_DIR/$name"
              test -e "$bundle_dest" && rm -rf "$bundle_dest"
3c5772717   Shrikant Sharat   OSX doesn't have ...
105
              ln -s "$clone_dir/$loc" "$bundle_dest"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
106
107
108
109
110
111
112
113
114
115
          else
              mkdir -p "$ANTIGEN_BUNDLE_DIR/$name"
              cp "$clone_dir/$loc" "$ANTIGEN_BUNDLE_DIR/$name"
          fi
  
          bundle-load "$name"
  
      done
  
  }
b05176f25   Shrikant Sharat   bundle-install!, ...
116
117
118
  bundle-install! () {
      bundle-install --update
  }
d22fb4b90   Shrikant Sharat   bundle-cleanup co...
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  bundle-cleanup () {
  
      # Find directores in ANTIGEN_BUNDLE_DIR, that are not in the bundles record.
      local unidentified_bundles="$(comm -13 \
          <(echo-non-empty "$bundles" | awk '{print $1}' | sort) \
          <(ls -1 "$ANTIGEN_BUNDLE_DIR"))"
  
      if [[ -z $unidentified_bundles ]]; then
          echo "You don't have any unidentified bundles."
          return 0
      fi
  
      echo The following bundles are not recorded:
      echo "$unidentified_bundles" | sed 's/^/  /'
  
      echo -n '
  Delete them all? [y/N] '
      if read -q; then
          echo
          echo
          echo "$unidentified_bundles" | while read name; do
              echo -n Deleting $name...
              rm -rf "$ANTIGEN_BUNDLE_DIR/$name"
              echo ' done.'
          done
      else
          echo
          echo Nothing deleted.
      fi
  }
e5dfac7ea   Shrikant Sharat   Initial commit. W...
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  bundle-load () {
      if [[ $1 == --init ]]; then
          do_init=true
          shift
      else
          do_init=false
      fi
  
      name="$1"
      bundle_dir="$ANTIGEN_BUNDLE_DIR/$name"
  
      # Source the plugin script
      script_loc="$bundle_dir/$name.plugin.zsh"
      if [[ -f $script_loc ]]; then
          source "$script_loc"
      fi
  
      # If the name of the plugin ends with `.lib`, all the *.zsh files in it are
      # sourced. This is kind of a hack to source the libraries of oh-my-zsh.
      if [[ $name == *.lib ]]; then
          # FIXME: This throws an error if no files match the given glob pattern.
          for lib ($bundle_dir/*.zsh) source $lib
      fi
  
      # If the name ends with `.theme`, it is handled as if it were a zsh-theme
      # plugin.
      if [[ $name == *.theme ]]; then
          source "$bundle_dir/${name%.theme}.zsh-theme"
      fi
5b2515888   Shrikant Sharat   Fix ash-users/zsh...
178
179
      # Add to $fpath, for completion(s)
      fpath=($bundle_dir $fpath)
e5dfac7ea   Shrikant Sharat   Initial commit. W...
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
  
      if $do_init; then
          bundle-init
      fi
  }
  
  bundle-lib () {
      bundle --name=oh-my-zsh.lib --loc=lib
  }
  
  bundle-theme () {
      local url="$ANTIGEN_DEFAULT_REPO_URL"
      local name="${1:-robbyrussell}"
      bundle "$url" --name=$name.theme --loc=themes/$name.zsh-theme
  }
1af74ea9e   Shrikant Sharat   Load completions ...
195
  bundle-apply () {
e5dfac7ea   Shrikant Sharat   Initial commit. W...
196
      # Initialize completion.
f554ec358   Shrikant Sharat   Autoload compinit...
197
198
199
      if ! type compinit &>/dev/null; then
          autoload -U compinit
      fi
e5dfac7ea   Shrikant Sharat   Initial commit. W...
200
201
      compinit -i
  }
e5dfac7ea   Shrikant Sharat   Initial commit. W...
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
  # Does what it says.
  echo-non-empty () {
      echo "$@" | while read line; do
          [[ $line != "" ]] && echo $line
      done
  }
  
  -bundle-env-setup () {
      -set-default ANTIGEN_DEFAULT_REPO_URL \
          https://github.com/robbyrussell/oh-my-zsh.git
      -set-default ANTIGEN_REPO_CACHE $HOME/.antigen/cache
      -set-default ANTIGEN_BUNDLE_DIR $HOME/.antigen/bundles
  }
  
  # Same as `export $1=$2`, but will only happen if the name specified by `$1` is
  # not already set.
  -set-default () {
      arg_name="$1"
      arg_value="$2"
      eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'"
  }
  
  -bundle-env-setup