Blame view

antigen.zsh 6.98 KB
e5dfac7ea   Shrikant Sharat   Initial commit. W...
1
2
3
4
5
6
  #!/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!
9dc631003   Shrikant Sharat   Rename bundle rec...
7
  local _ANTIGEN_BUNDLE_RECORD=""
e5dfac7ea   Shrikant Sharat   Initial commit. W...
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  
  # 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
43bb2cef1   Shrikant Sharat   Variable declarat...
23
24
          local arg_name="$(echo "$position_args" | cut -d\  -f$i)"
          local arg_value="$1"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
25
26
27
28
          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
      # 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...
37
38
          local arg_name="$(echo "$1" | cut -d= -f1 | sed 's/^--//')"
          local arg_value="$(echo "$1" | cut -d= -f2)"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
39
40
41
42
43
44
          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
      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
53
54
55
56
57
58
59
60
          | 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.
9dc631003   Shrikant Sharat   Rename bundle rec...
61
62
      _ANTIGEN_BUNDLE_RECORD="$_ANTIGEN_BUNDLE_RECORD
  $name $url $loc $clone_dir"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
63
64
65
66
67
68
69
70
  
      # Load it, unless specified otherwise.
      if $load; then
          bundle-load "$name"
      fi
  }
  
  bundle-install () {
43bb2cef1   Shrikant Sharat   Variable declarat...
71
      local update=false
e5dfac7ea   Shrikant Sharat   Initial commit. W...
72
      if [[ $1 == --update ]]; then
43bb2cef1   Shrikant Sharat   Variable declarat...
73
          update=true
64df9931b   Shrikant Sharat   Don't leave the -...
74
          shift
e5dfac7ea   Shrikant Sharat   Initial commit. W...
75
      fi
911cc8cb5   Shrikant Sharat   Use ADOTDIR as th...
76
      mkdir -p "$ADOTDIR/bundles"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
77
78
  
      local handled_repos=""
882de6912   Shrikant Sharat   Fix current theme...
79
      local install_bundles=""
e5dfac7ea   Shrikant Sharat   Initial commit. W...
80

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

882de6912   Shrikant Sharat   Fix current theme...
90
91
      # 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...
92
93
      # `$_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...
94
      echo "$install_bundles" | while read spec; do
e5dfac7ea   Shrikant Sharat   Initial commit. W...
95
96
97
98
99
100
101
  
          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...
102
              if [[ ! -d $clone_dir ]]; then
e5dfac7ea   Shrikant Sharat   Initial commit. W...
103
                  git clone "$url" "$clone_dir"
78b101b2e   Shrikant Sharat   bundle-install do...
104
105
              elif $update; then
                  git --git-dir "$clone_dir/.git" pull
e5dfac7ea   Shrikant Sharat   Initial commit. W...
106
107
108
109
110
111
112
              fi
  
              handled_repos="$handled_repos
  $url"
          fi
  
          if [[ $name != *.theme ]]; then
1ba08957c   Shrikant Sharat   Fix repo plugins ...
113
              echo Installing $name
911cc8cb5   Shrikant Sharat   Use ADOTDIR as th...
114
              local bundle_dest="$ADOTDIR/bundles/$name"
2b5d4a0c3   Shrikant Sharat   Create symlinks t...
115
              test -e "$bundle_dest" && rm -rf "$bundle_dest"
3c5772717   Shrikant Sharat   OSX doesn't have ...
116
              ln -s "$clone_dir/$loc" "$bundle_dest"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
117
          else
911cc8cb5   Shrikant Sharat   Use ADOTDIR as th...
118
119
              mkdir -p "$ADOTDIR/bundles/$name"
              cp "$clone_dir/$loc" "$ADOTDIR/bundles/$name"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
120
121
122
123
124
          fi
  
          bundle-load "$name"
  
      done
592f1460b   Shrikant Sharat   Load completions ...
125
126
      # Initialize completions after installing
      bundle-apply
e5dfac7ea   Shrikant Sharat   Initial commit. W...
127
  }
b05176f25   Shrikant Sharat   bundle-install!, ...
128
129
130
  bundle-install! () {
      bundle-install --update
  }
d22fb4b90   Shrikant Sharat   bundle-cleanup co...
131
  bundle-cleanup () {
911cc8cb5   Shrikant Sharat   Use ADOTDIR as th...
132
133
      if [[ ! -d "$ADOTDIR/bundles" || \
          "$(ls "$ADOTDIR/bundles/" | wc -l)" == 0 ]]; then
9dd10834e   Shrikant Sharat   Fix cleanup givin...
134
135
136
          echo "You don't have any bundles."
          return 0
      fi
911cc8cb5   Shrikant Sharat   Use ADOTDIR as th...
137
      # Find directores in ADOTDIR/bundles, that are not in the bundles record.
d22fb4b90   Shrikant Sharat   bundle-cleanup co...
138
      local unidentified_bundles="$(comm -13 \
60d0dc571   Shrikant Sharat   Refactor to priva...
139
          <(-bundle-echo-record | awk '{print $1}' | sort) \
911cc8cb5   Shrikant Sharat   Use ADOTDIR as th...
140
          <(ls -1 "$ADOTDIR/bundles"))"
