Skip to content

Commit

Permalink
FileConventions: improvements to Library.fs
Browse files Browse the repository at this point in the history
Finding printf and console methods in files & removed
printf methods used for debugging purposes & added
file filter to ReturnAllProjectSourceFile.
  • Loading branch information
Mersho committed Aug 29, 2023
1 parent acdac54 commit eadfd38
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 45 deletions.
30 changes: 24 additions & 6 deletions src/FileConventions.Test/FileConventions.Test.fs
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,10 @@ let ConsoleAppConvention1() =
)
)

Assert.That(NotFollowingConsoleAppConvention fileInfo, Is.EqualTo true)
Assert.That(
NotFollowingConsoleAppConvention fileInfo false,
Is.EqualTo true
)


[<Test>]
Expand All @@ -816,7 +819,10 @@ let ConsoleAppConvention2() =
)
)

Assert.That(NotFollowingConsoleAppConvention fileInfo, Is.EqualTo false)
Assert.That(
NotFollowingConsoleAppConvention fileInfo false,
Is.EqualTo false
)


[<Test>]
Expand All @@ -830,7 +836,10 @@ let ConsoleAppConvention3() =
)
)

Assert.That(NotFollowingConsoleAppConvention fileInfo, Is.EqualTo true)
Assert.That(
NotFollowingConsoleAppConvention fileInfo false,
Is.EqualTo true
)


[<Test>]
Expand All @@ -844,7 +853,10 @@ let ConsoleAppConvention4() =
)
)

Assert.That(NotFollowingConsoleAppConvention fileInfo, Is.EqualTo false)
Assert.That(
NotFollowingConsoleAppConvention fileInfo false,
Is.EqualTo false
)


[<Test>]
Expand All @@ -858,7 +870,10 @@ let ConsoleAppConvention5() =
)
)

Assert.That(NotFollowingConsoleAppConvention fileInfo, Is.EqualTo true)
Assert.That(
NotFollowingConsoleAppConvention fileInfo false,
Is.EqualTo true
)


[<Test>]
Expand All @@ -872,4 +887,7 @@ let ConsoleAppConvention6() =
)
)

Assert.That(NotFollowingConsoleAppConvention fileInfo, Is.EqualTo false)
Assert.That(
NotFollowingConsoleAppConvention fileInfo false,
Is.EqualTo false
)
82 changes: 43 additions & 39 deletions src/FileConventions/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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<string>) =
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<string>)
(shouldFilter: bool)
=

if not(fileText.Contains "<OutputType>Exe</OutputType>") 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 "<OutputType>Exe</OutputType>") 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
Expand Down

0 comments on commit eadfd38

Please sign in to comment.