|
| 1 | +from __future__ import unicode_literals |
| 2 | + |
| 3 | +import contextlib |
| 4 | +import os.path |
| 5 | + |
| 6 | +import toml |
| 7 | + |
| 8 | +from pre_commit.envcontext import envcontext |
| 9 | +from pre_commit.envcontext import Var |
| 10 | +from pre_commit.languages import helpers |
| 11 | +from pre_commit.util import clean_path_on_failure |
| 12 | +from pre_commit.util import cmd_output |
| 13 | +from pre_commit.xargs import xargs |
| 14 | + |
| 15 | + |
| 16 | +ENVIRONMENT_DIR = 'rustenv' |
| 17 | +get_default_version = helpers.basic_get_default_version |
| 18 | +healthy = helpers.basic_healthy |
| 19 | + |
| 20 | + |
| 21 | +def get_env_patch(target_dir): |
| 22 | + return ( |
| 23 | + ( |
| 24 | + 'PATH', |
| 25 | + (os.path.join(target_dir, 'bin'), os.pathsep, Var('PATH')), |
| 26 | + ), |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +@contextlib.contextmanager |
| 31 | +def in_env(prefix): |
| 32 | + target_dir = prefix.path( |
| 33 | + helpers.environment_dir(ENVIRONMENT_DIR, 'default'), |
| 34 | + ) |
| 35 | + with envcontext(get_env_patch(target_dir)): |
| 36 | + yield |
| 37 | + |
| 38 | + |
| 39 | +def _add_dependencies(cargo_toml_path, additional_dependencies): |
| 40 | + with open(cargo_toml_path, 'r+') as f: |
| 41 | + cargo_toml = toml.load(f) |
| 42 | + cargo_toml.setdefault('dependencies', {}) |
| 43 | + for dep in additional_dependencies: |
| 44 | + name, _, spec = dep.partition(':') |
| 45 | + cargo_toml['dependencies'][name] = spec or '*' |
| 46 | + f.seek(0) |
| 47 | + toml.dump(cargo_toml, f) |
| 48 | + f.truncate() |
| 49 | + |
| 50 | + |
| 51 | +def install_environment(prefix, version, additional_dependencies): |
| 52 | + helpers.assert_version_default('rust', version) |
| 53 | + directory = prefix.path( |
| 54 | + helpers.environment_dir(ENVIRONMENT_DIR, 'default'), |
| 55 | + ) |
| 56 | + |
| 57 | + # There are two cases where we might want to specify more dependencies: |
| 58 | + # as dependencies for the library being built, and as binary packages |
| 59 | + # to be `cargo install`'d. |
| 60 | + # |
| 61 | + # Unlike e.g. Python, if we just `cargo install` a library, it won't be |
| 62 | + # used for compilation. And if we add a crate providing a binary to the |
| 63 | + # `Cargo.toml`, the binary won't be built. |
| 64 | + # |
| 65 | + # Because of this, we allow specifying "cli" dependencies by prefixing |
| 66 | + # with 'cli:'. |
| 67 | + cli_deps = { |
| 68 | + dep for dep in additional_dependencies if dep.startswith('cli:') |
| 69 | + } |
| 70 | + lib_deps = set(additional_dependencies) - cli_deps |
| 71 | + |
| 72 | + if len(lib_deps) > 0: |
| 73 | + _add_dependencies(prefix.path('Cargo.toml'), lib_deps) |
| 74 | + |
| 75 | + with clean_path_on_failure(directory): |
| 76 | + packages_to_install = {()} |
| 77 | + for cli_dep in cli_deps: |
| 78 | + cli_dep = cli_dep[len('cli:'):] |
| 79 | + package, _, version = cli_dep.partition(':') |
| 80 | + if version != '': |
| 81 | + packages_to_install.add((package, '--version', version)) |
| 82 | + else: |
| 83 | + packages_to_install.add((package,)) |
| 84 | + |
| 85 | + for package in packages_to_install: |
| 86 | + cmd_output( |
| 87 | + 'cargo', 'install', '--bins', '--root', directory, *package, |
| 88 | + cwd=prefix.prefix_dir |
| 89 | + ) |
| 90 | + |
| 91 | + |
| 92 | +def run_hook(prefix, hook, file_args): |
| 93 | + with in_env(prefix): |
| 94 | + return xargs(helpers.to_cmd(hook), file_args) |
0 commit comments