Skip to content

Instantly share code, notes, and snippets.

@Demuirgos
Created September 22, 2022 10:31
Show Gist options
  • Save Demuirgos/66dc441859d1c408e40b8caff3519f3e to your computer and use it in GitHub Desktop.
Save Demuirgos/66dc441859d1c408e40b8caff3519f3e to your computer and use it in GitHub Desktop.
Compare ws changes
// For more information see https://aka.ms/fsharp-console-apps
open System
open System.IO
open System.Text.RegularExpressions
// assumption is folders to compare are in the same path and they have the same sub-folders hierarchy
let basePath = "" // root path
let targetFolders = [] // folders to compares
let latterPath = "" // sub paths in folders to compare
// extract all files ending with .cs and exclude files autogenerated or artifacts from build or run commands
let filesInDirectory path =
Directory.GetFiles(path, "*.cs", SearchOption.AllDirectories)
|> Array.map (fun path -> Path.GetFileName(path), path)
|> Array.filter (fun (name, path) -> ( name.EndsWith(".AssemblyAttributes.cs") = false
&& name.EndsWith(".g.cs") = false
&& name.EndsWith(".g.i.cs") = false
&& name.EndsWith("AssemblyInfo.cs") = false))
|> Map.ofArray
// map file names to their respective path in folders to compares
let createDictionary (maps:Map<_,_> list) : Map<_,_> =
let extractValue key =
maps
|> List.map (fun d -> d.[key])
let keys =
maps
|> List.map (fun d -> d.Keys)
|> List.map (Seq.toList)
|> List.concat
|> List.distinct
keys
|> List.map (fun key -> key, extractValue key)
|> Map.ofList
// extract text in files and remove all ws and them compare if stripped strings are the same
let diffFile (filename, filePaths) =
filePaths
|> List.map (fun path -> File.ReadAllText(path))
|> List.map (fun text -> Regex.Replace(text, @"\s+", ""))
|> List.distinct
|> fun list ->
if List.length list = 1 then
Ok "No changes"
else
Error (filename, filePaths)
// algorithm runner
let comparesFiles =
targetFolders
|> List.map (fun folder -> sprintf "%s%s%s" basePath folder latterPath)
|> List.map filesInDirectory
|> createDictionary
|> Map.toList
|> List.map diffFile
[<EntryPoint>]
let main _ =
comparesFiles
|> List.iter (
function
| Error (filename, filePaths) ->
printfn "File %s has unwanted changes : %A" filename filePaths
| _ -> ())
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment