Skip to content

Instantly share code, notes, and snippets.

@Kimserey
Last active October 1, 2015 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kimserey/1740cf35bd9260d4cc2f to your computer and use it in GitHub Desktop.
Save Kimserey/1740cf35bd9260d4cc2f to your computer and use it in GitHub Desktop.
Compile a script.fsx and get out a result from a console app fsharp. FSharp.Core.sigdata / FSharp.Core.optdata must be added to the project. Domain is in a separate library project, script.fsx and Program project reference the Domain library.
module Domain.Core
type Breed =
| Papillon
| Corgy
type Dog =
{ breed : Breed
name : string }
override x.ToString() =
match x.breed with
| Papillon -> sprintf "%s is a %s" x.name "Papillon"
| Corgy -> sprintf "%s is a %s" x.name "Corgy"
type Result =
| ResultSuccess of Dog list
| ResultFailure of string
open Microsoft.FSharp.Compiler.Interactive.Shell
open System
open System.IO
open System.Text
open Domain.Core
module Main =
let executeScript filename evaluate =
let sbOut = new StringBuilder()
let sbErr = new StringBuilder()
use inStream = new StringReader("")
use outStream = new StringWriter(sbOut)
use errStream = new StringWriter(sbErr)
let allArgs = [| "--noninteractive"; "--nologo"; "--gui-"; "--define:HOSTED" |]
let fsiConfig = FsiEvaluationSession.GetDefaultConfiguration()
let fsiSession = FsiEvaluationSession.Create(fsiConfig, allArgs, inStream, outStream, errStream, collectible = true)
fsiSession.EvalScript(filename)
match fsiSession.EvalExpression(evaluate) with
| Some v -> ResultSuccess (v.ReflectionValue :?> Dog list)
| None -> ResultFailure "Couldn't evaluate this script"
[<EntryPoint>]
let main argv =
executeScript @"..\..\script.fsx" "Script.Dogs.dogList"
|> (fun res -> match res with
| ResultSuccess dogs -> dogs |> List.iter (fun x -> printfn "%s" <| x.ToString())
| ResultFailure message -> printfn "%s" message)
Console.ReadKey() |> ignore
0 // return an integer exit code
module Script
#r @"bin\Debug\Domain.dll"
open Domain.Core
module Dogs =
let dogList = [ { breed = Papillon; name = "sausage" }; { breed = Corgy; name = "browny" } ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment