Git hooks made easy with Husky.Net internal task runner! 🐶 It brings the dev-dependency concept to the .NET world!
$ dotnet add package Husky

Husky improves your commits and more 🐶 woof!
You can use it to lint your commit messages, run tests, lint code, etc... when you commit or push.
Features
If you already know what is the lint-staged or Husky (npm packages), this is very similar but you can use Husky.Net without having node, yarn, etc.. installed, with a lot of more features! 🚀🚀
We, as developers, love platforms like GitHub, GitLab, Atlassian, Azure DevOps etc., as our managed git system and collaboration platform. We also love clean code and keep inventing new linters and rules to enforce it. In my opinion, every commit should allow the codebase to deploy to production. There is nothing worse than commits like “fixed style errors” or “fixed build”. These are often small mistakes you want to know as early as possible in your development cycle. You don’t want to break the build for the next developer because he pulled your ‘mistake’ or waste precious build minutes of your CI server. Say you have asked your teammate to review your code; in the meantime, the build server rejects your code. That means you have to go back and fix this, and your teammate has to come back and possibly review again after the changes (i.e., approvals reset on new commit). Doing so would waste a lot of time and effort. Husky.Net offers a very simple way to start using git hooks or running certain tasks, write custom scripts using c# and more ...
# local installation (recommended)
cd <Your project root directory>
dotnet new tool-manifest
dotnet tool install Husky
# global installation
dotnet tool install --global Husky
Note: With the global installation, you don't need to add the dotnet prefix to the commands.
cd <Your project root directory>
dotnet husky installdotnet husky add .husky/pre-commit "echo 'Husky is awesome!'"
git add .husky/pre-commitgit commit -m "Keep calm and commit"
# `echo 'Husky is awesome!'` will run every time you commitIf you installed husky locally, just add the below code to one of your projects (*.csproj *.vbproj).
Important: Just make sure to update the working directory depending on your folder structure.
<Target Name="husky" BeforeTargets="Restore;CollectPackageReferences">
<Exec Command="dotnet tool restore" StandardOutputImportance="Low" StandardErrorImportance="High"/>
<Exec Command="dotnet husky install" StandardOutputImportance="Low" StandardErrorImportance="High"
WorkingDirectory="../../" /> <!--Update this to the relative path to your project root dir -->
</Target>If you have only one multiple target project (TargetFrameworks) add the bellow condition IsCrossTargetingBuild to the target tag to prevent multiple execution
<Target Name="husky" BeforeTargets="Restore;CollectPackageReferences" Condition="'$(IsCrossTargetingBuild)' == 'true'">
...Or If you are using the npm, add the below code to your package.json file to automatically install husky after the installation process:
"scripts": {
"prepare": "dotnet tool restore && dotnet husky install"
}Linting makes more sense when run before committing your code. By doing so you can ensure no errors go into the repository and enforce code style. But running a lint process on a whole project is slow, and linting results can be irrelevant. Ultimately you only want to lint files that will be committed.
After installation, you must have task-runner.json file in your .husky directory that you can use to define your tasks.
you can run and test your tasks with husky run command. Once you are sure that your tasks are working properly, you can add it to the hook.
e.g.
dotnet husky add .husky/pre-commit "dotnet husky run"task-runner.json
{
"tasks": [
{
"name": "dotnet-format",
"group": "pre-commit",
"command": "dotnet",
"args": ["dotnet-format", "--include", "${staged}"],
"include": ["**/*.cs", "**/*.vb"]
},
{
"name": "eslint",
"group": "pre-commit",
"pathMode": "absolute",
"cwd": "Client",
"command": "npm",
"args": ["run", "lint", "${staged}"],
"include": ["**/*.ts", "**/*.vue", "**/*.js"]
},
{
"name": "prettier",
"group": "pre-commit",
"pathMode": "absolute",
"cwd": "Client",
"command": "npx",
"args": ["prettier", "--write", "${staged}"],
"include": [
"**/*.ts",
"**/*.vue",
"**/*.js",
"**/*.json",
"**/*.yml",
"**/*.css",
"**/*.scss"
]
},
{
"name": "Welcome",
"output": "always",
"command": "bash",
"args": ["-c", "echo Nice work! 🥂"],
"windows": {
"command": "cmd",
"args": ["/c", "echo Nice work! 🥂"]
}
}
]
}
Using bellow configuration you can define your task with a lot of options.
| name | optional | type | default | description |
|---|---|---|---|---|
| command | false | string | - | path to the executable file or script or executable name |
| args | true | [string array] | - | command arguments |
| include | true | [array of glob] | **/* | glob pattern to select files |
| name | true | string | command | name of the task (recommended) |
| group | true | string | - | group of the task (usually it should be the hook name) |
| branch | true | string (regex) | - | run task on specific branches only |
| pathMode | true | [absolute, relative] | relative | file path style (relative or absolute) |
| cwd | true | string | project root directory | current working directory for the command, can be relative or absolute |
| output | true | [always, verbose, never] | always | output log level |
| exclude | true | [array of glob] | - | glob pattern to exclude files |
| windows | true | object | - | overrides all the above settings for windows |
Husky.Net supports the standard dotnet FileSystemGlobbing patterns for include or exclude task configurations. read more here
There are some variables that you can use in your task arguments.
husky run command using --args optione.g."args": [ "${staged}" ]
You can define your own variables by adding a task to the variables section in task-runner.json.
e.g: defining custom ${root-dir} variable to access root directory files
{
"variables": [
{
"name": "root-dir",
"command": "cmd",
"args": ["/c", "dir", "/b"]
}
],
"tasks": [
{
"command": "cmd",
"args": ["/c", "echo", "${root-dir}"]
}
]
}You can use the exec command to execute a C# script.
e.g.
dotnet husky exec .husky/csx/hello.csxAlso, you can use your csx scripts in your tasks.
e.g task
{
"command": "dotnet",
"args": [ "husky", "exec", ".husky/csx/hello.csx" ],
}
This repo also using a csharp script to lint the commit messages, you can check it here ( commit-lint.csx / commit-msg hook / task-runner.json )
Consider all bellow 1.x versions as beta. ( we need a lot of tests before major release )
Don't forget to give a ⭐ on GitHub
This tool inspired of husky & lint-staged and a few other tools, for DotNet, so make sure to support them too!
I'd also like to thank kaylumah for his article that gave me the csharp scripting support idea.
husky run command doesn't have color when executed from hooks.