Imported Upstream version 0.4.0
Laurent Arnoud
10 years ago
0 | Copyright (c) 2011 Sam Stephenson | |
0 | Copyright (c) 2013 Sam Stephenson | |
1 | 1 | |
2 | 2 | Permission is hereby granted, free of charge, to any person obtaining |
3 | 3 | a copy of this software and associated documentation files (the |
0 | # Simple Ruby Version Management: rbenv | |
1 | ||
2 | rbenv lets you easily switch between multiple versions of Ruby. It's | |
3 | simple, unobtrusive, and follows the UNIX tradition of single-purpose | |
4 | tools that do one thing well. | |
5 | ||
6 | <img src="http://i.sstephenson.us/rbenv2.png" width="894" height="464"> | |
7 | ||
8 | ### rbenv _does…_ | |
9 | ||
10 | * Let you **change the global Ruby version** on a per-user basis. | |
11 | * Provide support for **per-project Ruby versions**. | |
12 | * Allow you to **override the Ruby version** with an environment | |
13 | variable. | |
14 | ||
15 | ### In contrast with rvm, rbenv _does not…_ | |
16 | ||
17 | * **Need to be loaded into your shell.** Instead, rbenv's shim | |
18 | approach works by adding a directory to your `$PATH`. | |
19 | * **Override shell commands like `cd`.** That's dangerous and | |
20 | error-prone. | |
21 | * **Have a configuration file.** There's nothing to configure except | |
22 | which version of Ruby you want to use. | |
23 | * **Install Ruby.** You can build and install Ruby yourself, or use | |
24 | [ruby-build](https://github.com/sstephenson/ruby-build) to | |
25 | automate the process. | |
26 | * **Manage gemsets.** [Bundler](http://gembundler.com/) is a better | |
27 | way to manage application dependencies. If you have projects that | |
28 | are not yet using Bundler you can install the | |
29 | [rbenv-gemset](https://github.com/jamis/rbenv-gemset) plugin. | |
30 | * **Require changes to Ruby libraries for compatibility.** The | |
31 | simplicity of rbenv means as long as it's in your `$PATH`, | |
32 | [nothing](https://rvm.beginrescueend.com/integration/bundler/) | |
33 | [else](https://rvm.beginrescueend.com/integration/capistrano/) | |
34 | needs to know about it. | |
35 | * **Prompt you with warnings when you switch to a project.** Instead | |
36 | of executing arbitrary code, rbenv reads just the version name | |
37 | from each project. There's nothing to "trust." | |
0 | # Groom your app’s Ruby environment with rbenv. | |
1 | ||
2 | Use rbenv to pick a Ruby version for your application and guarantee | |
3 | that your development environment matches production. Put rbenv to work | |
4 | with [Bundler](http://gembundler.com/) for painless Ruby upgrades and | |
5 | bulletproof deployments. | |
6 | ||
7 | **Powerful in development.** Specify your app's Ruby version once, | |
8 | in a single file. Keep all your teammates on the same page. No | |
9 | headaches running apps on different versions of Ruby. Just Works™ | |
10 | from the command line and with app servers like [Pow](http://pow.cx). | |
11 | Override the Ruby version anytime: just set an environment variable. | |
12 | ||
13 | **Rock-solid in production.** Your application's executables are its | |
14 | interface with ops. With rbenv and [Bundler | |
15 | binstubs](https://github.com/sstephenson/rbenv/wiki/Understanding-binstubs) | |
16 | you'll never again need to `cd` in a cron job or Chef recipe to | |
17 | ensure you've selected the right runtime. The Ruby version | |
18 | dependency lives in one place—your app—so upgrades and rollbacks are | |
19 | atomic, even when you switch versions. | |
20 | ||
21 | **One thing well.** rbenv is concerned solely with switching Ruby | |
22 | versions. It's simple and predictable. A rich plugin ecosystem lets | |
23 | you tailor it to suit your needs. Compile your own Ruby versions, or | |
24 | use the [ruby-build](https://github.com/sstephenson/ruby-build) | |
25 | plugin to automate the process. Specify per-application environment | |
26 | variables with [rbenv-vars](https://github.com/sstephenson/rbenv-vars). | |
27 | See more [plugins on the | |
28 | wiki](https://github.com/sstephenson/rbenv/wiki/Plugins). | |
29 | ||
30 | [**Why choose rbenv over | |
31 | RVM?**](https://github.com/sstephenson/rbenv/wiki/Why-rbenv%3F) | |
38 | 32 | |
39 | 33 | ## Table of Contents |
40 | 34 | |
41 | * [1 How It Works](#section_1) | |
42 | * [2 Installation](#section_2) | |
43 | * [2.1 Basic GitHub Checkout](#section_2.1) | |
44 | * [2.1.1 Upgrading](#section_2.1.1) | |
45 | * [2.2 Homebrew on Mac OS X](#section_2.2) | |
46 | * [2.3 Neckbeard Configuration](#section_2.3) | |
47 | * [3 Usage](#section_3) | |
48 | * [3.1 rbenv global](#section_3.1) | |
49 | * [3.2 rbenv local](#section_3.2) | |
50 | * [3.3 rbenv shell](#section_3.3) | |
51 | * [3.4 rbenv versions](#section_3.4) | |
52 | * [3.5 rbenv version](#section_3.5) | |
53 | * [3.6 rbenv rehash](#section_3.6) | |
54 | * [3.7 rbenv which](#section_3.7) | |
55 | * [3.8 rbenv whence](#section_3.8) | |
56 | * [4 Development](#section_4) | |
57 | * [4.1 Version History](#section_4.1) | |
58 | * [4.2 License](#section_4.2) | |
59 | ||
60 | ## <a name="section_1"></a> 1 How It Works | |
61 | ||
62 | rbenv operates on the per-user directory `~/.rbenv`. Version names in | |
63 | rbenv correspond to subdirectories of `~/.rbenv/versions`. For | |
64 | example, you might have `~/.rbenv/versions/1.8.7-p354` and | |
65 | `~/.rbenv/versions/1.9.3-rc1`. | |
66 | ||
67 | Each version is a working tree with its own binaries, like | |
68 | `~/.rbenv/versions/1.8.7-p354/bin/ruby` and | |
69 | `~/.rbenv/versions/1.9.3-rc1/bin/irb`. rbenv makes _shim binaries_ | |
70 | for every such binary across all installed versions of Ruby. | |
71 | ||
72 | These shims are simple wrapper scripts that live in `~/.rbenv/shims` | |
73 | and detect which Ruby version you want to use. They insert the | |
74 | directory for the selected version at the beginning of your `$PATH` | |
75 | and then execute the corresponding binary. | |
76 | ||
77 | Because of the simplicity of the shim approach, all you need to use | |
78 | rbenv is `~/.rbenv/shims` in your `$PATH`. | |
79 | ||
80 | ## <a name="section_2"></a> 2 Installation | |
81 | ||
82 | **Compatibility note**: rbenv is _incompatible_ with rvm. Things will | |
83 | appear to work until you try to install a gem. The problem is that | |
84 | rvm actually overrides the `gem` command with a shell function! | |
85 | Please remove any references to rvm before using rbenv. | |
86 | ||
87 | ### <a name="section_2.1"></a> 2.1 Basic GitHub Checkout | |
35 | * [How It Works](#how-it-works) | |
36 | * [Understanding PATH](#understanding-path) | |
37 | * [Understanding Shims](#understanding-shims) | |
38 | * [Choosing the Ruby Version](#choosing-the-ruby-version) | |
39 | * [Locating the Ruby Installation](#locating-the-ruby-installation) | |
40 | * [Installation](#installation) | |
41 | * [Basic GitHub Checkout](#basic-github-checkout) | |
42 | * [Upgrading](#upgrading) | |
43 | * [Homebrew on Mac OS X](#homebrew-on-mac-os-x) | |
44 | * [Neckbeard Configuration](#neckbeard-configuration) | |
45 | * [Uninstalling Ruby Versions](#uninstalling-ruby-versions) | |
46 | * [Command Reference](#command-reference) | |
47 | * [rbenv local](#rbenv-local) | |
48 | * [rbenv global](#rbenv-global) | |
49 | * [rbenv shell](#rbenv-shell) | |
50 | * [rbenv versions](#rbenv-versions) | |
51 | * [rbenv version](#rbenv-version) | |
52 | * [rbenv rehash](#rbenv-rehash) | |
53 | * [rbenv which](#rbenv-which) | |
54 | * [rbenv whence](#rbenv-whence) | |
55 | * [Development](#development) | |
56 | * [Version History](#version-history) | |
57 | * [License](#license) | |
58 | ||
59 | ## How It Works | |
60 | ||
61 | At at high level, rbenv intercepts Ruby commands using shim | |
62 | executables injected into your `PATH`, determines which Ruby version | |
63 | has been specified by your application, and passes your commands along | |
64 | to the correct Ruby installation. | |
65 | ||
66 | ### Understanding PATH | |
67 | ||
68 | When you run a command like `ruby` or `rake`, your operating system | |
69 | searches through a list of directories to find an executable file with | |
70 | that name. This list of directories lives in an environment variable | |
71 | called `PATH`, with each directory in the list separated by a colon: | |
72 | ||
73 | /usr/local/bin:/usr/bin:/bin | |
74 | ||
75 | Directories in `PATH` are searched from left to right, so a matching | |
76 | executable in a directory at the beginning of the list takes | |
77 | precedence over another one at the end. In this example, the | |
78 | `/usr/local/bin` directory will be searched first, then `/usr/bin`, | |
79 | then `/bin`. | |
80 | ||
81 | ### Understanding Shims | |
82 | ||
83 | rbenv works by inserting a directory of _shims_ at the front of your | |
84 | `PATH`: | |
85 | ||
86 | ~/.rbenv/shims:/usr/local/bin:/usr/bin:/bin | |
87 | ||
88 | Through a process called _rehashing_, rbenv maintains shims in that | |
89 | directory to match every Ruby command across every installed version | |
90 | of Ruby—`irb`, `gem`, `rake`, `rails`, `ruby`, and so on. | |
91 | ||
92 | Shims are lightweight executables that simply pass your command along | |
93 | to rbenv. So with rbenv installed, when you run, say, `rake`, your | |
94 | operating system will do the following: | |
95 | ||
96 | * Search your `PATH` for an executable file named `rake` | |
97 | * Find the rbenv shim named `rake` at the beginning of your `PATH` | |
98 | * Run the shim named `rake`, which in turn passes the command along to | |
99 | rbenv | |
100 | ||
101 | ### Choosing the Ruby Version | |
102 | ||
103 | When you execute a shim, rbenv determines which Ruby version to use by | |
104 | reading it from the following sources, in this order: | |
105 | ||
106 | 1. The `RBENV_VERSION` environment variable, if specified. You can use | |
107 | the [`rbenv shell`](#rbenv-shell) command to set this environment | |
108 | variable in your current shell session. | |
109 | ||
110 | 2. The application-specific `.ruby-version` file in the current | |
111 | directory, if present. You can modify the current directory's | |
112 | `.ruby-version` file with the [`rbenv local`](#rbenv-local) | |
113 | command. | |
114 | ||
115 | 3. The first `.ruby-version` file found by searching each parent | |
116 | directory until reaching the root of your filesystem, if any. | |
117 | ||
118 | 4. The global `~/.rbenv/version` file. You can modify this file using | |
119 | the [`rbenv global`](#rbenv-global) command. If the global version | |
120 | file is not present, rbenv assumes you want to use the "system" | |
121 | Ruby—i.e. whatever version would be run if rbenv weren't in your | |
122 | path. | |
123 | ||
124 | ### Locating the Ruby Installation | |
125 | ||
126 | Once rbenv has determined which version of Ruby your application has | |
127 | specified, it passes the command along to the corresponding Ruby | |
128 | installation. | |
129 | ||
130 | Each Ruby version is installed into its own directory under | |
131 | `~/.rbenv/versions`. For example, you might have these versions | |
132 | installed: | |
133 | ||
134 | * `~/.rbenv/versions/1.8.7-p371/` | |
135 | * `~/.rbenv/versions/1.9.3-p327/` | |
136 | * `~/.rbenv/versions/jruby-1.7.1/` | |
137 | ||
138 | Version names to rbenv are simply the names of the directories in | |
139 | `~/.rbenv/versions`. | |
140 | ||
141 | ## Installation | |
142 | ||
143 | **Compatibility note**: rbenv is _incompatible_ with RVM. Please make | |
144 | sure to fully uninstall RVM and remove any references to it from | |
145 | your shell initialization files before installing rbenv. | |
146 | ||
147 | If you're on Mac OS X, consider | |
148 | [installing with Homebrew](#homebrew-on-mac-os-x). | |
149 | ||
150 | ### Basic GitHub Checkout | |
88 | 151 | |
89 | 152 | This will get you going with the latest version of rbenv and make it |
90 | 153 | easy to fork and contribute any changes back upstream. |
91 | 154 | |
92 | 155 | 1. Check out rbenv into `~/.rbenv`. |
93 | 156 | |
94 | $ cd | |
95 | $ git clone git://github.com/sstephenson/rbenv.git .rbenv | |
157 | ~~~ sh | |
158 | $ git clone git://github.com/sstephenson/rbenv.git ~/.rbenv | |
159 | ~~~ | |
96 | 160 | |
97 | 161 | 2. Add `~/.rbenv/bin` to your `$PATH` for access to the `rbenv` |
98 | 162 | command-line utility. |
99 | 163 | |
100 | $ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile | |
101 | ||
102 | **Zsh note**: Modifiy your `~/.zshenv` file instead of `~/.bash_profile`. | |
103 | ||
104 | 3. Add rbenv init to your shell to enable shims and autocompletion. | |
105 | ||
106 | $ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile | |
107 | ||
108 | **Zsh note**: Modifiy your `~/.zshenv` file instead of `~/.bash_profile`. | |
109 | ||
110 | 4. Restart your shell so the path changes take effect. You can now | |
111 | begin using rbenv. | |
112 | ||
113 | $ exec $SHELL | |
114 | ||
115 | 5. Install Ruby versions into `~/.rbenv/versions`. For example, to | |
116 | install Ruby 1.9.2-p290, download and unpack the source, then run: | |
117 | ||
118 | $ ./configure --prefix=$HOME/.rbenv/versions/1.9.2-p290 | |
119 | $ make | |
120 | $ make install | |
121 | ||
122 | The [ruby-build](https://github.com/sstephenson/ruby-build) project | |
123 | provides an `rbenv install` command that simplifies the process of | |
124 | installing new Ruby versions to: | |
125 | ||
126 | $ rbenv install 1.9.2-p290 | |
127 | ||
128 | 6. Rebuild the shim binaries. You should do this any time you install | |
129 | a new Ruby binary (for example, when installing a new Ruby version, | |
130 | or when installing a gem that provides a binary). | |
131 | ||
132 | $ rbenv rehash | |
133 | ||
134 | #### <a name="section_2.1.1"></a> 2.1.1 Upgrading | |
135 | ||
136 | If you've installed rbenv using the instructions above, you can | |
137 | upgrade your installation at any time using git. | |
138 | ||
139 | To upgrade to the latest development version of rbenv, use `git pull`: | |
140 | ||
141 | $ cd ~/.rbenv | |
142 | $ git pull | |
143 | ||
144 | To upgrade to a specific release of rbenv, check out the corresponding | |
145 | tag: | |
146 | ||
147 | $ cd ~/.rbenv | |
148 | $ git fetch | |
149 | $ git tag | |
150 | v0.1.0 | |
151 | v0.1.1 | |
152 | v0.1.2 | |
153 | v0.2.0 | |
154 | $ git checkout v0.2.0 | |
155 | ||
156 | ### <a name="section_2.2"></a> 2.2 Homebrew on Mac OS X | |
164 | ~~~ sh | |
165 | $ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile | |
166 | ~~~ | |
167 | ||
168 | **Ubuntu note**: Modify your `~/.profile` instead of `~/.bash_profile`. | |
169 | ||
170 | **Zsh note**: Modify your `~/.zshrc` file instead of `~/.bash_profile`. | |
171 | ||
172 | 3. Add `rbenv init` to your shell to enable shims and autocompletion. | |
173 | ||
174 | ~~~ sh | |
175 | $ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile | |
176 | ~~~ | |
177 | ||
178 | _Same as in previous step, use `~/.profile` on Ubuntu, `~/.zshrc` for zsh._ | |
179 | ||
180 | 4. Restart your shell as a login shell so the path changes take effect. | |
181 | You can now begin using rbenv. | |
182 | ||
183 | ~~~ sh | |
184 | $ exec $SHELL -l | |
185 | ~~~ | |
186 | ||
187 | 5. Install [ruby-build](https://github.com/sstephenson/ruby-build), | |
188 | which provides an `rbenv install` command that simplifies the | |
189 | process of installing new Ruby versions. | |
190 | ||
191 | ~~~ | |
192 | $ rbenv install 1.9.3-p327 | |
193 | ~~~ | |
194 | ||
195 | As an alternative, you can download and compile Ruby yourself into | |
196 | `~/.rbenv/versions/`. | |
197 | ||
198 | 6. Rebuild the shim executables. You should do this any time you | |
199 | install a new Ruby executable (for example, when installing a new | |
200 | Ruby version, or when installing a gem that provides a command). | |
201 | ||
202 | ~~~ | |
203 | $ rbenv rehash | |
204 | ~~~ | |
205 | ||
206 | #### Upgrading | |
207 | ||
208 | If you've installed rbenv manually using git, you can upgrade your | |
209 | installation to the cutting-edge version at any time. | |
210 | ||
211 | ~~~ sh | |
212 | $ cd ~/.rbenv | |
213 | $ git pull | |
214 | ~~~ | |
215 | ||
216 | To use a specific release of rbenv, check out the corresponding tag: | |
217 | ||
218 | ~~~ sh | |
219 | $ cd ~/.rbenv | |
220 | $ git fetch | |
221 | $ git checkout v0.3.0 | |
222 | ~~~ | |
223 | ||
224 | ### Homebrew on Mac OS X | |
157 | 225 | |
158 | 226 | You can also install rbenv using the |
159 | 227 | [Homebrew](http://mxcl.github.com/homebrew/) package manager on Mac OS |
160 | 228 | X. |
161 | 229 | |
162 | $ brew update | |
163 | $ brew install rbenv | |
164 | $ brew install ruby-build | |
165 | ||
166 | The same commands can be used for upgrading. | |
230 | ~~~ | |
231 | $ brew update | |
232 | $ brew install rbenv | |
233 | $ brew install ruby-build | |
234 | ~~~ | |
235 | ||
236 | To later update these installs, use `upgrade` instead of `install`. | |
167 | 237 | |
168 | 238 | Afterwards you'll still need to add `eval "$(rbenv init -)"` to your |
169 | 239 | profile as stated in the caveats. You'll only ever have to do this |
170 | 240 | once. |
171 | 241 | |
172 | ### <a name="section_2.3"></a> 2.3 Neckbeard Configuration | |
242 | ### Neckbeard Configuration | |
173 | 243 | |
174 | 244 | Skip this section unless you must know what every line in your shell |
175 | 245 | profile is doing. |
201 | 271 | Run `rbenv init -` for yourself to see exactly what happens under the |
202 | 272 | hood. |
203 | 273 | |
204 | ## <a name="section_3"></a> 3 Usage | |
274 | ### Uninstalling Ruby Versions | |
275 | ||
276 | As time goes on, Ruby versions you install will accumulate in your | |
277 | `~/.rbenv/versions` directory. | |
278 | ||
279 | To remove old Ruby versions, simply `rm -rf` the directory of the | |
280 | version you want to remove. You can find the directory of a particular | |
281 | Ruby verison with the `rbenv prefix` command, e.g. `rbenv prefix | |
282 | 1.8.7-p357`. | |
283 | ||
284 | The [ruby-build](https://github.com/sstephenson/ruby-build) plugin | |
285 | provides an `rbenv uninstall` command to automate the removal | |
286 | process. | |
287 | ||
288 | ## Command Reference | |
205 | 289 | |
206 | 290 | Like `git`, the `rbenv` command delegates to subcommands based on its |
207 | 291 | first argument. The most common subcommands are: |
208 | 292 | |
209 | ### <a name="section_3.1"></a> 3.1 rbenv global | |
293 | ### rbenv local | |
294 | ||
295 | Sets a local application-specific Ruby version by writing the version | |
296 | name to a `.ruby-version` file in the current directory. This version | |
297 | overrides the global version, and can be overridden itself by setting | |
298 | the `RBENV_VERSION` environment variable or with the `rbenv shell` | |
299 | command. | |
300 | ||
301 | $ rbenv local 1.9.3-p327 | |
302 | ||
303 | When run without a version number, `rbenv local` reports the currently | |
304 | configured local version. You can also unset the local version: | |
305 | ||
306 | $ rbenv local --unset | |
307 | ||
308 | Previous versions of rbenv stored local version specifications in a | |
309 | file named `.rbenv-version`. For backwards compatibility, rbenv will | |
310 | read a local version specified in an `.rbenv-version` file, but a | |
311 | `.ruby-version` file in the same directory will take precedence. | |
312 | ||
313 | ### rbenv global | |
210 | 314 | |
211 | 315 | Sets the global version of Ruby to be used in all shells by writing |
212 | 316 | the version name to the `~/.rbenv/version` file. This version can be |
213 | overridden by a per-project `.rbenv-version` file, or by setting the | |
214 | `RBENV_VERSION` environment variable. | |
215 | ||
216 | $ rbenv global 1.9.2-p290 | |
317 | overridden by an application-specific `.ruby-version` file, or by | |
318 | setting the `RBENV_VERSION` environment variable. | |
319 | ||
320 | $ rbenv global 1.8.7-p352 | |
217 | 321 | |
218 | 322 | The special version name `system` tells rbenv to use the system Ruby |
219 | 323 | (detected by searching your `$PATH`). |
221 | 325 | When run without a version number, `rbenv global` reports the |
222 | 326 | currently configured global version. |
223 | 327 | |
224 | ### <a name="section_3.2"></a> 3.2 rbenv local | |
225 | ||
226 | Sets a local per-project Ruby version by writing the version name to | |
227 | an `.rbenv-version` file in the current directory. This version | |
228 | overrides the global, and can be overridden itself by setting the | |
229 | `RBENV_VERSION` environment variable or with the `rbenv shell` | |
230 | command. | |
231 | ||
232 | $ rbenv local rbx-1.2.4 | |
233 | ||
234 | When run without a version number, `rbenv local` reports the currently | |
235 | configured local version. You can also unset the local version: | |
236 | ||
237 | $ rbenv local --unset | |
238 | ||
239 | ### <a name="section_3.3"></a> 3.3 rbenv shell | |
328 | ### rbenv shell | |
240 | 329 | |
241 | 330 | Sets a shell-specific Ruby version by setting the `RBENV_VERSION` |
242 | environment variable in your shell. This version overrides both | |
243 | project-specific versions and the global version. | |
244 | ||
245 | $ rbenv shell jruby-1.6.4 | |
331 | environment variable in your shell. This version overrides | |
332 | application-specific versions and the global version. | |
333 | ||
334 | $ rbenv shell jruby-1.7.1 | |
246 | 335 | |
247 | 336 | When run without a version number, `rbenv shell` reports the current |
248 | 337 | value of `RBENV_VERSION`. You can also unset the shell version: |
254 | 343 | prefer not to use shell integration, you may simply set the |
255 | 344 | `RBENV_VERSION` variable yourself: |
256 | 345 | |
257 | $ export RBENV_VERSION=jruby-1.6.4 | |
258 | ||
259 | ### <a name="section_3.4"></a> 3.4 rbenv versions | |
346 | $ export RBENV_VERSION=jruby-1.7.1 | |
347 | ||
348 | ### rbenv versions | |
260 | 349 | |
261 | 350 | Lists all Ruby versions known to rbenv, and shows an asterisk next to |
262 | 351 | the currently active version. |
264 | 353 | $ rbenv versions |
265 | 354 | 1.8.7-p352 |
266 | 355 | 1.9.2-p290 |
267 | * 1.9.3-rc1 (set by /Users/sam/.rbenv/global) | |
268 | jruby-1.6.4 | |
356 | * 1.9.3-p327 (set by /Users/sam/.rbenv/version) | |
357 | jruby-1.7.1 | |
269 | 358 | rbx-1.2.4 |
270 | 359 | ree-1.8.7-2011.03 |
271 | 360 | |
272 | ### <a name="section_3.5"></a> 3.5 rbenv version | |
361 | ### rbenv version | |
273 | 362 | |
274 | 363 | Displays the currently active Ruby version, along with information on |
275 | 364 | how it was set. |
276 | 365 | |
277 | 366 | $ rbenv version |
278 | 1.8.7-p352 (set by /Volumes/37signals/basecamp/.rbenv-version) | |
279 | ||
280 | ### <a name="section_3.6"></a> 3.6 rbenv rehash | |
281 | ||
282 | Installs shims for all Ruby binaries known to rbenv (i.e., | |
367 | 1.8.7-p352 (set by /Volumes/37signals/basecamp/.ruby-version) | |
368 | ||
369 | ### rbenv rehash | |
370 | ||
371 | Installs shims for all Ruby executables known to rbenv (i.e., | |
283 | 372 | `~/.rbenv/versions/*/bin/*`). Run this command after you install a new |
284 | version of Ruby, or install a gem that provides binaries. | |
373 | version of Ruby, or install a gem that provides commands. | |
285 | 374 | |
286 | 375 | $ rbenv rehash |
287 | 376 | |
288 | ### <a name="section_3.7"></a> 3.7 rbenv which | |
289 | ||
290 | Displays the full path to the binary that rbenv will execute when you | |
291 | run the given command. | |
377 | ### rbenv which | |
378 | ||
379 | Displays the full path to the executable that rbenv will invoke when | |
380 | you run the given command. | |
292 | 381 | |
293 | 382 | $ rbenv which irb |
294 | /Users/sam/.rbenv/versions/1.9.2-p290/bin/irb | |
295 | ||
296 | ### <a name="section_3.8"></a> 3.8 rbenv whence | |
383 | /Users/sam/.rbenv/versions/1.9.3-p327/bin/irb | |
384 | ||
385 | ### rbenv whence | |
297 | 386 | |
298 | 387 | Lists all Ruby versions with the given command installed. |
299 | 388 | |
300 | 389 | $ rbenv whence rackup |
301 | 1.9.3-rc1 | |
302 | jruby-1.6.4 | |
390 | 1.9.3-p327 | |
391 | jruby-1.7.1 | |
303 | 392 | ree-1.8.7-2011.03 |
304 | 393 | |
305 | ## <a name="section_4"></a> 4 Development | |
394 | ## Development | |
306 | 395 | |
307 | 396 | The rbenv source code is [hosted on |
308 | 397 | GitHub](https://github.com/sstephenson/rbenv). It's clean, modular, |
311 | 400 | Please feel free to submit pull requests and file bugs on the [issue |
312 | 401 | tracker](https://github.com/sstephenson/rbenv/issues). |
313 | 402 | |
314 | ### <a name="section_4.1"></a> 4.1 Version History | |
403 | ### Version History | |
404 | ||
405 | **0.4.0** (January 4, 2013) | |
406 | ||
407 | * rbenv now prefers `.ruby-version` files to `.rbenv-version` files | |
408 | for specifying local application-specific versions. The | |
409 | `.ruby-version` file has the same format as `.rbenv-version` but is | |
410 | [compatible with other Ruby version | |
411 | managers](https://gist.github.com/1912050). | |
412 | * Deprecated `ruby-local-exec` and moved its functionality into the | |
413 | standard `ruby` shim. See the [ruby-local-exec wiki | |
414 | page](https://github.com/sstephenson/wiki/ruby-local-exec) for | |
415 | upgrade instructions. | |
416 | * Modified shims to include the full path to rbenv so that they can be | |
417 | invoked without having rbenv's bin directory in the `$PATH`. | |
418 | * Sped up `rbenv init` by avoiding rbenv reinintialization and by | |
419 | using a simpler indexing approach. (Users of | |
420 | [chef-rbenv](https://github.com/fnichol/chef-rbenv) should upgrade | |
421 | to the latest version to fix a [compatibility | |
422 | issue](https://github.com/fnichol/chef-rbenv/pull/26).) | |
423 | * Reworked `rbenv help` so that usage and documentation is stored as a | |
424 | comment in each subcommand, enabling plugin commands to hook into | |
425 | the help system. | |
426 | * Added support for full completion of the command line, not just the | |
427 | first argument. | |
428 | * Updated installation instructions for Zsh and Ubuntu users. | |
429 | * Fixed `rbenv which` and `rbenv prefix` with system Ruby versions. | |
430 | * Changed `rbenv exec` to avoid prepending the system Ruby location to | |
431 | `$PATH` to fix issues running system Ruby commands that invoke other | |
432 | commands. | |
433 | * Changed `rbenv rehash` to ensure it exits with a 0 status code under | |
434 | normal operation, and to ensure outdated shims are removed first | |
435 | when rehashing. | |
436 | * Modified `rbenv rehash` to run `hash -r` afterwards, when shell | |
437 | integration is enabled, to ensure the shell's command cache is | |
438 | cleared. | |
439 | * Removed use of the `+=` operator to support older versions of Bash. | |
440 | * Adjusted non-bare `rbenv versions` output to include `system`, if | |
441 | present. | |
442 | * Improved documentation for installing and uninstalling Ruby | |
443 | versions. | |
444 | * Fixed `rbenv versions` not to display a warning if the currently | |
445 | specified version doesn't exist. | |
446 | * Fixed an instance of local variable leakage in the `rbenv` shell | |
447 | function wrapper. | |
448 | * Changed `rbenv shell` to ensure it exits with a non-zero status on | |
449 | failure. | |
450 | * Added `rbenv --version` for printing the current version of rbenv. | |
451 | * Added `/usr/lib/rbenv/hooks` to the plugin hook search path. | |
452 | * Fixed `rbenv which` to account for path entries with spaces. | |
453 | * Changed `rbenv init` to accept option arguments in any order. | |
315 | 454 | |
316 | 455 | **0.3.0** (December 25, 2011) |
317 | 456 | |
387 | 526 | |
388 | 527 | * Initial public release. |
389 | 528 | |
390 | ### <a name="section_4.2"></a> 4.2 License | |
529 | ### License | |
391 | 530 | |
392 | 531 | (The MIT license) |
393 | 532 | |
394 | Copyright (c) 2011 Sam Stephenson | |
533 | Copyright (c) 2013 Sam Stephenson | |
395 | 534 | |
396 | 535 | Permission is hereby granted, free of charge, to any person obtaining |
397 | 536 | a copy of this software and associated documentation files (the |
12 | 12 | |
13 | 13 | set -e |
14 | 14 | export RBENV_DIR="${1%/*}" |
15 | ||
16 | [ -n "$RBENV_SILENCE_WARNINGS" ] || { | |
17 | echo "rbenv: \`ruby-local-exec' is deprecated and will be removed in the next release." | |
18 | echo " To upgrade: https://github.com/sstephenson/rbenv/wiki/ruby-local-exec" | |
19 | echo | |
20 | } >&2 | |
21 | ||
15 | 22 | exec ruby "$@" |
4 | 4 | if [ "$COMP_CWORD" -eq 1 ]; then |
5 | 5 | COMPREPLY=( $(compgen -W "$(rbenv commands)" -- "$word") ) |
6 | 6 | else |
7 | local command="${COMP_WORDS[1]}" | |
8 | local completions="$(rbenv completions "$command")" | |
7 | local words=("${COMP_WORDS[@]}") | |
8 | unset words[0] | |
9 | unset words[$COMP_CWORD] | |
10 | local completions=$(rbenv completions "${words[@]}") | |
9 | 11 | COMPREPLY=( $(compgen -W "$completions" -- "$word") ) |
10 | 12 | fi |
11 | 13 | } |
4 | 4 | compctl -K _rbenv rbenv |
5 | 5 | |
6 | 6 | _rbenv() { |
7 | local word words completions | |
7 | local words completions | |
8 | 8 | read -cA words |
9 | word="${words[2]}" | |
10 | 9 | |
11 | 10 | if [ "${#words}" -eq 2 ]; then |
12 | 11 | completions="$(rbenv commands)" |
13 | 12 | else |
14 | completions="$(rbenv completions "${word}")" | |
13 | completions="$(rbenv completions ${words[2,-1]})" | |
15 | 14 | fi |
16 | 15 | |
17 | 16 | reply=("${(ps:\n:)completions}") |
0 | # Simple Ruby Version Management: rbenv | |
1 | ||
2 | rbenv lets you easily switch between multiple versions of Ruby. It's | |
3 | simple, unobtrusive, and follows the UNIX tradition of single-purpose | |
4 | tools that do one thing well. | |
5 | ||
6 | <img src="http://i.sstephenson.us/rbenv2.png" width="894" height="464"> | |
7 | ||
8 | ### rbenv _does…_ | |
9 | ||
10 | * Let you **change the global Ruby version** on a per-user basis. | |
11 | * Provide support for **per-project Ruby versions**. | |
12 | * Allow you to **override the Ruby version** with an environment | |
13 | variable. | |
14 | ||
15 | ### In contrast with rvm, rbenv _does not…_ | |
16 | ||
17 | * **Need to be loaded into your shell.** Instead, rbenv's shim | |
18 | approach works by adding a directory to your `$PATH`. | |
19 | * **Override shell commands like `cd`.** That's dangerous and | |
20 | error-prone. | |
21 | * **Have a configuration file.** There's nothing to configure except | |
22 | which version of Ruby you want to use. | |
23 | * **Install Ruby.** You can build and install Ruby yourself, or use | |
24 | [ruby-build](https://github.com/sstephenson/ruby-build) to | |
25 | automate the process. | |
26 | * **Manage gemsets.** [Bundler](http://gembundler.com/) is a better | |
27 | way to manage application dependencies. If you have projects that | |
28 | are not yet using Bundler you can install the | |
29 | [rbenv-gemset](https://github.com/jamis/rbenv-gemset) plugin. | |
30 | * **Require changes to Ruby libraries for compatibility.** The | |
31 | simplicity of rbenv means as long as it's in your `$PATH`, | |
32 | [nothing](https://rvm.beginrescueend.com/integration/bundler/) | |
33 | [else](https://rvm.beginrescueend.com/integration/capistrano/) | |
34 | needs to know about it. | |
35 | * **Prompt you with warnings when you switch to a project.** Instead | |
36 | of executing arbitrary code, rbenv reads just the version name | |
37 | from each project. There's nothing to "trust." | |
38 | ||
39 | ## Table of Contents | |
40 | ||
41 | ## How It Works ## | |
42 | ||
43 | rbenv operates on the per-user directory `~/.rbenv`. Version names in | |
44 | rbenv correspond to subdirectories of `~/.rbenv/versions`. For | |
45 | example, you might have `~/.rbenv/versions/1.8.7-p354` and | |
46 | `~/.rbenv/versions/1.9.3-rc1`. | |
47 | ||
48 | Each version is a working tree with its own binaries, like | |
49 | `~/.rbenv/versions/1.8.7-p354/bin/ruby` and | |
50 | `~/.rbenv/versions/1.9.3-rc1/bin/irb`. rbenv makes _shim binaries_ | |
51 | for every such binary across all installed versions of Ruby. | |
52 | ||
53 | These shims are simple wrapper scripts that live in `~/.rbenv/shims` | |
54 | and detect which Ruby version you want to use. They insert the | |
55 | directory for the selected version at the beginning of your `$PATH` | |
56 | and then execute the corresponding binary. | |
57 | ||
58 | Because of the simplicity of the shim approach, all you need to use | |
59 | rbenv is `~/.rbenv/shims` in your `$PATH`. | |
60 | ||
61 | ## Installation ## | |
62 | ||
63 | **Compatibility note**: rbenv is _incompatible_ with rvm. Things will | |
64 | appear to work until you try to install a gem. The problem is that | |
65 | rvm actually overrides the `gem` command with a shell function! | |
66 | Please remove any references to rvm before using rbenv. | |
67 | ||
68 | ### Basic GitHub Checkout ### | |
69 | ||
70 | This will get you going with the latest version of rbenv and make it | |
71 | easy to fork and contribute any changes back upstream. | |
72 | ||
73 | 1. Check out rbenv into `~/.rbenv`. | |
74 | ||
75 | $ cd | |
76 | $ git clone git://github.com/sstephenson/rbenv.git .rbenv | |
77 | ||
78 | 2. Add `~/.rbenv/bin` to your `$PATH` for access to the `rbenv` | |
79 | command-line utility. | |
80 | ||
81 | $ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile | |
82 | ||
83 | **Zsh note**: Modifiy your `~/.zshenv` file instead of `~/.bash_profile`. | |
84 | ||
85 | 3. Add rbenv init to your shell to enable shims and autocompletion. | |
86 | ||
87 | $ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile | |
88 | ||
89 | **Zsh note**: Modifiy your `~/.zshenv` file instead of `~/.bash_profile`. | |
90 | ||
91 | 4. Restart your shell so the path changes take effect. You can now | |
92 | begin using rbenv. | |
93 | ||
94 | $ exec $SHELL | |
95 | ||
96 | 5. Install Ruby versions into `~/.rbenv/versions`. For example, to | |
97 | install Ruby 1.9.2-p290, download and unpack the source, then run: | |
98 | ||
99 | $ ./configure --prefix=$HOME/.rbenv/versions/1.9.2-p290 | |
100 | $ make | |
101 | $ make install | |
102 | ||
103 | The [ruby-build](https://github.com/sstephenson/ruby-build) project | |
104 | provides an `rbenv install` command that simplifies the process of | |
105 | installing new Ruby versions to: | |
106 | ||
107 | $ rbenv install 1.9.2-p290 | |
108 | ||
109 | 6. Rebuild the shim binaries. You should do this any time you install | |
110 | a new Ruby binary (for example, when installing a new Ruby version, | |
111 | or when installing a gem that provides a binary). | |
112 | ||
113 | $ rbenv rehash | |
114 | ||
115 | #### Upgrading #### | |
116 | ||
117 | If you've installed rbenv using the instructions above, you can | |
118 | upgrade your installation at any time using git. | |
119 | ||
120 | To upgrade to the latest development version of rbenv, use `git pull`: | |
121 | ||
122 | $ cd ~/.rbenv | |
123 | $ git pull | |
124 | ||
125 | To upgrade to a specific release of rbenv, check out the corresponding | |
126 | tag: | |
127 | ||
128 | $ cd ~/.rbenv | |
129 | $ git fetch | |
130 | $ git tag | |
131 | v0.1.0 | |
132 | v0.1.1 | |
133 | v0.1.2 | |
134 | v0.2.0 | |
135 | $ git checkout v0.2.0 | |
136 | ||
137 | ### Homebrew on Mac OS X ### | |
138 | ||
139 | You can also install rbenv using the | |
140 | [Homebrew](http://mxcl.github.com/homebrew/) package manager on Mac OS | |
141 | X. | |
142 | ||
143 | $ brew update | |
144 | $ brew install rbenv | |
145 | $ brew install ruby-build | |
146 | ||
147 | The same commands can be used for upgrading. | |
148 | ||
149 | Afterwards you'll still need to add `eval "$(rbenv init -)"` to your | |
150 | profile as stated in the caveats. You'll only ever have to do this | |
151 | once. | |
152 | ||
153 | ### Neckbeard Configuration ### | |
154 | ||
155 | Skip this section unless you must know what every line in your shell | |
156 | profile is doing. | |
157 | ||
158 | `rbenv init` is the only command that crosses the line of loading | |
159 | extra commands into your shell. Coming from rvm, some of you might be | |
160 | opposed to this idea. Here's what `rbenv init` actually does: | |
161 | ||
162 | 1. Sets up your shims path. This is the only requirement for rbenv to | |
163 | function properly. You can do this by hand by prepending | |
164 | `~/.rbenv/shims` to your `$PATH`. | |
165 | ||
166 | 2. Installs autocompletion. This is entirely optional but pretty | |
167 | useful. Sourcing `~/.rbenv/completions/rbenv.bash` will set that | |
168 | up. There is also a `~/.rbenv/completions/rbenv.zsh` for Zsh | |
169 | users. | |
170 | ||
171 | 3. Rehashes shims. From time to time you'll need to rebuild your | |
172 | shim files. Doing this on init makes sure everything is up to | |
173 | date. You can always run `rbenv rehash` manually. | |
174 | ||
175 | 4. Installs the sh dispatcher. This bit is also optional, but allows | |
176 | rbenv and plugins to change variables in your current shell, making | |
177 | commands like `rbenv shell` possible. The sh dispatcher doesn't do | |
178 | anything crazy like override `cd` or hack your shell prompt, but if | |
179 | for some reason you need `rbenv` to be a real script rather than a | |
180 | shell function, you can safely skip it. | |
181 | ||
182 | Run `rbenv init -` for yourself to see exactly what happens under the | |
183 | hood. | |
184 | ||
185 | ## Usage ## | |
186 | ||
187 | Like `git`, the `rbenv` command delegates to subcommands based on its | |
188 | first argument. The most common subcommands are: | |
189 | ||
190 | ### rbenv global ### | |
191 | ||
192 | Sets the global version of Ruby to be used in all shells by writing | |
193 | the version name to the `~/.rbenv/version` file. This version can be | |
194 | overridden by a per-project `.rbenv-version` file, or by setting the | |
195 | `RBENV_VERSION` environment variable. | |
196 | ||
197 | $ rbenv global 1.9.2-p290 | |
198 | ||
199 | The special version name `system` tells rbenv to use the system Ruby | |
200 | (detected by searching your `$PATH`). | |
201 | ||
202 | When run without a version number, `rbenv global` reports the | |
203 | currently configured global version. | |
204 | ||
205 | ### rbenv local ### | |
206 | ||
207 | Sets a local per-project Ruby version by writing the version name to | |
208 | an `.rbenv-version` file in the current directory. This version | |
209 | overrides the global, and can be overridden itself by setting the | |
210 | `RBENV_VERSION` environment variable or with the `rbenv shell` | |
211 | command. | |
212 | ||
213 | $ rbenv local rbx-1.2.4 | |
214 | ||
215 | When run without a version number, `rbenv local` reports the currently | |
216 | configured local version. You can also unset the local version: | |
217 | ||
218 | $ rbenv local --unset | |
219 | ||
220 | ### rbenv shell ### | |
221 | ||
222 | Sets a shell-specific Ruby version by setting the `RBENV_VERSION` | |
223 | environment variable in your shell. This version overrides both | |
224 | project-specific versions and the global version. | |
225 | ||
226 | $ rbenv shell jruby-1.6.4 | |
227 | ||
228 | When run without a version number, `rbenv shell` reports the current | |
229 | value of `RBENV_VERSION`. You can also unset the shell version: | |
230 | ||
231 | $ rbenv shell --unset | |
232 | ||
233 | Note that you'll need rbenv's shell integration enabled (step 3 of | |
234 | the installation instructions) in order to use this command. If you | |
235 | prefer not to use shell integration, you may simply set the | |
236 | `RBENV_VERSION` variable yourself: | |
237 | ||
238 | $ export RBENV_VERSION=jruby-1.6.4 | |
239 | ||
240 | ### rbenv versions ### | |
241 | ||
242 | Lists all Ruby versions known to rbenv, and shows an asterisk next to | |
243 | the currently active version. | |
244 | ||
245 | $ rbenv versions | |
246 | 1.8.7-p352 | |
247 | 1.9.2-p290 | |
248 | * 1.9.3-rc1 (set by /Users/sam/.rbenv/global) | |
249 | jruby-1.6.4 | |
250 | rbx-1.2.4 | |
251 | ree-1.8.7-2011.03 | |
252 | ||
253 | ### rbenv version ### | |
254 | ||
255 | Displays the currently active Ruby version, along with information on | |
256 | how it was set. | |
257 | ||
258 | $ rbenv version | |
259 | 1.8.7-p352 (set by /Volumes/37signals/basecamp/.rbenv-version) | |
260 | ||
261 | ### rbenv rehash ### | |
262 | ||
263 | Installs shims for all Ruby binaries known to rbenv (i.e., | |
264 | `~/.rbenv/versions/*/bin/*`). Run this command after you install a new | |
265 | version of Ruby, or install a gem that provides binaries. | |
266 | ||
267 | $ rbenv rehash | |
268 | ||
269 | ### rbenv which ### | |
270 | ||
271 | Displays the full path to the binary that rbenv will execute when you | |
272 | run the given command. | |
273 | ||
274 | $ rbenv which irb | |
275 | /Users/sam/.rbenv/versions/1.9.2-p290/bin/irb | |
276 | ||
277 | ### rbenv whence ### | |
278 | ||
279 | Lists all Ruby versions with the given command installed. | |
280 | ||
281 | $ rbenv whence rackup | |
282 | 1.9.3-rc1 | |
283 | jruby-1.6.4 | |
284 | ree-1.8.7-2011.03 | |
285 | ||
286 | ## Development ## | |
287 | ||
288 | The rbenv source code is [hosted on | |
289 | GitHub](https://github.com/sstephenson/rbenv). It's clean, modular, | |
290 | and easy to understand, even if you're not a shell hacker. | |
291 | ||
292 | Please feel free to submit pull requests and file bugs on the [issue | |
293 | tracker](https://github.com/sstephenson/rbenv/issues). | |
294 | ||
295 | ### Version History ### | |
296 | ||
297 | **0.3.0** (December 25, 2011) | |
298 | ||
299 | * Added an `rbenv root` command which prints the value of | |
300 | `$RBENV_ROOT`, or the default root directory if it's unset. | |
301 | * Clarified Zsh installation instructions in the readme. | |
302 | * Removed some redundant code in `rbenv rehash`. | |
303 | * Fixed an issue with calling `readlink` for paths with spaces. | |
304 | * Changed Zsh initialization code to install completion hooks only for | |
305 | interactive shells. | |
306 | * Added preliminary support for ksh. | |
307 | * `rbenv rehash` creates or removes shims only when necessary instead | |
308 | of removing and re-creating all shims on each invocation. | |
309 | * Fixed that `RBENV_DIR`, when specified, would be incorrectly | |
310 | expanded to its parent directory. | |
311 | * Removed the deprecated `set-default` and `set-local` commands. | |
312 | * Added a `--no-rehash` option to `rbenv init` for skipping the | |
313 | automatic rehash when opening a new shell. | |
314 | ||
315 | **0.2.1** (October 1, 2011) | |
316 | ||
317 | * Changed the `rbenv` command to ensure that `RBENV_DIR` is always an | |
318 | absolute path. This fixes an issue where Ruby scripts using the | |
319 | `ruby-local-exec` wrapper would go into an infinite loop when | |
320 | invoked with a relative path from the command line. | |
321 | ||
322 | **0.2.0** (September 28, 2011) | |
323 | ||
324 | * Renamed `rbenv set-default` to `rbenv global` and `rbenv set-local` | |
325 | to `rbenv local`. The `set-` commands are deprecated and will be | |
326 | removed in the next major release. | |
327 | * rbenv now uses `greadlink` on Solaris. | |
328 | * Added a `ruby-local-exec` command which can be used in shebangs in | |
329 | place of `#!/usr/bin/env ruby` to properly set the project-specific | |
330 | Ruby version regardless of current working directory. | |
331 | * Fixed an issue with `rbenv rehash` when no binaries are present. | |
332 | * Added support for `rbenv-sh-*` commands, which run inside the | |
333 | current shell instead of in a child process. | |
334 | * Added an `rbenv shell` command for conveniently setting the | |
335 | `$RBENV_VERSION` environment variable. | |
336 | * Added support for storing rbenv versions and shims in directories | |
337 | other than `~/.rbenv` with the `$RBENV_ROOT` environment variable. | |
338 | * Added support for debugging rbenv via `set -x` when the | |
339 | `$RBENV_DEBUG` environment variable is set. | |
340 | * Refactored the autocompletion system so that completions are now | |
341 | built-in to each command and shared between bash and Zsh. | |
342 | * Added support for plugin bundles in `~/.rbenv/plugins` as documented | |
343 | in [issue #102](https://github.com/sstephenson/rbenv/pull/102). | |
344 | * Added `/usr/local/etc/rbenv.d` to the list of directories searched | |
345 | for rbenv hooks. | |
346 | * Added support for an `$RBENV_DIR` environment variable which | |
347 | defaults to the current working directory for specifying where rbenv | |
348 | searches for local version files. | |
349 | ||
350 | **0.1.2** (August 16, 2011) | |
351 | ||
352 | * Fixed rbenv to be more resilient against nonexistent entries in | |
353 | `$PATH`. | |
354 | * Made the `rbenv rehash` command operate atomically. | |
355 | * Modified the `rbenv init` script to automatically run `rbenv | |
356 | rehash` so that shims are recreated whenever a new shell is opened. | |
357 | * Added initial support for Zsh autocompletion. | |
358 | * Removed the dependency on egrep for reading version files. | |
359 | ||
360 | **0.1.1** (August 14, 2011) | |
361 | ||
362 | * Fixed a syntax error in the `rbenv help` command. | |
363 | * Removed `-e` from the shebang in favor of `set -e` at the top of | |
364 | each file for compatibility with operating systems that do not | |
365 | support more than one argument in the shebang. | |
366 | ||
367 | **0.1.0** (August 11, 2011) | |
368 | ||
369 | * Initial public release. | |
370 | ||
371 | ### License ### | |
372 | ||
373 | (The MIT license) | |
374 | ||
375 | Copyright (c) 2011 Sam Stephenson | |
376 | ||
377 | Permission is hereby granted, free of charge, to any person obtaining | |
378 | a copy of this software and associated documentation files (the | |
379 | "Software"), to deal in the Software without restriction, including | |
380 | without limitation the rights to use, copy, modify, merge, publish, | |
381 | distribute, sublicense, and/or sell copies of the Software, and to | |
382 | permit persons to whom the Software is furnished to do so, subject to | |
383 | the following conditions: | |
384 | ||
385 | The above copyright notice and this permission notice shall be | |
386 | included in all copies or substantial portions of the Software. | |
387 | ||
388 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
389 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
390 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
391 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |
392 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |
393 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
394 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
0 | #!/usr/bin/env ruby | |
1 | ||
2 | # A little Markdown filter that scans your document for headings, | |
3 | # numbers them, adds anchors, and inserts a table of contents. | |
4 | # | |
5 | # To use it, make sure the headings you want numbered and linked are | |
6 | # in this format: | |
7 | # | |
8 | # ### Title ### | |
9 | # | |
10 | # I.e. they must have an equal number of octothorpes around the title | |
11 | # text. (In Markdown, `#` means `h1`, `##` means `h2`, and so on.) | |
12 | # The table of contents will be inserted before the first such | |
13 | # heading. | |
14 | # | |
15 | # Released into the public domain. | |
16 | # Sam Stephenson <sstephenson@gmail.com> | |
17 | # 2011-04-30 | |
18 | ||
19 | def mdtoc(markdown) | |
20 | titles = [] | |
21 | lines = markdown.split($/) | |
22 | start = nil | |
23 | ||
24 | # First pass: Scan the Markdown source looking for titles of the | |
25 | # format: `### Title ###`. Record the line number, header level | |
26 | # (number of octothorpes), and text of each matching title. | |
27 | lines.each_with_index do |line, line_no| | |
28 | if line.match(/^(\#{1,6})\s+(.+?)\s+\1$/) | |
29 | titles << [line_no, $1.length, $2] | |
30 | start ||= line_no | |
31 | end | |
32 | end | |
33 | ||
34 | last_section = nil | |
35 | last_level = nil | |
36 | ||
37 | # Second pass: Iterate over all matched titles and compute their | |
38 | # corresponding section numbers. Then replace the titles with | |
39 | # annotated anchors. | |
40 | titles.each do |title_info| | |
41 | line_no, level, text = title_info | |
42 | ||
43 | if last_section | |
44 | section = last_section.dup | |
45 | ||
46 | if last_level < level | |
47 | section << 1 | |
48 | else | |
49 | (last_level - level).times { section.pop } | |
50 | section[-1] += 1 | |
51 | end | |
52 | else | |
53 | section = [1] | |
54 | end | |
55 | ||
56 | name = section.join(".") | |
57 | lines[line_no] = %(#{"#" * level} <a name="section_#{name}"></a> #{name} #{text}) | |
58 | ||
59 | title_info << section | |
60 | last_section = section | |
61 | last_level = level | |
62 | end | |
63 | ||
64 | # Third pass: Iterate over matched titles once more to produce the | |
65 | # table of contents. Then insert it immediately above the first | |
66 | # matched title. | |
67 | if start | |
68 | toc = titles.map do |(line_no, level, text, section)| | |
69 | name = section.join(".") | |
70 | %(#{" " * (section.length * 3)}* [#{name} #{text}](#section_#{name})) | |
71 | end + [""] | |
72 | ||
73 | lines.insert(start, *toc) | |
74 | end | |
75 | ||
76 | lines.join("\n") | |
77 | end | |
78 | ||
79 | if __FILE__ == $0 | |
80 | puts mdtoc($<.read) | |
81 | end |
47 | 47 | done |
48 | 48 | export PATH="${bin_path}:${PATH}" |
49 | 49 | |
50 | hook_path="${RBENV_HOOK_PATH}:${RBENV_ROOT}/rbenv.d:/usr/local/etc/rbenv.d:/etc/rbenv.d" | |
50 | hook_path="${RBENV_HOOK_PATH}:${RBENV_ROOT}/rbenv.d:/usr/local/etc/rbenv.d:/etc/rbenv.d:/usr/lib/rbenv/hooks" | |
51 | 51 | for plugin_hook in "${RBENV_ROOT}/plugins/"*/etc/rbenv.d; do |
52 | 52 | hook_path="${hook_path}:${plugin_hook}" |
53 | 53 | done |
59 | 59 | command="$1" |
60 | 60 | case "$command" in |
61 | 61 | "" | "-h" | "--help" ) |
62 | echo -e "rbenv 0.3.0\n$(rbenv-help)" >&2 | |
62 | echo -e "$(rbenv---version)\n$(rbenv-help)" >&2 | |
63 | ;; | |
64 | "-v" ) | |
65 | exec rbenv---version | |
63 | 66 | ;; |
64 | 67 | * ) |
65 | 68 | command_path="$(command -v "rbenv-$command" || true)" |
0 | #!/usr/bin/env bash | |
1 | # Summary: Display the version of rbenv | |
2 | # | |
3 | # Displays the version number of this rbenv release, including the | |
4 | # current revision from git, if available. | |
5 | # | |
6 | # The format of the git revision is: | |
7 | # <version>-<num_commits>-<git_sha> | |
8 | # where `num_commits` is the number of commits since `version` was | |
9 | # tagged. | |
10 | ||
11 | set -e | |
12 | [ -n "$RBENV_DEBUG" ] && set -x | |
13 | ||
14 | version="0.4.0" | |
15 | ||
16 | cd "$RBENV_ROOT" | |
17 | git_revision="$(git describe --tags HEAD 2>/dev/null || true)" | |
18 | git_revision="${git_revision#v}" | |
19 | ||
20 | echo "rbenv ${git_revision:-$version}" |
0 | 0 | #!/usr/bin/env bash |
1 | # Summary: List all available rbenv commands | |
2 | # Usage: rbenv commands [--sh|--no-sh] | |
3 | ||
1 | 4 | set -e |
2 | 5 | [ -n "$RBENV_DEBUG" ] && set -x |
3 | 6 |
0 | 0 | #!/usr/bin/env bash |
1 | # Usage: rbenv completions <command> [arg1 arg2...] | |
2 | ||
1 | 3 | set -e |
2 | 4 | [ -n "$RBENV_DEBUG" ] && set -x |
3 | 5 | |
4 | 6 | COMMAND="$1" |
5 | 7 | if [ -z "$COMMAND" ]; then |
6 | echo "usage: rbenv completions COMMAND [arg1 arg2...]" >&2 | |
8 | rbenv-help --usage completions >&2 | |
7 | 9 | exit 1 |
8 | 10 | fi |
9 | 11 |
0 | 0 | #!/usr/bin/env bash |
1 | # | |
2 | # Summary: Run an executable with the selected Ruby version | |
3 | # | |
4 | # Usage: rbenv exec <command> [arg1 arg2...] | |
5 | # | |
6 | # Runs an executable by first preparing PATH so that the selected Ruby | |
7 | # version's `bin' directory is at the front. | |
8 | # | |
9 | # For example, if the currently selected Ruby version is 1.9.3-p327: | |
10 | # rbenv exec bundle install | |
11 | # | |
12 | # is equivalent to: | |
13 | # PATH="$RBENV_ROOT/versions/1.9.3-p327/bin:$PATH" bundle install | |
14 | ||
1 | 15 | set -e |
2 | 16 | [ -n "$RBENV_DEBUG" ] && set -x |
3 | 17 | |
6 | 20 | exec rbenv shims --short |
7 | 21 | fi |
8 | 22 | |
23 | export RBENV_VERSION="$(rbenv-version-name)" | |
9 | 24 | RBENV_COMMAND="$1" |
25 | ||
10 | 26 | if [ -z "$RBENV_COMMAND" ]; then |
11 | echo "usage: rbenv exec COMMAND [arg1 arg2...]" >&2 | |
27 | rbenv-help --usage exec >&2 | |
12 | 28 | exit 1 |
13 | 29 | fi |
14 | 30 | |
20 | 36 | done |
21 | 37 | |
22 | 38 | shift 1 |
23 | export PATH="${RBENV_BIN_PATH}:${PATH}" | |
39 | if [ "$RBENV_VERSION" != "system" ]; then | |
40 | export PATH="${RBENV_BIN_PATH}:${PATH}" | |
41 | fi | |
24 | 42 | exec -a "$RBENV_COMMAND" "$RBENV_COMMAND_PATH" "$@" |
0 | 0 | #!/usr/bin/env bash |
1 | # | |
2 | # Summary: Set or show the global Ruby version | |
3 | # | |
4 | # Usage: rbenv global <version> | |
5 | # | |
6 | # Sets the global Ruby version. You can override the global version at | |
7 | # any time by setting a directory-specific version with `rbenv local' | |
8 | # or by setting the `RBENV_VERSION' environment variable. | |
9 | # | |
10 | # <version> should be a string matching a Ruby version known to rbenv. | |
11 | # The special version string `system' will use your default system Ruby. | |
12 | # Run `rbenv versions' for a list of available Ruby versions. | |
13 | ||
1 | 14 | set -e |
2 | 15 | [ -n "$RBENV_DEBUG" ] && set -x |
3 | 16 |
0 | 0 | #!/usr/bin/env bash |
1 | # | |
2 | # Summary: Display help for a command | |
3 | # | |
4 | # Usage: rbenv help [--usage] COMMAND | |
5 | # | |
6 | # Parses and displays help contents from a command's source file. | |
7 | # | |
8 | # A command is considered documented if it starts with a comment block | |
9 | # that has a `Summary:' or `Usage:' section. Usage instructions can | |
10 | # span multiple lines as long as subsequent lines are indented. | |
11 | # The remainder of the comment block is displayed as extended | |
12 | # documentation. | |
13 | ||
1 | 14 | set -e |
2 | 15 | [ -n "$RBENV_DEBUG" ] && set -x |
3 | 16 | |
4 | print_set_version() { | |
5 | echo "<version> should be a string matching a Ruby version known by rbenv." | |
6 | ||
7 | local versions="$(rbenv-versions --bare)" | |
8 | if [ -z "$versions" ]; then | |
9 | echo "There are currently no Ruby versions installed for rbenv." | |
10 | else | |
11 | echo "The currently installed Ruby versions are:" | |
12 | echo "$versions" | sed 's/^/ /' | |
13 | fi | |
14 | ||
15 | echo | |
16 | echo "The special version string 'system' will use your default system Ruby." | |
17 | command_path() { | |
18 | local command="$1" | |
19 | command -v rbenv-"$command" || command -v rbenv-sh-"$command" || true | |
17 | 20 | } |
18 | 21 | |
19 | case "$1" in | |
20 | "") echo "usage: rbenv <command> [<args>] | |
22 | extract_initial_comment_block() { | |
23 | sed -ne " | |
24 | /^#/ !{ | |
25 | q | |
26 | } | |
21 | 27 | |
22 | Some useful rbenv commands are: | |
23 | commands List all rbenv commands | |
24 | rehash Rehash rbenv shims (run this after installing binaries) | |
25 | global Set or show the global Ruby version | |
26 | local Set or show the local directory-specific Ruby version | |
27 | shell Set or show the shell-specific Ruby version | |
28 | version Show the current Ruby version | |
29 | versions List all Ruby versions known by rbenv | |
30 | which Show the full path for the given Ruby command | |
31 | whence List all Ruby versions with the given command | |
28 | s/^#$/# / | |
32 | 29 | |
33 | See 'rbenv help <command>' for information on a specific command. | |
34 | For full documentation, see: https://github.com/sstephenson/rbenv#readme" | |
35 | ;; | |
36 | global) echo "usage: rbenv global <version> | |
30 | /^# / { | |
31 | s/^# // | |
32 | p | |
33 | } | |
34 | " | |
35 | } | |
37 | 36 | |
38 | Sets the global Ruby version. You can override the global version at | |
39 | any time by setting a directory-specific version with \`rbenv local' | |
40 | or by setting the RBENV_VERSION environment variable. | |
37 | collect_documentation() { | |
38 | awk ' | |
39 | /^Summary:/ { | |
40 | summary = substr($0, 10) | |
41 | next | |
42 | } | |
41 | 43 | |
42 | $(print_set_version)" | |
43 | ;; | |
44 | local) echo "usage: rbenv local <version> | |
45 | rbenv local --unset | |
44 | /^Usage:/ { | |
45 | reading_usage = 1 | |
46 | usage = usage "\n" $0 | |
47 | next | |
48 | } | |
46 | 49 | |
47 | Sets the local directory-specific Ruby version by writing the version | |
48 | name to a file named '.rbenv-version'. | |
50 | /^( *$| )/ && reading_usage { | |
51 | usage = usage "\n" $0 | |
52 | next | |
53 | } | |
49 | 54 | |
50 | When you run a Ruby command, rbenv will look for an '.rbenv-version' | |
51 | file in the current directory and each parent directory. If no such | |
52 | file is found in the tree, rbenv will use the global Ruby version | |
53 | specified with \`rbenv global', or the version specified in the | |
54 | RBENV_VERSION environment variable. | |
55 | { | |
56 | reading_usage = 0 | |
57 | help = help "\n" $0 | |
58 | } | |
55 | 59 | |
56 | $(print_set_version)" | |
57 | ;; | |
58 | shell) echo "usage: rbenv shell <version> | |
59 | rbenv shell --unset | |
60 | function escape(str) { | |
61 | gsub(/[`\\$"]/, "\\\\&", str) | |
62 | return str | |
63 | } | |
60 | 64 | |
61 | Sets a shell-specific Ruby version by setting the 'RBENV_VERSION' | |
62 | environment variable in your shell. This version overrides both | |
63 | project-specific versions and the global version. | |
65 | function trim(str) { | |
66 | gsub(/^\n*/, "", str) | |
67 | gsub(/\n*$/, "", str) | |
68 | return str | |
69 | } | |
64 | 70 | |
65 | $(print_set_version)" | |
66 | ;; | |
67 | which) echo "usage: rbenv which <command> | |
71 | END { | |
72 | if (usage || summary) { | |
73 | print "summary=\"" escape(summary) "\"" | |
74 | print "usage=\"" escape(trim(usage)) "\"" | |
75 | print "help=\"" escape(trim(help)) "\"" | |
76 | } | |
77 | } | |
78 | ' | |
79 | } | |
68 | 80 | |
69 | Displays the full path to the binary that rbenv will execute when you | |
70 | run the given command." | |
71 | ;; | |
72 | whence) echo "usage: rbenv whence <command> | |
81 | documentation_for() { | |
82 | local filename="$(command_path "$1")" | |
83 | if [ -n "$filename" ]; then | |
84 | extract_initial_comment_block < "$filename" | collect_documentation | |
85 | fi | |
86 | } | |
73 | 87 | |
74 | Lists all Ruby versions with the given command installed." | |
75 | ;; | |
76 | *) | |
77 | command_path="$(command -v "rbenv-$1" || true)" | |
78 | if [ -n "$command_path" ]; then | |
79 | echo "Sorry, the \`$1' command isn't documented yet." | |
80 | echo | |
81 | echo "You can view the command's source here:" | |
82 | echo "$command_path" | |
83 | echo | |
88 | print_summary() { | |
89 | local command="$1" | |
90 | local summary usage help | |
91 | eval "$(documentation_for "$command")" | |
92 | ||
93 | if [ -n "$summary" ]; then | |
94 | printf " %-9s %s\n" "$command" "$summary" | |
95 | fi | |
96 | } | |
97 | ||
98 | print_summaries() { | |
99 | for command; do | |
100 | print_summary "$command" | |
101 | done | |
102 | } | |
103 | ||
104 | print_help() { | |
105 | local command="$1" | |
106 | local summary usage help | |
107 | eval "$(documentation_for "$command")" | |
108 | [ -n "$help" ] || help="$summary" | |
109 | ||
110 | if [ -n "$usage" -o -n "$summary" ]; then | |
111 | if [ -n "$usage" ]; then | |
112 | echo "$usage" | |
113 | else | |
114 | echo "Usage: rbenv ${command}" | |
115 | fi | |
116 | if [ -n "$help" ]; then | |
117 | echo | |
118 | echo "$help" | |
119 | echo | |
120 | fi | |
84 | 121 | else |
85 | echo "rbenv: no such command \`$1'" | |
122 | echo "Sorry, this command isn't documented yet." >&2 | |
123 | return 1 | |
86 | 124 | fi |
87 | esac | |
125 | } | |
126 | ||
127 | print_usage() { | |
128 | local command="$1" | |
129 | local summary usage help | |
130 | eval "$(documentation_for "$command")" | |
131 | [ -z "$usage" ] || echo "$usage" | |
132 | } | |
133 | ||
134 | unset usage | |
135 | if [ "$1" = "--usage" ]; then | |
136 | usage="1" | |
137 | shift | |
138 | fi | |
139 | ||
140 | if [ -z "$1" ] || [ "$1" == "rbenv" ]; then | |
141 | echo "Usage: rbenv <command> [<args>]" | |
142 | [ -z "$usage" ] || exit | |
143 | echo | |
144 | echo "Some useful rbenv commands are:" | |
145 | print_summaries commands local global shell install uninstall rehash version versions which whence | |
146 | echo | |
147 | echo "See \`rbenv help <command>' for information on a specific command." | |
148 | echo "For full documentation, see: https://github.com/sstephenson/rbenv#readme" | |
149 | else | |
150 | command="$1" | |
151 | if [ -n "$(command_path "$command")" ]; then | |
152 | if [ -n "$usage" ]; then | |
153 | print_usage "$command" | |
154 | else | |
155 | print_help "$command" | |
156 | fi | |
157 | else | |
158 | echo "rbenv: no such command \`$command'" >&2 | |
159 | exit 1 | |
160 | fi | |
161 | fi |
0 | 0 | #!/usr/bin/env bash |
1 | # Summary: List hook scripts for a given rbenv command | |
2 | # Usage: rbenv hooks <command> | |
3 | ||
1 | 4 | set -e |
2 | 5 | [ -n "$RBENV_DEBUG" ] && set -x |
3 | 6 | |
11 | 14 | |
12 | 15 | RBENV_COMMAND="$1" |
13 | 16 | if [ -z "$RBENV_COMMAND" ]; then |
14 | echo "usage: rbenv hooks COMMAND" >&2 | |
17 | rbenv-help --usage hooks >&2 | |
15 | 18 | exit 1 |
16 | 19 | fi |
17 | 20 |
0 | 0 | #!/usr/bin/env bash |
1 | # Summary: Configure the shell environment for rbenv | |
2 | # Usage: eval "$(rbenv init - [--no-rehash] [<shell>])" | |
3 | ||
1 | 4 | set -e |
2 | 5 | [ -n "$RBENV_DEBUG" ] && set -x |
3 | 6 | |
4 | 7 | print="" |
5 | if [ "$1" = "-" ]; then | |
6 | print=1 | |
7 | shift | |
8 | fi | |
8 | no_rehash="" | |
9 | for args in "$@" | |
10 | do | |
11 | if [ "$args" = "-" ]; then | |
12 | print=1 | |
13 | shift | |
14 | fi | |
9 | 15 | |
10 | no_rehash="" | |
11 | if [ "$1" = "--no-rehash" ]; then | |
12 | no_rehash=1 | |
13 | shift | |
14 | fi | |
16 | if [ "$args" = "--no-rehash" ]; then | |
17 | no_rehash=1 | |
18 | shift | |
19 | fi | |
20 | done | |
15 | 21 | |
16 | 22 | shell="$1" |
17 | 23 | if [ -z "$shell" ]; then |
78 | 84 | echo 'rbenv rehash 2>/dev/null' |
79 | 85 | fi |
80 | 86 | |
81 | commands=(`rbenv commands --sh`) | |
87 | commands=(`rbenv-commands --sh`) | |
82 | 88 | IFS="|" |
83 | 89 | cat <<EOS |
84 | 90 | rbenv() { |
91 | typeset command | |
85 | 92 | command="\$1" |
86 | 93 | if [ "\$#" -gt 0 ]; then |
87 | 94 | shift |
0 | 0 | #!/usr/bin/env bash |
1 | # | |
2 | # Summary: Set or show the local application-specific Ruby version | |
3 | # | |
4 | # Usage: rbenv local <version> | |
5 | # rbenv local --unset | |
6 | # | |
7 | # Sets the local application-specific Ruby version by writing the | |
8 | # version name to a file named `.ruby-version'. | |
9 | # | |
10 | # When you run a Ruby command, rbenv will look for a `.ruby-version' | |
11 | # file in the current directory and each parent directory. If no such | |
12 | # file is found in the tree, rbenv will use the global Ruby version | |
13 | # specified with `rbenv global'. A version specified with the | |
14 | # `RBENV_VERSION' environment variable takes precedence over local | |
15 | # and global versions. | |
16 | # | |
17 | # For backwards compatibility, rbenv will also read version | |
18 | # specifications from `.rbenv-version' files, but a `.ruby-version' | |
19 | # file in the same directory takes precedence. | |
20 | # | |
21 | # <version> should be a string matching a Ruby version known to rbenv. | |
22 | # The special version string `system' will use your default system Ruby. | |
23 | # Run `rbenv versions' for a list of available Ruby versions. | |
24 | ||
1 | 25 | set -e |
2 | 26 | [ -n "$RBENV_DEBUG" ] && set -x |
3 | 27 | |
9 | 33 | fi |
10 | 34 | |
11 | 35 | RBENV_VERSION="$1" |
12 | RBENV_VERSION_FILE=".rbenv-version" | |
13 | 36 | |
14 | 37 | if [ "$RBENV_VERSION" = "--unset" ]; then |
15 | rm -f "$RBENV_VERSION_FILE" | |
38 | rm -f .ruby-version .rbenv-version | |
16 | 39 | elif [ -n "$RBENV_VERSION" ]; then |
17 | rbenv-version-file-write "$RBENV_VERSION_FILE" "$RBENV_VERSION" | |
40 | if [ "$(RBENV_VERSION= rbenv-version-origin)" -ef .rbenv-version ]; then | |
41 | rm -f .rbenv-version | |
42 | { echo "rbenv: removed existing \`.rbenv-version' file and migrated" | |
43 | echo " local version specification to \`.ruby-version' file" | |
44 | } >&2 | |
45 | fi | |
46 | rbenv-version-file-write .ruby-version "$RBENV_VERSION" | |
18 | 47 | else |
19 | rbenv-version-file-read "$RBENV_VERSION_FILE" || | |
48 | rbenv-version-file-read .ruby-version || | |
49 | rbenv-version-file-read .rbenv-version || | |
20 | 50 | { echo "rbenv: no local version configured for this directory" |
21 | 51 | exit 1 |
22 | 52 | } >&2 |
0 | 0 | #!/usr/bin/env bash |
1 | # Summary: Display prefix for a Ruby version | |
2 | # Usage: rbenv prefix [<version>] | |
3 | # | |
4 | # Displays the directory where a Ruby version is installed. If no | |
5 | # version is given, `rbenv prefix' displays the location of the | |
6 | # currently selected version. | |
7 | ||
1 | 8 | set -e |
2 | 9 | [ -n "$RBENV_DEBUG" ] && set -x |
3 | 10 | |
15 | 22 | |
16 | 23 | if [ "$RBENV_VERSION" = "system" ]; then |
17 | 24 | RUBY_PATH="$(rbenv-which ruby)" |
18 | echo "${RUBY_PATH%/*}" | |
25 | RUBY_PATH="${RUBY_PATH%/*}" | |
26 | echo "${RUBY_PATH%/bin}" | |
19 | 27 | exit |
20 | 28 | fi |
21 | 29 |
0 | 0 | #!/usr/bin/env bash |
1 | # Summary: Rehash rbenv shims (run this after installing executables) | |
2 | ||
1 | 3 | set -e |
2 | 4 | [ -n "$RBENV_DEBUG" ] && set -x |
3 | 5 | |
29 | 31 | |
30 | 32 | # The prototype shim file is a script that re-execs itself, passing |
31 | 33 | # its filename and any arguments to `rbenv exec`. This file is |
32 | # hard-linked for every binary and then removed. The linking technique | |
33 | # is fast, uses less disk space than unique files, and also serves as | |
34 | # a locking mechanism. | |
34 | # hard-linked for every executable and then removed. The linking | |
35 | # technique is fast, uses less disk space than unique files, and also | |
36 | # serves as a locking mechanism. | |
35 | 37 | create_prototype_shim() { |
36 | 38 | cat > "$PROTOTYPE_SHIM_PATH" <<SH |
37 | 39 | #!/usr/bin/env bash |
38 | 40 | set -e |
41 | [ -n "\$RBENV_DEBUG" ] && set -x | |
42 | ||
43 | program="\${0##*/}" | |
44 | if [ "\$program" = "ruby" ]; then | |
45 | for arg; do | |
46 | case "\$arg" in | |
47 | -e* | -- ) break ;; | |
48 | */* ) | |
49 | if [ -f "\$arg" ]; then | |
50 | export RBENV_DIR="\${arg%/*}" | |
51 | break | |
52 | fi | |
53 | ;; | |
54 | esac | |
55 | done | |
56 | fi | |
57 | ||
39 | 58 | export RBENV_ROOT="$RBENV_ROOT" |
40 | exec rbenv exec "\${0##*/}" "\$@" | |
59 | exec "$(command -v rbenv)" exec "\$program" "\$@" | |
41 | 60 | SH |
42 | 61 | chmod +x "$PROTOTYPE_SHIM_PATH" |
62 | } | |
63 | ||
64 | # If the contents of the prototype shim file differ from the contents | |
65 | # of the first shim in the shims directory, assume rbenv has been | |
66 | # upgraded and the existing shims need to be removed. | |
67 | remove_outdated_shims() { | |
68 | for shim in *; do | |
69 | if ! diff "$PROTOTYPE_SHIM_PATH" "$shim" >/dev/null 2>&1; then | |
70 | for shim in *; do rm -f "$shim"; done | |
71 | fi | |
72 | break | |
73 | done | |
43 | 74 | } |
44 | 75 | |
45 | 76 | # The basename of each argument passed to `make_shims` will be |
54 | 85 | done |
55 | 86 | } |
56 | 87 | |
57 | # Create an empty array for the list of registered shims. | |
88 | # Create an empty array for the list of registered shims and an empty | |
89 | # string to use as a search index. | |
58 | 90 | registered_shims=() |
91 | registered_shims_index="" | |
59 | 92 | |
60 | 93 | # We will keep track of shims registered for installation with the |
61 | # global `reigstered_shims` array and with a global variable for each | |
62 | # shim. The array will let us iterate over all registered shims. The | |
63 | # global variables will let us quickly check whether a shim with the | |
64 | # given name has been registered or not. | |
94 | # global `reigstered_shims` array and with a global search index | |
95 | # string. The array will let us iterate over all registered shims. The | |
96 | # index string will let us quickly check whether a shim with the given | |
97 | # name has been registered or not. | |
65 | 98 | register_shim() { |
66 | 99 | local shim="$@" |
67 | local var="$(shim_variable_name "$shim")" | |
68 | ||
69 | if [ -z "${!var}" ]; then | |
70 | registered_shims[${#registered_shims[*]}]="$shim" | |
71 | eval "${var}=1" | |
72 | fi | |
73 | } | |
74 | ||
75 | # To compute the global variable name for a given shim we must first | |
76 | # escape any non-alphanumeric characters. If the shim name is | |
77 | # alphanumeric (including a hyphen or underscore) we can take a | |
78 | # shorter path. Otherwise, we must iterate over each character and | |
79 | # escape the non-alphanumeric ones using `printf`. | |
80 | shim_variable_name() { | |
81 | local shim="$1" | |
82 | local result="_shim_" | |
83 | ||
84 | if [[ ! "$shim" =~ [^[:alnum:]_-] ]]; then | |
85 | shim="${shim//_/_5f}" | |
86 | shim="${shim//-/_2d}" | |
87 | result+="$shim" | |
88 | else | |
89 | local length="${#shim}" | |
90 | local char i | |
91 | ||
92 | for ((i=0; i<length; i++)); do | |
93 | char="${shim:$i:1}" | |
94 | if [[ "$char" =~ [[:alnum:]] ]]; then | |
95 | result+="$char" | |
96 | else | |
97 | result+="$(printf "_%02x" \'"$char")" | |
98 | fi | |
99 | done | |
100 | fi | |
101 | ||
102 | echo "$result" | |
100 | registered_shims["${#registered_shims[@]}"]="$shim" | |
101 | registered_shims_index="$registered_shims_index/$shim/" | |
103 | 102 | } |
104 | 103 | |
105 | 104 | # To install all the registered shims, we iterate over the |
106 | 105 | # `registered_shims` array and create a link if one does not already |
107 | 106 | # exist. |
108 | 107 | install_registered_shims() { |
108 | local shim | |
109 | 109 | for shim in "${registered_shims[@]}"; do |
110 | 110 | [ -e "$shim" ] || ln -f "$PROTOTYPE_SHIM_PATH" "$shim" |
111 | 111 | done |
116 | 116 | # in the directory but has not been registered as a shim should be |
117 | 117 | # removed. |
118 | 118 | remove_stale_shims() { |
119 | local var | |
119 | local shim | |
120 | 120 | for shim in *; do |
121 | var="$(shim_variable_name "$shim")" | |
122 | [ -z "${!var}" ] && rm -f "$shim" | |
121 | if [[ "$registered_shims_index" != *"/$shim/"* ]]; then | |
122 | rm -f "$shim" | |
123 | fi | |
123 | 124 | done |
124 | 125 | } |
125 | 126 | |
126 | 127 | |
127 | 128 | # Change to the shims directory. |
128 | 129 | cd "$SHIM_PATH" |
130 | shopt -s nullglob | |
129 | 131 | |
130 | # Create the prototype shim, then register shims for all known binaries. | |
132 | # Create the prototype shim, then register shims for all known | |
133 | # executables. | |
131 | 134 | create_prototype_shim |
132 | shopt -s nullglob | |
135 | remove_outdated_shims | |
133 | 136 | make_shims ../versions/*/bin/* |
134 | 137 | |
135 | 138 | # Restore the previous working directory. |
0 | 0 | #!/usr/bin/env bash |
1 | # Summary: Display the root directory where versions and shims are kept | |
1 | 2 | echo $RBENV_ROOT |
0 | #!/usr/bin/env bash | |
1 | set -e | |
2 | [ -n "$RBENV_DEBUG" ] && set -x | |
3 | ||
4 | # Provide rbenv completions | |
5 | if [ "$1" = "--complete" ]; then | |
6 | exec rbenv-rehash --complete | |
7 | fi | |
8 | ||
9 | # When rbenv shell integration is enabled, delegate to rbenv-rehash, | |
10 | # then tell the shell to empty its command lookup cache. | |
11 | rbenv-rehash | |
12 | echo "hash -r" |
0 | 0 | #!/usr/bin/env bash |
1 | # | |
2 | # Summary: Set or show the shell-specific Ruby version | |
3 | # | |
4 | # Usage: rbenv shell <version> | |
5 | # rbenv shell --unset | |
6 | # | |
7 | # Sets a shell-specific Ruby version by setting the `RBENV_VERSION' | |
8 | # environment variable in your shell. This version overrides local | |
9 | # application-specific versions and the global version. | |
10 | # | |
11 | # <version> should be a string matching a Ruby version known to rbenv. | |
12 | # The special version string `system' will use your default system Ruby. | |
13 | # Run `rbenv versions' for a list of available Ruby versions. | |
14 | ||
1 | 15 | set -e |
2 | 16 | [ -n "$RBENV_DEBUG" ] && set -x |
3 | 17 | |
22 | 36 | |
23 | 37 | if [ "$version" = "--unset" ]; then |
24 | 38 | echo "unset RBENV_VERSION" |
25 | exit 1 | |
39 | exit | |
26 | 40 | fi |
27 | 41 | |
28 | 42 | # Make sure the specified version is installed. |
29 | rbenv-prefix "$version" >/dev/null | |
30 | ||
31 | echo "export RBENV_VERSION=\"${version}\"" | |
43 | if rbenv-prefix "$version" >/dev/null; then | |
44 | echo "export RBENV_VERSION=\"${version}\"" | |
45 | else | |
46 | echo "return 1" | |
47 | exit 1 | |
48 | fi |
0 | 0 | #!/usr/bin/env bash |
1 | # Summary: List existing rbenv shims | |
2 | # Usage: rbenv shims [--short] | |
3 | ||
1 | 4 | set -e |
2 | 5 | [ -n "$RBENV_DEBUG" ] && set -x |
3 | 6 |
0 | 0 | #!/usr/bin/env bash |
1 | # Summary: Show the current Ruby version and its origin | |
2 | # | |
3 | # Shows the currently selected Ruby version and how it was | |
4 | # selected. To obtain only the version string, use `rbenv | |
5 | # version-name'. | |
6 | ||
1 | 7 | set -e |
2 | 8 | [ -n "$RBENV_DEBUG" ] && set -x |
3 | 9 |
0 | 0 | #!/usr/bin/env bash |
1 | # Summary: Detect the file that sets the current rbenv version | |
1 | 2 | set -e |
2 | 3 | [ -n "$RBENV_DEBUG" ] && set -x |
3 | 4 | |
4 | root="$RBENV_DIR" | |
5 | while [ -n "$root" ]; do | |
6 | if [ -e "${root}/.rbenv-version" ]; then | |
7 | echo "${root}/.rbenv-version" | |
8 | exit | |
9 | fi | |
10 | root="${root%/*}" | |
11 | done | |
5 | find_local_version_file() { | |
6 | local root="$1" | |
7 | while [ -n "$root" ]; do | |
8 | if [ -e "${root}/.ruby-version" ]; then | |
9 | echo "${root}/.ruby-version" | |
10 | exit | |
11 | elif [ -e "${root}/.rbenv-version" ]; then | |
12 | echo "${root}/.rbenv-version" | |
13 | exit | |
14 | fi | |
15 | root="${root%/*}" | |
16 | done | |
17 | } | |
18 | ||
19 | find_local_version_file "$RBENV_DIR" | |
20 | [ "$RBENV_DIR" = "$PWD" ] || find_local_version_file "$PWD" | |
12 | 21 | |
13 | 22 | global_version_file="${RBENV_ROOT}/version" |
14 | 23 |
0 | 0 | #!/usr/bin/env bash |
1 | # Usage: rbenv version-file-read <file> | |
1 | 2 | set -e |
2 | 3 | [ -n "$RBENV_DEBUG" ] && set -x |
3 | 4 | |
4 | 5 | VERSION_FILE="$1" |
5 | 6 | |
6 | 7 | if [ -e "$VERSION_FILE" ]; then |
7 | # Read and print the first non-whitespace word from the specified | |
8 | # version file. | |
8 | # Read the first non-whitespace word from the specified version file. | |
9 | # Be careful not to load it whole in case there's something crazy in it. | |
9 | 10 | version="" |
10 | 11 | while read -a words; do |
11 | 12 | word="${words[0]}" |
0 | 0 | #!/usr/bin/env bash |
1 | # Usage: rbenv version-file-write <file> <version> | |
2 | ||
1 | 3 | set -e |
2 | 4 | [ -n "$RBENV_DEBUG" ] && set -x |
3 | 5 | |
5 | 7 | RBENV_VERSION="$2" |
6 | 8 | |
7 | 9 | if [ -z "$RBENV_VERSION" ] || [ -z "$RBENV_VERSION_FILE" ]; then |
8 | echo "usage: rbenv write-version-file FILENAME VERSION" >&2 | |
10 | rbenv-help --usage version-file-write >&2 | |
9 | 11 | exit 1 |
10 | 12 | fi |
11 | 13 |
0 | 0 | #!/usr/bin/env bash |
1 | # Summary: Show the current Ruby version | |
1 | 2 | set -e |
2 | 3 | [ -n "$RBENV_DEBUG" ] && set -x |
3 | 4 | |
11 | 12 | exit |
12 | 13 | fi |
13 | 14 | |
14 | RBENV_VERSION_PATH="${RBENV_ROOT}/versions/${RBENV_VERSION}" | |
15 | version_exists() { | |
16 | local version="$1" | |
17 | [ -d "${RBENV_ROOT}/versions/${version}" ] | |
18 | } | |
15 | 19 | |
16 | if [ -d "$RBENV_VERSION_PATH" ]; then | |
20 | if version_exists "$RBENV_VERSION"; then | |
17 | 21 | echo "$RBENV_VERSION" |
22 | elif version_exists "${RBENV_VERSION#ruby-}"; then | |
23 | { echo "warning: ignoring extraneous \`ruby-' prefix in version \`${RBENV_VERSION}'" | |
24 | echo " (set by $(rbenv-version-origin))" | |
25 | } >&2 | |
26 | echo "${RBENV_VERSION#ruby-}" | |
18 | 27 | else |
19 | 28 | echo "rbenv: version \`$RBENV_VERSION' is not installed" >&2 |
20 | 29 | exit 1 |
0 | 0 | #!/usr/bin/env bash |
1 | # Summary: Explain how the current Ruby version is set | |
1 | 2 | set -e |
2 | 3 | [ -n "$RBENV_DEBUG" ] && set -x |
3 | 4 |
0 | 0 | #!/usr/bin/env bash |
1 | # Summary: List all Ruby versions available to rbenv | |
2 | # Usage: rbenv versions [--bare] | |
3 | # | |
4 | # Lists all Ruby versions found in `$RBENV_ROOT/versions/*'. | |
5 | ||
1 | 6 | set -e |
2 | 7 | [ -n "$RBENV_DEBUG" ] && set -x |
3 | ||
4 | RBENV_VERSION_NAME="$(rbenv-version-name)" | |
5 | 8 | |
6 | 9 | if [ "$1" = "--bare" ]; then |
7 | 10 | hit_prefix="" |
8 | 11 | miss_prefix="" |
9 | print_version="$RBENV_VERSION_NAME" | |
12 | current_version="" | |
13 | include_system="" | |
10 | 14 | else |
11 | 15 | hit_prefix="* " |
12 | 16 | miss_prefix=" " |
13 | print_version="$(rbenv-version)" | |
17 | current_version="$(rbenv-version-name || true)" | |
18 | include_system="1" | |
19 | fi | |
20 | ||
21 | print_version() { | |
22 | if [ "$1" == "$current_version" ]; then | |
23 | echo "${hit_prefix}$(rbenv-version 2>/dev/null)" | |
24 | else | |
25 | echo "${miss_prefix}$1" | |
26 | fi | |
27 | } | |
28 | ||
29 | # Include "system" in the non-bare output, if it exists | |
30 | if [ -n "$include_system" ] && RBENV_VERSION=system rbenv-which ruby >/dev/null 2>&1; then | |
31 | print_version system | |
14 | 32 | fi |
15 | 33 | |
16 | 34 | for path in "${RBENV_ROOT}/versions/"*; do |
17 | 35 | if [ -d "$path" ]; then |
18 | version="${path##*/}" | |
19 | ||
20 | if [ "$version" == "$RBENV_VERSION_NAME" ]; then | |
21 | echo "${hit_prefix}${print_version}" | |
22 | else | |
23 | echo "${miss_prefix}${version}" | |
24 | fi | |
36 | print_version "${path##*/}" | |
25 | 37 | fi |
26 | 38 | done |
0 | 0 | #!/usr/bin/env bash |
1 | # Summary: List all Ruby versions that contain the given executable | |
2 | # Usage: rbenv whence [--path] <command> | |
3 | ||
1 | 4 | set -e |
2 | 5 | [ -n "$RBENV_DEBUG" ] && set -x |
3 | 6 | |
26 | 29 | |
27 | 30 | RBENV_COMMAND="$1" |
28 | 31 | if [ -z "$RBENV_COMMAND" ]; then |
29 | echo "usage: rbenv whence [--path] COMMAND" >&2 | |
32 | rbenv-help --usage whence >&2 | |
30 | 33 | exit 1 |
31 | 34 | fi |
32 | 35 |
0 | 0 | #!/usr/bin/env bash |
1 | # | |
2 | # Summary: Display the full path to an executable | |
3 | # | |
4 | # Usage: rbenv which <command> | |
5 | # | |
6 | # Displays the full path to the executable that rbenv will invoke when | |
7 | # you run the given command. | |
8 | ||
1 | 9 | set -e |
2 | 10 | [ -n "$RBENV_DEBUG" ] && set -x |
3 | 11 | |
26 | 34 | return |
27 | 35 | fi |
28 | 36 | |
29 | for path in ${PATH//:/$'\n'}; do | |
37 | local paths | |
38 | IFS=: paths=($PATH) | |
39 | ||
40 | for path in "${paths[@]}"; do | |
30 | 41 | path="$(expand_path "$path" || true)" |
31 | 42 | if [ -n "$path" ] && [ "$path" != "$path_to_remove" ]; then |
32 | 43 | result="${result}${path}:" |
40 | 51 | RBENV_COMMAND="$1" |
41 | 52 | |
42 | 53 | if [ -z "$RBENV_COMMAND" ]; then |
43 | echo "usage: rbenv which COMMAND" >&2 | |
54 | rbenv-help --usage which >&2 | |
44 | 55 | exit 1 |
45 | 56 | fi |
46 | 57 | |
47 | 58 | if [ "$RBENV_VERSION" = "system" ]; then |
48 | 59 | PATH="$(remove_from_path "${RBENV_ROOT}/shims")" |
49 | RBENV_COMMAND_PATH="$(command -v "$RBENV_COMMAND")" | |
60 | RBENV_COMMAND_PATH="$(command -v "$RBENV_COMMAND" || true)" | |
50 | 61 | else |
51 | 62 | RBENV_COMMAND_PATH="${RBENV_ROOT}/versions/${RBENV_VERSION}/bin/${RBENV_COMMAND}" |
52 | 63 | fi |