Environments are now installed to version-specific locations. Resolves #229

This commit is contained in:
Anthony Sottile 2015-05-16 19:16:23 -04:00
parent 154d918ff1
commit 45d4a195ef
9 changed files with 71 additions and 44 deletions

View file

@ -2,6 +2,7 @@ from __future__ import unicode_literals
import contextlib
import io
import shutil
from pre_commit.languages import helpers
from pre_commit.util import CalledProcessError
@ -16,29 +17,36 @@ ENVIRONMENT_DIR = 'rbenv'
class RubyEnv(helpers.Environment):
@property
def env_prefix(self):
return '. {{prefix}}{0}/bin/activate &&'.format(ENVIRONMENT_DIR)
return '. {{prefix}}{0}/bin/activate &&'.format(
helpers.environment_dir(ENVIRONMENT_DIR, self.language_version)
)
@contextlib.contextmanager
def in_env(repo_cmd_runner):
yield RubyEnv(repo_cmd_runner)
def in_env(repo_cmd_runner, language_version):
yield RubyEnv(repo_cmd_runner, language_version)
def _install_rbenv(repo_cmd_runner, version='default'):
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
with tarfile_open(resource_filename('rbenv.tar.gz')) as tf:
tf.extractall(repo_cmd_runner.path('.'))
shutil.move(
repo_cmd_runner.path('rbenv'), repo_cmd_runner.path(directory),
)
# Only install ruby-build if the version is specified
if version != 'default':
# ruby-download
with tarfile_open(resource_filename('ruby-download.tar.gz')) as tf:
tf.extractall(repo_cmd_runner.path('rbenv', 'plugins'))
tf.extractall(repo_cmd_runner.path(directory, 'plugins'))
# ruby-build
with tarfile_open(resource_filename('ruby-build.tar.gz')) as tf:
tf.extractall(repo_cmd_runner.path('rbenv', 'plugins'))
tf.extractall(repo_cmd_runner.path(directory, 'plugins'))
activate_path = repo_cmd_runner.path('rbenv', 'bin', 'activate')
activate_path = repo_cmd_runner.path(directory, 'bin', 'activate')
with io.open(activate_path, 'w') as activate_file:
# This is similar to how you would install rbenv to your home directory
# However we do a couple things to make the executables exposed and
@ -54,7 +62,7 @@ def _install_rbenv(repo_cmd_runner, version='default'):
# directory
"export GEM_HOME='{0}/gems'\n"
'export PATH="$GEM_HOME/bin:$PATH"\n'
'\n'.format(repo_cmd_runner.path('rbenv'))
'\n'.format(repo_cmd_runner.path(directory))
)
# If we aren't using the system ruby, add a version here
@ -71,11 +79,12 @@ def _install_ruby(environment, version):
def install_environment(repo_cmd_runner, version='default'):
with clean_path_on_failure(repo_cmd_runner.path('rbenv')):
directory = helpers.environment_dir(ENVIRONMENT_DIR, version)
with clean_path_on_failure(repo_cmd_runner.path(directory)):
# TODO: this currently will fail if there's no version specified and
# there's no system ruby installed. Is this ok?
_install_rbenv(repo_cmd_runner, version=version)
with in_env(repo_cmd_runner) as ruby_env:
with in_env(repo_cmd_runner, version) as ruby_env:
if version != 'default':
_install_ruby(ruby_env, version)
ruby_env.run(
@ -84,5 +93,5 @@ def install_environment(repo_cmd_runner, version='default'):
def run_hook(repo_cmd_runner, hook, file_args):
with in_env(repo_cmd_runner) as env:
with in_env(repo_cmd_runner, hook['language_version']) as env:
return helpers.run_hook(env, hook, file_args)