Skip to content

Commit 003e4c2

Browse files
rkmasottile
authored andcommitted
add initial dotnet support
1 parent a85b9f7 commit 003e4c2

File tree

16 files changed

+203
-3
lines changed

16 files changed

+203
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
/.tox
77
/dist
88
/venv*
9+
.vscode/

pre_commit/languages/all.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pre_commit.languages import conda
99
from pre_commit.languages import docker
1010
from pre_commit.languages import docker_image
11+
from pre_commit.languages import dotnet
1112
from pre_commit.languages import fail
1213
from pre_commit.languages import golang
1314
from pre_commit.languages import node
@@ -42,6 +43,7 @@ class Language(NamedTuple):
4243
'conda': Language(name='conda', ENVIRONMENT_DIR=conda.ENVIRONMENT_DIR, get_default_version=conda.get_default_version, healthy=conda.healthy, install_environment=conda.install_environment, run_hook=conda.run_hook), # noqa: E501
4344
'docker': Language(name='docker', ENVIRONMENT_DIR=docker.ENVIRONMENT_DIR, get_default_version=docker.get_default_version, healthy=docker.healthy, install_environment=docker.install_environment, run_hook=docker.run_hook), # noqa: E501
4445
'docker_image': Language(name='docker_image', ENVIRONMENT_DIR=docker_image.ENVIRONMENT_DIR, get_default_version=docker_image.get_default_version, healthy=docker_image.healthy, install_environment=docker_image.install_environment, run_hook=docker_image.run_hook), # noqa: E501
46+
'dotnet': Language(name='dotnet', ENVIRONMENT_DIR=dotnet.ENVIRONMENT_DIR, get_default_version=dotnet.get_default_version, healthy=dotnet.healthy, install_environment=dotnet.install_environment, run_hook=dotnet.run_hook), # noqa: E501
4547
'fail': Language(name='fail', ENVIRONMENT_DIR=fail.ENVIRONMENT_DIR, get_default_version=fail.get_default_version, healthy=fail.healthy, install_environment=fail.install_environment, run_hook=fail.run_hook), # noqa: E501
4648
'golang': Language(name='golang', ENVIRONMENT_DIR=golang.ENVIRONMENT_DIR, get_default_version=golang.get_default_version, healthy=golang.healthy, install_environment=golang.install_environment, run_hook=golang.run_hook), # noqa: E501
4749
'node': Language(name='node', ENVIRONMENT_DIR=node.ENVIRONMENT_DIR, get_default_version=node.get_default_version, healthy=node.healthy, install_environment=node.install_environment, run_hook=node.run_hook), # noqa: E501

pre_commit/languages/dotnet.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import contextlib
2+
import os.path
3+
from typing import Generator
4+
from typing import Sequence
5+
from typing import Tuple
6+
7+
import pre_commit.constants as C
8+
from pre_commit.envcontext import envcontext
9+
from pre_commit.envcontext import PatchesT
10+
from pre_commit.envcontext import Var
11+
from pre_commit.hook import Hook
12+
from pre_commit.languages import helpers
13+
from pre_commit.prefix import Prefix
14+
from pre_commit.util import clean_path_on_failure
15+
from pre_commit.util import rmtree
16+
17+
ENVIRONMENT_DIR = 'dotnetenv'
18+
BIN_DIR = 'bin'
19+
20+
get_default_version = helpers.basic_get_default_version
21+
healthy = helpers.basic_healthy
22+
23+
24+
def get_env_patch(venv: str) -> PatchesT:
25+
return (
26+
('PATH', (os.path.join(venv, BIN_DIR), os.pathsep, Var('PATH'))),
27+
)
28+
29+
30+
@contextlib.contextmanager
31+
def in_env(prefix: Prefix) -> Generator[None, None, None]:
32+
directory = helpers.environment_dir(ENVIRONMENT_DIR, C.DEFAULT)
33+
envdir = prefix.path(directory)
34+
with envcontext(get_env_patch(envdir)):
35+
yield
36+
37+
38+
def install_environment(
39+
prefix: Prefix,
40+
version: str,
41+
additional_dependencies: Sequence[str],
42+
) -> None:
43+
helpers.assert_version_default('dotnet', version)
44+
helpers.assert_no_additional_deps('dotnet', additional_dependencies)
45+
46+
envdir = prefix.path(helpers.environment_dir(ENVIRONMENT_DIR, version))
47+
with clean_path_on_failure(envdir):
48+
build_dir = 'pre-commit-build'
49+
50+
# Build & pack nupkg file
51+
helpers.run_setup_cmd(
52+
prefix,
53+
(
54+
'dotnet', 'pack',
55+
'--configuration', 'Release',
56+
'--output', build_dir,
57+
),
58+
)
59+
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+
if len(build_outputs) != 1:
63+
raise NotImplementedError(
64+
f"Can't handle multiple build outputs. Got {build_outputs}",
65+
)
66+
tool_name = build_outputs[0].split('.')[0]
67+
68+
# Install to bin dir
69+
helpers.run_setup_cmd(
70+
prefix,
71+
(
72+
'dotnet', 'tool', 'install',
73+
'--tool-path', os.path.join(envdir, BIN_DIR),
74+
'--add-source', build_dir,
75+
tool_name,
76+
),
77+
)
78+
79+
# Cleanup build output
80+
for d in ('bin', 'obj', build_dir):
81+
rmtree(prefix.path(d))
82+
83+
84+
def run_hook(
85+
hook: Hook,
86+
file_args: Sequence[str],
87+
color: bool,
88+
) -> Tuple[int, bytes]:
89+
with in_env(hook.prefix):
90+
return helpers.run_xargs(hook, hook.cmd, file_args, color=color)

testing/gen-languages-all

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
import sys
33

44
LANGUAGES = [
5-
'conda', 'docker', 'docker_image', 'fail', 'golang', 'node', 'perl',
6-
'pygrep', 'python', 'ruby', 'rust', 'script', 'swift', 'system',
5+
'conda', 'docker', 'dotnet', 'docker_image', 'fail', 'golang',
6+
'node', 'perl', 'pygrep', 'python', 'ruby', 'rust', 'script', 'swift',
7+
'system',
78
]
89
FIELDS = [
910
'ENVIRONMENT_DIR', 'get_default_version', 'healthy', 'install_environment',
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
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>netcoreapp3.1</TargetFramework>
5+
<PackAsTool>true</PackAsTool>
6+
<ToolCommandName>testeroni</ToolCommandName>
7+
<PackageOutputPath>./nupkg</PackageOutputPath>
8+
</PropertyGroup>
9+
</Project>
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
4+
language: dotnet
5+
files: ''

0 commit comments

Comments
 (0)