Skip to content

Instantly share code, notes, and snippets.

@cloudRoutine
Last active July 20, 2016 12:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cloudRoutine/eb697e87f87e099dd95f to your computer and use it in GitHub Desktop.
Save cloudRoutine/eb697e87f87e099dd95f to your computer and use it in GitHub Desktop.
Since Visual Studio won't calculate code metrics for F# projects, this script will calculate some some rough stats.
open System
open System.IO
let find_files dir = Directory.GetFiles( dir, "*.fs?", SearchOption.AllDirectories )
let not_start (s:string) p = not <| s.StartsWith p
let has_type (s:string) = if s.Contains @"type" then 1 else 0
let has_module (s:string) = if s.Contains @"module" then 1 else 0
let has_binding (s:string) = if s.Contains @"let" ||
s.Contains @"member" then 1 else 0
type Statistics = { ``Num Of Lines `` : int ; ``Num Of Files `` : int;
``Num Of Chars `` : int ; ``Num Of Types `` : int;
``Num Of Modules `` : int ; ``Num Of Bindings `` : int;
}
let generate_stats =
find_files
>> Array.filter (fun f -> f.EndsWith ".fs" || f.EndsWith ".fsx" || f.EndsWith ".fsi")
>> Array.fold (fun acc f ->
let contents =
f |> File.ReadAllLines
|> Array.filter ( fun s -> not <| String.IsNullOrWhiteSpace s)
|> Array.map ( fun s -> s.Trim() )
|> Array.filter ( fun s -> not_start s "open" &&
not_start s @"///" &&
not_start s @"//" )
{ ``Num Of Lines `` = acc.``Num Of Lines `` + Array.length contents ;
``Num Of Files `` = acc.``Num Of Files `` + 1 ;
``Num Of Chars `` = acc.``Num Of Chars `` + Array.sumBy String.length contents;
``Num Of Types `` = acc.``Num Of Types `` + Array.sumBy has_type contents;
``Num Of Modules `` = acc.``Num Of Modules `` + Array.sumBy has_module contents;
``Num Of Bindings `` = acc.``Num Of Bindings `` + Array.sumBy has_binding contents;
}
)
{ ``Num Of Lines `` = 0; ``Num Of Files `` = 0;
``Num Of Chars `` = 0; ``Num Of Types `` = 0;
``Num Of Modules `` = 0; ``Num Of Bindings `` = 0;
}
;;
Path.GetDirectoryName __SOURCE_DIRECTORY__ |> generate_stats ;;
@"C:\Users\jared\Github\Clones\visualfsharp\src\fsharp" |> generate_stats ;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment