44import io
55
66from pre_commit .languages import helpers
7+ from pre_commit .prefixed_command_runner import CalledProcessError
78from pre_commit .util import clean_path_on_failure
89
910
@@ -28,6 +29,12 @@ def _install_rbenv(repo_cmd_runner, version='default'):
2829
2930 # Only install ruby-build if the version is specified
3031 if version != 'default' :
32+ # ruby-download
33+ repo_cmd_runner .run ([
34+ 'git' , 'clone' , 'git://github.com/garnieretienne/rvm-download' ,
35+ '{prefix}rbenv/plugins/ruby-download' ,
36+ ])
37+ # ruby-build
3138 repo_cmd_runner .run ([
3239 'git' , 'clone' , 'git://github.com/sstephenson/ruby-build' ,
3340 '{prefix}rbenv/plugins/ruby-build' ,
@@ -57,14 +64,22 @@ def _install_rbenv(repo_cmd_runner, version='default'):
5764 activate_file .write ('export RBENV_VERSION="{0}"\n ' .format (version ))
5865
5966
67+ def _install_ruby (environment , version ):
68+ try :
69+ environment .run ('rbenv download {0}' .format (version ))
70+ except CalledProcessError :
71+ # Failed to download from mirror for some reason, build it instead
72+ environment .run ('rbenv install {0}' .format (version ))
73+
74+
6075def install_environment (repo_cmd_runner , version = 'default' ):
6176 with clean_path_on_failure (repo_cmd_runner .path ('rbenv' )):
6277 # TODO: this currently will fail if there's no version specified and
6378 # there's no system ruby installed. Is this ok?
6479 _install_rbenv (repo_cmd_runner , version = version )
6580 with in_env (repo_cmd_runner ) as ruby_env :
6681 if version != 'default' :
67- ruby_env . run ( 'rbenv install {0}' . format ( version ) )
82+ _install_ruby ( ruby_env , version )
6883 ruby_env .run (
6984 'cd {prefix} && gem build *.gemspec && gem install *.gem' ,
7085 )
0 commit comments