|
| 1 | +from __future__ import unicode_literals |
| 2 | + |
1 | 3 | import contextlib |
| 4 | +import io |
| 5 | +import os |
2 | 6 |
|
3 | 7 | from pre_commit.languages import helpers |
4 | 8 | from pre_commit.util import clean_path_on_failure |
5 | 9 |
|
6 | 10 |
|
7 | | -ENVIRONMENT_DIR = 'rvm_env' |
| 11 | +ENVIRONMENT_DIR = 'rbenv' |
8 | 12 |
|
9 | 13 |
|
10 | 14 | class RubyEnv(helpers.Environment): |
11 | 15 | @property |
12 | 16 | def env_prefix(self): |
13 | | - raise NotImplementedError |
| 17 | + return '. {{prefix}}{0}/bin/activate &&'.format(ENVIRONMENT_DIR) |
| 18 | + |
| 19 | + def run(self, *args, **kwargs): |
| 20 | + # TODO: hardcoded version smell |
| 21 | + env = dict(os.environ, RBENV_VERSION='1.9.3-p545') |
| 22 | + return super(RubyEnv, self).run(*args, env=env, **kwargs) |
14 | 23 |
|
15 | 24 |
|
16 | 25 | @contextlib.contextmanager |
17 | 26 | def in_env(repo_cmd_runner): |
18 | 27 | yield RubyEnv(repo_cmd_runner) |
19 | 28 |
|
20 | 29 |
|
| 30 | +def _install_rbenv(repo_cmd_runner): |
| 31 | + repo_cmd_runner.run([ |
| 32 | + 'git', 'clone', 'git://github.com/sstephenson/rbenv', '{prefix}rbenv', |
| 33 | + ]) |
| 34 | + repo_cmd_runner.run([ |
| 35 | + 'git', 'clone', 'git://github.com/sstephenson/ruby-build', |
| 36 | + '{prefix}rbenv/plugins/ruby-build', |
| 37 | + ]) |
| 38 | + |
| 39 | + activate_path = repo_cmd_runner.path('rbenv', 'bin', 'activate') |
| 40 | + with io.open(activate_path, 'w') as activate_file: |
| 41 | + # This is similar to how you would install rbenv to your home directory |
| 42 | + # However we do a couple things to make the executables exposed and |
| 43 | + # configure it to work in our directory. |
| 44 | + # We also modify the PS1 variable for manual debugging sake. |
| 45 | + activate_file.write( |
| 46 | + '#!/usr/bin/env bash\n' |
| 47 | + "export RBENV_ROOT='{0}'\n" |
| 48 | + 'export PATH="$RBENV_ROOT/bin:$PATH"\n' |
| 49 | + 'eval "$(rbenv init -)"\n' |
| 50 | + 'export PS1="(rbenv)$PS1"\n' |
| 51 | + # This lets us install gems in an isolated and repeatable |
| 52 | + # directory |
| 53 | + "export GEM_HOME='{0}/gems'\n" |
| 54 | + 'export PATH="$GEM_HOME/bin:$PATH"\n' |
| 55 | + '\n'.format(repo_cmd_runner.path('rbenv')) |
| 56 | + ) |
| 57 | + |
| 58 | + |
21 | 59 | def install_environment(repo_cmd_runner): |
22 | | - # Return immediately if we already have a virtualenv |
23 | | - if repo_cmd_runner.exists(ENVIRONMENT_DIR): |
24 | | - return |
25 | | - |
26 | | - with clean_path_on_failure(repo_cmd_runner.path(ENVIRONMENT_DIR)): |
27 | | - repo_cmd_runner.run(['__rvm-env.sh', '{{prefix}}{0}'.format(ENVIRONMENT_DIR)]) |
28 | | - with in_env(repo_cmd_runner) as env: |
29 | | - env.run('cd {prefix} && bundle install') |
| 60 | + with clean_path_on_failure(repo_cmd_runner.path('rbenv')): |
| 61 | + _install_rbenv(repo_cmd_runner) |
| 62 | + with in_env(repo_cmd_runner) as ruby_env: |
| 63 | + # TODO: hardcoded version smell |
| 64 | + ruby_env.run('rbenv install 1.9.3-p545') |
| 65 | + ruby_env.run( |
| 66 | + 'cd {prefix} && gem build *.gemspec && gem install *.gem', |
| 67 | + ) |
30 | 68 |
|
31 | 69 |
|
32 | 70 | def run_hook(repo_cmd_runner, hook, file_args): |
|
0 commit comments