|
| 1 | +import contextlib |
| 2 | +import os.path |
| 3 | +from typing import Generator |
| 4 | +from typing import Sequence |
| 5 | +from typing import Tuple |
| 6 | + |
| 7 | +import pre_commit.constants as C |
| 8 | +from pre_commit.envcontext import envcontext |
| 9 | +from pre_commit.envcontext import PatchesT |
| 10 | +from pre_commit.envcontext import Var |
| 11 | +from pre_commit.hook import Hook |
| 12 | +from pre_commit.languages import helpers |
| 13 | +from pre_commit.prefix import Prefix |
| 14 | +from pre_commit.util import clean_path_on_failure |
| 15 | +from pre_commit.util import rmtree |
| 16 | + |
| 17 | +ENVIRONMENT_DIR = 'dotnetenv' |
| 18 | +BIN_DIR = 'bin' |
| 19 | + |
| 20 | +get_default_version = helpers.basic_get_default_version |
| 21 | +healthy = helpers.basic_healthy |
| 22 | + |
| 23 | + |
| 24 | +def get_env_patch(venv: str) -> PatchesT: |
| 25 | + return ( |
| 26 | + ('PATH', (os.path.join(venv, BIN_DIR), os.pathsep, Var('PATH'))), |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +@contextlib.contextmanager |
| 31 | +def in_env(prefix: Prefix) -> Generator[None, None, None]: |
| 32 | + directory = helpers.environment_dir(ENVIRONMENT_DIR, C.DEFAULT) |
| 33 | + envdir = prefix.path(directory) |
| 34 | + with envcontext(get_env_patch(envdir)): |
| 35 | + yield |
| 36 | + |
| 37 | + |
| 38 | +def install_environment( |
| 39 | + prefix: Prefix, |
| 40 | + version: str, |
| 41 | + additional_dependencies: Sequence[str], |
| 42 | +) -> None: |
| 43 | + helpers.assert_version_default('dotnet', version) |
| 44 | + helpers.assert_no_additional_deps('dotnet', additional_dependencies) |
| 45 | + |
| 46 | + envdir = prefix.path(helpers.environment_dir(ENVIRONMENT_DIR, version)) |
| 47 | + with clean_path_on_failure(envdir): |
| 48 | + build_dir = 'pre-commit-build' |
| 49 | + |
| 50 | + # Build & pack nupkg file |
| 51 | + helpers.run_setup_cmd( |
| 52 | + prefix, |
| 53 | + ( |
| 54 | + 'dotnet', 'pack', |
| 55 | + '--configuration', 'Release', |
| 56 | + '--output', build_dir, |
| 57 | + ), |
| 58 | + ) |
| 59 | + |
| 60 | + # Determine tool from the packaged file <tool_name>.<version>.nupkg |
| 61 | + build_outputs = os.listdir(os.path.join(prefix.prefix_dir, build_dir)) |
| 62 | + if len(build_outputs) != 1: |
| 63 | + raise NotImplementedError( |
| 64 | + f"Can't handle multiple build outputs. Got {build_outputs}", |
| 65 | + ) |
| 66 | + tool_name = build_outputs[0].split('.')[0] |
| 67 | + |
| 68 | + # Install to bin dir |
| 69 | + helpers.run_setup_cmd( |
| 70 | + prefix, |
| 71 | + ( |
| 72 | + 'dotnet', 'tool', 'install', |
| 73 | + '--tool-path', os.path.join(envdir, BIN_DIR), |
| 74 | + '--add-source', build_dir, |
| 75 | + tool_name, |
| 76 | + ), |
| 77 | + ) |
| 78 | + |
| 79 | + # Cleanup build output |
| 80 | + for d in ('bin', 'obj', build_dir): |
| 81 | + rmtree(prefix.path(d)) |
| 82 | + |
| 83 | + |
| 84 | +def run_hook( |
| 85 | + hook: Hook, |
| 86 | + file_args: Sequence[str], |
| 87 | + color: bool, |
| 88 | +) -> Tuple[int, bytes]: |
| 89 | + with in_env(hook.prefix): |
| 90 | + return helpers.run_xargs(hook, hook.cmd, file_args, color=color) |
0 commit comments