Skip to content

Commit

Permalink
wip: read package ID from xml
Browse files Browse the repository at this point in the history
  • Loading branch information
rkm committed Dec 13, 2022
1 parent 59ed2ee commit 46af90f
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions pre_commit/languages/dotnet.py
Expand Up @@ -3,6 +3,8 @@
import contextlib
import os.path
import re
import xml.etree.ElementTree
import zipfile
from typing import Generator
from typing import Sequence

Expand Down Expand Up @@ -58,16 +60,25 @@ def install_environment(
),
)

# Determine tool from the packaged file <tool_name>.<version>.nupkg
# https://learn.microsoft.com/en-us/nuget/concepts/package-versioning#version-basics
tool_name_re = re.compile(r'(.*)\.\d+\.\d+\.\d+(-.*)?\.nupkg')
nupkg_dir = os.path.join(prefix.prefix_dir, build_dir)
nupkgs = [
x for x in os.listdir(nupkg_dir)
if x.endswith('.nupkg')
]

build_outputs = os.listdir(os.path.join(prefix.prefix_dir, build_dir))
for output in build_outputs:
for nupkg in nupkgs:

tool_name_match = tool_name_re.match(output)
assert tool_name_match
tool_name = tool_name_match.group(1)
with zipfile.ZipFile(os.path.join(nupkg_dir, nupkg)) as z:
nuspec = [x for x in z.namelist() if x.endswith('.nuspec')][0]
with z.open(nuspec) as n:
tree = xml.etree.ElementTree.parse(n)

ns = re.match(r'{.*}', tree.getroot().tag)
assert ns
tool_id_attribute = tree.find(f'.//{ns.group(0)}id')
assert tool_id_attribute
tool_id = tool_id_attribute.text
assert tool_id

# Install to bin dir
helpers.run_setup_cmd(
Expand All @@ -76,7 +87,7 @@ def install_environment(
'dotnet', 'tool', 'install',
'--tool-path', os.path.join(envdir, BIN_DIR),
'--add-source', build_dir,
tool_name,
tool_id,
),
)

Expand Down

0 comments on commit 46af90f

Please sign in to comment.