diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-01-04 07:22:18 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2023-01-04 07:22:18 +0000 |
commit | 58a809aa43ae21566be4613eb42ac56faf54f901 (patch) | |
tree | 371b9cc8a265cbabea4b12e5508b8bde6edf0b89 /pre_commit/languages/dotnet.py | |
parent | Adding upstream version 2.20.0. (diff) | |
download | pre-commit-58a809aa43ae21566be4613eb42ac56faf54f901.tar.xz pre-commit-58a809aa43ae21566be4613eb42ac56faf54f901.zip |
Adding upstream version 2.21.0.upstream/2.21.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'pre_commit/languages/dotnet.py')
-rw-r--r-- | pre_commit/languages/dotnet.py | 67 |
1 files changed, 54 insertions, 13 deletions
diff --git a/pre_commit/languages/dotnet.py b/pre_commit/languages/dotnet.py index 3983c6f..e26b45c 100644 --- a/pre_commit/languages/dotnet.py +++ b/pre_commit/languages/dotnet.py @@ -2,6 +2,10 @@ from __future__ import annotations import contextlib import os.path +import re +import tempfile +import xml.etree.ElementTree +import zipfile from typing import Generator from typing import Sequence @@ -35,6 +39,22 @@ def in_env(prefix: Prefix) -> Generator[None, None, None]: yield +@contextlib.contextmanager +def _nuget_config_no_sources() -> Generator[str, None, None]: + with tempfile.TemporaryDirectory() as tmpdir: + nuget_config = os.path.join(tmpdir, 'nuget.config') + with open(nuget_config, 'w') as f: + f.write( + '<?xml version="1.0" encoding="utf-8"?>' + '<configuration>' + ' <packageSources>' + ' <clear />' + ' </packageSources>' + '</configuration>', + ) + yield nuget_config + + def install_environment( prefix: Prefix, version: str, @@ -57,21 +77,42 @@ def install_environment( ), ) - # Determine tool from the packaged file <tool_name>.<version>.nupkg - build_outputs = os.listdir(os.path.join(prefix.prefix_dir, build_dir)) - for output in build_outputs: - tool_name = output.split('.')[0] + nupkg_dir = prefix.path(build_dir) + nupkgs = [x for x in os.listdir(nupkg_dir) if x.endswith('.nupkg')] + + if not nupkgs: + raise AssertionError('could not find any build outputs to install') + + for nupkg in nupkgs: + with zipfile.ZipFile(os.path.join(nupkg_dir, nupkg)) as f: + nuspec, = (x for x in f.namelist() if x.endswith('.nuspec')) + with f.open(nuspec) as spec: + tree = xml.etree.ElementTree.parse(spec) + + namespace = re.match(r'{.*}', tree.getroot().tag) + if not namespace: + raise AssertionError('could not parse namespace from nuspec') + + tool_id_element = tree.find(f'.//{namespace[0]}id') + if tool_id_element is None: + raise AssertionError('expected to find an "id" element') + + tool_id = tool_id_element.text + if not tool_id: + raise AssertionError('"id" element missing tool name') # Install to bin dir - helpers.run_setup_cmd( - prefix, - ( - 'dotnet', 'tool', 'install', - '--tool-path', os.path.join(envdir, BIN_DIR), - '--add-source', build_dir, - tool_name, - ), - ) + with _nuget_config_no_sources() as nuget_config: + helpers.run_setup_cmd( + prefix, + ( + 'dotnet', 'tool', 'install', + '--configfile', nuget_config, + '--tool-path', os.path.join(envdir, BIN_DIR), + '--add-source', build_dir, + tool_id, + ), + ) # Clean the git dir, ignoring the environment dir clean_cmd = ('git', 'clean', '-ffxd', '-e', f'{ENVIRONMENT_DIR}-*') |