From e4c37bc98ecebd62d9e126dc69899b52d8d97219 Mon Sep 17 00:00:00 2001 From: Simon Bartlett Date: Wed, 2 Oct 2024 01:10:31 -0400 Subject: [PATCH] Add husky and csharpier --- .config/dotnet-tools.json | 18 +++++++++++++++ .github/workflows/ci.yml | 4 ++-- .husky/pre-commit | 22 ++++++++++++++++++ .husky/task-runner.json | 18 +++++++++++++++ .nuke/build.schema.json | 1 + Geo/Geo.csproj | 6 +++++ build/Build.cs | 48 +++++++++++++++++++++++++++------------ 7 files changed, 100 insertions(+), 17 deletions(-) create mode 100644 .config/dotnet-tools.json create mode 100755 .husky/pre-commit create mode 100644 .husky/task-runner.json diff --git a/.config/dotnet-tools.json b/.config/dotnet-tools.json new file mode 100644 index 0000000..84b4c14 --- /dev/null +++ b/.config/dotnet-tools.json @@ -0,0 +1,18 @@ +{ + "version": 1, + "isRoot": true, + "tools": { + "husky": { + "version": "0.7.1", + "commands": [ + "husky" + ] + }, + "csharpier": { + "version": "0.29.2", + "commands": [ + "dotnet-csharpier" + ] + } + } +} \ No newline at end of file diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index df6531a..10938f3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,5 +31,5 @@ jobs: .nuke/temp ~/.nuget/packages key: ${{ runner.os }}-${{ hashFiles('**/global.json', '**/*.csproj', '**/Directory.Packages.props') }} - - name: 'Run: Test' - run: ./build.cmd Test + - name: 'Run: CheckForUncommittedChanges, Test' + run: ./build.cmd CheckForUncommittedChanges Test diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100755 index 0000000..65130a6 --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1,22 @@ +#!/bin/sh +. "$(dirname "$0")/_/husky.sh" + +## husky task runner examples ------------------- +## Note : for local installation use 'dotnet' prefix. e.g. 'dotnet husky' + +## run all tasks +#husky run + +### run all tasks with group: 'group-name' +#husky run --group group-name + +## run task with name: 'task-name' +#husky run --name task-name + +## pass hook arguments to task +#husky run --args "$1" "$2" + +## or put your custom commands ------------------- +#echo 'Husky.Net is awesome!' + +dotnet husky run --group pre-commit diff --git a/.husky/task-runner.json b/.husky/task-runner.json new file mode 100644 index 0000000..8bf95f0 --- /dev/null +++ b/.husky/task-runner.json @@ -0,0 +1,18 @@ +{ + "tasks": [ + { + "name": "Run csharpier on staged files", + "group": "pre-commit", + "command": "dotnet", + "args": ["csharpier", "${staged}"], + "include": ["**/*.cs"] + }, + { + "name": "Check csharpier on entire repository", + "group": "verify", + "command": "dotnet", + "args": ["csharpier", "."], + "include": ["**/*.cs"] + } + ] +} diff --git a/.nuke/build.schema.json b/.nuke/build.schema.json index 557f2b3..c51992c 100644 --- a/.nuke/build.schema.json +++ b/.nuke/build.schema.json @@ -38,6 +38,7 @@ "ExecutableTarget": { "type": "string", "enum": [ + "CheckForUncommittedChanges", "Clean", "Compile", "Publish", diff --git a/Geo/Geo.csproj b/Geo/Geo.csproj index 468883c..e5dbff8 100644 --- a/Geo/Geo.csproj +++ b/Geo/Geo.csproj @@ -38,4 +38,10 @@ README.md + + + + + + diff --git a/build/Build.cs b/build/Build.cs index 473deea..6b1b448 100644 --- a/build/Build.cs +++ b/build/Build.cs @@ -1,13 +1,14 @@ using Nuke.Common; using Nuke.Common.CI.GitHubActions; using Nuke.Common.ProjectModel; +using Nuke.Common.Tooling; using Nuke.Common.Tools.DotNet; [GitHubActions( "ci", GitHubActionsImage.UbuntuLatest, On = new[] { GitHubActionsTrigger.Push }, - InvokedTargets = new[] { nameof(Test) } + InvokedTargets = new[] { nameof(CheckForUncommittedChanges), nameof(Test) } )] class Build : NukeBuild { @@ -17,20 +18,39 @@ class Build : NukeBuild /// - Microsoft VisualStudio https://nuke.build/visualstudio /// - Microsoft VSCode https://nuke.build/vscode - public static int Main () => Execute(x => x.Compile); + public static int Main() => Execute(x => x.Compile); [Parameter("Configuration to build - Default is 'Debug' (local) or 'Release' (server)")] - readonly Configuration Configuration = IsLocalBuild ? Configuration.Debug : Configuration.Release; + readonly Configuration Configuration = IsLocalBuild + ? Configuration.Debug + : Configuration.Release; [Solution] readonly Solution Solution; + [PathVariable] + readonly Tool Git; + + Target CheckForUncommittedChanges => + _ => + _.Executes(() => + { + DotNetTasks.DotNet("husky run --group verify"); + + if (Git($"status --porcelain").Count > 0) + { + Assert.Fail("Uncommitted changes - run csharpier and prettier"); + } + }); + Target Clean => _ => _.Before(Restore) .Executes(() => { - DotNetTasks.DotNetClean(_ => _.SetProject(Solution).SetConfiguration(Configuration)); + DotNetTasks.DotNetClean(_ => + _.SetProject(Solution).SetConfiguration(Configuration) + ); }); Target Restore => @@ -45,11 +65,10 @@ class Build : NukeBuild _.DependsOn(Restore) .Executes(() => { - DotNetTasks.DotNetBuild( - _ => - _.SetProjectFile(Solution) - .SetNoRestore(InvokedTargets.Contains(Restore)) - .SetConfiguration(Configuration) + DotNetTasks.DotNetBuild(_ => + _.SetProjectFile(Solution) + .SetNoRestore(InvokedTargets.Contains(Restore)) + .SetConfiguration(Configuration) ); }); @@ -61,8 +80,10 @@ class Build : NukeBuild var projects = Solution.GetAllProjects("*.Tests"); foreach (var project in projects) { - DotNetTasks.DotNetTest( - _ => _.SetProjectFile(project.Path).SetConfiguration(Configuration).EnableNoBuild() + DotNetTasks.DotNetTest(_ => + _.SetProjectFile(project.Path) + .SetConfiguration(Configuration) + .EnableNoBuild() ); } }); @@ -71,9 +92,6 @@ class Build : NukeBuild _ => _.Executes(() => { - DotNetTasks.DotNetPublish( - _ => _.SetConfiguration("Release") - ); + DotNetTasks.DotNetPublish(_ => _.SetConfiguration("Release")); }); - }