|
11 | 11 | from typing import Generator |
12 | 12 | from typing import Sequence |
13 | 13 |
|
14 | | -import toml |
15 | | - |
16 | 14 | import pre_commit.constants as C |
17 | 15 | from pre_commit import parse_shebang |
18 | 16 | from pre_commit.envcontext import envcontext |
@@ -82,18 +80,13 @@ def in_env( |
82 | 80 |
|
83 | 81 |
|
84 | 82 | def _add_dependencies( |
85 | | - cargo_toml_path: str, |
| 83 | + prefix: Prefix, |
86 | 84 | additional_dependencies: set[str], |
87 | 85 | ) -> None: |
88 | | - with open(cargo_toml_path, 'r+') as f: |
89 | | - cargo_toml = toml.load(f) |
90 | | - cargo_toml.setdefault('dependencies', {}) |
91 | | - for dep in additional_dependencies: |
92 | | - name, _, spec = dep.partition(':') |
93 | | - cargo_toml['dependencies'][name] = spec or '*' |
94 | | - f.seek(0) |
95 | | - toml.dump(cargo_toml, f) |
96 | | - f.truncate() |
| 86 | + for dep in additional_dependencies: |
| 87 | + name, _, spec = dep.partition(':') |
| 88 | + crate = name + (f'@{spec}' if spec else '') |
| 89 | + helpers.run_setup_cmd(prefix, ('cargo', 'add', crate)) |
97 | 90 |
|
98 | 91 |
|
99 | 92 | def install_rust_with_toolchain(toolchain: str) -> None: |
@@ -152,7 +145,7 @@ def install_environment( |
152 | 145 | lib_deps = set(additional_dependencies) - cli_deps |
153 | 146 |
|
154 | 147 | if len(lib_deps) > 0: |
155 | | - _add_dependencies(prefix.path('Cargo.toml'), lib_deps) |
| 148 | + _add_dependencies(prefix, lib_deps) |
156 | 149 |
|
157 | 150 | with clean_path_on_failure(directory): |
158 | 151 | packages_to_install: set[tuple[str, ...]] = {('--path', '.')} |
|
0 commit comments