Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
moomiji committed Jan 2, 2024
1 parent eb3a292 commit b4e348f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,13 @@ jobs:

- name: Build
run: dotnet build --configuration Release --no-restore --framework net${{ matrix.dotnet }}

lint:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0
cache: true
- run: dotnet tool install -g dotnet-script ; cd tools/Linters ; dotnet script ./NamespaceLinter.cs
68 changes: 68 additions & 0 deletions tools/Linters/NamespaceLinter2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Text.RegularExpressions;

var directoryPath = Path.GetFullPath(Environment.CurrentDirectory + "/../../src");
var directoriesNamespaces = new Dictionary<string, string>
{
{ Path.GetFullPath($"{directoryPath}/MaaFramework.Binding.Grpc/Interop/"), "MaaFramework.Binding.Interop.Grpc" },
{ Path.GetFullPath($"{directoryPath}/MaaFramework.Binding.Native/Interop/"), "MaaFramework.Binding.Interop.Native" },


{ Path.GetFullPath($"{directoryPath}/MaaFramework.Binding/Abstractions/"), "MaaFramework.Binding.Abstractions" },
{ Path.GetFullPath($"{directoryPath}/MaaFramework.Binding.Grpc/Abstractions/"), "MaaFramework.Binding.Abstractions.Grpc" },
{ Path.GetFullPath($"{directoryPath}/MaaFramework.Binding.Native/Abstractions/"), "MaaFramework.Binding.Abstractions.Native" },

{ Path.GetFullPath($"{directoryPath}/MaaFramework.Binding/Buffers/"), "MaaFramework.Binding.Buffers" },
{ Path.GetFullPath($"{directoryPath}/MaaFramework.Binding.Grpc/Buffers/"), "MaaFramework.Binding.Buffers" },
{ Path.GetFullPath($"{directoryPath}/MaaFramework.Binding.Native/Buffers/"), "MaaFramework.Binding.Buffers" },

{ Path.GetFullPath($"{directoryPath}/MaaFramework.Binding/Custom/"), "MaaFramework.Binding.Custom" },

{ Path.GetFullPath($"{directoryPath}/MaaFramework.Binding.UnitTests/"), string.Empty },

{ Path.GetFullPath($"{directoryPath}/MaaFramework.Binding"), "MaaFramework.Binding" },
};
var protosNamespaces = new Dictionary<string, string>
{
{ Path.GetFullPath($"{directoryPath}/MaaFramework.Binding.Grpc/Protos"), "MaaFramework.Binding.Interop.Grpc" },
};

var failure = false;
foreach (var filePath in Directory.EnumerateFiles(directoryPath, "*.cs", SearchOption.AllDirectories))
{
if (new List<string> { @"\obj\", "/obj", @"\bin\", "bin" }.Exists(filePath.Contains))
continue;

var matchPath = directoriesNamespaces.Keys.FirstOrDefault(filePath.Contains);
if (matchPath is null
|| !directoriesNamespaces.TryGetValue(matchPath, out var matchNamespace)
|| string.IsNullOrEmpty(matchNamespace))
{
Console.WriteLine($"::notice file={filePath},title=NamespaceLinter::Skip file");
continue;
}

var text = File.ReadAllText(filePath);
var namespacePattern = $@"(?<=namespace\s){Regex.Escape(matchNamespace)}(?=\s*(\{{|;))";
var match = Regex.Match(text, namespacePattern);

if (!match.Success)
{
failure = true;
Console.WriteLine($"::error file={filePath},title=NamespaceLinter::Namespace must be '{matchNamespace}'");
}
}

foreach (var item in protosNamespaces)
{
foreach (var filePath in Directory.EnumerateFiles(item.Key, "*.proto", SearchOption.AllDirectories))
{
var text = File.ReadAllText(filePath);
if (!text.Contains(item.Value))
{
failure = true;
Console.WriteLine($"::error file={filePath},title=NamespaceLinter::Namespace must be '{item.Value}'");
}
}
}

return failure.GetHashCode();

0 comments on commit b4e348f

Please sign in to comment.