From 541a03bbd14dcbad19932cf5d1a801378141ad49 Mon Sep 17 00:00:00 2001 From: Mehrshad Date: Thu, 24 Aug 2023 15:00:37 +0330 Subject: [PATCH] scripts: add dotNetFileConventions.fsx The newly developed script can detect wrong empty strings, wrong project directories, incorrect namespace usage, and incorrect methods in non-console applications. --- scripts/dotnetFileConventions.fsx | 79 +++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100755 scripts/dotnetFileConventions.fsx diff --git a/scripts/dotnetFileConventions.fsx b/scripts/dotnetFileConventions.fsx new file mode 100755 index 000000000..86a16465b --- /dev/null +++ b/scripts/dotnetFileConventions.fsx @@ -0,0 +1,79 @@ +#!/usr/bin/env -S dotnet fsi + +open System +open System.IO +open System.Text.RegularExpressions + +#r "nuget: Mono.Unix, Version=7.1.0-final.1.21458.1" +#r "nuget: Fsdk, Version=0.6.0--date20230821-0702.git-5488853" + +open Fsdk +open Fsdk.Process + +#load "../src/FileConventions/Helpers.fs" +#load "../src/FileConventions/Library.fs" + +open FileConventions +open Helpers + +let args = Misc.FsxOnlyArguments() + +if args.Length > 1 then + Console.Error.WriteLine + "Usage: dotnetFileConventions.fsx [projectFolder(optional)]" + + Environment.Exit 1 + +let rootDir = DirectoryInfo args.[0] + +// DefiningEmptyStringsWithDoubleQuotes +let allSourceFiles = ReturnAllProjectSourceFile rootDir [ "*.cs"; "*.fs" ] true +printfn "%A" (String.Join("\n", allSourceFiles)) + +let allProjFiles = + ReturnAllProjectSourceFile rootDir [ "*.csproj"; "*.fsproj" ] true + +for sourceFile in allSourceFiles do + let isStringEmpty = DefiningEmptyStringsWithDoubleQuotes sourceFile + + if isStringEmpty then + failwith( + sprintf + "%s file: Contains empty strings specifed with \"\" , you should use String.Empty()" + sourceFile.FullName + ) + + +// ProjFilesNamingConvention + +for projfile in allProjFiles do + let isWrongProjFile = ProjFilesNamingConvention projfile + + if isWrongProjFile then + failwith( + sprintf + "%s file: Project file or Project directory is incorrect!\n + Fix: use same name on .csproj/.fsproj on parrent project directory" + projfile.FullName + ) + +// notfollowingnamespaceconvention +for sourcefile in allSourceFiles do + let iswrongnamespace = NotFollowingNamespaceConvention sourcefile + + if iswrongnamespace then + failwith(sprintf "%s file: has wrong namespace!" sourcefile.FullName) + +// NotFollowingConsoleAppConvention +for projfile in allProjFiles do + let isWrongConsoleApplication = + NotFollowingConsoleAppConvention projfile true + + printfn "%A" projfile + + if isWrongConsoleApplication then + failwith( + sprintf + "%s project: Should not contain console methods or printf" + projfile.FullName + )