mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-19 17:14:43 +04:00
Merge pull request #2568 from m-rsha/cargler-bargler
Change Rust to install environment with `cargo add` over `toml`
This commit is contained in:
commit
71925c47ea
3 changed files with 12 additions and 17 deletions
|
|
@ -11,8 +11,6 @@ import urllib.request
|
||||||
from typing import Generator
|
from typing import Generator
|
||||||
from typing import Sequence
|
from typing import Sequence
|
||||||
|
|
||||||
import toml
|
|
||||||
|
|
||||||
import pre_commit.constants as C
|
import pre_commit.constants as C
|
||||||
from pre_commit import parse_shebang
|
from pre_commit import parse_shebang
|
||||||
from pre_commit.envcontext import envcontext
|
from pre_commit.envcontext import envcontext
|
||||||
|
|
@ -82,18 +80,16 @@ def in_env(
|
||||||
|
|
||||||
|
|
||||||
def _add_dependencies(
|
def _add_dependencies(
|
||||||
cargo_toml_path: str,
|
prefix: Prefix,
|
||||||
additional_dependencies: set[str],
|
additional_dependencies: set[str],
|
||||||
) -> None:
|
) -> None:
|
||||||
with open(cargo_toml_path, 'r+') as f:
|
crates = []
|
||||||
cargo_toml = toml.load(f)
|
for dep in additional_dependencies:
|
||||||
cargo_toml.setdefault('dependencies', {})
|
name, _, spec = dep.partition(':')
|
||||||
for dep in additional_dependencies:
|
crate = f'{name}@{spec or "*"}'
|
||||||
name, _, spec = dep.partition(':')
|
crates.append(crate)
|
||||||
cargo_toml['dependencies'][name] = spec or '*'
|
|
||||||
f.seek(0)
|
helpers.run_setup_cmd(prefix, ('cargo', 'add', *crates))
|
||||||
toml.dump(cargo_toml, f)
|
|
||||||
f.truncate()
|
|
||||||
|
|
||||||
|
|
||||||
def install_rust_with_toolchain(toolchain: str) -> None:
|
def install_rust_with_toolchain(toolchain: str) -> None:
|
||||||
|
|
@ -151,9 +147,6 @@ def install_environment(
|
||||||
}
|
}
|
||||||
lib_deps = set(additional_dependencies) - cli_deps
|
lib_deps = set(additional_dependencies) - cli_deps
|
||||||
|
|
||||||
if len(lib_deps) > 0:
|
|
||||||
_add_dependencies(prefix.path('Cargo.toml'), lib_deps)
|
|
||||||
|
|
||||||
with clean_path_on_failure(directory):
|
with clean_path_on_failure(directory):
|
||||||
packages_to_install: set[tuple[str, ...]] = {('--path', '.')}
|
packages_to_install: set[tuple[str, ...]] = {('--path', '.')}
|
||||||
for cli_dep in cli_deps:
|
for cli_dep in cli_deps:
|
||||||
|
|
@ -168,6 +161,9 @@ def install_environment(
|
||||||
if version != 'system':
|
if version != 'system':
|
||||||
install_rust_with_toolchain(_rust_toolchain(version))
|
install_rust_with_toolchain(_rust_toolchain(version))
|
||||||
|
|
||||||
|
if len(lib_deps) > 0:
|
||||||
|
_add_dependencies(prefix, lib_deps)
|
||||||
|
|
||||||
for args in packages_to_install:
|
for args in packages_to_install:
|
||||||
cmd_output_b(
|
cmd_output_b(
|
||||||
'cargo', 'install', '--bins', '--root', directory, *args,
|
'cargo', 'install', '--bins', '--root', directory, *args,
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,6 @@ install_requires =
|
||||||
identify>=1.0.0
|
identify>=1.0.0
|
||||||
nodeenv>=0.11.1
|
nodeenv>=0.11.1
|
||||||
pyyaml>=5.1
|
pyyaml>=5.1
|
||||||
toml
|
|
||||||
virtualenv>=20.10.0
|
virtualenv>=20.10.0
|
||||||
importlib-metadata;python_version<"3.8"
|
importlib-metadata;python_version<"3.8"
|
||||||
python_requires = >=3.7
|
python_requires = >=3.7
|
||||||
|
|
|
||||||
|
|
@ -485,7 +485,7 @@ def test_additional_rust_lib_dependencies_installed(
|
||||||
path = make_repo(tempdir_factory, 'rust_hooks_repo')
|
path = make_repo(tempdir_factory, 'rust_hooks_repo')
|
||||||
config = make_config_from_repo(path)
|
config = make_config_from_repo(path)
|
||||||
# A small rust package with no dependencies.
|
# A small rust package with no dependencies.
|
||||||
deps = ['shellharden:3.1.0']
|
deps = ['shellharden:3.1.0', 'git-version']
|
||||||
config['hooks'][0]['additional_dependencies'] = deps
|
config['hooks'][0]['additional_dependencies'] = deps
|
||||||
hook = _get_hook(config, store, 'rust-hook')
|
hook = _get_hook(config, store, 'rust-hook')
|
||||||
binaries = os.listdir(
|
binaries = os.listdir(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue