66import shutil
77import tempfile
88import textwrap
9+ import typing
910from collections .abc import Generator
1011from collections .abc import Sequence
1112
2021ENVIRONMENT_DIR = 'renv'
2122RSCRIPT_OPTS = ('--no-save' , '--no-restore' , '--no-site-file' , '--no-environ' )
2223get_default_version = lang_base .basic_get_default_version
23- health_check = lang_base .basic_health_check
24+
25+
26+ def _execute_vanilla_r_code_as_script (code : str , ** kwargs : typing .Any ) -> str :
27+ with _r_code_in_tempfile (code ) as f :
28+ cmd_out = cmd_output_b (_rscript_exec (), '--vanilla' , f , ** kwargs )
29+ return cmd_out [1 ].decode ().rstrip ('\n ' )
30+
31+
32+ def _read_installed_version (envdir : str ) -> str :
33+ return _execute_vanilla_r_code_as_script (
34+ 'cat(renv::settings$r.version())' , cwd = envdir ,
35+ )
36+
37+
38+ def _read_executable_version (envdir : str ) -> str :
39+ return _execute_vanilla_r_code_as_script (
40+ 'cat(as.character(getRversion()))' ,
41+ cwd = envdir ,
42+ )
43+
44+
45+ def _write_current_r_version (envdir : str ) -> None :
46+ _execute_vanilla_r_code_as_script (
47+ 'renv::settings$r.version(as.character(getRversion()))' ,
48+ cwd = envdir ,
49+ )
50+
51+
52+ def health_check (prefix : Prefix , version : str ) -> str | None :
53+ # prefix: cloned hook repo .../{precommit}
54+ # env dir .../{precommit}/renv-default/
55+ envdir = lang_base .environment_dir (prefix , ENVIRONMENT_DIR , version )
56+
57+ r_version_installation = _read_installed_version (envdir = envdir )
58+ # TODO what if none set? -> record current?
59+ # TODO how is this affecting old hooks environments
60+ r_version_current_executable = _read_executable_version (envdir = envdir )
61+ if r_version_installation in {'NULL' , '' }:
62+ return (
63+ 'Hooks were installed with an unknown R version. R version for '
64+ f'hook repo now set to { r_version_current_executable } '
65+ )
66+ elif r_version_installation != r_version_current_executable :
67+ return (
68+ f'Hooks were installed for R version { r_version_installation } , '
69+ 'but current R executable has version '
70+ f'{ r_version_current_executable } '
71+ )
72+
73+ return None
2474
2575
2676@contextlib .contextmanager
@@ -130,7 +180,6 @@ def install_environment(
130180 'setwd(old); ',
131181 'renv::load("', getwd(), '");}})'
132182 )
133- writeLines(activate_statement, 'activate.R')
134183 is_package <- tryCatch(
135184 {{
136185 path_desc <- file.path(prefix_dir, 'DESCRIPTION')
@@ -142,11 +191,13 @@ def install_environment(
142191 if (is_package) {{
143192 renv::install(prefix_dir)
144193 }}
194+ writeLines(activate_statement, 'activate.R')
145195 """
146196
147197 with _r_code_in_tempfile (r_code_inst_environment ) as f :
148198 cmd_output_b (_rscript_exec (), '--vanilla' , f , cwd = env_dir )
149199
200+ _write_current_r_version (envdir = env_dir )
150201 if additional_dependencies :
151202 r_code_inst_add = 'renv::install(commandArgs(trailingOnly = TRUE))'
152203 with in_env (prefix , version ):
0 commit comments