Blame view

antigen.zsh 6.53 KB
e5dfac7ea   Shrikant Sharat   Initial commit. W...
1
2
3
4
  #!/bin/zsh
  
  # Each line in this string has the following entries separated by a space
  # character.
280ec4481   Shrikant Sharat   name argument for...
5
  # <repo-url>, <plugin-location>, <repo-local-clone-dir>,
d535ba468   Shrikant Sharat   Introduced a new ...
6
  # <bundle-type>
e5dfac7ea   Shrikant Sharat   Initial commit. W...
7
  # FIXME: Is not kept local by zsh!
9dc631003   Shrikant Sharat   Rename bundle rec...
8
  local _ANTIGEN_BUNDLE_RECORD=""
e5dfac7ea   Shrikant Sharat   Initial commit. W...
9
10
  
  # Syntaxes
280ec4481   Shrikant Sharat   name argument for...
11
  #   bundle <url> [<loc>=/]
e5dfac7ea   Shrikant Sharat   Initial commit. W...
12
13
14
15
16
  bundle () {
  
      # Bundle spec arguments' default values.
      local url="$ANTIGEN_DEFAULT_REPO_URL"
      local loc=/
d535ba468   Shrikant Sharat   Introduced a new ...
17
      local btype=plugin
e5dfac7ea   Shrikant Sharat   Initial commit. W...
18
19
20
      local load=true
  
      # Set spec values based on the positional arguments.
280ec4481   Shrikant Sharat   name argument for...
21
      local position_args='url loc'
e5dfac7ea   Shrikant Sharat   Initial commit. W...
22
23
      local i=1
      while ! [[ -z $1 || $1 == --*=* ]]; do
43bb2cef1   Shrikant Sharat   Variable declarat...
24
25
          local arg_name="$(echo "$position_args" | cut -d\  -f$i)"
          local arg_value="$1"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
26
27
28
29
          eval "local $arg_name='$arg_value'"
          shift
          i=$(($i + 1))
      done
1abf1faf6   Shrikant Sharat   Plugins can be sp...
30
31
32
33
34
      # 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...
35
36
37
      # Set spec values from keyword arguments, if any. The remaining arguments
      # are all assumed to be keyword arguments.
      while [[ $1 == --*=* ]]; do
43bb2cef1   Shrikant Sharat   Variable declarat...
38
39
          local arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')"
          local arg_value="$(echo "$1" | cut -d= -f2)"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
40
41
42
43
44
45
          eval "local $arg_name='$arg_value'"
          shift
      done
  
      # Resolve the url.
      if [[ $url != git://* && $url != https://* ]]; then
bb140864c   Shrikant Sharat   The plugin name s...
46
          url="${url%.git}"
bb140864c   Shrikant Sharat   The plugin name s...
47
          url="https://github.com/$url.git"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
48
49
50
      fi
  
      # Plugin's repo will be cloned here.
911cc8cb5   Shrikant Sharat   Use ADOTDIR as th...
51
      local clone_dir="$ADOTDIR/repos/$(echo "$url" \
e5dfac7ea   Shrikant Sharat   Initial commit. W...
52
          | sed -e 's/\.git$//' -e 's./.-SLASH-.g' -e 's.:.-COLON-.g')"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
53
      # Add it to the record.
280ec4481   Shrikant Sharat   name argument for...
54
55
      _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD
  $url $loc $clone_dir $btype"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
56
57
58
  
      # Load it, unless specified otherwise.
      if $load; then
d535ba468   Shrikant Sharat   Introduced a new ...
59
          bundle-load "$clone_dir/$loc" "$btype"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
60
61
62
63
      fi
  }
  
  bundle-install () {
43bb2cef1   Shrikant Sharat   Variable declarat...
64
      local update=false
e5dfac7ea   Shrikant Sharat   Initial commit. W...
65
      if [[ $1 == --update ]]; then
43bb2cef1   Shrikant Sharat   Variable declarat...
66
          update=true
64df9931b   Shrikant Sharat   Don't leave the -...
67
          shift
e5dfac7ea   Shrikant Sharat   Initial commit. W...
68
      fi
911cc8cb5   Shrikant Sharat   Use ADOTDIR as th...
69
      mkdir -p "$ADOTDIR/bundles"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
70
71
  
      local handled_repos=""
882de6912   Shrikant Sharat   Fix current theme...
72
      local install_bundles=""
e5dfac7ea   Shrikant Sharat   Initial commit. W...
73

de4644c0f   Shrikant Sharat   Install plugins i...
74
75
76
      if [[ $# != 0 ]]; then
          # Record and install just the given plugin here and now.
          bundle "$@"
9dc631003   Shrikant Sharat   Rename bundle rec...
77
          install_bundles="$(-bundle-echo-record | tail -1)"
de4644c0f   Shrikant Sharat   Install plugins i...
78
79
      else
          # Install all the plugins, previously recorded.
60d0dc571   Shrikant Sharat   Refactor to priva...
80
          install_bundles="$(-bundle-echo-record)"
882de6912   Shrikant Sharat   Fix current theme...
81
      fi
de4644c0f   Shrikant Sharat   Install plugins i...
82

882de6912   Shrikant Sharat   Fix current theme...
83
84
      # If the above `if` is directly piped to the below `while`, the contents
      # inside the `if` construct are run in a new subshell, so changes to the
9dc631003   Shrikant Sharat   Rename bundle rec...
85
86
      # `$_ANTIGEN_BUNDLE_RECORD` variable are lost after the `if` construct
      # finishes. So, we need the temporary `$install_bundles` variable.
882de6912   Shrikant Sharat   Fix current theme...
87
      echo "$install_bundles" | while read spec; do
e5dfac7ea   Shrikant Sharat   Initial commit. W...
88
89
90
91
92
  
          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}')"
d535ba468   Shrikant Sharat   Introduced a new ...
93
          local btype="$(echo "$spec" | awk '{print $5}')"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
94
95
  
          if [[ -z "$(echo "$handled_repos" | grep -Fm1 "$url")" ]]; then
78b101b2e   Shrikant Sharat   bundle-install do...
96
              if [[ ! -d $clone_dir ]]; then
e5dfac7ea   Shrikant Sharat   Initial commit. W...
97
                  git clone "$url" "$clone_dir"
78b101b2e   Shrikant Sharat   bundle-install do...
98
99
              elif $update; then
                  git --git-dir "$clone_dir/.git" pull
e5dfac7ea   Shrikant Sharat   Initial commit. W...
100
101
102
103
104
              fi
  
              handled_repos="$handled_repos
  $url"
          fi
d535ba468   Shrikant Sharat   Introduced a new ...
105
          bundle-load "$clone_dir/$loc" "$btype"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
106
107
  
      done
592f1460b   Shrikant Sharat   Load completions ...
108
109
      # Initialize completions after installing
      bundle-apply
e5dfac7ea   Shrikant Sharat   Initial commit. W...
110
  }
b05176f25   Shrikant Sharat   bundle-install!, ...
111
112
113
  bundle-install! () {
      bundle-install --update
  }
d22fb4b90   Shrikant Sharat   bundle-cleanup co...
114
  bundle-cleanup () {
911cc8cb5   Shrikant Sharat   Use ADOTDIR as th...
115
116
      if [[ ! -d "$ADOTDIR/bundles" || \
          "$(ls "$ADOTDIR/bundles/" | wc -l)" == 0 ]]; then
9dd10834e   Shrikant Sharat   Fix cleanup givin...
117
118
119
          echo "You don't have any bundles."
          return 0
      fi
911cc8cb5   Shrikant Sharat   Use ADOTDIR as th...
120
      # Find directores in ADOTDIR/bundles, that are not in the bundles record.
d22fb4b90   Shrikant Sharat   bundle-cleanup co...
121
      local unidentified_bundles="$(comm -13 \
60d0dc571   Shrikant Sharat   Refactor to priva...
122
          <(-bundle-echo-record | awk '{print $1}' | sort) \
911cc8cb5   Shrikant Sharat   Use ADOTDIR as th...
123
          <(ls -1 "$ADOTDIR/bundles"))"
d22fb4b90   Shrikant Sharat   bundle-cleanup co...
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  
      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...
911cc8cb5   Shrikant Sharat   Use ADOTDIR as th...
140
              rm -rf "$ADOTDIR/bundles/$name"
d22fb4b90   Shrikant Sharat   bundle-cleanup co...
141
142
143
144
145
146
147
              echo ' done.'
          done
      else
          echo
          echo Nothing deleted.
      fi
  }
e5dfac7ea   Shrikant Sharat   Initial commit. W...
148
  bundle-load () {
e5dfac7ea   Shrikant Sharat   Initial commit. W...
149

d535ba468   Shrikant Sharat   Introduced a new ...
150
151
      local location="$1"
      local btype="$2"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
152

d535ba468   Shrikant Sharat   Introduced a new ...
153
      if [[ $btype == theme ]]; then
e5dfac7ea   Shrikant Sharat   Initial commit. W...
154

d535ba468   Shrikant Sharat   Introduced a new ...
155
156
157
          # Of course, if its a theme, the location would point to the script
          # file.
          source "$location"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
158

d535ba468   Shrikant Sharat   Introduced a new ...
159
      else
e5dfac7ea   Shrikant Sharat   Initial commit. W...
160

d535ba468   Shrikant Sharat   Introduced a new ...
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
          # Source the plugin script
          # FIXME: I don't know. Looks very very ugly. Needs a better
          # implementation once tests are ready.
          local script_loc="$(ls "$location" | grep -m1 '.plugin.zsh$')"
          if [[ -f $script_loc ]]; then
              # If we have a `*.plugin.zsh`, source it.
              source "$script_loc"
          elif [[ ! -z "$(ls "$location" | grep -m1 '.zsh$')" ]]; then
              # If there is no `*.plugin.zsh` file, source *all* the `*.zsh`
              # files.
              for script ($location/*.zsh) source "$script"
          fi
  
          # Add to $fpath, for completion(s)
          fpath=($location $fpath)
  
      fi
e5dfac7ea   Shrikant Sharat   Initial commit. W...
178

e5dfac7ea   Shrikant Sharat   Initial commit. W...
179
180
181
  }
  
  bundle-lib () {
d535ba468   Shrikant Sharat   Introduced a new ...
182
      bundle --loc=lib
e5dfac7ea   Shrikant Sharat   Initial commit. W...
183
184
185
186
187
  }
  
  bundle-theme () {
      local url="$ANTIGEN_DEFAULT_REPO_URL"
      local name="${1:-robbyrussell}"
d535ba468   Shrikant Sharat   Introduced a new ...
188
      bundle --loc=themes/$name.zsh-theme --btype=theme
e5dfac7ea   Shrikant Sharat   Initial commit. W...
189
  }
1af74ea9e   Shrikant Sharat   Load completions ...
190
  bundle-apply () {
e5dfac7ea   Shrikant Sharat   Initial commit. W...
191
      # Initialize completion.
24fbcce0a   Shrikant Sharat   Add comments to b...
192
      # TODO: Doesn't look like this is really necessary. Need to investigate.
e5dfac7ea   Shrikant Sharat   Initial commit. W...
193
194
      compinit -i
  }
df942673b   Shrikant Sharat   Added bundle-list...
195
196
  bundle-list () {
      # List all currently installed bundles
9dc631003   Shrikant Sharat   Rename bundle rec...
197
      if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then
df942673b   Shrikant Sharat   Added bundle-list...
198
199
200
          echo "You don't have any bundles." >&2
          return 1
      else
60d0dc571   Shrikant Sharat   Refactor to priva...
201
          -bundle-echo-record | awk '{print $1 " " $2 " " $3}'
df942673b   Shrikant Sharat   Added bundle-list...
202
203
      fi
  }
bc9d20c7a   Shrikant Sharat   Add comments.
204
205
  # Echo the bundle specs as in the record. The first line is not echoed since it
  # is a blank line.
60d0dc571   Shrikant Sharat   Refactor to priva...
206
  -bundle-echo-record () {
9dc631003   Shrikant Sharat   Rename bundle rec...
207
      echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p'
e5dfac7ea   Shrikant Sharat   Initial commit. W...
208
209
210
  }
  
  -bundle-env-setup () {
3047bfeb7   Shrikant Sharat   Load compinit at ...
211
      # Pre-startup initializations
e5dfac7ea   Shrikant Sharat   Initial commit. W...
212
213
      -set-default ANTIGEN_DEFAULT_REPO_URL \
          https://github.com/robbyrussell/oh-my-zsh.git
911cc8cb5   Shrikant Sharat   Use ADOTDIR as th...
214
      -set-default ADOTDIR $HOME/.antigen
3047bfeb7   Shrikant Sharat   Load compinit at ...
215
216
217
218
219
220
  
      # Load the compinit module
      autoload -U compinit
  
      # Without the following, `compdef` function is not defined.
      compinit -i
e5dfac7ea   Shrikant Sharat   Initial commit. W...
221
222
223
224
225
  }
  
  # Same as `export $1=$2`, but will only happen if the name specified by `$1` is
  # not already set.
  -set-default () {
43bb2cef1   Shrikant Sharat   Variable declarat...
226
227
      local arg_name="$1"
      local arg_value="$2"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
228
229
230
231
      eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'"
  }
  
  -bundle-env-setup