From 9811a820ba77c8868470b6f20c41e5b5e7b69321 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Thu, 21 Apr 2022 19:47:09 +0100 Subject: [PATCH] [github actions] Add custom source validator --- .github/workflows/source_validator.yml | 34 ++++++++++++++ X10D.SourceValidator/Program.cs | 46 +++++++++++++++++++ .../X10D.SourceValidator.csproj | 14 ++++++ 3 files changed, 94 insertions(+) create mode 100644 .github/workflows/source_validator.yml create mode 100644 X10D.SourceValidator/Program.cs create mode 100644 X10D.SourceValidator/X10D.SourceValidator.csproj diff --git a/.github/workflows/source_validator.yml b/.github/workflows/source_validator.yml new file mode 100644 index 0000000..caff681 --- /dev/null +++ b/.github/workflows/source_validator.yml @@ -0,0 +1,34 @@ +name: Source Validator + +on: + push: + branches: + - master + - develop + +jobs: + source_validator: + runs-on: ubuntu-latest + if: "!contains(format('{0} {1}', github.event.head_commit.message, github.event.pull_request.title), '[ci-skip]')" + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Setup .NET + uses: actions/setup-dotnet@v2 + with: + dotnet-version: 6.0.x + + - name: Add GitHub NuGet source + run: dotnet nuget add source --username oliverbooth --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/oliverbooth/index.json" + + - name: Restore dependencies + run: dotnet restore + + - name: Build + run: dotnet build -c Debug + + - name: Run Source Validation + run: dotnet run --project X10D.SourceValidator ./X10D/src + diff --git a/X10D.SourceValidator/Program.cs b/X10D.SourceValidator/Program.cs new file mode 100644 index 0000000..447a8ae --- /dev/null +++ b/X10D.SourceValidator/Program.cs @@ -0,0 +1,46 @@ +using System.Text; + +var directories = new Stack(Directory.GetDirectories(args[0])); +var problems = 0; +var files = 0; + +while (directories.Count > 0) +{ + string path = Path.GetFullPath(directories.Pop()); + + foreach (string directory in Directory.EnumerateDirectories(path)) + { + directories.Push(directory); + } + + foreach (string file in Directory.EnumerateFiles(path, "*.cs")) + { + files++; + await using var stream = File.OpenRead(file); + using var reader = new StreamReader(stream, Encoding.UTF8); + + var lineNumber = 1; + while (await reader.ReadLineAsync() is { } line) + { + if (line.Length > 130) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.Out.WriteLine($"{file}({lineNumber}): Line is too long ({line.Length})"); + Console.ResetColor(); + problems++; + } + else if (line.Length > 0 && char.IsWhiteSpace(line[^1])) + { + Console.ForegroundColor = ConsoleColor.Red; + Console.Out.WriteLine($"{file}({lineNumber}): Line contains trailing whitespace"); + Console.ResetColor(); + problems++; + } + + lineNumber++; + } + } +} + +Console.Out.WriteLine($"Finished scanning {files} files, {problems} problems encountered."); +return problems; diff --git a/X10D.SourceValidator/X10D.SourceValidator.csproj b/X10D.SourceValidator/X10D.SourceValidator.csproj new file mode 100644 index 0000000..8957a5d --- /dev/null +++ b/X10D.SourceValidator/X10D.SourceValidator.csproj @@ -0,0 +1,14 @@ + + + + Exe + net6.0 + enable + enable + + + + + + +