Skip to content

Commit

Permalink
Merge pull request #9 from elizabethking2/add-dotnet-format-hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Mounce authored Aug 6, 2020
2 parents cc5f03d + 63542b9 commit 20338c2
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 4 deletions.
16 changes: 13 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ exclude: vendor|\.pb\.go

repos:
- repo: https://github.com/adrienverge/yamllint
rev: v1.20.0
rev: v1.24.2
hooks:
- id: yamllint
args:
- --format=parsable
- --strict

- repo: https://github.com/jumanjihouse/pre-commit-hook-yamlfmt
rev: 0.0.10
rev: 0.0.11
hooks:
- id: yamlfmt
args:
Expand All @@ -27,7 +27,7 @@ repos:
- --width=1200 # match .yamllint.yaml; we don't care about line-length.

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
rev: v3.2.0
hooks:
- id: check-added-large-files
- id: check-byte-order-marker
Expand All @@ -36,6 +36,7 @@ repos:
- id: check-json
- id: check-merge-conflict
- id: check-symlinks
- id: check-xml
- id: detect-private-key
- id: end-of-file-fixer
- id: forbid-new-submodules
Expand All @@ -59,3 +60,12 @@ repos:
- -w # autofix in place
- -d # show diff
- -i=2 # indent 2 spaces

# Example usage of dotnet-format: pre-commit run dotnet-format --all-files
# To restrict to only listed directory add: args: [ path/to/a.csproj, path/to/b.csproj ]
- repo: https://github.com/improbable-eng/pre-commit-plugins/
rev: 0.1.3
hooks:
- id: dotnet-format
args:
- samples/dotnet-hook-samples/Test.csproj
11 changes: 10 additions & 1 deletion .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
files: prometheus.ya?ml$

- id: prometheus_check_rules
name: Check prometheus rules
name: prometheus_check_rules
description: Validates prometheus rules
entry: script/check-prometheus-rules.sh
language: script
Expand All @@ -130,3 +130,12 @@
- text
- yaml
files: \.rules?\.test\.ya?ml$

- id: dotnet-format
name: dotnet-format
description: Check C# source code
entry: script/check-dotnet-format.sh
language: script
types:
- c#
pass_filenames: false
7 changes: 7 additions & 0 deletions samples/dotnet-hook-samples/Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
</Project>
12 changes: 12 additions & 0 deletions samples/dotnet-hook-samples/Testing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Test
{
public class Testing
{
public void DoSomething()
{
Console.WriteLine("Do something");
}
}
}
21 changes: 21 additions & 0 deletions script/check-dotnet-format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash
# shellcheck source=../lib/setup.sh
source "$(dirname "${BASH_SOURCE[0]}")/../lib/setup.sh" || exit "${EXIT_FAILED_TO_SOURCE}"

# It's legitimate for a linter to exit non-zero; some indicate success-but-lint that way.
set +o errexit

# Skip bash stack-trace, otherwise our bash stack-traces clutter up the output.
export SKIP_BASH_STACKTRACE=1

if [[ $# -eq 0 ]]
then
# Run dotnet-format on entire repository
exec dotnet-format --check . --folder
else
# Run dotnet-format on the directories of any C# project files found
for project in "${@}"
do
dotnet-format --check "$(dirname "${project}")"
done
fi

0 comments on commit 20338c2

Please sign in to comment.