5 people like it.

Loading .fs files

This script facilitates to load all the .fs files in the specified F# project in correct compilation order.

get .fs files in .fsproj file

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
open System
open System.IO
open System.Text
open System.Text.RegularExpressions

let getFsFilesIn projectDirectory =
   
  let fsprojFile = 
    Directory.GetFiles projectDirectory
    |> Seq.tryFind(fun file -> (FileInfo file).Extension = ".fsproj")

  if fsprojFile.IsSome then
    let content = File.ReadAllText fsprojFile.Value
    let matches = Regex.Matches(content,@"<Compile Include=""(\w+\.fs+)\"" />")
    seq { for m in matches -> m.Groups.[1].Value }
    |> Seq.map(fun filename -> sprintf @"#load ""%s\%s""" projectDirectory filename)
    |> String.concat "\n"
  else failwith ".fsproj file couldn't find !!!"

Usage

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
// 1. generate #load directive strings
getFsFilesIn @"(Your F# project directory)"
// 2. get them from the Clipboard
|> System.Windows.Forms.Clipboard.SetText

// 3. put them to a script file
// #load "hoge.fs"
// #load "foo.fs"
// #load "bar.fs"
// ..
namespace System
namespace System.IO
namespace System.Text
namespace System.Text.RegularExpressions
val getFsFilesIn : projectDirectory:string -> string

Full name: Script.getFsFilesIn
val projectDirectory : string
val fsprojFile : string option
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.GetFiles(path: string) : string []
Directory.GetFiles(path: string, searchPattern: string) : string []
Directory.GetFiles(path: string, searchPattern: string, searchOption: SearchOption) : string []
module Seq

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

Full name: Microsoft.FSharp.Collections.Seq.tryFind
val file : string
Multiple items
type FileInfo =
  inherit FileSystemInfo
  new : fileName:string -> FileInfo
  member AppendText : unit -> StreamWriter
  member CopyTo : destFileName:string -> FileInfo + 1 overload
  member Create : unit -> FileStream
  member CreateText : unit -> StreamWriter
  member Decrypt : unit -> unit
  member Delete : unit -> unit
  member Directory : DirectoryInfo
  member DirectoryName : string
  member Encrypt : unit -> unit
  ...

Full name: System.IO.FileInfo

--------------------
FileInfo(fileName: string) : unit
property Option.IsSome: bool
val content : string
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.ReadAllText(path: string) : string
File.ReadAllText(path: string, encoding: Encoding) : string
property Option.Value: string
val matches : MatchCollection
Multiple items
type Regex =
  new : pattern:string -> Regex + 1 overload
  member GetGroupNames : unit -> string[]
  member GetGroupNumbers : unit -> int[]
  member GroupNameFromNumber : i:int -> string
  member GroupNumberFromName : name:string -> int
  member IsMatch : input:string -> bool + 1 overload
  member Match : input:string -> Match + 2 overloads
  member Matches : input:string -> MatchCollection + 1 overload
  member Options : RegexOptions
  member Replace : input:string * replacement:string -> string + 5 overloads
  ...

Full name: System.Text.RegularExpressions.Regex

--------------------
Regex(pattern: string) : unit
Regex(pattern: string, options: RegexOptions) : unit
Regex.Matches(input: string, pattern: string) : MatchCollection
Regex.Matches(input: string, pattern: string, options: RegexOptions) : MatchCollection
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

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

--------------------
type seq<'T> = Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val m : Match
property Match.Groups: GroupCollection
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val filename : string
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
Multiple items
type String =
  new : value:char -> string + 7 overloads
  member Chars : int -> char
  member Clone : unit -> obj
  member CompareTo : value:obj -> int + 1 overload
  member Contains : value:string -> bool
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EndsWith : value:string -> bool + 2 overloads
  member Equals : obj:obj -> bool + 2 overloads
  member GetEnumerator : unit -> CharEnumerator
  member GetHashCode : unit -> int
  ...

Full name: System.String

--------------------
String(value: nativeptr<char>) : unit
String(value: nativeptr<sbyte>) : unit
String(value: char []) : unit
String(c: char, count: int) : unit
String(value: nativeptr<char>, startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
String(value: char [], startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Encoding) : unit
val concat : sep:string -> strings:seq<string> -> string

Full name: Microsoft.FSharp.Core.String.concat
val failwith : message:string -> 'T

Full name: Microsoft.FSharp.Core.Operators.failwith
namespace System.Windows
namespace System.Windows.Forms
type Clipboard =
  static member Clear : unit -> unit
  static member ContainsAudio : unit -> bool
  static member ContainsData : format:string -> bool
  static member ContainsFileDropList : unit -> bool
  static member ContainsImage : unit -> bool
  static member ContainsText : unit -> bool + 1 overload
  static member GetAudioStream : unit -> Stream
  static member GetData : format:string -> obj
  static member GetDataObject : unit -> IDataObject
  static member GetFileDropList : unit -> StringCollection
  ...

Full name: System.Windows.Forms.Clipboard
Windows.Forms.Clipboard.SetText(text: string) : unit
Windows.Forms.Clipboard.SetText(text: string, format: Windows.Forms.TextDataFormat) : unit

More information

Link:http://fssnip.net/a2
Posted:12 years ago
Author:nagat01
Tags: scripting , input and output , parsing