3 people like it.

How many lines of code does your project contain?

A simple way to count the non-blank, non-comment lines in your code. (Doesn't use project files to identify source; just a wildcard. Doesn't support multi-line comments.)

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
open System
open System.IO

let countLines path wildcard recurse =
    
    let lineCount file = 

        let isEmpty (line : string) = line.Trim() = ""
        let isComment (line : string) = line.Trim().StartsWith("//")
        let isCode (line : string) = not (isEmpty line) && not (isComment line)

        File.ReadAllLines file 
        |> Seq.filter (fun line -> isCode line) 
        |> Seq.length

    Directory.EnumerateFiles(path, wildcard, if recurse then SearchOption.AllDirectories else SearchOption.TopDirectoryOnly)
    |> Seq.map (fun file -> lineCount file )
    |> Seq.sum

// Example
let test1 =
    countLines @"D:\FSExperience\FSExperience" "*.fs" false

let test2 =
    countLines @"D:\FSExperience" "*.fs" true
namespace System
namespace System.IO
val countLines : path:string -> wildcard:string -> recurse:bool -> int

Full name: Script.countLines
val path : string
val wildcard : string
val recurse : bool
val lineCount : (string -> int)
val file : string
val isEmpty : (string -> bool)
val line : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
String.Trim() : string
String.Trim([<ParamArray>] trimChars: char []) : string
val isComment : (string -> bool)
val isCode : (string -> bool)
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
type File =
  static member AppendAllLines : path:string * contents:IEnumerable<string> -> unit + 1 overload
  static member AppendAllText : path:string * contents:string -> unit + 1 overload
  static member AppendText : path:string -> StreamWriter
  static member Copy : sourceFileName:string * destFileName:string -> unit + 1 overload
  static member Create : path:string -> FileStream + 3 overloads
  static member CreateText : path:string -> StreamWriter
  static member Decrypt : path:string -> unit
  static member Delete : path:string -> unit
  static member Encrypt : path:string -> unit
  static member Exists : path:string -> bool
  ...

Full name: System.IO.File
File.ReadAllLines(path: string) : string []
File.ReadAllLines(path: string, encoding: Text.Encoding) : string []
module Seq

from Microsoft.FSharp.Collections
val filter : predicate:('T -> bool) -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.filter
val length : source:seq<'T> -> int

Full name: Microsoft.FSharp.Collections.Seq.length
type Directory =
  static member CreateDirectory : path:string -> DirectoryInfo + 1 overload
  static member Delete : path:string -> unit + 1 overload
  static member EnumerateDirectories : path:string -> IEnumerable<string> + 2 overloads
  static member EnumerateFileSystemEntries : path:string -> IEnumerable<string> + 2 overloads
  static member EnumerateFiles : path:string -> IEnumerable<string> + 2 overloads
  static member Exists : path:string -> bool
  static member GetAccessControl : path:string -> DirectorySecurity + 1 overload
  static member GetCreationTime : path:string -> DateTime
  static member GetCreationTimeUtc : path:string -> DateTime
  static member GetCurrentDirectory : unit -> string
  ...

Full name: System.IO.Directory
Directory.EnumerateFiles(path: string) : Collections.Generic.IEnumerable<string>
Directory.EnumerateFiles(path: string, searchPattern: string) : Collections.Generic.IEnumerable<string>
Directory.EnumerateFiles(path: string, searchPattern: string, searchOption: SearchOption) : Collections.Generic.IEnumerable<string>
type SearchOption =
  | TopDirectoryOnly = 0
  | AllDirectories = 1

Full name: System.IO.SearchOption
field SearchOption.AllDirectories = 1
field SearchOption.TopDirectoryOnly = 0
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val sum : source:seq<'T> -> 'T (requires member ( + ) and member get_Zero)

Full name: Microsoft.FSharp.Collections.Seq.sum
val test1 : int

Full name: Script.test1
val test2 : int

Full name: Script.test2
Raw view Test code New version

More information

Link:http://fssnip.net/b6
Posted:12 years ago
Author:Kit Eason
Tags: source code , line count