|
| 1 | +import contextlib |
| 2 | +import os.path |
| 3 | +import shutil |
| 4 | +import tempfile |
| 5 | +from typing import Generator |
| 6 | +from typing import Sequence |
| 7 | +from typing import Tuple |
| 8 | + |
| 9 | +import pre_commit.constants as C |
| 10 | +from pre_commit.envcontext import envcontext |
| 11 | +from pre_commit.envcontext import PatchesT |
| 12 | +from pre_commit.envcontext import Var |
| 13 | +from pre_commit.hook import Hook |
| 14 | +from pre_commit.languages import helpers |
| 15 | +from pre_commit.prefix import Prefix |
| 16 | +from pre_commit.util import clean_path_on_failure |
| 17 | +from pre_commit.util import win_exe |
| 18 | +from pre_commit.util import yaml_load |
| 19 | + |
| 20 | +ENVIRONMENT_DIR = 'dartenv' |
| 21 | + |
| 22 | +get_default_version = helpers.basic_get_default_version |
| 23 | +healthy = helpers.basic_healthy |
| 24 | + |
| 25 | + |
| 26 | +def get_env_patch(venv: str) -> PatchesT: |
| 27 | + return ( |
| 28 | + ('PATH', (os.path.join(venv, 'bin'), os.pathsep, Var('PATH'))), |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +@contextlib.contextmanager |
| 33 | +def in_env(prefix: Prefix) -> Generator[None, None, None]: |
| 34 | + directory = helpers.environment_dir(ENVIRONMENT_DIR, C.DEFAULT) |
| 35 | + envdir = prefix.path(directory) |
| 36 | + with envcontext(get_env_patch(envdir)): |
| 37 | + yield |
| 38 | + |
| 39 | + |
| 40 | +def install_environment( |
| 41 | + prefix: Prefix, |
| 42 | + version: str, |
| 43 | + additional_dependencies: Sequence[str], |
| 44 | +) -> None: |
| 45 | + helpers.assert_version_default('dart', version) |
| 46 | + |
| 47 | + envdir = prefix.path(helpers.environment_dir(ENVIRONMENT_DIR, version)) |
| 48 | + bin_dir = os.path.join(envdir, 'bin') |
| 49 | + |
| 50 | + def _install_dir(prefix_p: Prefix, pub_cache: str) -> None: |
| 51 | + dart_env = {**os.environ, 'PUB_CACHE': pub_cache} |
| 52 | + |
| 53 | + with open(prefix_p.path('pubspec.yaml')) as f: |
| 54 | + pubspec_contents = yaml_load(f) |
| 55 | + |
| 56 | + helpers.run_setup_cmd(prefix_p, ('dart', 'pub', 'get'), env=dart_env) |
| 57 | + |
| 58 | + for executable in pubspec_contents['executables']: |
| 59 | + helpers.run_setup_cmd( |
| 60 | + prefix_p, |
| 61 | + ( |
| 62 | + 'dart', 'compile', 'exe', |
| 63 | + '--output', os.path.join(bin_dir, win_exe(executable)), |
| 64 | + prefix_p.path('bin', f'{executable}.dart'), |
| 65 | + ), |
| 66 | + env=dart_env, |
| 67 | + ) |
| 68 | + |
| 69 | + with clean_path_on_failure(envdir): |
| 70 | + os.makedirs(bin_dir) |
| 71 | + |
| 72 | + with tempfile.TemporaryDirectory() as tmp: |
| 73 | + _install_dir(prefix, tmp) |
| 74 | + |
| 75 | + for dep_s in additional_dependencies: |
| 76 | + with tempfile.TemporaryDirectory() as dep_tmp: |
| 77 | + dep, _, version = dep_s.partition(':') |
| 78 | + if version: |
| 79 | + dep_cmd: Tuple[str, ...] = (dep, '--version', version) |
| 80 | + else: |
| 81 | + dep_cmd = (dep,) |
| 82 | + |
| 83 | + helpers.run_setup_cmd( |
| 84 | + prefix, |
| 85 | + ('dart', 'pub', 'cache', 'add', *dep_cmd), |
| 86 | + env={**os.environ, 'PUB_CACHE': dep_tmp}, |
| 87 | + ) |
| 88 | + |
| 89 | + # try and find the 'pubspec.yaml' that just got added |
| 90 | + for root, _, filenames in os.walk(dep_tmp): |
| 91 | + if 'pubspec.yaml' in filenames: |
| 92 | + with tempfile.TemporaryDirectory() as copied: |
| 93 | + pkg = os.path.join(copied, 'pkg') |
| 94 | + shutil.copytree(root, pkg) |
| 95 | + _install_dir(Prefix(pkg), dep_tmp) |
| 96 | + break |
| 97 | + else: |
| 98 | + raise AssertionError( |
| 99 | + f'could not find pubspec.yaml for {dep_s}', |
| 100 | + ) |
| 101 | + |
| 102 | + |
| 103 | +def run_hook( |
| 104 | + hook: Hook, |
| 105 | + file_args: Sequence[str], |
| 106 | + color: bool, |
| 107 | +) -> Tuple[int, bytes]: |
| 108 | + with in_env(hook.prefix): |
| 109 | + return helpers.run_xargs(hook, hook.cmd, file_args, color=color) |
0 commit comments