11import contextlib
2+ import functools
23import os .path
34import shutil
45import tarfile
78from typing import Tuple
89
910import pre_commit .constants as C
11+ from pre_commit import parse_shebang
1012from pre_commit .envcontext import envcontext
1113from pre_commit .envcontext import PatchesT
1214from pre_commit .envcontext import UNSET
1921from pre_commit .util import resource_bytesio
2022
2123ENVIRONMENT_DIR = 'rbenv'
22- get_default_version = helpers .basic_get_default_version
2324healthy = helpers .basic_healthy
2425
2526
27+ @functools .lru_cache (maxsize = 1 )
28+ def get_default_version () -> str :
29+ if all (parse_shebang .find_executable (exe ) for exe in ('ruby' , 'gem' )):
30+ return 'system'
31+ else :
32+ return C .DEFAULT
33+
34+
2635def get_env_patch (
2736 venv : str ,
2837 language_version : str ,
29- ) -> PatchesT : # pragma: win32 no cover
38+ ) -> PatchesT :
3039 patches : PatchesT = (
3140 ('GEM_HOME' , os .path .join (venv , 'gems' )),
3241 ('GEM_PATH' , UNSET ),
33- ('RBENV_ROOT' , venv ),
3442 ('BUNDLE_IGNORE_CONFIG' , '1' ),
35- (
36- 'PATH' , (
37- os .path .join (venv , 'gems' , 'bin' ), os .pathsep ,
38- os .path .join (venv , 'shims' ), os .pathsep ,
39- os .path .join (venv , 'bin' ), os .pathsep , Var ('PATH' ),
40- ),
41- ),
4243 )
43- if language_version != C .DEFAULT :
44- patches += (('RBENV_VERSION' , language_version ),)
44+ if language_version == 'system' :
45+ patches += (
46+ (
47+ 'PATH' , (
48+ os .path .join (venv , 'gems' , 'bin' ), os .pathsep ,
49+ Var ('PATH' ),
50+ ),
51+ ),
52+ )
53+ else : # pragma: win32 no cover
54+ patches += (
55+ ('RBENV_ROOT' , venv ),
56+ ('RBENV_VERSION' , language_version ),
57+ (
58+ 'PATH' , (
59+ os .path .join (venv , 'gems' , 'bin' ), os .pathsep ,
60+ os .path .join (venv , 'shims' ), os .pathsep ,
61+ os .path .join (venv , 'bin' ), os .pathsep , Var ('PATH' ),
62+ ),
63+ ),
64+ )
4565 return patches
4666
4767
48- @contextlib .contextmanager # pragma: win32 no cover
68+ @contextlib .contextmanager
4969def in_env (
5070 prefix : Prefix ,
5171 language_version : str ,
@@ -65,7 +85,7 @@ def _extract_resource(filename: str, dest: str) -> None:
6585
6686def _install_rbenv (
6787 prefix : Prefix ,
68- version : str = C . DEFAULT ,
88+ version : str ,
6989) -> None : # pragma: win32 no cover
7090 directory = helpers .environment_dir (ENVIRONMENT_DIR , version )
7191
@@ -92,21 +112,22 @@ def _install_ruby(
92112
93113def install_environment (
94114 prefix : Prefix , version : str , additional_dependencies : Sequence [str ],
95- ) -> None : # pragma: win32 no cover
115+ ) -> None :
96116 additional_dependencies = tuple (additional_dependencies )
97117 directory = helpers .environment_dir (ENVIRONMENT_DIR , version )
98118 with clean_path_on_failure (prefix .path (directory )):
99- # TODO: this currently will fail if there's no version specified and
100- # there's no system ruby installed. Is this ok?
101- _install_rbenv (prefix , version = version )
102- with in_env (prefix , version ):
103- # Need to call this before installing so rbenv's directories are
104- # set up
105- helpers .run_setup_cmd (prefix , ('rbenv' , 'init' , '-' ))
106- if version != C .DEFAULT :
119+ if version != 'system' : # pragma: win32 no cover
120+ _install_rbenv (prefix , version )
121+ with in_env (prefix , version ):
122+ # Need to call this before installing so rbenv's directories
123+ # are set up
124+ helpers .run_setup_cmd (prefix , ('rbenv' , 'init' , '-' ))
125+ # XXX: this will *always* fail if `version == C.DEFAULT`
107126 _install_ruby (prefix , version )
108- # Need to call this after installing to set up the shims
109- helpers .run_setup_cmd (prefix , ('rbenv' , 'rehash' ))
127+ # Need to call this after installing to set up the shims
128+ helpers .run_setup_cmd (prefix , ('rbenv' , 'rehash' ))
129+
130+ with in_env (prefix , version ):
110131 helpers .run_setup_cmd (
111132 prefix , ('gem' , 'build' , * prefix .star ('.gemspec' )),
112133 )
@@ -123,6 +144,6 @@ def run_hook(
123144 hook : Hook ,
124145 file_args : Sequence [str ],
125146 color : bool ,
126- ) -> Tuple [int , bytes ]: # pragma: win32 no cover
147+ ) -> Tuple [int , bytes ]:
127148 with in_env (hook .prefix , hook .language_version ):
128149 return helpers .run_xargs (hook , hook .cmd , file_args , color = color )
0 commit comments