Skip to content

Commit e904628

Browse files
rkmasottile
authored andcommitted
fix dotnet hooks with prefixes
1 parent d7b8b12 commit e904628

File tree

6 files changed

+57
-5
lines changed

6 files changed

+57
-5
lines changed

pre_commit/languages/dotnet.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import contextlib
44
import os.path
5+
import re
6+
import xml.etree.ElementTree
7+
import zipfile
58
from typing import Generator
69
from typing import Sequence
710

@@ -57,10 +60,29 @@ def install_environment(
5760
),
5861
)
5962

60-
# Determine tool from the packaged file <tool_name>.<version>.nupkg
61-
build_outputs = os.listdir(os.path.join(prefix.prefix_dir, build_dir))
62-
for output in build_outputs:
63-
tool_name = output.split('.')[0]
63+
nupkg_dir = prefix.path(build_dir)
64+
nupkgs = [x for x in os.listdir(nupkg_dir) if x.endswith('.nupkg')]
65+
66+
if not nupkgs:
67+
raise AssertionError('could not find any build outputs to install')
68+
69+
for nupkg in nupkgs:
70+
with zipfile.ZipFile(os.path.join(nupkg_dir, nupkg)) as f:
71+
nuspec, = (x for x in f.namelist() if x.endswith('.nuspec'))
72+
with f.open(nuspec) as spec:
73+
tree = xml.etree.ElementTree.parse(spec)
74+
75+
namespace = re.match(r'{.*}', tree.getroot().tag)
76+
if not namespace:
77+
raise AssertionError('could not parse namespace from nuspec')
78+
79+
tool_id_element = tree.find(f'.//{namespace[0]}id')
80+
if tool_id_element is None:
81+
raise AssertionError('expected to find an "id" element')
82+
83+
tool_id = tool_id_element.text
84+
if not tool_id:
85+
raise AssertionError('"id" element missing tool name')
6486

6587
# Install to bin dir
6688
helpers.run_setup_cmd(
@@ -69,7 +91,7 @@ def install_environment(
6991
'dotnet', 'tool', 'install',
7092
'--tool-path', os.path.join(envdir, BIN_DIR),
7193
'--add-source', build_dir,
72-
tool_name,
94+
tool_id,
7395
),
7496
)
7597

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bin/
2+
obj/
3+
nupkg/
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- id: dotnet-example-hook
2+
name: dotnet example hook
3+
entry: testeroni.tool
4+
language: dotnet
5+
files: ''
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace dotnet_hooks_repo
4+
{
5+
class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
Console.WriteLine("Hello from dotnet!");
10+
}
11+
}
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<PackAsTool>true</PackAsTool>
6+
<ToolCommandName>testeroni.tool</ToolCommandName>
7+
<PackageOutputPath>./nupkg</PackageOutputPath>
8+
</PropertyGroup>
9+
</Project>

tests/repository_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,7 @@ def test_local_perl_additional_dependencies(store):
10311031
'dotnet_hooks_csproj_repo',
10321032
'dotnet_hooks_sln_repo',
10331033
'dotnet_hooks_combo_repo',
1034+
'dotnet_hooks_csproj_prefix_repo',
10341035
),
10351036
)
10361037
def test_dotnet_hook(tempdir_factory, store, repo):

0 commit comments

Comments
 (0)