d22fb4b90   Shrikant Sharat   bundle-cleanup co...
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
  
      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...
157
              rm -rf "$ADOTDIR/bundles/$name"
d22fb4b90   Shrikant Sharat   bundle-cleanup co...
158
159
160
161
162
163
164
              echo ' done.'
          done
      else
          echo
          echo Nothing deleted.
      fi
  }
e5dfac7ea   Shrikant Sharat   Initial commit. W...
165
  bundle-load () {
e5dfac7ea   Shrikant Sharat   Initial commit. W...
166

43bb2cef1   Shrikant Sharat   Variable declarat...
167
      local name="$1"
911cc8cb5   Shrikant Sharat   Use ADOTDIR as th...
168
      local bundle_dir="$ADOTDIR/bundles/$name"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
169
170
  
      # Source the plugin script
43bb2cef1   Shrikant Sharat   Variable declarat...
171
      local script_loc="$bundle_dir/$name.plugin.zsh"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
172
173
174
175
176
177
178
179
180
181
182
183
184
185
      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
6a4fbde8d   Shrikant Sharat   On spot theme swi...
186
187
          local theme_file="$bundle_dir/${name%.theme}.zsh-theme"
          test -f "$theme_file" && source "$theme_file"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
188
      fi
5b2515888   Shrikant Sharat   Fix ash-users/zsh...
189
190
      # Add to $fpath, for completion(s)
      fpath=($bundle_dir $fpath)
e5dfac7ea   Shrikant Sharat   Initial commit. W...
191

e5dfac7ea   Shrikant Sharat   Initial commit. W...
192
193
194
195
196
197
198
199
200
  }
  
  bundle-lib () {
      bundle --name=oh-my-zsh.lib --loc=lib
  }
  
  bundle-theme () {
      local url="$ANTIGEN_DEFAULT_REPO_URL"
      local name="${1:-robbyrussell}"
6a4fbde8d   Shrikant Sharat   On spot theme swi...
201
      bundle-install "$url" --name=$name.theme --loc=themes/$name.zsh-theme
e5dfac7ea   Shrikant Sharat   Initial commit. W...
202
  }
1af74ea9e   Shrikant Sharat   Load completions ...
203
  bundle-apply () {
e5dfac7ea   Shrikant Sharat   Initial commit. W...
204
      # Initialize completion.
e5dfac7ea   Shrikant Sharat   Initial commit. W...
205
206
      compinit -i
  }
df942673b   Shrikant Sharat   Added bundle-list...
207
208
  bundle-list () {
      # List all currently installed bundles
9dc631003   Shrikant Sharat   Rename bundle rec...
209
      if [[ -z "$_ANTIGEN_BUNDLE_RECORD" ]]; then
df942673b   Shrikant Sharat   Added bundle-list...
210
211
212
          echo "You don't have any bundles." >&2
          return 1
      else
60d0dc571   Shrikant Sharat   Refactor to priva...
213
          -bundle-echo-record | awk '{print $1 " " $2 " " $3}'
df942673b   Shrikant Sharat   Added bundle-list...
214
215
      fi
  }
bc9d20c7a   Shrikant Sharat   Add comments.
216
217
  # 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...
218
  -bundle-echo-record () {
9dc631003   Shrikant Sharat   Rename bundle rec...
219
      echo "$_ANTIGEN_BUNDLE_RECORD" | sed -n '1!p'
e5dfac7ea   Shrikant Sharat   Initial commit. W...
220
221
222
  }
  
  -bundle-env-setup () {
3047bfeb7   Shrikant Sharat   Load compinit at ...
223
      # Pre-startup initializations
e5dfac7ea   Shrikant Sharat   Initial commit. W...
224
225
      -set-default ANTIGEN_DEFAULT_REPO_URL \
          https://github.com/robbyrussell/oh-my-zsh.git
911cc8cb5   Shrikant Sharat   Use ADOTDIR as th...
226
      -set-default ADOTDIR $HOME/.antigen
3047bfeb7   Shrikant Sharat   Load compinit at ...
227
228
229
230
231
232
  
      # Load the compinit module
      autoload -U compinit
  
      # Without the following, `compdef` function is not defined.
      compinit -i
e5dfac7ea   Shrikant Sharat   Initial commit. W...
233
234
235
236
237
  }
  
  # 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...
238
239
      local arg_name="$1"
      local arg_value="$2"
e5dfac7ea   Shrikant Sharat   Initial commit. W...
240
241
242
243
      eval "test -z \"\$$arg_name\" && export $arg_name='$arg_value'"
  }
  
  -bundle-env-setup