From eadfd38c58094d79b85b6d5422977530f40d1b22 Mon Sep 17 00:00:00 2001 From: Mehrshad Date: Thu, 24 Aug 2023 14:59:58 +0330 Subject: [PATCH] FileConventions: improvements to Library.fs Finding printf and console methods in files & removed printf methods used for debugging purposes & added file filter to ReturnAllProjectSourceFile. --- .../FileConventions.Test.fs | 30 +++++-- src/FileConventions/Library.fs | 82 ++++++++++--------- 2 files changed, 67 insertions(+), 45 deletions(-) diff --git a/src/FileConventions.Test/FileConventions.Test.fs b/src/FileConventions.Test/FileConventions.Test.fs index 135498704..0e86a8531 100644 --- a/src/FileConventions.Test/FileConventions.Test.fs +++ b/src/FileConventions.Test/FileConventions.Test.fs @@ -802,7 +802,10 @@ let ConsoleAppConvention1() = ) ) - Assert.That(NotFollowingConsoleAppConvention fileInfo, Is.EqualTo true) + Assert.That( + NotFollowingConsoleAppConvention fileInfo false, + Is.EqualTo true + ) [] @@ -816,7 +819,10 @@ let ConsoleAppConvention2() = ) ) - Assert.That(NotFollowingConsoleAppConvention fileInfo, Is.EqualTo false) + Assert.That( + NotFollowingConsoleAppConvention fileInfo false, + Is.EqualTo false + ) [] @@ -830,7 +836,10 @@ let ConsoleAppConvention3() = ) ) - Assert.That(NotFollowingConsoleAppConvention fileInfo, Is.EqualTo true) + Assert.That( + NotFollowingConsoleAppConvention fileInfo false, + Is.EqualTo true + ) [] @@ -844,7 +853,10 @@ let ConsoleAppConvention4() = ) ) - Assert.That(NotFollowingConsoleAppConvention fileInfo, Is.EqualTo false) + Assert.That( + NotFollowingConsoleAppConvention fileInfo false, + Is.EqualTo false + ) [] @@ -858,7 +870,10 @@ let ConsoleAppConvention5() = ) ) - Assert.That(NotFollowingConsoleAppConvention fileInfo, Is.EqualTo true) + Assert.That( + NotFollowingConsoleAppConvention fileInfo false, + Is.EqualTo true + ) [] @@ -872,4 +887,7 @@ let ConsoleAppConvention6() = ) ) - Assert.That(NotFollowingConsoleAppConvention fileInfo, Is.EqualTo false) + Assert.That( + NotFollowingConsoleAppConvention fileInfo false, + Is.EqualTo false + ) diff --git a/src/FileConventions/Library.fs b/src/FileConventions/Library.fs index b388c8a99..ac3865fb9 100644 --- a/src/FileConventions/Library.fs +++ b/src/FileConventions/Library.fs @@ -418,10 +418,6 @@ let ProjFilesNamingConvention(fileInfo: FileInfo) = let parentDirectoryName = Path.GetDirectoryName fileInfo.FullName |> Path.GetFileName - printfn - "File name: %s, Parent directory name: %s" - fileName - parentDirectoryName fileName <> parentDirectoryName @@ -460,10 +456,8 @@ let NotFollowingNamespaceConvention(fileInfo: FileInfo) = (fileInfo.FullName.EndsWith ".fs" || fileInfo.FullName.EndsWith ".cs") sourceFileAssertionError - let fileName = Path.GetFileNameWithoutExtension fileInfo.FullName let parentDir = Path.GetDirectoryName fileInfo.FullName |> DirectoryInfo - printfn "File name: %s, Parent directory name: %s" fileName parentDir.Name if parentDir.Parent.Name = "src" then DoesNamespaceInclude fileInfo parentDir.Name |> not @@ -479,50 +473,60 @@ let NotFollowingNamespaceConvention(fileInfo: FileInfo) = let ContainsConsoleMethods(fileInfo: FileInfo) = - let fileText = File.ReadAllText fileInfo.FullName - - let consoleMethods = - [ - "printf" - "Console." - "Async.RunSynchronously" - ] + let fileLines = File.ReadAllLines fileInfo.FullName |> Array.toList + + let rec checkLine(lines: list) = + match lines with + | [] -> false + | line :: tail -> + if + line.TrimStart().StartsWith("Console.Write") + || line.TrimStart().StartsWith("printf") + || line.TrimStart().Contains("Async.RunSynchronously") + then + true + else + checkLine tail - consoleMethods |> List.exists fileText.Contains + checkLine fileLines -let NotFollowingConsoleAppConvention(fileInfo: FileInfo) = - let fileText = File.ReadAllText fileInfo.FullName - let parentDir = Path.GetDirectoryName fileInfo.FullName +let ReturnAllProjectSourceFile + (parentDir: DirectoryInfo) + (patterns: List) + (shouldFilter: bool) + = - if not(fileText.Contains "Exe") then - let rec allFiles dirs = - if Seq.isEmpty dirs then - Seq.empty + seq { + for pattern in patterns do + if shouldFilter then + yield Helpers.GetFiles parentDir pattern else - let csFiles = - dirs - |> Seq.collect(fun dir -> - Directory.EnumerateFiles(dir, "*.cs") + yield + Directory.GetFiles( + parentDir.FullName, + pattern, + SearchOption.AllDirectories ) + |> Seq.map(fun pathStr -> FileInfo pathStr) - let fsFiles = - dirs - |> Seq.collect(fun dir -> - Directory.EnumerateFiles(dir, "*.fs") - ) + } + |> Seq.concat - let projectDirectories = - dirs - |> Seq.collect Directory.EnumerateDirectories - |> allFiles - Seq.append csFiles <| Seq.append fsFiles projectDirectories +let NotFollowingConsoleAppConvention (fileInfo: FileInfo) (shouldFilter: bool) = + Misc.BetterAssert (fileInfo.FullName.EndsWith "proj") projAssertionError + let fileText = File.ReadAllText fileInfo.FullName + let parentDir = Path.GetDirectoryName fileInfo.FullName - let sourceFiles = allFiles(parentDir |> Seq.singleton) + if not(fileText.Contains "Exe") then + let sourceFiles = + ReturnAllProjectSourceFile + (DirectoryInfo parentDir) + [ "*.cs"; "*.fs" ] + shouldFilter - sourceFiles - |> Seq.exists(fun value -> ContainsConsoleMethods(FileInfo value)) + sourceFiles |> Seq.exists(fun value -> ContainsConsoleMethods value) else // project name should ends with .Console